Skip to content
Merged
Show file tree
Hide file tree
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
27 changes: 21 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,24 @@ 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),
};
console.log(`Secret TTL set to ${ttl}`);
}

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

console.log(`Created secret ${secret.name}`);
Expand Down
14 changes: 12 additions & 2 deletions secret-manager/test/secretmanager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,21 @@ describe('Secret Manager samples', () => {
assert.match(stdout, new RegExp('Payload: bar'));
});

it('creates a secret', async () => {
it('creates a secret with TTL', async () => {
const ttl = '900s';
const output = execSync(
`node createSecret.js projects/${projectId} ${secretId}-2`
`node createSecret.js projects/${projectId} ${secretId}-2 ${ttl}`
);
assert.match(output, new RegExp('Created secret'));
Comment thread
briandorsey marked this conversation as resolved.
assert.match(output, new RegExp(`Secret TTL set to ${ttl}`));
});

it('creates a secret without TTL', async () => {
const output = execSync(
`node createSecret.js projects/${projectId} ${secretId}-7`
);
assert.match(output, new RegExp('Created secret'));
assert.notMatch(output, new RegExp('Secret TTL set to'));
});

it('creates a regional secret', async () => {
Expand Down
Loading