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(secretmanager): add samples for secret annotations #3825

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
55 changes: 55 additions & 0 deletions secret-manager/createSecretWithAnnotations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

async function main(parent, secretId, annotationKey, annotationValue) {
// [START secretmanager_create_secret_with_annotations]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const parent = 'projects/my-project';
// const secretId = 'my-secret';
// const annotationKey = 'exampleannotationkey';
// const annotationValue = 'exampleannotationvalue';

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

// Instantiates a client
const client = new SecretManagerServiceClient();

async function createSecretWithAnnotations() {
const [secret] = await client.createSecret({
parent: parent,
secretId: secretId,
secret: {
replication: {
automatic: {},
},
annotations: {
[annotationKey]: annotationValue,
},
},
});

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

createSecretWithAnnotations();
// [END secretmanager_create_secret_with_annotations]
}

const args = process.argv.slice(2);
main(...args).catch(console.error);
61 changes: 61 additions & 0 deletions secret-manager/editSecretAnnotations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

async function main(name, annotationKey, annotationValue) {
// [START secretmanager_edit_secret_annotations]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const name = 'projects/my-project/secrets/my-secret';
// const annotationKey = 'updatedannotationkey';
// const annotationValue = 'updatedannotationvalue';

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

// Instantiates a client
const client = new SecretManagerServiceClient();

async function getSecret() {
const [secret] = await client.getSecret({
name: name,
});

return secret;
}

async function editSecretAnnotations() {
const oldSecret = await getSecret();
oldSecret.annotations[annotationKey] = annotationValue;
const [secret] = await client.updateSecret({
secret: {
name: name,
annotations: oldSecret.annotations,
},
updateMask: {
paths: ['annotations'],
},
});

console.info(`Updated secret ${secret.name}`);
}

editSecretAnnotations();
// [END secretmanager_edit_secret_annotations]
}

const args = process.argv.slice(2);
main(...args).catch(console.error);
36 changes: 36 additions & 0 deletions secret-manager/test/secretmanager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
const labelValue = 'rocks';
const labelKeyUpdated = 'gcp';
const labelValueUpdated = 'rock';
const annotationKey = 'annotationkey';
const annotationValue = 'annotationvalue';
const annotationKeyUpdated = 'updatedannotationekey';
const annotationValueUpdated = 'updatedannotationvalue';

let secret;
let regionalSecret;
Expand Down Expand Up @@ -58,6 +62,9 @@
labels: {
[labelKey]: labelValue,
},
annotations: {
[annotationKey]: annotationValue,
}

Check failure on line 67 in secret-manager/test/secretmanager.test.js

View workflow job for this annotation

GitHub Actions / lint

Insert `,`
},
});

Expand Down Expand Up @@ -170,6 +177,16 @@
throw err;
}
}

try {
await client.deleteSecret({
name: `${secret.name}-5`,
});
} catch (err) {
if (!err.message.includes('NOT_FOUND')) {
throw err;
}
}
});

it('runs the quickstart', async () => {
Expand Down Expand Up @@ -218,6 +235,13 @@
assert.match(output, new RegExp('Created secret'));
});

it('creates a secret with annotations', async () => {
const output = execSync(
`node createSecretWithAnnotations.js projects/${projectId} ${secretId}-5 ${annotationKey} ${annotationValue}`
);
assert.match(output, new RegExp('Created secret'));
});

it('lists secrets', async () => {
const output = execSync(`node listSecrets.js projects/${projectId}`);
assert.match(output, new RegExp(`${secret.name}`));
Expand All @@ -240,6 +264,11 @@
assert.match(output, new RegExp(`${labelKey}`));
});

it('view a secret annotations', async () => {
const output = execSync(`node viewSecretAnnotations.js ${secret.name}`);
assert.match(output, new RegExp(`${annotationKey}`));
});

it('gets a regional secret', async () => {
const output = execSync(
`node regional_samples/getRegionalSecret.js ${projectId} ${locationId} ${secretId}`
Expand Down Expand Up @@ -271,6 +300,13 @@
assert.match(output, new RegExp(`Updated secret ${secret.name}`));
});

it('edits a secret annotation', async () => {
const output = execSync(
`node editSecretAnnotations.js ${secret.name} ${annotationKeyUpdated} ${annotationValueUpdated}`
);
assert.match(output, new RegExp(`Updated secret ${secret.name}`));
});

it('updates a regional secret with an alias', async () => {
const output = execSync(
`node regional_samples/updateRegionalSecretWithAlias.js ${projectId} ${locationId} ${secretId}`
Expand Down
45 changes: 45 additions & 0 deletions secret-manager/viewSecretAnnotations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

async function main(name) {
// [START secretmanager_view_secret_annotations]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const parent = 'projects/my-project/secrets/my-secret';

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

// Instantiates a client
const client = new SecretManagerServiceClient();

async function getSecretAnnotations() {
const [secret] = await client.getSecret({
name: name,
});

for (const key in secret.annotations) {
console.log(`${key} : ${secret.annotations[key]}`);
}
}

getSecretAnnotations();
// [END secretmanager_view_secret_annotations]
}

const args = process.argv.slice(2);
main(...args).catch(console.error);
Loading