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