Skip to content

Commit d702269

Browse files
feat(SecretManager): Add samples for labels and annotations in Secret Manager (#2146)
1 parent cf3c3c5 commit d702269

18 files changed

+1496
-0
lines changed
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_create_regional_secret_with_annotations]
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 $annotationKey Your annotation key (e.g. 'annotation-key')
39+
* @param string $annotationValue Your annotation value (e.g. 'annotation-value')
40+
*/
41+
function create_regional_secret_with_annotations(string $projectId, string $locationId, string $secretId, string $annotationKey, string $annotationValue): 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 annotations.
55+
$annotations = [$annotationKey => $annotationValue];
56+
$secret->setAnnotations($annotations);
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 annotations', $newSecret->getName());
66+
}
67+
// [END secretmanager_create_regional_secret_with_annotations]
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: 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_create_regional_secret_with_labels]
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 $labelKey Your label key (e.g. 'label-key')
39+
* @param string $labelValue Your label value (e.g. 'label-value')
40+
*/
41+
function create_regional_secret_with_labels(string $projectId, string $locationId, string $secretId, string $labelKey, string $labelValue): 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 labels.
55+
$labels = [$labelKey => $labelValue];
56+
$secret->setLabels($labels);
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 labels', $newSecret->getName());
66+
}
67+
// [END secretmanager_create_regional_secret_with_labels]
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_annotations]
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 $annotationKey Your annotation key (e.g. 'annotation-key')
40+
* @param string $annotationValue Your annotation value (e.g. 'annotation-value')
41+
*/
42+
function create_secret_with_annotations(string $projectId, string $secretId, string $annotationKey, string $annotationValue): 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 annoation.
57+
$annotation = [$annotationKey => $annotationValue];
58+
$secret->setAnnotations($annotation);
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 annotations', $newSecret->getName());
68+
}
69+
// [END secretmanager_create_secret_with_annotations]
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);
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_labels]
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 $labelKey Your label key (e.g. 'label-key')
40+
* @param string $labelValue Your label value (e.g. 'label-value')
41+
*/
42+
function create_secret_with_labels(string $projectId, string $secretId, string $labelKey, string $labelValue): 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 labels.
57+
$labels = [$labelKey => $labelValue];
58+
$secret->setLabels($labels);
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 labels', $newSecret->getName());
68+
}
69+
// [END secretmanager_create_secret_with_labels]
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);
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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_delete_regional_secret_annotation]
29+
// Import the Secret Manager client library.
30+
use Google\Cloud\SecretManager\V1\Client\SecretManagerServiceClient;
31+
use Google\Cloud\SecretManager\V1\GetSecretRequest;
32+
use Google\Cloud\SecretManager\V1\Secret;
33+
use Google\Cloud\SecretManager\V1\UpdateSecretRequest;
34+
use Google\Protobuf\FieldMask;
35+
36+
/**
37+
* @param string $projectId Your Google Cloud Project ID (e.g. 'my-project')
38+
* @param string $locationId Your secret Location (e.g. 'us-central1')
39+
* @param string $secretId Your secret ID (e.g. 'my-secret')
40+
* @param string $annotationKey Your annotation key (e.g. 'annotation-key')
41+
*/
42+
function delete_regional_secret_annotation(string $projectId, string $locationId, string $secretId, string $annotationKey): void
43+
{
44+
// Specify regional endpoint.
45+
$options = ['apiEndpoint' => "secretmanager.$locationId.rep.googleapis.com"];
46+
47+
// Create the Secret Manager client.
48+
$client = new SecretManagerServiceClient($options);
49+
50+
// Build the resource name of the secret.
51+
$name = $client->projectLocationSecretName($projectId, $locationId, $secretId);
52+
53+
// Build the request.
54+
$request = GetSecretRequest::build($name);
55+
56+
// get the secret.
57+
$getSecret = $client->getSecret($request);
58+
59+
// get the annotations
60+
$annotations = $getSecret->getAnnotations();
61+
62+
// delete the annotation
63+
unset($annotations[$annotationKey]);
64+
65+
// set the field mask
66+
$fieldMask = new FieldMask();
67+
$fieldMask->setPaths(['annotations']);
68+
69+
// build the secret
70+
$secret = new Secret();
71+
$secret->setAnnotations($annotations);
72+
$secret->setName($getSecret->getName());
73+
74+
// build the request
75+
$request = new UpdateSecretRequest();
76+
$request->setSecret($getSecret);
77+
$request->setUpdateMask($fieldMask);
78+
79+
// update the secret
80+
$updateSecret = $client->updateSecret($request);
81+
82+
// print the secret name
83+
printf('Updated secret %s' . PHP_EOL, $updateSecret->getName());
84+
}
85+
// [END secretmanager_delete_regional_secret_annotation]
86+
87+
// The following 2 lines are only needed to execute the samples on the CLI
88+
require_once __DIR__ . '/../../testing/sample_helpers.php';
89+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

0 commit comments

Comments
 (0)