Skip to content

Commit d86d265

Browse files
authored
chore(secretmanager): add global samples for delayed destroy (#4073)
1 parent 3cd9396 commit d86d265

File tree

4 files changed

+213
-0
lines changed

4 files changed

+213
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
async function main(parent, secretId, timeToLive) {
18+
// [START secretmanager_create_secret_with_delayed_destroy]
19+
/**
20+
* TODO(developer): Uncomment these variables before running the sample.
21+
*/
22+
// const parent = 'projects/my-project';
23+
// const secretId = 'my-secret';
24+
// const timeToLive = 86400;
25+
26+
// Imports the Secret Manager library
27+
const {SecretManagerServiceClient} = require('@google-cloud/secret-manager');
28+
29+
// Instantiates a client
30+
const client = new SecretManagerServiceClient();
31+
32+
async function createSecretWithDelayedDestroy() {
33+
const [secret] = await client.createSecret({
34+
parent: parent,
35+
secretId: secretId,
36+
secret: {
37+
replication: {
38+
automatic: {},
39+
},
40+
version_destroy_ttl: {
41+
seconds: timeToLive,
42+
},
43+
},
44+
});
45+
46+
console.log(`Created secret ${secret.name}`);
47+
}
48+
49+
createSecretWithDelayedDestroy();
50+
// [END secretmanager_create_secret_with_delayed_destroy]
51+
}
52+
53+
const args = process.argv.slice(2);
54+
main(...args).catch(console.error);
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
async function main(name = 'projects/my-project/secrets/my-secret') {
18+
// [START secretmanager_disable_secret_delayed_destroy]
19+
/**
20+
* TODO(developer): Uncomment these variables before running the sample.
21+
*/
22+
// const name = 'projects/my-project/secrets/my-secret';
23+
24+
// Imports the Secret Manager library
25+
const {SecretManagerServiceClient} = require('@google-cloud/secret-manager');
26+
27+
// Instantiates a client
28+
const client = new SecretManagerServiceClient();
29+
30+
async function disableSecretDelayedDestroy() {
31+
const [secret] = await client.updateSecret({
32+
secret: {
33+
name: name,
34+
},
35+
updateMask: {
36+
paths: ['version_destroy_ttl'],
37+
},
38+
});
39+
40+
console.info(`Disabled delayed destroy ${secret.name}`);
41+
}
42+
43+
disableSecretDelayedDestroy();
44+
// [END secretmanager_disable_secret_delayed_destroy]
45+
}
46+
47+
const args = process.argv.slice(2);
48+
main(...args).catch(console.error);

secret-manager/test/secretmanager.test.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,4 +543,60 @@ describe('Secret Manager samples', () => {
543543
);
544544
assert.match(output, new RegExp(`Destroyed ${regionalVersion.name}`));
545545
});
546+
547+
it('creates a secret with delayed destroy enabled', async () => {
548+
const timeToLive = 24 * 60 * 60;
549+
const output = execSync(
550+
`node createSecretWithDelayedDestroy.js projects/${projectId} ${secretId}-2 ${timeToLive}`
551+
);
552+
assert.match(output, new RegExp('Created secret'));
553+
});
554+
555+
it('disables a secret delayed destroy', async () => {
556+
await client.createSecret({
557+
parent: `projects/${projectId}`,
558+
secretId: `${secretId}-delayedDestroy`,
559+
secret: {
560+
replication: {
561+
automatic: {},
562+
},
563+
version_destroy_ttl: {
564+
seconds: 24 * 60 * 60,
565+
},
566+
},
567+
});
568+
569+
const output = execSync(
570+
`node disableSecretDelayedDestroy.js ${secret.name}-delayedDestroy`
571+
);
572+
assert.match(output, new RegExp('Disabled delayed destroy'));
573+
574+
await client.deleteSecret({
575+
name: `${secret.name}-delayedDestroy`,
576+
});
577+
});
578+
579+
it('updates a secret delayed destroy', async () => {
580+
const updatedTimeToLive = 24 * 60 * 60 * 2;
581+
await client.createSecret({
582+
parent: `projects/${projectId}`,
583+
secretId: `${secretId}-delayedDestroy`,
584+
secret: {
585+
replication: {
586+
automatic: {},
587+
},
588+
version_destroy_ttl: {
589+
seconds: 24 * 60 * 60,
590+
},
591+
},
592+
});
593+
594+
const output = execSync(
595+
`node updateSecretWithDelayedDestroy.js ${secret.name}-delayedDestroy ${updatedTimeToLive}`
596+
);
597+
assert.match(output, new RegExp('Updated secret'));
598+
await client.deleteSecret({
599+
name: `${secret.name}-delayedDestroy`,
600+
});
601+
});
546602
});
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
async function main(
18+
name = 'projects/my-project/secrets/my-secret',
19+
updatedTimeToLive
20+
) {
21+
// [START secretmanager_update_secret_with_delayed_destroy]
22+
/**
23+
* TODO(developer): Uncomment these variables before running the sample.
24+
*/
25+
// const name = 'projects/my-project/secrets/my-secret';
26+
// const updatedTimeToLive = 86400;
27+
28+
// Imports the Secret Manager library
29+
const {SecretManagerServiceClient} = require('@google-cloud/secret-manager');
30+
31+
// Instantiates a client
32+
const client = new SecretManagerServiceClient();
33+
34+
async function updateSecret() {
35+
const [secret] = await client.updateSecret({
36+
secret: {
37+
name: name,
38+
version_destroy_ttl: {
39+
seconds: updatedTimeToLive,
40+
},
41+
},
42+
updateMask: {
43+
paths: ['version_destroy_ttl'],
44+
},
45+
});
46+
47+
console.info(`Updated secret ${secret.name}`);
48+
}
49+
50+
updateSecret();
51+
// [END secretmanager_update_secret_with_delayed_destroy]
52+
}
53+
54+
const args = process.argv.slice(2);
55+
main(...args).catch(console.error);

0 commit comments

Comments
 (0)