Skip to content

Commit dbd7b1d

Browse files
authored
chore(secretmanager): add regional code samples for delayed destroy (#4133)
* chore(secretmanager): add regional code samples for delayed destroy * chore(secretmanager): fix linting issue * chore(secretmanager): fix spell errors in comment
1 parent d86d265 commit dbd7b1d

File tree

4 files changed

+221
-0
lines changed

4 files changed

+221
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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(projectId, locationId, secretId, timeToLive) {
18+
// [START secretmanager_create_regional_secret_with_delayed_destroy]
19+
/**
20+
* TODO(developer): Uncomment these variables before running the sample.
21+
*/
22+
// const project = 'my-project';
23+
// const locationId = 'my-location';
24+
// const secretId = 'my-secret';
25+
// const timeToLive = 86400;
26+
const parent = `projects/${projectId}/locations/${locationId}`;
27+
28+
// Imports the Secret Manager library
29+
const {SecretManagerServiceClient} = require('@google-cloud/secret-manager');
30+
31+
// Adding the endpoint to call the regional secret manager server
32+
const options = {};
33+
options.apiEndpoint = `secretmanager.${locationId}.rep.googleapis.com`;
34+
35+
// Instantiates a client
36+
const client = new SecretManagerServiceClient(options);
37+
38+
async function createRegionalSecretWithDelayedDestroy() {
39+
const [secret] = await client.createSecret({
40+
parent: parent,
41+
secretId: secretId,
42+
secret: {
43+
version_destroy_ttl: {
44+
seconds: timeToLive,
45+
},
46+
},
47+
});
48+
49+
console.log(`Created regional secret ${secret.name}`);
50+
}
51+
52+
createRegionalSecretWithDelayedDestroy();
53+
// [END secretmanager_create_regional_secret_with_delayed_destroy]
54+
}
55+
56+
const args = process.argv.slice(2);
57+
main(...args).catch(console.error);
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(projectId, locationId, secretId) {
18+
// [START secretmanager_disable_regional_secret_delayed_destroy]
19+
/**
20+
* TODO(developer): Uncomment these variables before running the sample.
21+
*/
22+
// const projectId = 'my-project';
23+
// const locationId = 'my-location';
24+
// const secretId = 'my-secret';
25+
const name = `projects/${projectId}/locations/${locationId}/secrets/${secretId}`;
26+
27+
// Imports the Secret Manager library
28+
const {SecretManagerServiceClient} = require('@google-cloud/secret-manager');
29+
30+
// Adding the endpoint to call the regional secret manager server
31+
const options = {};
32+
options.apiEndpoint = `secretmanager.${locationId}.rep.googleapis.com`;
33+
34+
// Instantiates a client
35+
const client = new SecretManagerServiceClient(options);
36+
37+
async function disableRegionalSecretDelayedDestroy() {
38+
const [secret] = await client.updateSecret({
39+
secret: {
40+
name: name,
41+
},
42+
updateMask: {
43+
paths: ['version_destroy_ttl'],
44+
},
45+
});
46+
47+
console.info(`Disabled delayed destroy ${secret.name}`);
48+
}
49+
50+
disableRegionalSecretDelayedDestroy();
51+
// [END secretmanager_disable_regional_secret_delayed_destroy]
52+
}
53+
54+
const args = process.argv.slice(2);
55+
main(...args).catch(console.error);
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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(projectId, locationId, secretId, updatedTimeToLive) {
18+
// [START secretmanager_update_regional_secret_with_delayed_destroy]
19+
/**
20+
* TODO(developer): Uncomment these variables before running the sample.
21+
*/
22+
// const projectId = 'my-project';
23+
// const locationId = 'my-location';
24+
// const secretId = 'my-secret';
25+
// const updatedTimeToLive = 86400;
26+
const name = `projects/${projectId}/locations/${locationId}/secrets/${secretId}`;
27+
28+
// Imports the Secret Manager library
29+
const {SecretManagerServiceClient} = require('@google-cloud/secret-manager');
30+
31+
// Adding the endpoint to call the regional secret manager server
32+
const options = {};
33+
options.apiEndpoint = `secretmanager.${locationId}.rep.googleapis.com`;
34+
35+
// Instantiates a client
36+
const client = new SecretManagerServiceClient(options);
37+
38+
async function updateRegionalSecret() {
39+
const [secret] = await client.updateSecret({
40+
secret: {
41+
name: name,
42+
version_destroy_ttl: {
43+
seconds: updatedTimeToLive,
44+
},
45+
},
46+
updateMask: {
47+
paths: ['version_destroy_ttl'],
48+
},
49+
});
50+
51+
console.info(`Updated regional secret ${secret.name}`);
52+
}
53+
54+
updateRegionalSecret();
55+
// [END secretmanager_update_regional_secret_with_delayed_destroy]
56+
}
57+
58+
const args = process.argv.slice(2);
59+
main(...args).catch(console.error);

secret-manager/test/secretmanager.test.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,4 +599,54 @@ describe('Secret Manager samples', () => {
599599
name: `${secret.name}-delayedDestroy`,
600600
});
601601
});
602+
603+
it('creates a regional secret with delayed destroy', async () => {
604+
const timeToLive = 24 * 60 * 60;
605+
const output = execSync(
606+
`node regional_samples/createRegionalSecretWithDelayedDestroy.js ${projectId} ${locationId} ${secretId}-2-dd ${timeToLive}`
607+
);
608+
assert.match(output, new RegExp('Created regional secret'));
609+
});
610+
611+
it('disables a regional secret delayed destroy', async () => {
612+
await regionalClient.createSecret({
613+
parent: `projects/${projectId}/locations/${locationId}`,
614+
secretId: `${secretId}-delayedDestroy`,
615+
secret: {
616+
version_destroy_ttl: {
617+
seconds: 24 * 60 * 60,
618+
},
619+
},
620+
});
621+
622+
const output = execSync(
623+
`node regional_samples/disableRegionalSecretDelayedDestroy.js ${projectId} ${locationId} ${secretId}-delayedDestroy`
624+
);
625+
assert.match(output, new RegExp('Disabled delayed destroy'));
626+
627+
await regionalClient.deleteSecret({
628+
name: `projects/${projectId}/locations/${locationId}/secrets/${secretId}-delayedDestroy`,
629+
});
630+
});
631+
632+
it('updates a regional secret delayed destroy', async () => {
633+
const updatedTimeToLive = 24 * 60 * 60 * 2;
634+
await regionalClient.createSecret({
635+
parent: `projects/${projectId}/locations/${locationId}`,
636+
secretId: `${secretId}-delayedDestroy`,
637+
secret: {
638+
version_destroy_ttl: {
639+
seconds: 24 * 60 * 60,
640+
},
641+
},
642+
});
643+
644+
const output = execSync(
645+
`node regional_samples/updateRegionalSecretWithDelayedDestroy.js ${projectId} ${locationId} ${secretId}-delayedDestroy ${updatedTimeToLive}`
646+
);
647+
assert.match(output, new RegExp('Updated regional secret'));
648+
await regionalClient.deleteSecret({
649+
name: `projects/${projectId}/locations/${locationId}/secrets/${secretId}-delayedDestroy`,
650+
});
651+
});
602652
});

0 commit comments

Comments
 (0)