Skip to content

Commit 598200e

Browse files
feat(secretmanager): Add samples for ETag-based secret and secret version operations
1 parent d6f1572 commit 598200e

12 files changed

Lines changed: 824 additions & 5 deletions
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
/*
3+
* Copyright 2026 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_using_etag]
29+
use Google\Cloud\SecretManager\V1\Client\SecretManagerServiceClient;
30+
use Google\Cloud\SecretManager\V1\GetSecretRequest;
31+
use Google\Cloud\SecretManager\V1\DeleteSecretRequest;
32+
33+
/**
34+
* Delete a regional secret using a stored etag (optimistic concurrency).
35+
*
36+
* @param string $projectId Your Google Cloud Project ID (e.g. 'my-project')
37+
* @param string $locationId Secret location (e.g. 'us-central1')
38+
* @param string $secretId Your secret ID (e.g. 'my-secret')
39+
*/
40+
function delete_regional_secret_using_etag(string $projectId, string $locationId, string $secretId): void
41+
{
42+
$options = ['apiEndpoint' => "secretmanager.$locationId.rep.googleapis.com"];
43+
$client = new SecretManagerServiceClient($options);
44+
45+
$name = $client->projectLocationSecretName($projectId, $locationId, $secretId);
46+
47+
// Get the current secret to read the etag.
48+
$getRequest = GetSecretRequest::build($name);
49+
$current = $client->getSecret($getRequest);
50+
51+
$etag = $current->getEtag();
52+
53+
// Build the delete request with the etag.
54+
$deleteRequest = (new DeleteSecretRequest())
55+
->setName($name)
56+
->setEtag($etag);
57+
58+
// Delete the secret.
59+
$client->deleteSecret($deleteRequest);
60+
61+
printf('Deleted secret %s' . PHP_EOL, $secretId);
62+
}
63+
// [END secretmanager_delete_regional_secret_using_etag]
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);
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
/*
3+
* Copyright 2026 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_secret_using_etag]
29+
use Google\Cloud\SecretManager\V1\Client\SecretManagerServiceClient;
30+
use Google\Cloud\SecretManager\V1\GetSecretRequest;
31+
use Google\Cloud\SecretManager\V1\DeleteSecretRequest;
32+
33+
/**
34+
* Delete a secret using a stored etag (optimistic concurrency).
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 delete_secret_using_etag(string $projectId, string $secretId): void
40+
{
41+
$client = new SecretManagerServiceClient();
42+
43+
$name = $client->secretName($projectId, $secretId);
44+
45+
// Get the current secret to read the etag.
46+
$getRequest = GetSecretRequest::build($name);
47+
$current = $client->getSecret($getRequest);
48+
49+
$etag = $current->getEtag();
50+
51+
// Build the delete request with the etag.
52+
$deleteRequest = (new DeleteSecretRequest())
53+
->setName($name)
54+
->setEtag($etag);
55+
56+
// Delete the secret.
57+
$client->deleteSecret($deleteRequest);
58+
59+
printf('Deleted secret %s' . PHP_EOL, $secretId);
60+
}
61+
// [END secretmanager_delete_secret_using_etag]
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);
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/*
3+
* Copyright 2026 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_using_etag]
29+
use Google\Cloud\SecretManager\V1\Client\SecretManagerServiceClient;
30+
use Google\Cloud\SecretManager\V1\GetSecretVersionRequest;
31+
use Google\Cloud\SecretManager\V1\DestroySecretVersionRequest;
32+
33+
/**
34+
* Destroy a regional secret version using a stored etag.
35+
*
36+
* @param string $projectId Your Google Cloud Project ID (e.g. 'my-project')
37+
* @param string $locationId Secret location (e.g. 'us-central1')
38+
* @param string $secretId Your secret ID (e.g. 'my-secret')
39+
* @param string $versionId Your version ID (e.g. 'latest' or '5')
40+
*/
41+
function destroy_regional_secret_version_using_etag(string $projectId, string $locationId, string $secretId, string $versionId): void
42+
{
43+
$options = ['apiEndpoint' => "secretmanager.$locationId.rep.googleapis.com"];
44+
$client = new SecretManagerServiceClient($options);
45+
46+
$name = $client->projectLocationSecretSecretVersionName($projectId, $locationId, $secretId, $versionId);
47+
48+
// Read current etag for the version.
49+
$getRequest = GetSecretVersionRequest::build($name);
50+
$current = $client->getSecretVersion($getRequest);
51+
$etag = $current->getEtag();
52+
53+
$request = DestroySecretVersionRequest::build($name)->setEtag($etag);
54+
55+
$response = $client->destroySecretVersion($request);
56+
57+
printf('Destroyed secret version: %s' . PHP_EOL, $response->getName());
58+
}
59+
// [END secretmanager_destroy_regional_secret_version_using_etag]
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);
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
/*
3+
* Copyright 2026 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_secret_version_using_etag]
29+
use Google\Cloud\SecretManager\V1\Client\SecretManagerServiceClient;
30+
use Google\Cloud\SecretManager\V1\GetSecretVersionRequest;
31+
use Google\Cloud\SecretManager\V1\DestroySecretVersionRequest;
32+
33+
/**
34+
* Destroy a secret version using a stored etag.
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+
* @param string $versionId Your version ID (e.g. 'latest' or '5')
39+
*/
40+
function destroy_secret_version_using_etag(string $projectId, string $secretId, string $versionId): void
41+
{
42+
$client = new SecretManagerServiceClient();
43+
44+
$name = $client->secretVersionName($projectId, $secretId, $versionId);
45+
46+
// Read current etag for the version.
47+
$getRequest = GetSecretVersionRequest::build($name);
48+
$current = $client->getSecretVersion($getRequest);
49+
$etag = $current->getEtag();
50+
51+
$request = DestroySecretVersionRequest::build($name)->setEtag($etag);
52+
53+
$response = $client->destroySecretVersion($request);
54+
55+
printf('Destroyed secret version: %s' . PHP_EOL, $response->getName());
56+
}
57+
// [END secretmanager_destroy_secret_version_using_etag]
58+
59+
// The following 2 lines are only needed to execute the samples on the CLI
60+
require_once __DIR__ . '/../../testing/sample_helpers.php';
61+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/*
3+
* Copyright 2026 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_disable_regional_secret_version_using_etag]
29+
use Google\Cloud\SecretManager\V1\Client\SecretManagerServiceClient;
30+
use Google\Cloud\SecretManager\V1\GetSecretVersionRequest;
31+
use Google\Cloud\SecretManager\V1\DisableSecretVersionRequest;
32+
33+
/**
34+
* Disable a regional secret version using a stored etag.
35+
*
36+
* @param string $projectId Your Google Cloud Project ID (e.g. 'my-project')
37+
* @param string $locationId Secret location (e.g. 'us-central1')
38+
* @param string $secretId Your secret ID (e.g. 'my-secret')
39+
* @param string $versionId Your version ID (e.g. 'latest' or '5')
40+
*/
41+
function disable_regional_secret_version_using_etag(string $projectId, string $locationId, string $secretId, string $versionId): void
42+
{
43+
$options = ['apiEndpoint' => "secretmanager.$locationId.rep.googleapis.com"];
44+
$client = new SecretManagerServiceClient($options);
45+
46+
$name = $client->projectLocationSecretSecretVersionName($projectId, $locationId, $secretId, $versionId);
47+
48+
// Read current etag for the version.
49+
$getRequest = GetSecretVersionRequest::build($name);
50+
$current = $client->getSecretVersion($getRequest);
51+
$etag = $current->getEtag();
52+
53+
$request = DisableSecretVersionRequest::build($name)->setEtag($etag);
54+
55+
$response = $client->disableSecretVersion($request);
56+
57+
printf('Disabled secret version: %s' . PHP_EOL, $response->getName());
58+
}
59+
// [END secretmanager_disable_regional_secret_version_using_etag]
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);
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
/*
3+
* Copyright 2026 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_disable_secret_version_using_etag]
29+
use Google\Cloud\SecretManager\V1\Client\SecretManagerServiceClient;
30+
use Google\Cloud\SecretManager\V1\GetSecretVersionRequest;
31+
use Google\Cloud\SecretManager\V1\DisableSecretVersionRequest;
32+
33+
/**
34+
* Disable a secret version using a stored etag.
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+
* @param string $versionId Your version ID (e.g. 'latest' or '5')
39+
*/
40+
function disable_secret_version_using_etag(string $projectId, string $secretId, string $versionId): void
41+
{
42+
$client = new SecretManagerServiceClient();
43+
44+
$name = $client->secretVersionName($projectId, $secretId, $versionId);
45+
46+
// Read current etag for the version.
47+
$getRequest = GetSecretVersionRequest::build($name);
48+
$current = $client->getSecretVersion($getRequest);
49+
$etag = $current->getEtag();
50+
51+
$request = DisableSecretVersionRequest::build($name)->setEtag($etag);
52+
53+
$response = $client->disableSecretVersion($request);
54+
55+
printf('Disabled secret version: %s' . PHP_EOL, $response->getName());
56+
}
57+
// [END secretmanager_disable_secret_version_using_etag]
58+
59+
// The following 2 lines are only needed to execute the samples on the CLI
60+
require_once __DIR__ . '/../../testing/sample_helpers.php';
61+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

0 commit comments

Comments
 (0)