Skip to content

Commit 4c0e644

Browse files
authored
chore: upgrade secretmanager samples to new format (GoogleCloudPlatform#1647)
1 parent 4a4faf7 commit 4c0e644

15 files changed

+349
-321
lines changed

secretmanager/src/access_secret_version.php

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,35 +23,37 @@
2323

2424
declare(strict_types=1);
2525

26-
require_once __DIR__ . '/../vendor/autoload.php';
27-
28-
if (count($argv) != 4) {
29-
return printf("Usage: php %s PROJECT_ID SECRET_ID VERSION_ID\n", basename(__FILE__));
30-
}
31-
list($_, $projectId, $secretId, $versionId) = $argv;
26+
namespace Google\Cloud\Samples\SecretManager;
3227

3328
// [START secretmanager_access_secret_version]
3429
// Import the Secret Manager client library.
3530
use Google\Cloud\SecretManager\V1\SecretManagerServiceClient;
3631

37-
/** Uncomment and populate these variables in your code */
38-
// $projectId = 'YOUR_GOOGLE_CLOUD_PROJECT' (e.g. 'my-project');
39-
// $secretId = 'YOUR_SECRET_ID' (e.g. 'my-secret');
40-
// $versionId = 'YOUR_VERSION_ID' (e.g. 'latest' or '5');
41-
42-
// Create the Secret Manager client.
43-
$client = new SecretManagerServiceClient();
44-
45-
// Build the resource name of the secret version.
46-
$name = $client->secretVersionName($projectId, $secretId, $versionId);
47-
48-
// Access the secret version.
49-
$response = $client->accessSecretVersion($name);
50-
51-
// Print the secret payload.
52-
//
53-
// WARNING: Do not print the secret in a production environment - this
54-
// snippet is showing how to access the secret material.
55-
$payload = $response->getPayload()->getData();
56-
printf('Plaintext: %s', $payload);
32+
/**
33+
* @param string $projectId Your Google Cloud Project ID (e.g. 'my-project')
34+
* @param string $secretId Your secret ID (e.g. 'my-secret')
35+
* @param string $versionId Your version ID (e.g. 'latest' or '5');
36+
*/
37+
function access_secret_version(string $projectId, string $secretId, string $versionId): void
38+
{
39+
// Create the Secret Manager client.
40+
$client = new SecretManagerServiceClient();
41+
42+
// Build the resource name of the secret version.
43+
$name = $client->secretVersionName($projectId, $secretId, $versionId);
44+
45+
// Access the secret version.
46+
$response = $client->accessSecretVersion($name);
47+
48+
// Print the secret payload.
49+
//
50+
// WARNING: Do not print the secret in a production environment - this
51+
// snippet is showing how to access the secret material.
52+
$payload = $response->getPayload()->getData();
53+
printf('Plaintext: %s', $payload);
54+
}
5755
// [END secretmanager_access_secret_version]
56+
57+
// The following 2 lines are only needed to execute the samples on the CLI
58+
require_once __DIR__ . '/../../testing/sample_helpers.php';
59+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

secretmanager/src/add_secret_version.php

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,33 +23,35 @@
2323

2424
declare(strict_types=1);
2525

26-
require_once __DIR__ . '/../vendor/autoload.php';
27-
28-
if (count($argv) != 3) {
29-
return printf("Usage: php %s PROJECT_ID SECRET_ID\n", basename(__FILE__));
30-
}
31-
list($_, $projectId, $secretId) = $argv;
26+
namespace Google\Cloud\Samples\SecretManager;
3227

3328
// [START secretmanager_add_secret_version]
3429
// Import the Secret Manager client library.
3530
use Google\Cloud\SecretManager\V1\SecretManagerServiceClient;
3631
use Google\Cloud\SecretManager\V1\SecretPayload;
3732

38-
/** Uncomment and populate these variables in your code */
39-
// $projectId = 'YOUR_GOOGLE_CLOUD_PROJECT' (e.g. 'my-project');
40-
// $secretId = 'YOUR_SECRET_ID' (e.g. 'my-secret');
41-
42-
// Create the Secret Manager client.
43-
$client = new SecretManagerServiceClient();
33+
/**
34+
* @param string $projectId Your Google Cloud Project ID (e.g. 'my-project')
35+
* @param string $secretId Your secret ID (e.g. 'my-secret')
36+
*/
37+
function add_secret_version(string $projectId, string $secretId): void
38+
{
39+
// Create the Secret Manager client.
40+
$client = new SecretManagerServiceClient();
4441

45-
// Build the resource name of the parent secret.
46-
$parent = $client->secretName($projectId, $secretId);
42+
// Build the resource name of the parent secret.
43+
$parent = $client->secretName($projectId, $secretId);
4744

48-
// Access the secret version.
49-
$response = $client->addSecretVersion($parent, new SecretPayload([
50-
'data' => 'my super secret data',
51-
]));
45+
// Access the secret version.
46+
$response = $client->addSecretVersion($parent, new SecretPayload([
47+
'data' => 'my super secret data',
48+
]));
5249

53-
// Print the new secret version name.
54-
printf('Added secret version: %s', $response->getName());
50+
// Print the new secret version name.
51+
printf('Added secret version: %s', $response->getName());
52+
}
5553
// [END secretmanager_add_secret_version]
54+
55+
// The following 2 lines are only needed to execute the samples on the CLI
56+
require_once __DIR__ . '/../../testing/sample_helpers.php';
57+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

secretmanager/src/create_secret.php

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,7 @@
2323

2424
declare(strict_types=1);
2525

26-
require_once __DIR__ . '/../vendor/autoload.php';
27-
28-
if (count($argv) != 3) {
29-
return printf("Usage: php %s PROJECT_ID SECRET_ID\n", basename(__FILE__));
30-
}
31-
list($_, $projectId, $secretId) = $argv;
26+
namespace Google\Cloud\Samples\SecretManager;
3227

3328
// [START secretmanager_create_secret]
3429
// Import the Secret Manager client library.
@@ -37,25 +32,32 @@
3732
use Google\Cloud\SecretManager\V1\Secret;
3833
use Google\Cloud\SecretManager\V1\SecretManagerServiceClient;
3934

40-
/** Uncomment and populate these variables in your code */
41-
// $projectId = 'YOUR_GOOGLE_CLOUD_PROJECT' (e.g. 'my-project');
42-
// $secretId = 'YOUR_SECRET_ID' (e.g. 'my-secret');
43-
44-
// Create the Secret Manager client.
45-
$client = new SecretManagerServiceClient();
46-
47-
// Build the resource name of the parent project.
48-
$parent = $client->projectName($projectId);
49-
50-
// Create the secret.
51-
$secret = $client->createSecret($parent, $secretId,
52-
new Secret([
53-
'replication' => new Replication([
54-
'automatic' => new Automatic(),
55-
]),
56-
])
57-
);
58-
59-
// Print the new secret name.
60-
printf('Created secret: %s', $secret->getName());
35+
/**
36+
* @param string $projectId Your Google Cloud Project ID (e.g. 'my-project')
37+
* @param string $secretId Your secret ID (e.g. 'my-secret')
38+
*/
39+
function create_secret(string $projectId, string $secretId): void
40+
{
41+
// Create the Secret Manager client.
42+
$client = new SecretManagerServiceClient();
43+
44+
// Build the resource name of the parent project.
45+
$parent = $client->projectName($projectId);
46+
47+
// Create the secret.
48+
$secret = $client->createSecret($parent, $secretId,
49+
new Secret([
50+
'replication' => new Replication([
51+
'automatic' => new Automatic(),
52+
]),
53+
])
54+
);
55+
56+
// Print the new secret name.
57+
printf('Created secret: %s', $secret->getName());
58+
}
6159
// [END secretmanager_create_secret]
60+
61+
// The following 2 lines are only needed to execute the samples on the CLI
62+
require_once __DIR__ . '/../../testing/sample_helpers.php';
63+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

secretmanager/src/delete_secret.php

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,30 @@
2323

2424
declare(strict_types=1);
2525

26-
require_once __DIR__ . '/../vendor/autoload.php';
27-
28-
if (count($argv) != 3) {
29-
return printf("Usage: php %s PROJECT_ID SECRET_ID\n", basename(__FILE__));
30-
}
31-
list($_, $projectId, $secretId) = $argv;
26+
namespace Google\Cloud\Samples\SecretManager;
3227

3328
// [START secretmanager_delete_secret]
3429
// Import the Secret Manager client library.
3530
use Google\Cloud\SecretManager\V1\SecretManagerServiceClient;
3631

37-
/** Uncomment and populate these variables in your code */
38-
// $projectId = 'YOUR_GOOGLE_CLOUD_PROJECT' (e.g. 'my-project');
39-
// $secretId = 'YOUR_SECRET_ID' (e.g. 'my-secret');
40-
41-
// Create the Secret Manager client.
42-
$client = new SecretManagerServiceClient();
32+
/**
33+
* @param string $projectId Your Google Cloud Project ID (e.g. 'my-project')
34+
* @param string $secretId Your secret ID (e.g. 'my-secret')
35+
*/
36+
function delete_secret(string $projectId, string $secretId): void
37+
{
38+
// Create the Secret Manager client.
39+
$client = new SecretManagerServiceClient();
4340

44-
// Build the resource name of the secret.
45-
$name = $client->secretName($projectId, $secretId);
41+
// Build the resource name of the secret.
42+
$name = $client->secretName($projectId, $secretId);
4643

47-
// Delete the secret.
48-
$client->deleteSecret($name);
49-
printf('Deleted secret %s', $secretId);
44+
// Delete the secret.
45+
$client->deleteSecret($name);
46+
printf('Deleted secret %s', $secretId);
47+
}
5048
// [END secretmanager_delete_secret]
49+
50+
// The following 2 lines are only needed to execute the samples on the CLI
51+
require_once __DIR__ . '/../../testing/sample_helpers.php';
52+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

secretmanager/src/destroy_secret_version.php

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,31 +23,33 @@
2323

2424
declare(strict_types=1);
2525

26-
require_once __DIR__ . '/../vendor/autoload.php';
27-
28-
if (count($argv) != 4) {
29-
return printf("Usage: php %s PROJECT_ID SECRET_ID VERSION_ID\n", basename(__FILE__));
30-
}
31-
list($_, $projectId, $secretId, $versionId) = $argv;
26+
namespace Google\Cloud\Samples\SecretManager;
3227

3328
// [START secretmanager_destroy_secret_version]
3429
// Import the Secret Manager client library.
3530
use Google\Cloud\SecretManager\V1\SecretManagerServiceClient;
3631

37-
/** Uncomment and populate these variables in your code */
38-
// $projectId = 'YOUR_GOOGLE_CLOUD_PROJECT' (e.g. 'my-project');
39-
// $secretId = 'YOUR_SECRET_ID' (e.g. 'my-secret');
40-
// $versionId = 'YOUR_VERSION_ID' (e.g. 'latest' or '5');
41-
42-
// Create the Secret Manager client.
43-
$client = new SecretManagerServiceClient();
32+
/**
33+
* @param string $projectId Your Google Cloud Project ID (e.g. 'my-project')
34+
* @param string $secretId Your secret ID (e.g. 'my-secret')
35+
* @param string $versionId Your version ID (e.g. 'latest' or '5');
36+
*/
37+
function destroy_secret_version(string $projectId, string $secretId, string $versionId): void
38+
{
39+
// Create the Secret Manager client.
40+
$client = new SecretManagerServiceClient();
4441

45-
// Build the resource name of the secret version.
46-
$name = $client->secretVersionName($projectId, $secretId, $versionId);
42+
// Build the resource name of the secret version.
43+
$name = $client->secretVersionName($projectId, $secretId, $versionId);
4744

48-
// Destroy the secret version.
49-
$response = $client->destroySecretVersion($name);
45+
// Destroy the secret version.
46+
$response = $client->destroySecretVersion($name);
5047

51-
// Print a success message.
52-
printf('Destroyed secret version: %s', $response->getName());
48+
// Print a success message.
49+
printf('Destroyed secret version: %s', $response->getName());
50+
}
5351
// [END secretmanager_destroy_secret_version]
52+
53+
// The following 2 lines are only needed to execute the samples on the CLI
54+
require_once __DIR__ . '/../../testing/sample_helpers.php';
55+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

secretmanager/src/disable_secret_version.php

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,31 +23,33 @@
2323

2424
declare(strict_types=1);
2525

26-
require_once __DIR__ . '/../vendor/autoload.php';
27-
28-
if (count($argv) != 4) {
29-
return printf("Usage: php %s PROJECT_ID SECRET_ID VERSION_ID\n", basename(__FILE__));
30-
}
31-
list($_, $projectId, $secretId, $versionId) = $argv;
26+
namespace Google\Cloud\Samples\SecretManager;
3227

3328
// [START secretmanager_disable_secret_version]
3429
// Import the Secret Manager client library.
3530
use Google\Cloud\SecretManager\V1\SecretManagerServiceClient;
3631

37-
/** Uncomment and populate these variables in your code */
38-
// $projectId = 'YOUR_GOOGLE_CLOUD_PROJECT' (e.g. 'my-project');
39-
// $secretId = 'YOUR_SECRET_ID' (e.g. 'my-secret');
40-
// $versionId = 'YOUR_VERSION_ID' (e.g. 'latest' or '5');
41-
42-
// Create the Secret Manager client.
43-
$client = new SecretManagerServiceClient();
32+
/**
33+
* @param string $projectId Your Google Cloud Project ID (e.g. 'my-project')
34+
* @param string $secretId Your secret ID (e.g. 'my-secret')
35+
* @param string $versionId Your version ID (e.g. 'latest' or '5');
36+
*/
37+
function disable_secret_version(string $projectId, string $secretId, string $versionId): void
38+
{
39+
// Create the Secret Manager client.
40+
$client = new SecretManagerServiceClient();
4441

45-
// Build the resource name of the secret version.
46-
$name = $client->secretVersionName($projectId, $secretId, $versionId);
42+
// Build the resource name of the secret version.
43+
$name = $client->secretVersionName($projectId, $secretId, $versionId);
4744

48-
// Disable the secret version.
49-
$response = $client->disableSecretVersion($name);
45+
// Disable the secret version.
46+
$response = $client->disableSecretVersion($name);
5047

51-
// Print a success message.
52-
printf('Disabled secret version: %s', $response->getName());
48+
// Print a success message.
49+
printf('Disabled secret version: %s', $response->getName());
50+
}
5351
// [END secretmanager_disable_secret_version]
52+
53+
// The following 2 lines are only needed to execute the samples on the CLI
54+
require_once __DIR__ . '/../../testing/sample_helpers.php';
55+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

secretmanager/src/enable_secret_version.php

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,31 +23,33 @@
2323

2424
declare(strict_types=1);
2525

26-
require_once __DIR__ . '/../vendor/autoload.php';
27-
28-
if (count($argv) != 4) {
29-
return printf("Usage: php %s PROJECT_ID SECRET_ID VERSION_ID\n", basename(__FILE__));
30-
}
31-
list($_, $projectId, $secretId, $versionId) = $argv;
26+
namespace Google\Cloud\Samples\SecretManager;
3227

3328
// [START secretmanager_enable_secret_version]
3429
// Import the Secret Manager client library.
3530
use Google\Cloud\SecretManager\V1\SecretManagerServiceClient;
3631

37-
/** Uncomment and populate these variables in your code */
38-
// $projectId = 'YOUR_GOOGLE_CLOUD_PROJECT' (e.g. 'my-project');
39-
// $secretId = 'YOUR_SECRET_ID' (e.g. 'my-secret');
40-
// $versionId = 'YOUR_VERSION_ID' (e.g. 'latest' or '5');
41-
42-
// Create the Secret Manager client.
43-
$client = new SecretManagerServiceClient();
32+
/**
33+
* @param string $projectId Your Google Cloud Project ID (e.g. 'my-project')
34+
* @param string $secretId Your secret ID (e.g. 'my-secret')
35+
* @param string $versionId Your version ID (e.g. 'latest' or '5');
36+
*/
37+
function enable_secret_version(string $projectId, string $secretId, string $versionId): void
38+
{
39+
// Create the Secret Manager client.
40+
$client = new SecretManagerServiceClient();
4441

45-
// Build the resource name of the secret version.
46-
$name = $client->secretVersionName($projectId, $secretId, $versionId);
42+
// Build the resource name of the secret version.
43+
$name = $client->secretVersionName($projectId, $secretId, $versionId);
4744

48-
// Enable the secret version.
49-
$response = $client->enableSecretVersion($name);
45+
// Enable the secret version.
46+
$response = $client->enableSecretVersion($name);
5047

51-
// Print a success message.
52-
printf('Enabled secret version: %s', $response->getName());
48+
// Print a success message.
49+
printf('Enabled secret version: %s', $response->getName());
50+
}
5351
// [END secretmanager_enable_secret_version]
52+
53+
// The following 2 lines are only needed to execute the samples on the CLI
54+
require_once __DIR__ . '/../../testing/sample_helpers.php';
55+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

0 commit comments

Comments
 (0)