Skip to content

Commit 62fe6e3

Browse files
DLP: Added sample for create and get job (#3361)
* DLP: Added sample for create and get job Added unit test cases for same * fixes test failures * Updated infoTypes in the createJob sample --------- Co-authored-by: Patti Shin <[email protected]>
1 parent 372d548 commit 62fe6e3

File tree

3 files changed

+175
-0
lines changed

3 files changed

+175
-0
lines changed

dlp/createJob.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Copyright 2023 Google LLC
2+
//
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+
'use strict';
16+
17+
// sample-metadata:
18+
// title: Create an inspection job
19+
// description: Create an inspection job
20+
// usage: node createJob.js my-project bucket-url
21+
function main(projectId, cloudFileUrl) {
22+
// [START dlp_create_job]
23+
// Imports the Google Cloud Data Loss Prevention library
24+
const DLP = require('@google-cloud/dlp');
25+
26+
// Initialize google DLP Client
27+
const dlp = new DLP.DlpServiceClient();
28+
29+
async function jobsCreate() {
30+
// Construct cloud storage configuration
31+
const cloudStorageConfig = {
32+
cloudStorageOptions: {
33+
fileSet: {
34+
url: cloudFileUrl,
35+
},
36+
},
37+
timespanConfig: {
38+
enableAutoPopulationOfTimespanConfig: true,
39+
},
40+
};
41+
42+
// Construct inspect job configuration
43+
const inspectJob = {
44+
storageConfig: cloudStorageConfig,
45+
};
46+
47+
// Construct inspect configuration
48+
const inspectConfig = {
49+
infoTypes: [
50+
{name: 'EMAIL_ADDRESS'},
51+
{name: 'PERSON_NAME'},
52+
{name: 'LOCATION'},
53+
{name: 'PHONE_NUMBER'},
54+
],
55+
includeQuote: true,
56+
minLikelihood: DLP.protos.google.privacy.dlp.v2.Likelihood.LIKELY,
57+
excludeInfoTypes: false,
58+
};
59+
60+
// Combine configurations into a request for the service.
61+
const request = {
62+
parent: `projects/${projectId}/locations/global`,
63+
inspectJob: inspectJob,
64+
inspectConfig: inspectConfig,
65+
};
66+
67+
// Send the request and receive response from the service
68+
const [response] = await dlp.createDlpJob(request);
69+
// Print the results
70+
console.log(`Job created successfully: ${response.name}`);
71+
}
72+
73+
jobsCreate();
74+
// [END dlp_create_job]
75+
}
76+
77+
process.on('unhandledRejection', err => {
78+
console.error(err.message);
79+
process.exitCode = 1;
80+
});
81+
82+
main(...process.argv.slice(2));

dlp/getJob.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2023 Google LLC
2+
//
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+
'use strict';
16+
17+
// sample-metadata:
18+
// title: Get an inspection job
19+
// description: Get DLP inspection job using job name.
20+
// usage: node getJob.js jobName
21+
function main(jobName) {
22+
// [START dlp_get_job]
23+
// Imports the Google Cloud Data Loss Prevention library
24+
const DLP = require('@google-cloud/dlp');
25+
26+
// Instantiates a client
27+
const dlp = new DLP.DlpServiceClient();
28+
29+
// Job name to look for
30+
// const jobName = 'your-job-name';
31+
32+
async function getJob() {
33+
// Construct request for finding job using job name.
34+
const request = {
35+
name: jobName,
36+
};
37+
38+
// Send the request and receive response from the service
39+
const [job] = await dlp.getDlpJob(request);
40+
41+
// Print results.
42+
console.log(`Job ${job.name} status: ${job.state}`);
43+
}
44+
45+
getJob();
46+
// [END dlp_get_job]
47+
}
48+
49+
process.on('unhandledRejection', err => {
50+
console.error(err.message);
51+
process.exitCode = 1;
52+
});
53+
54+
main(...process.argv.slice(2));

dlp/system-test/jobs.test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const testTableProjectId = 'bigquery-public-data';
2727
const testDatasetId = 'san_francisco';
2828
const testTableId = 'bikeshare_trips';
2929
const testColumnName = 'zip_code';
30+
const bucketName = 'nodejs-dlp-test-bucket';
3031

3132
const client = new DLP.DlpServiceClient();
3233

@@ -156,4 +157,42 @@ describe('test', () => {
156157
console.log(output);
157158
assert.match(output, /Error in deleteJob/);
158159
});
160+
161+
// dlp_create_job
162+
it('should create job', () => {
163+
const output = execSync(
164+
`node createJob.js ${projectId} gs://${bucketName}/test.txt`
165+
);
166+
assert.match(output, /Job created successfully:/);
167+
});
168+
169+
it('should handle job creation errors', () => {
170+
let output;
171+
try {
172+
output = execSync(
173+
`node createJob.js BAD_PROJECT_IDgs://${bucketName}/test.txt `
174+
);
175+
} catch (err) {
176+
output = err.message;
177+
}
178+
assert.include(output, 'INVALID_ARGUMENT');
179+
});
180+
181+
// dlp_get_job
182+
it('should get job details using job name', async () => {
183+
const jobName = await createTestJob();
184+
const output = execSync(`node getJob.js ${jobName}`);
185+
assert.match(output, /Job .+ status: \w/);
186+
await client.deleteDlpJob({name: jobName});
187+
});
188+
189+
it('should handle error while fetching job details', () => {
190+
let output;
191+
try {
192+
output = execSync('node getJob.js INVALID_JOB');
193+
} catch (err) {
194+
output = err.message;
195+
}
196+
assert.include(output, 'INVALID_ARGUMENT');
197+
});
159198
});

0 commit comments

Comments
 (0)