Skip to content

Commit 0a13650

Browse files
authored
feat(secretmanager): add regional secrets samples (#2065)
1 parent 2405523 commit 0a13650

18 files changed

+1355
-3
lines changed

secretmanager/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"require": {
3-
"google/cloud-secret-manager": "^1.13"
3+
"google/cloud-secret-manager": "^1.15.2"
44
}
55
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/*
3+
* Copyright 2024 Google LLC.
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 full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/secretmanager/README.md
22+
*/
23+
24+
declare(strict_types=1);
25+
26+
namespace Google\Cloud\Samples\SecretManager;
27+
28+
// [START secretmanager_access_regional_secret_version]
29+
// Import the Secret Manager client library.
30+
use Google\Cloud\SecretManager\V1\Client\SecretManagerServiceClient;
31+
use Google\Cloud\SecretManager\V1\AccessSecretVersionRequest;
32+
33+
/**
34+
* @param string $projectId Your Google Cloud Project ID (e.g. 'my-project')
35+
* @param string $locationId Your secret Location (e.g. "us-central1")
36+
* @param string $secretId Your secret ID (e.g. 'my-secret')
37+
* @param string $versionId Your version ID (e.g. 'latest' or '5');
38+
*/
39+
function access_regional_secret_version(
40+
string $projectId,
41+
string $locationId,
42+
string $secretId,
43+
string $versionId
44+
): void {
45+
// Specify regional endpoint.
46+
$options = ['apiEndpoint' => "secretmanager.$locationId.rep.googleapis.com"];
47+
48+
// Create the Secret Manager client.
49+
$client = new SecretManagerServiceClient($options);
50+
51+
// Build the resource name of the secret version.
52+
$name = $client->projectLocationSecretSecretVersionName($projectId, $locationId, $secretId, $versionId);
53+
54+
// Build the request.
55+
$request = AccessSecretVersionRequest::build($name);
56+
57+
// Access the secret version.
58+
$response = $client->accessSecretVersion($request);
59+
60+
// Print the secret payload.
61+
//
62+
// WARNING: Do not print the secret in a production environment - this
63+
// snippet is showing how to access the secret material.
64+
$payload = $response->getPayload()->getData();
65+
printf('Plaintext: %s', $payload);
66+
}
67+
// [END secretmanager_access_regional_secret_version]
68+
69+
// The following 2 lines are only needed to execute the samples on the CLI
70+
require_once __DIR__ . '/../../testing/sample_helpers.php';
71+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
/*
3+
* Copyright 2024 Google LLC.
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 full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/secretmanager/README.md
22+
*/
23+
24+
declare(strict_types=1);
25+
26+
namespace Google\Cloud\Samples\SecretManager;
27+
28+
// [START secretmanager_add_regional_secret_version]
29+
// Import the Secret Manager client library.
30+
use Google\Cloud\SecretManager\V1\Client\SecretManagerServiceClient;
31+
use Google\Cloud\SecretManager\V1\AddSecretVersionRequest;
32+
use Google\Cloud\SecretManager\V1\SecretPayload;
33+
34+
/**
35+
* @param string $projectId Your Google Cloud Project ID (e.g. 'my-project')
36+
* @param string $locationId Your secret Location (e.g. "us-central1")
37+
* @param string $secretId Your secret ID (e.g. 'my-secret')
38+
*/
39+
function add_regional_secret_version(string $projectId, string $locationId, string $secretId): void
40+
{
41+
// Specify regional endpoint.
42+
$options = ['apiEndpoint' => "secretmanager.$locationId.rep.googleapis.com"];
43+
44+
// Create the Secret Manager client.
45+
$client = new SecretManagerServiceClient($options);
46+
47+
// Build the resource name of the parent secret and the payload.
48+
$parent = $client->projectLocationSecretName($projectId, $locationId, $secretId);
49+
$secretPayload = new SecretPayload([
50+
'data' => 'my super secret data',
51+
]);
52+
53+
// Build the request.
54+
$request = AddSecretVersionRequest::build($parent, $secretPayload);
55+
56+
// Access the secret version.
57+
$response = $client->addSecretVersion($request);
58+
59+
// Print the new secret version name.
60+
printf('Added secret version: %s', $response->getName());
61+
}
62+
// [END secretmanager_add_regional_secret_version]
63+
64+
// The following 2 lines are only needed to execute the samples on the CLI
65+
require_once __DIR__ . '/../../testing/sample_helpers.php';
66+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
/*
3+
* Copyright 2024 Google LLC.
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 full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/secretmanager/README.md
22+
*/
23+
24+
declare(strict_types=1);
25+
26+
namespace Google\Cloud\Samples\SecretManager;
27+
28+
// [START secretmanager_create_regional_secret]
29+
// Import the Secret Manager client library.
30+
use Google\Cloud\SecretManager\V1\CreateSecretRequest;
31+
use Google\Cloud\SecretManager\V1\Secret;
32+
use Google\Cloud\SecretManager\V1\Client\SecretManagerServiceClient;
33+
34+
/**
35+
* @param string $projectId Your Google Cloud Project ID (e.g. 'my-project')
36+
* @param string $locationId Your secret Location (e.g. "us-central1")
37+
* @param string $secretId Your secret ID (e.g. 'my-secret')
38+
*/
39+
function create_regional_secret(string $projectId, string $locationId, string $secretId): void
40+
{
41+
// Specify regional endpoint.
42+
$options = ['apiEndpoint' => "secretmanager.$locationId.rep.googleapis.com"];
43+
44+
// Create the Secret Manager client.
45+
$client = new SecretManagerServiceClient($options);
46+
47+
// Build the resource name of the parent project.
48+
$parent = $client->locationName($projectId, $locationId);
49+
50+
$secret = new Secret();
51+
52+
// Build the request.
53+
$request = CreateSecretRequest::build($parent, $secretId, $secret);
54+
55+
// Create the secret.
56+
$newSecret = $client->createSecret($request);
57+
58+
// Print the new secret name.
59+
printf('Created secret: %s', $newSecret->getName());
60+
}
61+
// [END secretmanager_create_regional_secret]
62+
63+
// The following 2 lines are only needed to execute the samples on the CLI
64+
require_once __DIR__ . '/../../testing/sample_helpers.php';
65+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

secretmanager/src/create_secret_with_user_managed_replication.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,11 @@
3838
* @param string $secretId Your secret ID (e.g. 'my-secret')
3939
* @param array $locations Replication locations (e.g. array('us-east1', 'us-east4'))
4040
*/
41-
function create_secret_with_user_managed_replication(string $projectId, string $secretId, array $locations): void
42-
{
41+
function create_secret_with_user_managed_replication(
42+
string $projectId,
43+
string $secretId,
44+
array $locations
45+
): void {
4346
// Create the Secret Manager client.
4447
$client = new SecretManagerServiceClient();
4548

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
/*
3+
* Copyright 2024 Google LLC.
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 full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/secretmanager/README.md
22+
*/
23+
24+
declare(strict_types=1);
25+
26+
namespace Google\Cloud\Samples\SecretManager;
27+
28+
// [START secretmanager_delete_regional_secret]
29+
// Import the Secret Manager client library.
30+
use Google\Cloud\SecretManager\V1\Client\SecretManagerServiceClient;
31+
use Google\Cloud\SecretManager\V1\DeleteSecretRequest;
32+
33+
/**
34+
* @param string $projectId Your Google Cloud Project ID (e.g. 'my-project')
35+
* @param string $locationId Your secret Location (e.g. "us-central1")
36+
* @param string $secretId Your secret ID (e.g. 'my-secret')
37+
*/
38+
function delete_regional_secret(string $projectId, string $locationId, string $secretId): void
39+
{
40+
// Specify regional endpoint.
41+
$options = ['apiEndpoint' => "secretmanager.$locationId.rep.googleapis.com"];
42+
43+
// Create the Secret Manager client.
44+
$client = new SecretManagerServiceClient($options);
45+
46+
// Build the resource name of the secret.
47+
$name = $client->projectLocationSecretName($projectId, $locationId, $secretId);
48+
49+
// Build the request.
50+
$request = DeleteSecretRequest::build($name);
51+
52+
// Delete the secret.
53+
$client->deleteSecret($request);
54+
printf('Deleted secret %s', $secretId);
55+
}
56+
// [END secretmanager_delete_regional_secret]
57+
58+
// The following 2 lines are only needed to execute the samples on the CLI
59+
require_once __DIR__ . '/../../testing/sample_helpers.php';
60+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
/*
3+
* Copyright 2024 Google LLC.
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 full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/secretmanager/README.md
22+
*/
23+
24+
declare(strict_types=1);
25+
26+
namespace Google\Cloud\Samples\SecretManager;
27+
28+
// [START secretmanager_destroy_regional_secret_version]
29+
// Import the Secret Manager client library.
30+
use Google\Cloud\SecretManager\V1\Client\SecretManagerServiceClient;
31+
use Google\Cloud\SecretManager\V1\DestroySecretVersionRequest;
32+
33+
/**
34+
* @param string $projectId Your Google Cloud Project ID (e.g. 'my-project')
35+
* @param string $locationId Your secret Location (e.g. "us-central1")
36+
* @param string $secretId Your secret ID (e.g. 'my-secret')
37+
* @param string $versionId Your version ID (e.g. 'latest' or '5');
38+
*/
39+
function destroy_regional_secret_version(
40+
string $projectId,
41+
string $locationId,
42+
string $secretId,
43+
string $versionId
44+
): void {
45+
// Specify regional endpoint.
46+
$options = ['apiEndpoint' => "secretmanager.$locationId.rep.googleapis.com"];
47+
48+
// Create the Secret Manager client.
49+
$client = new SecretManagerServiceClient($options);
50+
51+
// Build the resource name of the secret version.
52+
$name = $client->projectLocationSecretSecretVersionName($projectId, $locationId, $secretId, $versionId);
53+
54+
// Build the request.
55+
$request = DestroySecretVersionRequest::build($name);
56+
57+
// Destroy the secret version.
58+
$response = $client->destroySecretVersion($request);
59+
60+
// Print a success message.
61+
printf('Destroyed secret version: %s', $response->getName());
62+
}
63+
// [END secretmanager_destroy_regional_secret_version]
64+
65+
// The following 2 lines are only needed to execute the samples on the CLI
66+
require_once __DIR__ . '/../../testing/sample_helpers.php';
67+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

0 commit comments

Comments
 (0)