Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(secret-manager): add optional ttl field to createSecret function #3950

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all 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
Loading