-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new client upgrade fixer (#143)
- Loading branch information
Showing
40 changed files
with
2,178 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
composer.lock | ||
vendor/ | ||
build/ | ||
.php_cs.cache | ||
.php_cs.cache | ||
.php-cs-fixer.cache | ||
.phpunit.result.cache | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace Google\Cloud\Samples\Dlp; | ||
|
||
// new client surface doesn't exist (yet) | ||
use Google\ApiCore\LongRunning\OperationsClient; | ||
// new client surface exists | ||
use Google\Cloud\Dlp\V2\DlpServiceClient; | ||
// invalid client | ||
use Google\Cloud\Dlp\V2\NonexistentClient; | ||
// new client surface exists | ||
use Google\Cloud\SecretManager\V1\SecretManagerServiceClient; | ||
// new client surface won't exist (not a generator client) | ||
use Google\Cloud\Storage\StorageClient; | ||
|
||
class ClientWrapper extends TestCase | ||
{ | ||
public $dlp; | ||
public $secretmanager; | ||
|
||
public function __construct() | ||
{ | ||
$this->dlp = new DlpServiceClient(); | ||
$this->secretmanager = new SecretManagerServiceClient(); | ||
} | ||
|
||
public function callDlp() | ||
{ | ||
$infoTypes = $this->dlp->listInfoTypes(); | ||
} | ||
|
||
public function callSecretManager() | ||
{ | ||
$secrets = $this->secretmanager->listSecrets('this/is/a/parent'); | ||
} | ||
} | ||
|
||
// Instantiate a wrapping object. | ||
$wrapper = new ClientWrapper(); | ||
|
||
// these should update | ||
$infoTypes = $wrapper->dlp->listInfoTypes(); | ||
$secrets = $wrapper->secretmanager->listSecrets('this/is/a/parent'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
require_once 'vendor/autoload.php'; | ||
|
||
use Google\Cloud\SecretManager\V1\SecretManagerServiceClient; | ||
use Google\Cloud\Dlp\V2\DlpServiceClient; | ||
|
||
// Instantiate clients for calling the Google Cloud APIs. | ||
$secretManagerClient = new SecretManagerServiceClient(); | ||
$dlpClient = new DlpServiceClient(); | ||
|
||
// define the parent | ||
$parent = 'path/to/parent'; | ||
|
||
/** | ||
* Helper function to pretty-print a Protobuf message. | ||
*/ | ||
function print_message(Message $message) | ||
{ | ||
return json_encode( | ||
json_decode($message->serializeToJsonString(), true), | ||
JSON_PRETTY_PRINT | ||
); | ||
} | ||
?> | ||
|
||
<!doctype html> | ||
<html> | ||
<head><meta charset="utf-8"></head> | ||
<body> | ||
<header><h1>Google Cloud Sample App</h1></header> | ||
<div class="main-content"> | ||
<h2 class="collapsible">List Secrets</h2> | ||
<div id="listSecrets" class="collapsible-content"> | ||
<?php foreach ($secretManagerClient->listSecrets($parent) as $secret): ?> | ||
<pre><?= print_message($secret) ?></pre> | ||
<?php endforeach ?> | ||
</div> | ||
|
||
<h2 class="collapsible">List DLP Jobs</h2> | ||
<div id="listDlpJobs" class="collapsible-content"> | ||
<?php foreach ($dlpClient->listDlpJobs($parent) as $job): ?> | ||
<pre><?= print_message($job) ?></pre> | ||
<?php endforeach ?> | ||
</div> | ||
</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
namespace Google\Cloud\Samples\Dlp; | ||
|
||
use Google\Cloud\Dlp\V2\DlpServiceClient; | ||
use Google\Cloud\Dlp\V2\InspectConfig; | ||
use Google\Cloud\Dlp\V2\InspectJobConfig; | ||
use Google\Cloud\Dlp\V2\Likelihood; | ||
use Google\Cloud\Dlp\V2\StorageConfig; | ||
use Google\Cloud\PubSub\PubSubClient; | ||
use Google\Cloud\SecretManager\V1\Replication; | ||
use Google\Cloud\SecretManager\V1\Replication\Automatic; | ||
use Google\Cloud\SecretManager\V1\Secret; | ||
use Google\Cloud\SecretManager\V1\SecretManagerServiceClient; | ||
use Google\Cloud\Unordered\Namespace; | ||
|
||
// Instantiate a client. | ||
$dlp = new DlpServiceClient(); | ||
|
||
// no args | ||
$infoTypes = $dlp->listInfoTypes(); | ||
|
||
// optional args array (variable form) | ||
$dlp->listInfoTypes($foo); | ||
|
||
// required args variable | ||
$dlp->createDlpJob($foo); | ||
|
||
// required args string | ||
$dlp->createDlpJob('this/is/a/parent'); | ||
|
||
// required args array | ||
$dlp->createDlpJob(['jobId' => 'abc', 'locationId' => 'def']); | ||
|
||
// required args variable and optional args array | ||
$dlp->createDlpJob($parent, ['jobId' => 'abc', 'locationId' => 'def']); | ||
|
||
// required args variable and optional args variable | ||
$dlp->createDlpJob($parent, $optionalArgs); | ||
|
||
// required args variable and optional args array with nested array | ||
$job = $dlp->createDlpJob($parent, [ | ||
'inspectJob' => new InspectJobConfig([ | ||
'inspect_config' => (new InspectConfig()) | ||
->setMinLikelihood(likelihood::LIKELIHOOD_UNSPECIFIED) | ||
->setLimits($limits) | ||
->setInfoTypes($infoTypes) | ||
->setIncludeQuote(true), | ||
'storage_config' => (new StorageConfig()) | ||
->setCloudStorageOptions(($cloudStorageOptions)) | ||
->setTimespanConfig($timespanConfig), | ||
]), | ||
'trailingComma' => true, | ||
]); | ||
|
||
$projectId = 'my-project'; | ||
$secretId = 'my-secret'; | ||
|
||
// Create the Secret Manager client. | ||
$client = new SecretManagerServiceClient(); | ||
|
||
// Build the parent name from the project. | ||
$parent = $client->projectName($projectId); | ||
|
||
// Create the parent secret. | ||
$secret = $client->createSecret($parent, $secretId, | ||
new Secret([ | ||
'replication' => new Replication([ | ||
'automatic' => new Automatic(), | ||
]), | ||
]) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Google\Cloud\Samples\Dlp; | ||
|
||
use Google\Cloud\SecretManager\V1\SecretManagerServiceClient; | ||
use Google\Cloud\Iam\V1\Binding; | ||
use Google\Type\Expr; | ||
|
||
$secretManager = new SecretManagerServiceClient(); | ||
$resource = sprintf('projects/%s/instances/%s/databases/%s', $projectId, $instanceId, $databaseId); | ||
$policy = $secretManager->getIamPolicy($resource); | ||
|
||
// IAM conditions need at least version 3 | ||
if ($policy->getVersion() != 3) { | ||
$policy->setVersion(3); | ||
} | ||
|
||
$binding = new Binding([ | ||
'role' => 'roles/spanner.fineGrainedAccessUser', | ||
'members' => [$iamMember], | ||
'condition' => new Expr([ | ||
'title' => $title, | ||
'expression' => sprintf("resource.name.endsWith('/databaseRoles/%s')", $databaseRole) | ||
]) | ||
]); | ||
$policy->setBindings([$binding]); | ||
$secretManager->setIamPolicy($resource, $policy); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace Google\Cloud\Samples\Dlp; | ||
|
||
// new client surface doesn't exist (yet) | ||
use Google\ApiCore\LongRunning\OperationsClient; | ||
// new client surface exists | ||
use Google\Cloud\Dlp\V2\DlpServiceClient; | ||
// invalid client | ||
use Google\Cloud\Dlp\V2\NonexistentClient; | ||
// new client surface exists | ||
use Google\Cloud\SecretManager\V1\SecretManagerServiceClient; | ||
// new client surface won't exist (not a generator client) | ||
use Google\Cloud\Storage\StorageClient; | ||
|
||
// Instantiate a client. | ||
$dlp = new DlpServiceClient(); | ||
$longrunning = new OperationsClient(); | ||
$secretmanager = new SecretManagerServiceClient(); | ||
$storage = new StorageClient(); | ||
|
||
// these should update | ||
$infoTypes = $dlp->listInfoTypes(); | ||
$secrets = $secretmanager->listSecrets('this/is/a/parent'); | ||
|
||
// these shouldn't update | ||
$operations = $longrunning->listOperations(); | ||
$serviceAccount = $storage->getServiceAccount(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Google\Cloud\Samples\Dlp; | ||
|
||
use Google\Cloud\Dlp\V2\DlpServiceClient; | ||
|
||
// Instantiate a client. | ||
$dlp = new DlpServiceClient(); | ||
|
||
// no args | ||
$infoTypes = $dlp->listInfoTypes(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace Google\Cloud\Samples\Dlp; | ||
|
||
|
||
use Google\Cloud\Dlp\V2\DlpServiceClient; | ||
|
||
// Instantiate a client. | ||
$dlp = new DlpServiceClient(); | ||
|
||
// Call a client method which is NOT an RPC | ||
$jobName = $dlp->dlpJobName('my-project', 'my-job'); | ||
|
||
// Call the "close" method | ||
$job = $dlp->close(); | ||
|
||
// Call an RPC method | ||
$job = $dlp->getDlpJob($jobName); | ||
|
||
// Call a non-existant method! | ||
$job = $dlp->getJob($jobName); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Google\Cloud\Samples\Dlp; | ||
|
||
use Google\Cloud\Dlp\V2\DlpServiceClient; | ||
use Google\Cloud\Dlp\V2\InspectConfig; | ||
use Google\Cloud\Dlp\V2\InspectJobConfig; | ||
use Google\Cloud\Dlp\V2\Likelihood; | ||
use Google\Cloud\Dlp\V2\StorageConfig; | ||
|
||
// Instantiate a client. | ||
$dlp = new DlpServiceClient(); | ||
|
||
// optional args array (variable) | ||
$infoTypes = $dlp->listInfoTypes($parent); | ||
|
||
// optional args array (inline array) | ||
$job = $dlp->createDlpJob($parent, ['jobId' => 'abc', 'locationId' => 'def']); | ||
|
||
// optional args array (inline with nested arrays) | ||
$job = $dlp->createDlpJob($parent, [ | ||
'inspectJob' => new InspectJobConfig([ | ||
'inspect_config' => (new InspectConfig()) | ||
->setMinLikelihood(likelihood::LIKELIHOOD_UNSPECIFIED) | ||
->setLimits($limits) | ||
->setInfoTypes($infoTypes) | ||
->setIncludeQuote(true), | ||
'storage_config' => (new StorageConfig()) | ||
->setCloudStorageOptions(($cloudStorageOptions)) | ||
->setTimespanConfig($timespanConfig), | ||
]) | ||
]); |
32 changes: 32 additions & 0 deletions
32
examples/ClientUpgradeFixer/legacy_optional_args_array_keyword.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Google\Cloud\Samples\Dlp; | ||
|
||
use Google\Cloud\Dlp\V2\DlpServiceClient; | ||
use Google\Cloud\Dlp\V2\InspectConfig; | ||
use Google\Cloud\Dlp\V2\InspectJobConfig; | ||
use Google\Cloud\Dlp\V2\Likelihood; | ||
use Google\Cloud\Dlp\V2\StorageConfig; | ||
|
||
// Instantiate a client. | ||
$dlp = new DlpServiceClient(); | ||
|
||
// optional args array (variable) | ||
$infoTypes = $dlp->listInfoTypes($args); | ||
|
||
// optional args array (inline array) | ||
$job = $dlp->createDlpJob($parent, array('jobId' => 'abc', 'locationId' => 'def')); | ||
|
||
// optional args array (inline with nested arrays) | ||
$job = $dlp->createDlpJob($parent, array( | ||
'inspectJob' => new InspectJobConfig(array( | ||
'inspect_config' => (new InspectConfig()) | ||
->setMinLikelihood(likelihood::LIKELIHOOD_UNSPECIFIED) | ||
->setLimits($limits) | ||
->setInfoTypes($infoTypes) | ||
->setIncludeQuote(true), | ||
'storage_config' => (new StorageConfig()) | ||
->setCloudStorageOptions(($cloudStorageOptions)) | ||
->setTimespanConfig($timespanConfig), | ||
)) | ||
)); |
34 changes: 34 additions & 0 deletions
34
examples/ClientUpgradeFixer/legacy_optional_args_variable.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace Google\Cloud\Samples\Dlp; | ||
|
||
use Google\Cloud\Dlp\V2\DlpServiceClient; | ||
use Google\Cloud\Dlp\V2\InspectConfig; | ||
use Google\Cloud\Dlp\V2\InspectJobConfig; | ||
use Google\Cloud\Dlp\V2\Likelihood; | ||
use Google\Cloud\Dlp\V2\StorageConfig; | ||
|
||
// Instantiate a client. | ||
$dlp = new DlpServiceClient(); | ||
|
||
// optional args array (variable) | ||
$infoTypes = $dlp->listInfoTypes($parent); | ||
|
||
// optional args array (inline array) | ||
$options = ['jobId' => 'abc', 'locationId' => 'def']; | ||
$job = $dlp->createDlpJob($parent, $options); | ||
|
||
// optional args array (inline with nested arrays) | ||
$options2 = [ | ||
'inspectJob' => new InspectJobConfig([ | ||
'inspect_config' => (new InspectConfig()) | ||
->setMinLikelihood(likelihood::LIKELIHOOD_UNSPECIFIED) | ||
->setLimits($limits) | ||
->setInfoTypes($infoTypes) | ||
->setIncludeQuote(true), | ||
'storage_config' => (new StorageConfig()) | ||
->setCloudStorageOptions(($cloudStorageOptions)) | ||
->setTimespanConfig($timespanConfig), | ||
]) | ||
]; | ||
$job = $dlp->createDlpJob($parent, $options2); |
Oops, something went wrong.