Skip to content

Commit

Permalink
feat: add new client upgrade fixer (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
bshaffer authored Jun 26, 2024
1 parent f0aab14 commit 8df1b94
Show file tree
Hide file tree
Showing 40 changed files with 2,178 additions and 3 deletions.
5 changes: 4 additions & 1 deletion .gitignore
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

8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
],
"autoload": {
"psr-4": {
"Google\\Cloud\\Fixers\\": "src/Fixers/",
"Google\\Cloud\\TestUtils\\": "src/TestUtils/",
"Google\\Cloud\\Utils\\": "src/Utils/"
}
Expand All @@ -38,7 +39,10 @@
"google/gax": "^1.0.0",
"paragonie/random_compat": ">=2",
"phpunit/phpunit": "^9",
"friendsofphp/php-cs-fixer": "^3.17",
"phpspec/prophecy-phpunit": "^2.0"
"phpspec/prophecy-phpunit": "^2.0",
"friendsofphp/php-cs-fixer": "^3.21",
"google/cloud-dlp": "^1.10",
"google/cloud-storage": "^1.33",
"google/cloud-secret-manager": "^1.12"
}
}
43 changes: 43 additions & 0 deletions examples/ClientUpgradeFixer/legacy_class_vars.php
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');
48 changes: 48 additions & 0 deletions examples/ClientUpgradeFixer/legacy_inline_html.php
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>
72 changes: 72 additions & 0 deletions examples/ClientUpgradeFixer/legacy_kitchen_sink.php
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(),
]),
])
);
27 changes: 27 additions & 0 deletions examples/ClientUpgradeFixer/legacy_mixins.php
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);
28 changes: 28 additions & 0 deletions examples/ClientUpgradeFixer/legacy_multiple_clients.php
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();
11 changes: 11 additions & 0 deletions examples/ClientUpgradeFixer/legacy_no_args.php
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();
21 changes: 21 additions & 0 deletions examples/ClientUpgradeFixer/legacy_non_rpc_methods.php
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);
32 changes: 32 additions & 0 deletions examples/ClientUpgradeFixer/legacy_optional_args.php
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 examples/ClientUpgradeFixer/legacy_optional_args_array_keyword.php
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 examples/ClientUpgradeFixer/legacy_optional_args_variable.php
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);
Loading

0 comments on commit 8df1b94

Please sign in to comment.