Skip to content

Commit ac839ba

Browse files
feat(secret-manager): add optional ttl field to createSecret function (#3950)
* feat(secret-manager): add optional ttl field to createSecret function * fix: update TTL format * fix(secret-manager): Update unit test for createSecret.js * fix(secret-manager): Add TTL support to secret creation with improved test validation
1 parent 8a59f53 commit ac839ba

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

secret-manager/createSecret.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,18 @@
1414

1515
'use strict';
1616

17-
async function main(parent = 'projects/my-project', secretId = 'my-secret') {
17+
async function main(
18+
parent = 'projects/my-project',
19+
secretId = 'my-secret',
20+
ttl = undefined
21+
) {
1822
// [START secretmanager_create_secret]
1923
/**
2024
* TODO(developer): Uncomment these variables before running the sample.
2125
*/
2226
// const parent = 'projects/my-project';
2327
// const secretId = 'my-secret';
28+
// const ttl = undefined // Optional: Specify TTL in seconds (e.g., '900s' for 15 minutes).
2429

2530
// Imports the Secret Manager library
2631
const {SecretManagerServiceClient} = require('@google-cloud/secret-manager');
@@ -29,14 +34,24 @@ async function main(parent = 'projects/my-project', secretId = 'my-secret') {
2934
const client = new SecretManagerServiceClient();
3035

3136
async function createSecret() {
37+
const secretConfig = {
38+
replication: {
39+
automatic: {},
40+
},
41+
};
42+
43+
// Add TTL to the secret configuration if provided
44+
if (ttl) {
45+
secretConfig.ttl = {
46+
seconds: parseInt(ttl.replace('s', ''), 10),
47+
};
48+
console.log(`Secret TTL set to ${ttl}`);
49+
}
50+
3251
const [secret] = await client.createSecret({
3352
parent: parent,
3453
secretId: secretId,
35-
secret: {
36-
replication: {
37-
automatic: {},
38-
},
39-
},
54+
secret: secretConfig,
4055
});
4156

4257
console.log(`Created secret ${secret.name}`);

secret-manager/test/secretmanager.test.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,11 +245,21 @@ describe('Secret Manager samples', () => {
245245
assert.match(stdout, new RegExp('Payload: bar'));
246246
});
247247

248-
it('creates a secret', async () => {
248+
it('creates a secret with TTL', async () => {
249+
const ttl = '900s';
249250
const output = execSync(
250-
`node createSecret.js projects/${projectId} ${secretId}-2`
251+
`node createSecret.js projects/${projectId} ${secretId}-2 ${ttl}`
251252
);
252253
assert.match(output, new RegExp('Created secret'));
254+
assert.match(output, new RegExp(`Secret TTL set to ${ttl}`));
255+
});
256+
257+
it('creates a secret without TTL', async () => {
258+
const output = execSync(
259+
`node createSecret.js projects/${projectId} ${secretId}-7`
260+
);
261+
assert.match(output, new RegExp('Created secret'));
262+
assert.notMatch(output, new RegExp('Secret TTL set to'));
253263
});
254264

255265
it('creates a regional secret', async () => {

0 commit comments

Comments
 (0)