Skip to content
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions secret-manager/createSecret.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@

'use strict';

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

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

async function createSecret() {
const secretConfig = {
replication: {
automatic: {},
},
};

// Add TTL to the secret configuration if provided
if (ttl) {
secretConfig.ttl = {
seconds: parseInt(ttl.replace('s', ''), 10),
};
}

const [secret] = await client.createSecret({
parent: parent,
secretId: secretId,
secret: {
replication: {
automatic: {},
},
},
secret: secretConfig,
});

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