Skip to content

Commit 70bd560

Browse files
authored
feat(Dlp): upgrade remaining samples to v2 (#2114)
1 parent ce10901 commit 70bd560

17 files changed

+186
-127
lines changed

dlp/src/create_stored_infotype.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@
2727

2828
use Google\Cloud\Dlp\V2\BigQueryField;
2929
use Google\Cloud\Dlp\V2\BigQueryTable;
30-
use Google\Cloud\Dlp\V2\DlpServiceClient;
30+
use Google\Cloud\Dlp\V2\Client\DlpServiceClient;
3131
use Google\Cloud\Dlp\V2\CloudStoragePath;
32+
use Google\Cloud\Dlp\V2\CreateStoredInfoTypeRequest;
3233
use Google\Cloud\Dlp\V2\FieldId;
3334
use Google\Cloud\Dlp\V2\LargeCustomDictionaryConfig;
3435
use Google\Cloud\Dlp\V2\StoredInfoTypeConfig;
@@ -77,9 +78,11 @@ function create_stored_infotype(
7778

7879
// Send the stored infoType creation request and process the response.
7980
$parent = "projects/$callingProjectId/locations/global";
80-
$response = $dlp->createStoredInfoType($parent, $storedInfoTypeConfig, [
81-
'storedInfoTypeId' => $storedInfoTypeId
82-
]);
81+
$createStoredInfoTypeRequest = (new CreateStoredInfoTypeRequest())
82+
->setParent($parent)
83+
->setConfig($storedInfoTypeConfig)
84+
->setStoredInfoTypeId($storedInfoTypeId);
85+
$response = $dlp->createStoredInfoType($createStoredInfoTypeRequest);
8386

8487
// Print results.
8588
printf('Successfully created Stored InfoType : %s', $response->getName());

dlp/src/deidentify_replace_infotype.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@
2424
namespace Google\Cloud\Samples\Dlp;
2525

2626
# [START dlp_deidentify_replace_infotype]
27-
use Google\Cloud\Dlp\V2\DlpServiceClient;
28-
use Google\Cloud\Dlp\V2\PrimitiveTransformation;
29-
use Google\Cloud\Dlp\V2\InfoType;
27+
use Google\Cloud\Dlp\V2\Client\DlpServiceClient;
28+
use Google\Cloud\Dlp\V2\ContentItem;
3029
use Google\Cloud\Dlp\V2\DeidentifyConfig;
31-
use Google\Cloud\Dlp\V2\InfoTypeTransformations\InfoTypeTransformation;
30+
use Google\Cloud\Dlp\V2\DeidentifyContentRequest;
31+
use Google\Cloud\Dlp\V2\InfoType;
3232
use Google\Cloud\Dlp\V2\InfoTypeTransformations;
33-
use Google\Cloud\Dlp\V2\ContentItem;
33+
use Google\Cloud\Dlp\V2\InfoTypeTransformations\InfoTypeTransformation;
3434
use Google\Cloud\Dlp\V2\InspectConfig;
35+
use Google\Cloud\Dlp\V2\PrimitiveTransformation;
3536
use Google\Cloud\Dlp\V2\ReplaceWithInfoTypeConfig;
3637

3738
/**
@@ -83,12 +84,12 @@ function deidentify_replace_infotype(
8384
->setInfoTypeTransformations($infoTypeTransformations);
8485

8586
// Run request.
86-
$response = $dlp->deidentifyContent([
87-
'parent' => $parent,
88-
'deidentifyConfig' => $deidentifyConfig,
89-
'item' => $content,
90-
'inspectConfig' => $inspectConfig
91-
]);
87+
$deidentifyContentRequest = (new DeidentifyContentRequest())
88+
->setParent($parent)
89+
->setDeidentifyConfig($deidentifyConfig)
90+
->setItem($content)
91+
->setInspectConfig($inspectConfig);
92+
$response = $dlp->deidentifyContent($deidentifyContentRequest);
9293

9394
// Print the results.
9495
printf('Text after replace with infotype config: %s', $response->getItem()->getValue());

dlp/src/inspect_bigquery_send_to_scc.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,20 @@
2424
namespace Google\Cloud\Samples\Dlp;
2525

2626
# [START dlp_inspect_bigquery_send_to_scc]
27-
use Google\Cloud\Dlp\V2\DlpServiceClient;
28-
use Google\Cloud\Dlp\V2\InfoType;
29-
use Google\Cloud\Dlp\V2\InspectConfig;
30-
use Google\Cloud\Dlp\V2\InspectConfig\FindingLimits;
31-
use Google\Cloud\Dlp\V2\StorageConfig;
32-
use Google\Cloud\Dlp\V2\Likelihood;
3327
use Google\Cloud\Dlp\V2\Action;
3428
use Google\Cloud\Dlp\V2\Action\PublishSummaryToCscc;
3529
use Google\Cloud\Dlp\V2\BigQueryOptions;
3630
use Google\Cloud\Dlp\V2\BigQueryTable;
37-
use Google\Cloud\Dlp\V2\InspectJobConfig;
31+
use Google\Cloud\Dlp\V2\Client\DlpServiceClient;
32+
use Google\Cloud\Dlp\V2\CreateDlpJobRequest;
3833
use Google\Cloud\Dlp\V2\DlpJob\JobState;
34+
use Google\Cloud\Dlp\V2\GetDlpJobRequest;
35+
use Google\Cloud\Dlp\V2\InfoType;
36+
use Google\Cloud\Dlp\V2\InspectConfig;
37+
use Google\Cloud\Dlp\V2\InspectConfig\FindingLimits;
38+
use Google\Cloud\Dlp\V2\InspectJobConfig;
39+
use Google\Cloud\Dlp\V2\Likelihood;
40+
use Google\Cloud\Dlp\V2\StorageConfig;
3941

4042
/**
4143
* (BIGQUERY) Send Cloud DLP scan results to Security Command Center.
@@ -95,15 +97,18 @@ function inspect_bigquery_send_to_scc(
9597

9698
// Send the job creation request and process the response.
9799
$parent = "projects/$callingProjectId/locations/global";
98-
$job = $dlp->createDlpJob($parent, [
99-
'inspectJob' => $inspectJobConfig
100-
]);
100+
$createDlpJobRequest = (new CreateDlpJobRequest())
101+
->setParent($parent)
102+
->setInspectJob($inspectJobConfig);
103+
$job = $dlp->createDlpJob($createDlpJobRequest);
101104

102105
$numOfAttempts = 10;
103106
do {
104107
printf('Waiting for job to complete' . PHP_EOL);
105108
sleep(10);
106-
$job = $dlp->getDlpJob($job->getName());
109+
$getDlpJobRequest = (new GetDlpJobRequest())
110+
->setName($job->getName());
111+
$job = $dlp->getDlpJob($getDlpJobRequest);
107112
if ($job->getState() == JobState::DONE) {
108113
break;
109114
}

dlp/src/inspect_bigquery_with_sampling.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,20 @@
2626

2727
# [START dlp_inspect_bigquery_with_sampling]
2828

29-
use Google\Cloud\Dlp\V2\DlpServiceClient;
30-
use Google\Cloud\Dlp\V2\BigQueryOptions;
31-
use Google\Cloud\Dlp\V2\InfoType;
32-
use Google\Cloud\Dlp\V2\InspectConfig;
33-
use Google\Cloud\Dlp\V2\StorageConfig;
34-
use Google\Cloud\Dlp\V2\BigQueryTable;
35-
use Google\Cloud\Dlp\V2\DlpJob\JobState;
3629
use Google\Cloud\Dlp\V2\Action;
3730
use Google\Cloud\Dlp\V2\Action\PublishToPubSub;
31+
use Google\Cloud\Dlp\V2\BigQueryOptions;
3832
use Google\Cloud\Dlp\V2\BigQueryOptions\SampleMethod;
33+
use Google\Cloud\Dlp\V2\BigQueryTable;
34+
use Google\Cloud\Dlp\V2\Client\DlpServiceClient;
35+
use Google\Cloud\Dlp\V2\CreateDlpJobRequest;
36+
use Google\Cloud\Dlp\V2\DlpJob\JobState;
3937
use Google\Cloud\Dlp\V2\FieldId;
38+
use Google\Cloud\Dlp\V2\GetDlpJobRequest;
39+
use Google\Cloud\Dlp\V2\InfoType;
40+
use Google\Cloud\Dlp\V2\InspectConfig;
4041
use Google\Cloud\Dlp\V2\InspectJobConfig;
42+
use Google\Cloud\Dlp\V2\StorageConfig;
4143
use Google\Cloud\PubSub\PubSubClient;
4244

4345
/**
@@ -113,9 +115,10 @@ function inspect_bigquery_with_sampling(
113115

114116
// Submit request
115117
$parent = "projects/$callingProjectId/locations/global";
116-
$job = $dlp->createDlpJob($parent, [
117-
'inspectJob' => $inspectJob
118-
]);
118+
$createDlpJobRequest = (new CreateDlpJobRequest())
119+
->setParent($parent)
120+
->setInspectJob($inspectJob);
121+
$job = $dlp->createDlpJob($createDlpJobRequest);
119122

120123
// Poll Pub/Sub using exponential backoff until job finishes
121124
// Consider using an asynchronous execution model such as Cloud Functions
@@ -130,7 +133,9 @@ function inspect_bigquery_with_sampling(
130133
$subscription->acknowledge($message);
131134
// Get the updated job. Loop to avoid race condition with DLP API.
132135
do {
133-
$job = $dlp->getDlpJob($job->getName());
136+
$getDlpJobRequest = (new GetDlpJobRequest())
137+
->setName($job->getName());
138+
$job = $dlp->getDlpJob($getDlpJobRequest);
134139
} while ($job->getState() == JobState::RUNNING);
135140
break 2; // break from parent do while
136141
}

dlp/src/inspect_datastore_send_to_scc.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,21 @@
2424
namespace Google\Cloud\Samples\Dlp;
2525

2626
# [START dlp_inspect_datastore_send_to_scc]
27-
use Google\Cloud\Dlp\V2\DlpServiceClient;
28-
use Google\Cloud\Dlp\V2\InfoType;
29-
use Google\Cloud\Dlp\V2\InspectConfig;
30-
use Google\Cloud\Dlp\V2\InspectConfig\FindingLimits;
31-
use Google\Cloud\Dlp\V2\StorageConfig;
32-
use Google\Cloud\Dlp\V2\Likelihood;
3327
use Google\Cloud\Dlp\V2\Action;
3428
use Google\Cloud\Dlp\V2\Action\PublishSummaryToCscc;
29+
use Google\Cloud\Dlp\V2\Client\DlpServiceClient;
30+
use Google\Cloud\Dlp\V2\CreateDlpJobRequest;
3531
use Google\Cloud\Dlp\V2\DatastoreOptions;
32+
use Google\Cloud\Dlp\V2\DlpJob\JobState;
33+
use Google\Cloud\Dlp\V2\GetDlpJobRequest;
34+
use Google\Cloud\Dlp\V2\InfoType;
35+
use Google\Cloud\Dlp\V2\InspectConfig;
36+
use Google\Cloud\Dlp\V2\InspectConfig\FindingLimits;
3637
use Google\Cloud\Dlp\V2\InspectJobConfig;
3738
use Google\Cloud\Dlp\V2\KindExpression;
39+
use Google\Cloud\Dlp\V2\Likelihood;
3840
use Google\Cloud\Dlp\V2\PartitionId;
39-
use Google\Cloud\Dlp\V2\DlpJob\JobState;
41+
use Google\Cloud\Dlp\V2\StorageConfig;
4042

4143
/**
4244
* (DATASTORE) Send Cloud DLP scan results to Security Command Center.
@@ -93,15 +95,18 @@ function inspect_datastore_send_to_scc(
9395

9496
// Send the job creation request and process the response.
9597
$parent = "projects/$callingProjectId/locations/global";
96-
$job = $dlp->createDlpJob($parent, [
97-
'inspectJob' => $inspectJobConfig
98-
]);
98+
$createDlpJobRequest = (new CreateDlpJobRequest())
99+
->setParent($parent)
100+
->setInspectJob($inspectJobConfig);
101+
$job = $dlp->createDlpJob($createDlpJobRequest);
99102

100103
$numOfAttempts = 10;
101104
do {
102105
printf('Waiting for job to complete' . PHP_EOL);
103106
sleep(10);
104-
$job = $dlp->getDlpJob($job->getName());
107+
$getDlpJobRequest = (new GetDlpJobRequest())
108+
->setName($job->getName());
109+
$job = $dlp->getDlpJob($getDlpJobRequest);
105110
if ($job->getState() == JobState::DONE) {
106111
break;
107112
}

dlp/src/inspect_gcs_send_to_scc.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,20 @@
2424
namespace Google\Cloud\Samples\Dlp;
2525

2626
# [START dlp_inspect_gcs_send_to_scc]
27+
use Google\Cloud\Dlp\V2\Action;
28+
use Google\Cloud\Dlp\V2\Action\PublishSummaryToCscc;
29+
use Google\Cloud\Dlp\V2\Client\DlpServiceClient;
2730
use Google\Cloud\Dlp\V2\CloudStorageOptions;
2831
use Google\Cloud\Dlp\V2\CloudStorageOptions\FileSet;
29-
use Google\Cloud\Dlp\V2\DlpServiceClient;
32+
use Google\Cloud\Dlp\V2\CreateDlpJobRequest;
33+
use Google\Cloud\Dlp\V2\DlpJob\JobState;
34+
use Google\Cloud\Dlp\V2\GetDlpJobRequest;
3035
use Google\Cloud\Dlp\V2\InfoType;
3136
use Google\Cloud\Dlp\V2\InspectConfig;
3237
use Google\Cloud\Dlp\V2\InspectConfig\FindingLimits;
33-
use Google\Cloud\Dlp\V2\StorageConfig;
34-
use Google\Cloud\Dlp\V2\Likelihood;
35-
use Google\Cloud\Dlp\V2\Action;
36-
use Google\Cloud\Dlp\V2\Action\PublishSummaryToCscc;
3738
use Google\Cloud\Dlp\V2\InspectJobConfig;
38-
use Google\Cloud\Dlp\V2\DlpJob\JobState;
39+
use Google\Cloud\Dlp\V2\Likelihood;
40+
use Google\Cloud\Dlp\V2\StorageConfig;
3941

4042
/**
4143
* (GCS) Send Cloud DLP scan results to Security Command Center.
@@ -88,15 +90,18 @@ function inspect_gcs_send_to_scc(
8890

8991
// Send the job creation request and process the response.
9092
$parent = "projects/$callingProjectId/locations/global";
91-
$job = $dlp->createDlpJob($parent, [
92-
'inspectJob' => $inspectJobConfig
93-
]);
93+
$createDlpJobRequest = (new CreateDlpJobRequest())
94+
->setParent($parent)
95+
->setInspectJob($inspectJobConfig);
96+
$job = $dlp->createDlpJob($createDlpJobRequest);
9497

9598
$numOfAttempts = 10;
9699
do {
97100
printf('Waiting for job to complete' . PHP_EOL);
98101
sleep(10);
99-
$job = $dlp->getDlpJob($job->getName());
102+
$getDlpJobRequest = (new GetDlpJobRequest())
103+
->setName($job->getName());
104+
$job = $dlp->getDlpJob($getDlpJobRequest);
100105
if ($job->getState() == JobState::DONE) {
101106
break;
102107
}

dlp/src/inspect_gcs_with_sampling.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,19 @@
2424
namespace Google\Cloud\Samples\Dlp;
2525

2626
# [START dlp_inspect_gcs_with_sampling]
27-
use Google\Cloud\Dlp\V2\DlpServiceClient;
28-
use Google\Cloud\Dlp\V2\InfoType;
29-
use Google\Cloud\Dlp\V2\InspectConfig;
30-
use Google\Cloud\Dlp\V2\StorageConfig;
31-
use Google\Cloud\Dlp\V2\DlpJob\JobState;
3227
use Google\Cloud\Dlp\V2\Action;
3328
use Google\Cloud\Dlp\V2\Action\PublishToPubSub;
3429
use Google\Cloud\Dlp\V2\BigQueryOptions\SampleMethod;
30+
use Google\Cloud\Dlp\V2\Client\DlpServiceClient;
3531
use Google\Cloud\Dlp\V2\CloudStorageOptions;
3632
use Google\Cloud\Dlp\V2\CloudStorageOptions\FileSet;
33+
use Google\Cloud\Dlp\V2\CreateDlpJobRequest;
34+
use Google\Cloud\Dlp\V2\DlpJob\JobState;
35+
use Google\Cloud\Dlp\V2\GetDlpJobRequest;
36+
use Google\Cloud\Dlp\V2\InfoType;
37+
use Google\Cloud\Dlp\V2\InspectConfig;
3738
use Google\Cloud\Dlp\V2\InspectJobConfig;
39+
use Google\Cloud\Dlp\V2\StorageConfig;
3840
use Google\Cloud\PubSub\PubSubClient;
3941

4042
/**
@@ -101,9 +103,10 @@ function inspect_gcs_with_sampling(
101103

102104
// Submit request.
103105
$parent = "projects/$callingProjectId/locations/global";
104-
$job = $dlp->createDlpJob($parent, [
105-
'inspectJob' => $inspectJob
106-
]);
106+
$createDlpJobRequest = (new CreateDlpJobRequest())
107+
->setParent($parent)
108+
->setInspectJob($inspectJob);
109+
$job = $dlp->createDlpJob($createDlpJobRequest);
107110

108111
// Poll Pub/Sub using exponential backoff until job finishes.
109112
// Consider using an asynchronous execution model such as Cloud Functions.
@@ -118,7 +121,9 @@ function inspect_gcs_with_sampling(
118121
$subscription->acknowledge($message);
119122
// Get the updated job. Loop to avoid race condition with DLP API.
120123
do {
121-
$job = $dlp->getDlpJob($job->getName());
124+
$getDlpJobRequest = (new GetDlpJobRequest())
125+
->setName($job->getName());
126+
$job = $dlp->getDlpJob($getDlpJobRequest);
122127
} while ($job->getState() == JobState::RUNNING);
123128
break 2; // break from parent do while.
124129
}

dlp/src/inspect_send_data_to_hybrid_job_trigger.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,16 @@
2626
# [START dlp_inspect_send_data_to_hybrid_job_trigger]
2727

2828
use Google\ApiCore\ApiException;
29+
use Google\Cloud\Dlp\V2\ActivateJobTriggerRequest;
30+
use Google\Cloud\Dlp\V2\Client\DlpServiceClient;
2931
use Google\Cloud\Dlp\V2\Container;
30-
use Google\Cloud\Dlp\V2\DlpServiceClient;
3132
use Google\Cloud\Dlp\V2\ContentItem;
3233
use Google\Cloud\Dlp\V2\DlpJob\JobState;
34+
use Google\Cloud\Dlp\V2\GetDlpJobRequest;
3335
use Google\Cloud\Dlp\V2\HybridContentItem;
3436
use Google\Cloud\Dlp\V2\HybridFindingDetails;
37+
use Google\Cloud\Dlp\V2\HybridInspectJobTriggerRequest;
38+
use Google\Cloud\Dlp\V2\ListDlpJobsRequest;
3539

3640
/**
3741
* Inspect data hybrid job trigger.
@@ -76,23 +80,31 @@ function inspect_send_data_to_hybrid_job_trigger(
7680

7781
$triggerJob = null;
7882
try {
79-
$triggerJob = $dlp->activateJobTrigger($name);
83+
$activateJobTriggerRequest = (new ActivateJobTriggerRequest())
84+
->setName($name);
85+
$triggerJob = $dlp->activateJobTrigger($activateJobTriggerRequest);
8086
} catch (ApiException $e) {
81-
$result = $dlp->listDlpJobs($parent, ['filter' => 'trigger_name=' . $name]);
87+
$listDlpJobsRequest = (new ListDlpJobsRequest())
88+
->setParent($parent)
89+
->setFilter('trigger_name=' . $name);
90+
$result = $dlp->listDlpJobs($listDlpJobsRequest);
8291
foreach ($result as $job) {
8392
$triggerJob = $job;
8493
}
8594
}
95+
$hybridInspectJobTriggerRequest = (new HybridInspectJobTriggerRequest())
96+
->setName($name)
97+
->setHybridItem($hybridItem);
8698

87-
$dlp->hybridInspectJobTrigger($name, [
88-
'hybridItem' => $hybridItem,
89-
]);
99+
$dlp->hybridInspectJobTrigger($hybridInspectJobTriggerRequest);
90100

91101
$numOfAttempts = 10;
92102
do {
93103
printf('Waiting for job to complete' . PHP_EOL);
94104
sleep(10);
95-
$job = $dlp->getDlpJob($triggerJob->getName());
105+
$getDlpJobRequest = (new GetDlpJobRequest())
106+
->setName($triggerJob->getName());
107+
$job = $dlp->getDlpJob($getDlpJobRequest);
96108
if ($job->getState() != JobState::RUNNING) {
97109
break;
98110
}

0 commit comments

Comments
 (0)