Skip to content

chore: upgrade secretmanager samples to new client surface #1946

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dialogflow/composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"require": {
"google/cloud-dialogflow": "^1.10",
"google/cloud-dialogflow": "^1.11",
"symfony/console": "^5.0"
},
"autoload": {
Expand Down
2 changes: 1 addition & 1 deletion recaptcha/composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"require": {
"google/cloud-recaptcha-enterprise": "^1.7"
"google/cloud-recaptcha-enterprise": "^1.8"
}
}
2 changes: 1 addition & 1 deletion secretmanager/composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"require": {
"google/cloud-secret-manager": "^1.0.0"
"google/cloud-secret-manager": "^1.13"
}
}
24 changes: 17 additions & 7 deletions secretmanager/quickstart.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@

// [START secretmanager_quickstart]
// Import the Secret Manager client library.
use Google\Cloud\SecretManager\V1\AccessSecretVersionRequest;
use Google\Cloud\SecretManager\V1\AddSecretVersionRequest;
use Google\Cloud\SecretManager\V1\Client\SecretManagerServiceClient;
use Google\Cloud\SecretManager\V1\CreateSecretRequest;
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\SecretManager\V1\SecretPayload;

/** Uncomment and populate these variables in your code */
Expand All @@ -43,21 +46,28 @@
$parent = $client->projectName($projectId);

// Create the parent secret.
$secret = $client->createSecret($parent, $secretId,
new Secret([
$createSecretRequest = (new CreateSecretRequest())
->setParent($parent)
->setSecretId($secretId)
->setSecret(new Secret([
'replication' => new Replication([
'automatic' => new Automatic(),
]),
])
);
]));
$secret = $client->createSecret($createSecretRequest);

// Add the secret version.
$version = $client->addSecretVersion($secret->getName(), new SecretPayload([
$addSecretVersionRequest = (new AddSecretVersionRequest())
->setParent($secret->getName())
->setPayload(new SecretPayload([
'data' => 'hello world',
]));
$version = $client->addSecretVersion($addSecretVersionRequest);

// Access the secret version.
$response = $client->accessSecretVersion($version->getName());
$accessSecretVersionRequest = (new AccessSecretVersionRequest())
->setName($version->getName());
$response = $client->accessSecretVersion($accessSecretVersionRequest);

// Print the secret payload.
//
Expand Down
7 changes: 5 additions & 2 deletions secretmanager/test/quickstartTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
declare(strict_types=1);

use Google\ApiCore\ApiException as GaxApiException;
use Google\Cloud\SecretManager\V1\SecretManagerServiceClient;
use Google\Cloud\SecretManager\V1\Client\SecretManagerServiceClient;
use Google\Cloud\SecretManager\V1\DeleteSecretRequest;
use Google\Cloud\TestUtils\TestTrait;
use PHPUnit\Framework\TestCase;

Expand All @@ -39,7 +40,9 @@ public static function tearDownAfterClass(): void
$name = $client->secretName(self::$projectId, self::$secretId);

try {
$client->deleteSecret($name);
$deleteSecretRequest = (new DeleteSecretRequest())
->setName($name);
$client->deleteSecret($deleteSecretRequest);
} catch (GaxApiException $e) {
if ($e->getStatus() != 'NOT_FOUND') {
throw $e;
Expand Down
31 changes: 22 additions & 9 deletions secretmanager/test/secretmanagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@
namespace Google\Cloud\Samples\SecretManager;

use Google\ApiCore\ApiException as GaxApiException;
use Google\Cloud\SecretManager\V1\AddSecretVersionRequest;
use Google\Cloud\SecretManager\V1\Client\SecretManagerServiceClient;
use Google\Cloud\SecretManager\V1\CreateSecretRequest;
use Google\Cloud\SecretManager\V1\DeleteSecretRequest;
use Google\Cloud\SecretManager\V1\DisableSecretVersionRequest;
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\SecretManager\V1\SecretPayload;
use Google\Cloud\SecretManager\V1\SecretVersion;
use Google\Cloud\TestUtils\TestTrait;
Expand Down Expand Up @@ -81,32 +85,41 @@ private static function createSecret(): Secret
{
$parent = self::$client->projectName(self::$projectId);
$secretId = self::randomSecretId();

return self::$client->createSecret($parent, $secretId,
new Secret([
$createSecretRequest = (new CreateSecretRequest())
->setParent($parent)
->setSecretId($secretId)
->setSecret(new Secret([
'replication' => new Replication([
'automatic' => new Automatic(),
]),
])
);
]));

return self::$client->createSecret($createSecretRequest);
}

private static function addSecretVersion(Secret $secret): SecretVersion
{
return self::$client->addSecretVersion($secret->getName(), new SecretPayload([
$addSecretVersionRequest = (new AddSecretVersionRequest())
->setParent($secret->getName())
->setPayload(new SecretPayload([
'data' => 'my super secret data',
]));
return self::$client->addSecretVersion($addSecretVersionRequest);
}

private static function disableSecretVersion(SecretVersion $version): SecretVersion
{
return self::$client->disableSecretVersion($version->getName());
$disableSecretVersionRequest = (new DisableSecretVersionRequest())
->setName($version->getName());
return self::$client->disableSecretVersion($disableSecretVersionRequest);
}

private static function deleteSecret(string $name)
{
try {
self::$client->deleteSecret($name);
$deleteSecretRequest = (new DeleteSecretRequest())
->setName($name);
self::$client->deleteSecret($deleteSecretRequest);
} catch (GaxApiException $e) {
if ($e->getStatus() != 'NOT_FOUND') {
throw $e;
Expand Down