-
Notifications
You must be signed in to change notification settings - Fork 1k
/
inspect_send_data_to_hybrid_job_trigger.php
137 lines (121 loc) · 4.56 KB
/
inspect_send_data_to_hybrid_job_trigger.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
/**
* Copyright 2023 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* For instructions on how to run the samples:
*
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/dlp/README.md
*/
namespace Google\Cloud\Samples\Dlp;
# [START dlp_inspect_send_data_to_hybrid_job_trigger]
use Google\ApiCore\ApiException;
use Google\Cloud\Dlp\V2\Container;
use Google\Cloud\Dlp\V2\DlpServiceClient;
use Google\Cloud\Dlp\V2\ContentItem;
use Google\Cloud\Dlp\V2\DlpJob\JobState;
use Google\Cloud\Dlp\V2\HybridContentItem;
use Google\Cloud\Dlp\V2\HybridFindingDetails;
/**
* Inspect data hybrid job trigger.
* Send data to the hybrid job or hybrid job trigger.
*
* @param string $callingProjectId The Google Cloud project id to use as a parent resource.
* @param string $string The string to inspect (will be treated as text).
*/
function inspect_send_data_to_hybrid_job_trigger(
// TODO(developer): Replace sample parameters before running the code.
string $callingProjectId,
string $jobTriggerId,
string $string
): void {
// Instantiate a client.
$dlp = new DlpServiceClient();
$content = (new ContentItem())
->setValue($string);
$container = (new Container())
->setFullPath('10.0.0.2:logs1:app1')
->setRelativePath('app1')
->setRootPath('10.0.0.2:logs1')
->setType('logging_sys')
->setVersion('1.2');
$findingDetails = (new HybridFindingDetails())
->setContainerDetails($container)
->setLabels([
'env' => 'prod',
'appointment-bookings-comments' => ''
]);
$hybridItem = (new HybridContentItem())
->setItem($content)
->setFindingDetails($findingDetails);
$parent = "projects/$callingProjectId/locations/global";
$name = "projects/$callingProjectId/locations/global/jobTriggers/" . $jobTriggerId;
$triggerJob = null;
try {
$triggerJob = $dlp->activateJobTrigger($name);
} catch (ApiException $e) {
$result = $dlp->listDlpJobs($parent, ['filter' => 'trigger_name=' . $name]);
foreach ($result as $job) {
$triggerJob = $job;
}
}
$dlp->hybridInspectJobTrigger($name, [
'hybridItem' => $hybridItem,
]);
$numOfAttempts = 10;
do {
printf('Waiting for job to complete' . PHP_EOL);
sleep(10);
$job = $dlp->getDlpJob($triggerJob->getName());
if ($job->getState() != JobState::RUNNING) {
break;
}
$numOfAttempts--;
} while ($numOfAttempts > 0);
// Print finding counts.
printf('Job %s status: %s' . PHP_EOL, $job->getName(), JobState::name($job->getState()));
switch ($job->getState()) {
case JobState::DONE:
$infoTypeStats = $job->getInspectDetails()->getResult()->getInfoTypeStats();
if (count($infoTypeStats) === 0) {
printf('No findings.' . PHP_EOL);
} else {
foreach ($infoTypeStats as $infoTypeStat) {
printf(
' Found %s instance(s) of infoType %s' . PHP_EOL,
$infoTypeStat->getCount(),
$infoTypeStat->getInfoType()->getName()
);
}
}
break;
case JobState::FAILED:
printf('Job %s had errors:' . PHP_EOL, $job->getName());
$errors = $job->getErrors();
foreach ($errors as $error) {
var_dump($error->getDetails());
}
break;
case JobState::PENDING:
printf('Job has not completed. Consider a longer timeout or an asynchronous execution model' . PHP_EOL);
break;
default:
printf('Unexpected job state. Most likely, the job is either running or has not yet started.');
}
}
# [END dlp_inspect_send_data_to_hybrid_job_trigger]
// The following 2 lines are only needed to run the samples.
require_once __DIR__ . '/../../testing/sample_helpers.php';
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);