Skip to content

Commit 8cd81dd

Browse files
feat(secret-manager): add optional ttl field to createSecret function
- Introduced the 'ttl' parameter in the Node.js sample for creating secrets. - Ensured backward compatibility by handling cases where 'ttl' is undefined.
1 parent 22a46bd commit 8cd81dd

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

secret-manager/createSecret.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,38 @@
1414

1515
'use strict';
1616

17-
async function main(parent = 'projects/my-project', secretId = 'my-secret') {
17+
async function main(parent = 'projects/my-project', secretId = 'my-secret', ttl = undefined) {
1818
// [START secretmanager_create_secret]
1919
/**
2020
* TODO(developer): Uncomment these variables before running the sample.
2121
*/
2222
// const parent = 'projects/my-project';
2323
// const secretId = 'my-secret';
24+
// const ttl = '900s' // Optional: Specify TTL in seconds (e.g., '900s' for 15 minutes).
2425

2526
// Imports the Secret Manager library
26-
const {SecretManagerServiceClient} = require('@google-cloud/secret-manager');
27+
const { SecretManagerServiceClient } = require('@google-cloud/secret-manager');
2728

2829
// Instantiates a client
2930
const client = new SecretManagerServiceClient();
3031

3132
async function createSecret() {
33+
const secretConfig = {
34+
replication: {
35+
automatic: {}
36+
},
37+
};
38+
39+
// Add TTL to the secret configuration if provided
40+
if (ttl) {
41+
secretConfig.ttl = ttl;
42+
}
43+
3244
const [secret] = await client.createSecret({
3345
parent: parent,
3446
secretId: secretId,
35-
secret: {
36-
replication: {
37-
automatic: {},
38-
},
39-
},
40-
});
47+
secret: secretConfig,
48+
})
4149

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

0 commit comments

Comments
 (0)