Skip to content

Commit

Permalink
chore: upgrade dlp samples to new surface (#1898)
Browse files Browse the repository at this point in the history
  • Loading branch information
bshaffer authored Jan 5, 2024
1 parent c3054b4 commit fd2b039
Show file tree
Hide file tree
Showing 73 changed files with 772 additions and 603 deletions.
4 changes: 2 additions & 2 deletions dlp/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "google/dlp-sample",
"type": "project",
"require": {
"google/cloud-dlp": "^1.0.0",
"google/cloud-pubsub": "^1.11.1"
"google/cloud-dlp": "^1.12",
"google/cloud-pubsub": "^1.49"
}
}
15 changes: 8 additions & 7 deletions dlp/quickstart.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
require __DIR__ . '/vendor/autoload.php';

# [START dlp_quickstart]
use Google\Cloud\Dlp\V2\DlpServiceClient;
use Google\Cloud\Dlp\V2\Client\DlpServiceClient;
use Google\Cloud\Dlp\V2\ContentItem;
use Google\Cloud\Dlp\V2\InfoType;
use Google\Cloud\Dlp\V2\InspectConfig;
use Google\Cloud\Dlp\V2\Likelihood;
use Google\Cloud\Dlp\V2\InspectConfig\FindingLimits;
use Google\Cloud\Dlp\V2\InspectContentRequest;
use Google\Cloud\Dlp\V2\Likelihood;

// Instantiate a client.
$dlp = new DlpServiceClient();
Expand Down Expand Up @@ -66,11 +67,11 @@
$parent = $dlp->projectName($projectId);

// Run request
$response = $dlp->inspectContent([
'parent' => $parent,
'inspectConfig' => $inspectConfig,
'item' => $content
]);
$inspectContentRequest = (new InspectContentRequest())
->setParent($parent)
->setInspectConfig($inspectConfig)
->setItem($content);
$response = $dlp->inspectContent($inspectContentRequest);

// Print the results
$findings = $response->getResult()->getFindings();
Expand Down
25 changes: 15 additions & 10 deletions dlp/src/categorical_stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@
namespace Google\Cloud\Samples\Dlp;

# [START dlp_categorical_stats]
use Google\Cloud\Dlp\V2\DlpServiceClient;
use Google\Cloud\Dlp\V2\RiskAnalysisJobConfig;
use Google\Cloud\Dlp\V2\BigQueryTable;
use Google\Cloud\Dlp\V2\DlpJob\JobState;
use Google\Cloud\Dlp\V2\Action;
use Google\Cloud\Dlp\V2\Action\PublishToPubSub;
use Google\Cloud\Dlp\V2\PrivacyMetric\CategoricalStatsConfig;
use Google\Cloud\Dlp\V2\PrivacyMetric;
use Google\Cloud\Dlp\V2\BigQueryTable;
use Google\Cloud\Dlp\V2\Client\DlpServiceClient;
use Google\Cloud\Dlp\V2\CreateDlpJobRequest;
use Google\Cloud\Dlp\V2\DlpJob\JobState;
use Google\Cloud\Dlp\V2\FieldId;
use Google\Cloud\Dlp\V2\GetDlpJobRequest;
use Google\Cloud\Dlp\V2\PrivacyMetric;
use Google\Cloud\Dlp\V2\PrivacyMetric\CategoricalStatsConfig;
use Google\Cloud\Dlp\V2\RiskAnalysisJobConfig;
use Google\Cloud\PubSub\PubSubClient;

