Skip to content

Commit a0812fb

Browse files
Joe Cervinofhinkel
authored andcommitted
Add BatchDelete Sample for Jobs v3 (GoogleCloudPlatform#1085)
1 parent fd8f5dd commit a0812fb

4 files changed

Lines changed: 147 additions & 2 deletions

File tree

jobs/v3/batchdelete-jobs-sample.js

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/**
2+
* Copyright 2019, Google, LLC.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
'use strict';
17+
18+
const basicCompanySample = require(`./basic-company-sample`);
19+
const basicJobSample = require(`./basic-job-sample`);
20+
const createAuthCredential = require(`./create-auth-credential`);
21+
const customAttributeSample = require('./custom-attribute-sample');
22+
const sleep = require('./sleep');
23+
24+
const PROJECT_ID = process.env.GOOGLE_CLOUD_PROJECT;
25+
26+
/**
27+
* The sample in this file introduces how to perform a batch delete of jobs.
28+
* NOTE: Both company name and requisition ID are required to perform batchDelete.
29+
*/
30+
31+
// [START jobs_batchDelete]
32+
33+
/**
34+
* Batch delete jobs.
35+
* NOTE: @property [filter] formatting is strict. Do not change.
36+
*/
37+
const batchDelete = async (jobServiceClient, companyName, jobs) => {
38+
try {
39+
let batchDeleteQuery = `companyName = "${companyName}"`;
40+
jobs.forEach(job => {
41+
batchDeleteQuery += ` AND requisitionId = "${job.requisitionId}"`;
42+
});
43+
44+
const request = {
45+
parent: `projects/${PROJECT_ID}`,
46+
requestBody: {
47+
filter: batchDeleteQuery,
48+
},
49+
};
50+
51+
const result = await jobServiceClient.projects.jobs.batchDelete(request);
52+
console.log(JSON.stringify('Batch deleted.', result));
53+
return;
54+
} catch (e) {
55+
console.error(e);
56+
throw e;
57+
}
58+
};
59+
// [END jobs_batchDelete]
60+
61+
// [START list_jobs]
62+
63+
/**
64+
* List Jobs.
65+
* NOTE: @property [filter] formatting is strict. Do not change.
66+
*/
67+
const listJobs = async (jobServiceClient, companyName) => {
68+
try {
69+
const request = {
70+
parent: `projects/${PROJECT_ID}`,
71+
filter: `companyName = "${companyName}"`,
72+
};
73+
const jobsObj = await jobServiceClient.projects.jobs.list(request);
74+
75+
console.log(JSON.stringify(jobsObj.data));
76+
return jobsObj.data.jobs;
77+
} catch (e) {
78+
console.error(e);
79+
throw e;
80+
}
81+
};
82+
// [END list_jobs]
83+
84+
// Run Sample
85+
(async () => {
86+
try {
87+
// Create an authorized client
88+
const jobServiceClient = await createAuthCredential();
89+
90+
// Create a company
91+
const companyToBeCreated = basicCompanySample.generateCompany();
92+
const companyCreated = await basicCompanySample.createCompany(
93+
jobServiceClient,
94+
companyToBeCreated
95+
);
96+
const companyName = companyCreated.name;
97+
98+
// Create multiple jobs
99+
for (let i = 0; i < 5; i += 1) {
100+
const jobToBeCreated = customAttributeSample.generateJobWithACustomAttribute(
101+
companyName
102+
);
103+
await basicJobSample.createJob(jobServiceClient, jobToBeCreated);
104+
}
105+
106+
// Wait several seconds for post processing
107+
await sleep(10000);
108+
109+
// Get a list of jobs
110+
const jobs = await listJobs(jobServiceClient, companyName);
111+
112+
// Batch delete jobs
113+
await batchDelete(jobServiceClient, companyName, jobs);
114+
} catch (e) {
115+
console.log(e);
116+
throw e;
117+
}
118+
})();

jobs/v3/custom-attribute-sample.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const REQUEST_META_DATA = {
4141
* Generate a job with a custom attribute.
4242
*/
4343
const generateJobWithACustomAttribute = companyName => {
44-
const requisitionId = `"jobWithACustomAttribute: ${new Date().getTime()}}`;
44+
const requisitionId = `jobWithACustomAttribute: ${new Date().getTime()}`;
4545
const jobTitle = 'Software Engineer';
4646
const applicationUrls = ['http://careers.google.com'];
4747
const description =

jobs/v3/histogram-sample.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const sleep = require('./sleep');
2424
const PROJECT_ID = process.env.GOOGLE_CLOUD_PROJECT;
2525

2626
/**
27-
* The sample in this file introduce how to do a histogram search.
27+
* The sample in this file introduces how to do a histogram search.
2828
*/
2929

3030
// [START histogram_search]
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Copyright 2019, Google, LLC.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
'use strict';
17+
18+
const test = require(`ava`);
19+
const tools = require(`@google-cloud/nodejs-repo-tools`);
20+
const runSample = `'require("./basic-job-sample").runSample()'`;
21+
22+
test(`Should batchDelete jobs.`, async t => {
23+
const output = await tools.runAsync(`node -e ${runSample}`);
24+
const pattern = `.*Batch deleted.*\n` + `.*{}*`;
25+
26+
t.regex(output, new RegExp(pattern));
27+
});

0 commit comments

Comments
 (0)