Skip to content

Commit 76706dc

Browse files
authored
feat(dlp): implement create_and_get_job sample (GoogleCloudPlatform#1848)
1 parent 29c9bb3 commit 76706dc

File tree

3 files changed

+204
-0
lines changed

3 files changed

+204
-0
lines changed

dlp/src/create_job.php

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2023 Google Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/**
20+
* For instructions on how to run the samples:
21+
*
22+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/dlp/README.md
23+
*/
24+
25+
namespace Google\Cloud\Samples\Dlp;
26+
27+
# [START dlp_create_job]
28+
use Google\Cloud\Dlp\V2\CloudStorageOptions;
29+
use Google\Cloud\Dlp\V2\CloudStorageOptions\FileSet;
30+
use Google\Cloud\Dlp\V2\DlpServiceClient;
31+
use Google\Cloud\Dlp\V2\InfoType;
32+
use Google\Cloud\Dlp\V2\InspectConfig;
33+
use Google\Cloud\Dlp\V2\InspectConfig\FindingLimits;
34+
use Google\Cloud\Dlp\V2\StorageConfig;
35+
use Google\Cloud\Dlp\V2\Likelihood;
36+
use Google\Cloud\Dlp\V2\Action;
37+
use Google\Cloud\Dlp\V2\Action\PublishSummaryToCscc;
38+
use Google\Cloud\Dlp\V2\InspectJobConfig;
39+
use Google\Cloud\Dlp\V2\StorageConfig\TimespanConfig;
40+
41+
/**
42+
* Creates an inspection job with the Cloud Data Loss Prevention API.
43+
*
44+
* @param string $callingProjectId The project ID to run the API call under.
45+
* @param string $gcsPath GCS file to be inspected. Example : gs://GOOGLE_STORAGE_BUCKET_NAME/dlp_sample.csv
46+
*/
47+
function create_job(
48+
string $callingProjectId,
49+
string $gcsPath
50+
): void {
51+
// Instantiate a client.
52+
$dlp = new DlpServiceClient();
53+
54+
// Set autoPopulateTimespan to true to scan only new content.
55+
$timespanConfig = (new TimespanConfig())
56+
->setEnableAutoPopulationOfTimespanConfig(true);
57+
58+
// Specify the GCS file to be inspected.
59+
$cloudStorageOptions = (new CloudStorageOptions())
60+
->setFileSet((new FileSet())
61+
->setUrl($gcsPath));
62+
$storageConfig = (new StorageConfig())
63+
->setCloudStorageOptions(($cloudStorageOptions))
64+
->setTimespanConfig($timespanConfig);
65+
66+
// ----- Construct inspection config -----
67+
$emailAddressInfoType = (new InfoType())
68+
->setName('EMAIL_ADDRESS');
69+
$personNameInfoType = (new InfoType())
70+
->setName('PERSON_NAME');
71+
$locationInfoType = (new InfoType())
72+
->setName('LOCATION');
73+
$phoneNumberInfoType = (new InfoType())
74+
->setName('PHONE_NUMBER');
75+
$infoTypes = [$emailAddressInfoType, $personNameInfoType, $locationInfoType, $phoneNumberInfoType];
76+
77+
// Whether to include the matching string in the response.
78+
$includeQuote = true;
79+
// The minimum likelihood required before returning a match.
80+
$minLikelihood = likelihood::LIKELIHOOD_UNSPECIFIED;
81+
82+
// The maximum number of findings to report (0 = server maximum).
83+
$limits = (new FindingLimits())
84+
->setMaxFindingsPerRequest(100);
85+
86+
// Create the Inspect configuration object.
87+
$inspectConfig = (new InspectConfig())
88+
->setMinLikelihood($minLikelihood)
89+
->setLimits($limits)
90+
->setInfoTypes($infoTypes)
91+
->setIncludeQuote($includeQuote);
92+
93+
// Specify the action that is triggered when the job completes.
94+
$action = (new Action())
95+
->setPublishSummaryToCscc(new PublishSummaryToCscc());
96+
97+
// Configure the inspection job we want the service to perform.
98+
$inspectJobConfig = (new InspectJobConfig())
99+
->setInspectConfig($inspectConfig)
100+
->setStorageConfig($storageConfig)
101+
->setActions([$action]);
102+
103+
// Send the job creation request and process the response.
104+
$parent = "projects/$callingProjectId/locations/global";
105+
$job = $dlp->createDlpJob($parent, [
106+
'inspectJob' => $inspectJobConfig
107+
]);
108+
109+
// Print results.
110+
printf($job->getName());
111+
}
112+
# [END dlp_create_job]
113+
// The following 2 lines are only needed to run the samples.
114+
require_once __DIR__ . '/../../testing/sample_helpers.php';
115+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

dlp/src/get_job.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2023 Google Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/**
20+
* For instructions on how to run the samples:
21+
*
22+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/dlp/README.md
23+
*/
24+
25+
namespace Google\Cloud\Samples\Dlp;
26+
27+
# [START dlp_get_job]
28+
use Google\Cloud\Dlp\V2\DlpServiceClient;
29+
30+
/**
31+
* Get DLP inspection job.
32+
* @param string $jobName Dlp job name
33+
*/
34+
function get_job(
35+
string $jobName
36+
): void {
37+
// Instantiate a client.
38+
$dlp = new DlpServiceClient();
39+
try {
40+
// Send the get job request
41+
$response = $dlp->getDlpJob($jobName);
42+
printf('Job %s status: %s' . PHP_EOL, $response->getName(), $response->getState());
43+
} finally {
44+
$dlp->close();
45+
}
46+
}
47+
# [END dlp_get_job]
48+
49+
// The following 2 lines are only needed to run the samples
50+
require_once __DIR__ . '/../../testing/sample_helpers.php';
51+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

dlp/test/dlpTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,44 @@ public function testInspectTable()
655655
$this->assertStringNotContainsString('Info type: PERSON_NAME', $output);
656656
}
657657

658+
public function testGetJob()
659+
{
660+
661+
// Set filter to only go back a day, so that we do not pull every job.
662+
$filter = sprintf(
663+
'state=DONE AND end_time>"%sT00:00:00+00:00"',
664+
date('Y-m-d', strtotime('-1 day'))
665+
);
666+
$jobIdRegex = "~projects/.*/dlpJobs/i-\d+~";
667+
$getJobName = $this->runFunctionSnippet('list_jobs', [
668+
self::$projectId,
669+
$filter,
670+
]);
671+
preg_match($jobIdRegex, $getJobName, $jobIds);
672+
$jobName = $jobIds[0];
673+
674+
$output = $this->runFunctionSnippet('get_job', [
675+
$jobName
676+
]);
677+
$this->assertStringContainsString('Job ' . $jobName . ' status:', $output);
678+
}
679+
680+
public function testCreateJob()
681+
{
682+
$gcsPath = $this->requireEnv('GCS_PATH');
683+
$jobIdRegex = "~projects/.*/dlpJobs/i-\d+~";
684+
$jobName = $this->runFunctionSnippet('create_job', [
685+
self::$projectId,
686+
$gcsPath
687+
]);
688+
$this->assertRegExp($jobIdRegex, $jobName);
689+
$output = $this->runFunctionSnippet(
690+
'delete_job',
691+
[$jobName]
692+
);
693+
$this->assertStringContainsString('Successfully deleted job ' . $jobName, $output);
694+
}
695+
658696
public function testRedactImageListedInfotypes()
659697
{
660698
$imagePath = __DIR__ . '/data/test.png';

0 commit comments

Comments
 (0)