/**
Expand Down Expand Up @@ -91,9 +93,10 @@ function categorical_stats(

// Submit request
$parent = "projects/$callingProjectId/locations/global";
$job = $dlp->createDlpJob($parent, [
'riskJob' => $riskJob
]);
$createDlpJobRequest = (new CreateDlpJobRequest())
->setParent($parent)
->setRiskJob($riskJob);
$job = $dlp->createDlpJob($createDlpJobRequest);

// Listen for job notifications via an existing topic/subscription.
$subscription = $topic->subscription($subscriptionId);
Expand All @@ -111,7 +114,9 @@ function categorical_stats(
$subscription->acknowledge($message);
// Get the updated job. Loop to avoid race condition with DLP API.
do {
$job = $dlp->getDlpJob($job->getName());
$getDlpJobRequest = (new GetDlpJobRequest())
->setName($job->getName());
$job = $dlp->getDlpJob($getDlpJobRequest);
} while ($job->getState() == JobState::RUNNING);
break 2; // break from parent do while
}
Expand Down
13 changes: 8 additions & 5 deletions dlp/src/create_inspect_template.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@
namespace Google\Cloud\Samples\Dlp;

// [START dlp_create_inspect_template]
use Google\Cloud\Dlp\V2\DlpServiceClient;
use Google\Cloud\Dlp\V2\Client\DlpServiceClient;
use Google\Cloud\Dlp\V2\CreateInspectTemplateRequest;
use Google\Cloud\Dlp\V2\InfoType;
use Google\Cloud\Dlp\V2\InspectConfig;
use Google\Cloud\Dlp\V2\InspectConfig\FindingLimits;
use Google\Cloud\Dlp\V2\InspectTemplate;
use Google\Cloud\Dlp\V2\Likelihood;
use Google\Cloud\Dlp\V2\InspectConfig\FindingLimits;

/**
* Create a new DLP inspection configuration template.
Expand Down Expand Up @@ -84,9 +85,11 @@ function create_inspect_template(

// Run request
$parent = "projects/$callingProjectId/locations/global";
$template = $dlp->createInspectTemplate($parent, $inspectTemplate, [
'templateId' => $templateId
]);
$createInspectTemplateRequest = (new CreateInspectTemplateRequest())
->setParent($parent)
->setInspectTemplate($inspectTemplate)
->setTemplateId($templateId);
$template = $dlp->createInspectTemplate($createInspectTemplateRequest);

// Print results
printf('Successfully created template %s' . PHP_EOL, $template->getName());
Expand Down
18 changes: 10 additions & 8 deletions dlp/src/create_job.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,18 @@
namespace Google\Cloud\Samples\Dlp;

# [START dlp_create_job]
use Google\Cloud\Dlp\V2\Action;
use Google\Cloud\Dlp\V2\Action\PublishSummaryToCscc;
use Google\Cloud\Dlp\V2\Client\DlpServiceClient;
use Google\Cloud\Dlp\V2\CloudStorageOptions;
use Google\Cloud\Dlp\V2\CloudStorageOptions\FileSet;
use Google\Cloud\Dlp\V2\DlpServiceClient;
use Google\Cloud\Dlp\V2\CreateDlpJobRequest;
use Google\Cloud\Dlp\V2\InfoType;
use Google\Cloud\Dlp\V2\InspectConfig;
use Google\Cloud\Dlp\V2\InspectConfig\FindingLimits;
use Google\Cloud\Dlp\V2\StorageConfig;
use Google\Cloud\Dlp\V2\Likelihood;
use Google\Cloud\Dlp\V2\Action;
use Google\Cloud\Dlp\V2\Action\PublishSummaryToCscc;
use Google\Cloud\Dlp\V2\InspectJobConfig;
use Google\Cloud\Dlp\V2\Likelihood;
use Google\Cloud\Dlp\V2\StorageConfig;
use Google\Cloud\Dlp\V2\StorageConfig\TimespanConfig;

/**
Expand Down Expand Up @@ -102,9 +103,10 @@ function create_job(

// Send the job creation request and process the response.
$parent = "projects/$callingProjectId/locations/global";
$job = $dlp->createDlpJob($parent, [
'inspectJob' => $inspectJobConfig
]);
$createDlpJobRequest = (new CreateDlpJobRequest())
->setParent($parent)
->setInspectJob($inspectJobConfig);
$job = $dlp->createDlpJob($createDlpJobRequest);

// Print results.
printf($job->getName());
Expand Down
28 changes: 15 additions & 13 deletions dlp/src/create_trigger.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,20 @@
namespace Google\Cloud\Samples\Dlp;

// [START dlp_create_trigger]
use Google\Cloud\Dlp\V2\DlpServiceClient;
use Google\Cloud\Dlp\V2\JobTrigger;
use Google\Cloud\Dlp\V2\JobTrigger\Trigger;
use Google\Cloud\Dlp\V2\JobTrigger\Status;
use Google\Cloud\Dlp\V2\InspectConfig;
use Google\Cloud\Dlp\V2\InspectJobConfig;
use Google\Cloud\Dlp\V2\Schedule;
use Google\Cloud\Dlp\V2\Client\DlpServiceClient;
use Google\Cloud\Dlp\V2\CloudStorageOptions;
use Google\Cloud\Dlp\V2\CloudStorageOptions\FileSet;
use Google\Cloud\Dlp\V2\StorageConfig;
use Google\Cloud\Dlp\V2\CreateJobTriggerRequest;
use Google\Cloud\Dlp\V2\InfoType;
use Google\Cloud\Dlp\V2\Likelihood;
use Google\Cloud\Dlp\V2\InspectConfig;
use Google\Cloud\Dlp\V2\InspectConfig\FindingLimits;
use Google\Cloud\Dlp\V2\InspectJobConfig;
use Google\Cloud\Dlp\V2\JobTrigger;
use Google\Cloud\Dlp\V2\JobTrigger\Status;
use Google\Cloud\Dlp\V2\JobTrigger\Trigger;
use Google\Cloud\Dlp\V2\Likelihood;
use Google\Cloud\Dlp\V2\Schedule;
use Google\Cloud\Dlp\V2\StorageConfig;
use Google\Cloud\Dlp\V2\StorageConfig\TimespanConfig;
use Google\Protobuf\Duration;

Expand Down Expand Up @@ -125,11 +126,12 @@ function create_trigger(
->setDescription($description);

// Run trigger creation request
// $parent = "projects/$callingProjectId/locations/global";
$parent = $dlp->locationName($callingProjectId, 'global');
$trigger = $dlp->createJobTrigger($parent, $jobTriggerObject, [
'triggerId' => $triggerId
]);
$createJobTriggerRequest = (new CreateJobTriggerRequest())
->setParent($parent)
->setJobTrigger($jobTriggerObject)
->setTriggerId($triggerId);
$trigger = $dlp->createJobTrigger($createJobTriggerRequest);

// Print results
printf('Successfully created trigger %s' . PHP_EOL, $trigger->getName());
Expand Down
27 changes: 16 additions & 11 deletions dlp/src/deidentify_cloud_storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,22 @@
namespace Google\Cloud\Samples\Dlp;

# [START dlp_deidentify_cloud_storage]
use Google\Cloud\Dlp\V2\CloudStorageOptions;
use Google\Cloud\Dlp\V2\CloudStorageOptions\FileSet;
use Google\Cloud\Dlp\V2\DlpServiceClient;
use Google\Cloud\Dlp\V2\InfoType;
use Google\Cloud\Dlp\V2\InspectConfig;
use Google\Cloud\Dlp\V2\StorageConfig;
use Google\Cloud\Dlp\V2\Action;
use Google\Cloud\Dlp\V2\Action\Deidentify;
use Google\Cloud\Dlp\V2\BigQueryTable;
use Google\Cloud\Dlp\V2\Client\DlpServiceClient;
use Google\Cloud\Dlp\V2\CloudStorageOptions;
use Google\Cloud\Dlp\V2\CloudStorageOptions\FileSet;
use Google\Cloud\Dlp\V2\CreateDlpJobRequest;
use Google\Cloud\Dlp\V2\DlpJob\JobState;
use Google\Cloud\Dlp\V2\FileType;
use Google\Cloud\Dlp\V2\GetDlpJobRequest;
use Google\Cloud\Dlp\V2\InfoType;
use Google\Cloud\Dlp\V2\InspectConfig;
use Google\Cloud\Dlp\V2\InspectJobConfig;
use Google\Cloud\Dlp\V2\StorageConfig;
use Google\Cloud\Dlp\V2\TransformationConfig;
use Google\Cloud\Dlp\V2\TransformationDetailsStorageConfig;
use Google\Cloud\Dlp\V2\DlpJob\JobState;

/**
* De-identify sensitive data stored in Cloud Storage using the API.
Expand Down Expand Up @@ -128,15 +130,18 @@ function deidentify_cloud_storage(
->setActions([$action]);

// Send the job creation request and process the response.
$job = $dlp->createDlpJob($parent, [
'inspectJob' => $inspectJobConfig
]);
$createDlpJobRequest = (new CreateDlpJobRequest())
->setParent($parent)
->setInspectJob($inspectJobConfig);
$job = $dlp->createDlpJob($createDlpJobRequest);

$numOfAttempts = 10;
do {
printf('Waiting for job to complete' . PHP_EOL);
sleep(30);
$job = $dlp->getDlpJob($job->getName());
$getDlpJobRequest = (new GetDlpJobRequest())
->setName($job->getName());
$job = $dlp->getDlpJob($getDlpJobRequest);
if ($job->getState() == JobState::DONE) {
break;
}
Expand Down
13 changes: 7 additions & 6 deletions dlp/src/deidentify_dates.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@
# [START dlp_deidentify_date_shift]
use DateTime;
use Exception;
use Google\Cloud\Dlp\V2\Client\DlpServiceClient;
use Google\Cloud\Dlp\V2\ContentItem;
use Google\Cloud\Dlp\V2\CryptoKey;
use Google\Cloud\Dlp\V2\DateShiftConfig;
use Google\Cloud\Dlp\V2\DeidentifyConfig;
use Google\Cloud\Dlp\V2\DlpServiceClient;
use Google\Cloud\Dlp\V2\DeidentifyContentRequest;
use Google\Cloud\Dlp\V2\FieldId;
use Google\Cloud\Dlp\V2\FieldTransformation;
use Google\Cloud\Dlp\V2\KmsWrappedCryptoKey;
Expand Down Expand Up @@ -155,11 +156,11 @@ function deidentify_dates(
$parent = "projects/$callingProjectId/locations/global";

// Run request
$response = $dlp->deidentifyContent([
'parent' => $parent,
'deidentifyConfig' => $deidentifyConfig,
'item' => $item
]);
$deidentifyContentRequest = (new DeidentifyContentRequest())
->setParent($parent)
->setDeidentifyConfig($deidentifyConfig)
->setItem($item);
$response = $dlp->deidentifyContent($deidentifyContentRequest);

// Check for errors
foreach ($response->getOverview()->getTransformationSummaries() as $summary) {
Expand Down
24 changes: 12 additions & 12 deletions dlp/src/deidentify_deterministic.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,18 @@

# [START dlp_deidentify_deterministic]

use Google\Cloud\Dlp\V2\DlpServiceClient;
use Google\Cloud\Dlp\V2\PrimitiveTransformation;
use Google\Cloud\Dlp\V2\DeidentifyConfig;
use Google\Cloud\Dlp\V2\Client\DlpServiceClient;
use Google\Cloud\Dlp\V2\ContentItem;
use Google\Cloud\Dlp\V2\CryptoDeterministicConfig;
use Google\Cloud\Dlp\V2\CryptoKey;
use Google\Cloud\Dlp\V2\DeidentifyConfig;
use Google\Cloud\Dlp\V2\DeidentifyContentRequest;
use Google\Cloud\Dlp\V2\InfoType;
use Google\Cloud\Dlp\V2\InfoTypeTransformations\InfoTypeTransformation;
use Google\Cloud\Dlp\V2\InfoTypeTransformations;
use Google\Cloud\Dlp\V2\InfoTypeTransformations\InfoTypeTransformation;
use Google\Cloud\Dlp\V2\InspectConfig;
use Google\Cloud\Dlp\V2\KmsWrappedCryptoKey;
use Google\Cloud\Dlp\V2\CryptoKey;
use Google\Cloud\Dlp\V2\PrimitiveTransformation;

/**
* De-identify content through deterministic encryption.
Expand Down Expand Up @@ -108,13 +109,12 @@ function deidentify_deterministic(
->setInfoTypeTransformations($infoTypeTransformations);

// Send the request and receive response from the service.
$response = $dlp->deidentifyContent([
'parent' => $parent,
'deidentifyConfig' => $deidentifyConfig,
'item' => $content,
'inspectConfig' => $inspectConfig

]);
$deidentifyContentRequest = (new DeidentifyContentRequest())
->setParent($parent)
->setDeidentifyConfig($deidentifyConfig)
->setItem($content)
->setInspectConfig($inspectConfig);
$response = $dlp->deidentifyContent($deidentifyContentRequest);

// Print the results.
printf($response->getItem()->getValue());
Expand Down
21 changes: 11 additions & 10 deletions dlp/src/deidentify_dictionary_replacement.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@
namespace Google\Cloud\Samples\Dlp;

# [START dlp_deidentify_dictionary_replacement]
use Google\Cloud\Dlp\V2\Client\DlpServiceClient;
use Google\Cloud\Dlp\V2\ContentItem;
use Google\Cloud\Dlp\V2\DlpServiceClient;
use Google\Cloud\Dlp\V2\CustomInfoType\Dictionary\WordList;
use Google\Cloud\Dlp\V2\InfoType;
use Google\Cloud\Dlp\V2\DeidentifyConfig;
use Google\Cloud\Dlp\V2\InspectConfig;
use Google\Cloud\Dlp\V2\PrimitiveTransformation;
use Google\Cloud\Dlp\V2\DeidentifyContentRequest;
use Google\Cloud\Dlp\V2\InfoType;
use Google\Cloud\Dlp\V2\InfoTypeTransformations;
use Google\Cloud\Dlp\V2\InfoTypeTransformations\InfoTypeTransformation;
use Google\Cloud\Dlp\V2\InspectConfig;
use Google\Cloud\Dlp\V2\PrimitiveTransformation;
use Google\Cloud\Dlp\V2\ReplaceDictionaryConfig;

/**
Expand Down Expand Up @@ -90,12 +91,12 @@ function deidentify_dictionary_replacement(

// Send the request and receive response from the service.
$parent = "projects/$callingProjectId/locations/global";
$response = $dlp->deidentifyContent([
'parent' => $parent,
'deidentifyConfig' => $deidentifyConfig,
'inspectConfig' => $inspectConfig,
'item' => $contentItem
]);
$deidentifyContentRequest = (new DeidentifyContentRequest())
->setParent($parent)
->setDeidentifyConfig($deidentifyConfig)
->setInspectConfig($inspectConfig)
->setItem($contentItem);
$response = $dlp->deidentifyContent($deidentifyContentRequest);

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

0 comments on commit fd2b039

Please sign in to comment.