Skip to content

Commit cf3c3c5

Browse files
feat(secretmanager): Added samples for tags field (#2140)
* feat(secretmanager): added samples for tags field * fix(secretmanager): Fix lint * fix(secretmanager): Added comments for sleep
1 parent 36f8daa commit cf3c3c5

7 files changed

+604
-1
lines changed

secretmanager/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"require": {
3-
"google/cloud-secret-manager": "^2.0.0"
3+
"google/cloud-secret-manager": "^2.1.0",
4+
"google/cloud-resource-manager": "^1.0"
45
}
56
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
/*
3+
* Copyright 2025 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_bind_tags_to_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+
use Google\Cloud\ResourceManager\V3\Client\TagBindingsClient;
34+
use Google\Cloud\ResourceManager\V3\CreateTagBindingRequest;
35+
use Google\Cloud\ResourceManager\V3\TagBinding;
36+
37+
/**
38+
* @param string $projectId Your Google Cloud Project ID (e.g. 'my-project')
39+
* @param string $locationId Your Google Cloud Location ID (e.g. 'us-central1')
40+
* @param string $secretId Your secret ID (e.g. 'my-secret')
41+
* @param string $tagValue Your tag value (e.g. 'tagValues/281476592621530')
42+
*/
43+
function bind_tags_to_regional_secret(string $projectId, string $locationId, string $secretId, string $tagValue): void
44+
{
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 parent project.
52+
$parent = $client->locationName($projectId, $locationId);
53+
54+
$secret = new Secret();
55+
56+
// Build the request.
57+
$request = CreateSecretRequest::build($parent, $secretId, $secret);
58+
59+
// Create the secret.
60+
$newSecret = $client->createSecret($request);
61+
62+
// Print the new secret name.
63+
printf('Created secret %s' . PHP_EOL, $newSecret->getName());
64+
65+
// Specify regional endpoint.
66+
$tagBindOptions = ['apiEndpoint' => "$locationId-cloudresourcemanager.googleapis.com"];
67+
$tagBindingsClient = new TagBindingsClient($tagBindOptions);
68+
$tagBinding = (new TagBinding())
69+
->setParent('//secretmanager.googleapis.com/' . $newSecret->getName())
70+
->setTagValue($tagValue);
71+
72+
// Build the request.
73+
$request = (new CreateTagBindingRequest())
74+
->setTagBinding($tagBinding);
75+
76+
// Create the tag binding.
77+
$operationResponse = $tagBindingsClient->createTagBinding($request);
78+
$operationResponse->pollUntilComplete();
79+
80+
// Check if the operation succeeded.
81+
if ($operationResponse->operationSucceeded()) {
82+
printf('Tag binding created for secret %s with tag value %s' . PHP_EOL, $newSecret->getName(), $tagValue);
83+
} else {
84+
$error = $operationResponse->getError();
85+
printf('Error in creating tag binding: %s' . PHP_EOL, $error->getMessage());
86+
}
87+
}
88+
// [END secretmanager_bind_tags_to_regional_secret]
89+
90+
// The following 2 lines are only needed to execute the samples on the CLI
91+
require_once __DIR__ . '/../../testing/sample_helpers.php';
92+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
/*
3+
* Copyright 2025 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_bind_tags_to_secret]
29+
// Import the Secret Manager client library.
30+
use Google\Cloud\SecretManager\V1\CreateSecretRequest;
31+
use Google\Cloud\SecretManager\V1\Replication;
32+
use Google\Cloud\SecretManager\V1\Replication\Automatic;
33+
use Google\Cloud\SecretManager\V1\Secret;
34+
use Google\Cloud\SecretManager\V1\Client\SecretManagerServiceClient;
35+
use Google\Cloud\ResourceManager\V3\Client\TagBindingsClient;
36+
use Google\Cloud\ResourceManager\V3\CreateTagBindingRequest;
37+
use Google\Cloud\ResourceManager\V3\TagBinding;
38+
39+
/**
40+
* @param string $projectId Your Google Cloud Project ID (e.g. 'my-project')
41+
* @param string $secretId Your secret ID (e.g. 'my-secret')
42+
* @param string $tagValue Your tag value (e.g. 'tagValues/281476592621530')
43+
*/
44+
function bind_tags_to_secret(string $projectId, string $secretId, string $tagValue): void
45+
{
46+
// Create the Secret Manager client.
47+
$client = new SecretManagerServiceClient();
48+
49+
// Build the resource name of the parent project.
50+
$parent = $client->projectName($projectId);
51+
52+
$secret = new Secret([
53+
'replication' => new Replication([
54+
'automatic' => new Automatic(),
55+
]),
56+
]);
57+
58+
// Build the request.
59+
$request = CreateSecretRequest::build($parent, $secretId, $secret);
60+
61+
// Create the secret.
62+
$newSecret = $client->createSecret($request);
63+
64+
// Print the new secret name.
65+
printf('Created secret %s' . PHP_EOL, $newSecret->getName());
66+
67+
$tagBindingsClient = new TagBindingsClient();
68+
$tagBinding = (new TagBinding())
69+
->setParent('//secretmanager.googleapis.com/' . $newSecret->getName())
70+
->setTagValue($tagValue);
71+
72+
// Build the binding request.
73+
$request = (new CreateTagBindingRequest())
74+
->setTagBinding($tagBinding);
75+
76+
// Create the tag binding.
77+
$operationResponse = $tagBindingsClient->createTagBinding($request);
78+
$operationResponse->pollUntilComplete();
79+
80+
// Check if the operation succeeded.
81+
if ($operationResponse->operationSucceeded()) {
82+
printf('Tag binding created for secret %s with tag value %s' . PHP_EOL, $newSecret->getName(), $tagValue);
83+
} else {
84+
$error = $operationResponse->getError();
85+
printf('Error in creating tag binding: %s' . PHP_EOL, $error->getMessage());
86+
}
87+
}
88+
// [END secretmanager_bind_tags_to_secret]
89+
90+
// The following 2 lines are only needed to execute the samples on the CLI
91+
require_once __DIR__ . '/../../testing/sample_helpers.php';
92+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/*
3+
* Copyright 2025 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_regional_create_secret_with_tags]
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 Google Cloud Location ID (e.g. 'us-central1')
37+
* @param string $secretId Your secret ID (e.g. 'my-secret')
38+
* @param string $tagKey Your tag key (e.g. 'tagKeys/281475012216835')
39+
* @param string $tagValue Your tag value (e.g. 'tagValues/281476592621530')
40+
*/
41+
function create_regional_secret_with_tags(string $projectId, string $locationId, string $secretId, string $tagKey, string $tagValue): void
42+
{
43+
// Specify regional endpoint.
44+
$options = ['apiEndpoint' => "secretmanager.$locationId.rep.googleapis.com"];
45+
46+
// Create the Secret Manager client.
47+
$client = new SecretManagerServiceClient($options);
48+
49+
// Build the resource name of the parent project.
50+
$parent = $client->locationName($projectId, $locationId);
51+
52+
$secret = new Secret();
53+
54+
// set the tags.
55+
$tags = [$tagKey => $tagValue];
56+
$secret ->setTags($tags);
57+
58+
// Build the request.
59+
$request = CreateSecretRequest::build($parent, $secretId, $secret);
60+
61+
// Create the secret.
62+
$newSecret = $client->createSecret($request);
63+
64+
// Print the new secret name.
65+
printf('Created secret %s with tag', $newSecret->getName());
66+
}
67+
// [END secretmanager_regional_create_secret_with_tags]
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: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
/*
3+
* Copyright 2025 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_secret_with_tags]
29+
// Import the Secret Manager client library.
30+
use Google\Cloud\SecretManager\V1\CreateSecretRequest;
31+
use Google\Cloud\SecretManager\V1\Replication;
32+
use Google\Cloud\SecretManager\V1\Replication\Automatic;
33+
use Google\Cloud\SecretManager\V1\Secret;
34+
use Google\Cloud\SecretManager\V1\Client\SecretManagerServiceClient;
35+
36+
/**
37+
* @param string $projectId Your Google Cloud Project ID (e.g. 'my-project')
38+
* @param string $secretId Your secret ID (e.g. 'my-secret')
39+
* @param string $tagKey Your tag key (e.g. 'tagKeys/281475012216835')
40+
* @param string $tagValue Your tag value (e.g. 'tagValues/281476592621530')
41+
*/
42+
function create_secret_with_tags(string $projectId, string $secretId, string $tagKey, string $tagValue): void
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+
$secret = new Secret([
51+
'replication' => new Replication([
52+
'automatic' => new Automatic(),
53+
]),
54+
]);
55+
56+
// set the tags.
57+
$tags = [$tagKey => $tagValue];
58+
$secret->setTags($tags);
59+
60+
// Build the request.
61+
$request = CreateSecretRequest::build($parent, $secretId, $secret);
62+
63+
// Create the secret.
64+
$newSecret = $client->createSecret($request);
65+
66+
// Print the new secret name.
67+
printf('Created secret %s with tag', $newSecret->getName());
68+
}
69+
// [END secretmanager_create_secret_with_tags]
70+
71+
// The following 2 lines are only needed to execute the samples on the CLI
72+
require_once __DIR__ . '/../../testing/sample_helpers.php';
73+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

0 commit comments

Comments
 (0)