Skip to content

Commit 750985c

Browse files
chore(secretmanager): Added code samples for delayed destroy (#2148)
* chore(secretmanager): Added code samples for delayed destroy * chore(secretmanager): Update argument type * chore(secretmanager): Updated test assertions
1 parent c17d228 commit 750985c

8 files changed

+568
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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_delayed_destroy]
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\Protobuf\Duration;
34+
35+
/**
36+
* @param string $projectId Your Google Cloud Project ID (e.g. 'my-project')
37+
* @param string $locationId Your Google Cloud Location ID (e.g. 'us-central1')
38+
* @param string $secretId Your secret ID (e.g. 'my-secret')
39+
* @param int $versionDestroyTtl Your Version Destroy Ttl (e.g. 86400)
40+
*/
41+
function create_regional_secret_with_delayed_destroy(string $projectId, string $locationId, string $secretId, int $versionDestroyTtl): 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+
// Build the secret.
53+
$secret = new Secret([
54+
'version_destroy_ttl' => new Duration([
55+
'seconds' => $versionDestroyTtl
56+
])
57+
]);
58+
59+
// Build the request.
60+
$request = CreateSecretRequest::build($parent, $secretId, $secret);
61+
62+
// Create the secret.
63+
$newSecret = $client->createSecret($request);
64+
65+
// Print the new secret name.
66+
printf('Created secret: %s', $newSecret->getName());
67+
}
68+
// [END secretmanager_create_regional_secret_with_delayed_destroy]
69+
70+
// The following 2 lines are only needed to execute the samples on the CLI
71+
require_once __DIR__ . '/../../testing/sample_helpers.php';
72+
\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_delayed_destroy]
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\Protobuf\Duration;
36+
37+
/**
38+
* @param string $projectId Your Google Cloud Project ID (e.g. 'my-project')
39+
* @param string $secretId Your secret ID (e.g. 'my-secret')
40+
* @param int $versionDestroyTtl Your Version Destroy Ttl (e.g. 86400)
41+
*/
42+
function create_secret_with_delayed_destroy(string $projectId, string $secretId, int $versionDestroyTtl): 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+
// Build the secret.
51+
$secret = new Secret([
52+
'replication' => new Replication([
53+
'automatic' => new Automatic(),
54+
]),
55+
'version_destroy_ttl' => new Duration([
56+
'seconds' => $versionDestroyTtl
57+
])
58+
]);
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', $newSecret->getName());
68+
}
69+
// [END secretmanager_create_secret_with_delayed_destroy]
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: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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_disable_regional_secret_delayed_destroy]
29+
// Import the Secret Manager client library.
30+
use Google\Cloud\SecretManager\V1\Secret;
31+
use Google\Cloud\SecretManager\V1\Client\SecretManagerServiceClient;
32+
use Google\Cloud\SecretManager\V1\UpdateSecretRequest;
33+
use Google\Protobuf\FieldMask;
34+
35+
/**
36+
* @param string $projectId Your Google Cloud Project ID (e.g. 'my-project')
37+
* @param string $locationId Your Google Cloud Location ID (e.g. 'us-central1')
38+
* @param string $secretId Your secret ID (e.g. 'my-secret')
39+
*/
40+
function disable_regional_secret_delayed_destroy(string $projectId, string $locationId, string $secretId): void
41+
{
42+
// Specify regional endpoint.
43+
$options = ['apiEndpoint' => "secretmanager.$locationId.rep.googleapis.com"];
44+
45+
// Create the Secret Manager client.
46+
$client = new SecretManagerServiceClient($options);
47+
48+
// Build the resource name of the secret.
49+
$name = $client->projectLocationSecretName($projectId, $locationId, $secretId);
50+
51+
// Build the secret.
52+
$secret = new Secret([
53+
'name' => $name,
54+
]);
55+
56+
// Set the field mask.
57+
$fieldMask = new FieldMask();
58+
$fieldMask->setPaths(['version_destroy_ttl']);
59+
60+
// Build the request.
61+
$request = new UpdateSecretRequest();
62+
$request->setSecret($secret);
63+
$request->setUpdateMask($fieldMask);
64+
65+
// Update the secret.
66+
$newSecret = $client->updateSecret($request);
67+
68+
// Print the new secret name.
69+
printf('Updated secret: %s', $newSecret->getName());
70+
}
71+
// [END secretmanager_disable_regional_secret_delayed_destroy]
72+
73+
// The following 2 lines are only needed to execute the samples on the CLI
74+
require_once __DIR__ . '/../../testing/sample_helpers.php';
75+
\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_disable_secret_delayed_destroy]
29+
// Import the Secret Manager client library.
30+
use Google\Cloud\SecretManager\V1\Secret;
31+
use Google\Cloud\SecretManager\V1\Client\SecretManagerServiceClient;
32+
use Google\Cloud\SecretManager\V1\UpdateSecretRequest;
33+
use Google\Protobuf\FieldMask;
34+
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 disable_secret_delayed_destroy(string $projectId, string $secretId): void
40+
{
41+
// Create the Secret Manager client.
42+
$client = new SecretManagerServiceClient();
43+
44+
// Build the resource name of the secret.
45+
$name = $client->secretName($projectId, $secretId);
46+
47+
// Build the secret.
48+
$secret = new Secret([
49+
'name' => $name
50+
]);
51+
52+
// Set the field mask.
53+
$fieldMask = new FieldMask();
54+
$fieldMask->setPaths(['version_destroy_ttl']);
55+
56+
// Build the request.
57+
$request = new UpdateSecretRequest();
58+
$request->setSecret($secret);
59+
$request->setUpdateMask($fieldMask);
60+
61+
// Update the secret.
62+
$newSecret = $client->updateSecret($request);
63+
64+
// Print the new secret name.
65+
printf('Updated secret: %s', $newSecret->getName());
66+
}
67+
// [END secretmanager_disable_secret_delayed_destroy]
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: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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_update_regional_secret_with_delayed_destroy]
29+
// Import the Secret Manager client library.
30+
use Google\Cloud\SecretManager\V1\Secret;
31+
use Google\Cloud\SecretManager\V1\Client\SecretManagerServiceClient;
32+
use Google\Cloud\SecretManager\V1\UpdateSecretRequest;
33+
use Google\Protobuf\Duration;
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 Google Cloud Location ID (e.g. 'us-central1')
39+
* @param string $secretId Your secret ID (e.g. 'my-secret')
40+
* @param int $versionDestroyTtl Your Version Destroy Ttl (e.g. 86400)
41+
*/
42+
function update_regional_secret_with_delayed_destroy(string $projectId, string $locationId, string $secretId, int $versionDestroyTtl): 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 secret.
54+
$secret = new Secret([
55+
'name' => $name,
56+
'version_destroy_ttl' => new Duration([
57+
'seconds' => $versionDestroyTtl,
58+
])
59+
]);
60+
61+
// Set the field mask.
62+
$fieldMask = new FieldMask();
63+
$fieldMask->setPaths(['version_destroy_ttl']);
64+
65+
// Build the request.
66+
$request = new UpdateSecretRequest();
67+
$request->setSecret($secret);
68+
$request->setUpdateMask($fieldMask);
69+
70+
// Update the secret.
71+
$newSecret = $client->updateSecret($request);
72+
73+
// Print the new secret name.
74+
printf('Updated secret: %s', $newSecret->getName());
75+
}
76+
// [END secretmanager_update_regional_secret_with_delayed_destroy]
77+
78+
// The following 2 lines are only needed to execute the samples on the CLI
79+
require_once __DIR__ . '/../../testing/sample_helpers.php';
80+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

0 commit comments

Comments
 (0)