Skip to content

Commit b12530e

Browse files
feat(secretmanager): Added samples for delete secret annotation
1 parent 8b72f8f commit b12530e

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright 2019 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
async function main(name, annotationKey) {
18+
// [START secretmanager_delete_secret_annotation]
19+
/**
20+
* TODO(developer): Uncomment these variables before running the sample.
21+
*/
22+
// const name = 'projects/my-project/secrets/my-secret';
23+
// const annotationKey = 'secretmanager';
24+
25+
// Imports the Secret Manager library
26+
const {SecretManagerServiceClient} = require('@google-cloud/secret-manager');
27+
28+
// Instantiates a client
29+
const client = new SecretManagerServiceClient();
30+
31+
async function getSecret() {
32+
const [secret] = await client.getSecret({
33+
name: name,
34+
});
35+
36+
return secret;
37+
}
38+
39+
async function deleteSecretAnnotation() {
40+
const oldSecret = await getSecret();
41+
delete oldSecret.annotations[annotationKey];
42+
const [secret] = await client.updateSecret({
43+
secret: {
44+
name: name,
45+
annotations: oldSecret.annotations,
46+
},
47+
updateMask: {
48+
paths: ['annotations'],
49+
},
50+
});
51+
52+
console.info(`Updated secret ${secret.name}`);
53+
}
54+
55+
deleteSecretAnnotation();
56+
// [END secretmanager_delete_secret_annotation]
57+
}
58+
59+
const args = process.argv.slice(2);
60+
main(...args).catch(console.error);
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
async function main(projectId, locationId, secretId, annotationKey) {
18+
// [START secretmanager_delete_regional_secret_annotation]
19+
/**
20+
* TODO(developer): Uncomment these variables before running the sample.
21+
*/
22+
// const projectId = 'my-project'
23+
// const locationId = 'locationId';
24+
// const secretId = 'my-secret';
25+
// const annotationKey = 'secretmanager';
26+
const name = `projects/${projectId}/locations/${locationId}/secrets/${secretId}`;
27+
28+
// Imports the Secret Manager library
29+
const {SecretManagerServiceClient} = require('@google-cloud/secret-manager');
30+
31+
// Adding the endpoint to call the regional secret manager sever
32+
const options = {};
33+
options.apiEndpoint = `secretmanager.${locationId}.rep.googleapis.com`;
34+
35+
// Instantiates a client
36+
const client = new SecretManagerServiceClient(options);
37+
38+
async function getSecret() {
39+
const [secret] = await client.getSecret({
40+
name: name,
41+
});
42+
43+
return secret;
44+
}
45+
46+
async function deleteRegionalSecretAnnotation() {
47+
const oldSecret = await getSecret();
48+
delete oldSecret.annotations[annotationKey];
49+
const [secret] = await client.updateSecret({
50+
secret: {
51+
name: name,
52+
annotations: oldSecret.annotations,
53+
},
54+
updateMask: {
55+
paths: ['annotations'],
56+
},
57+
});
58+
59+
console.info(`Updated secret ${secret.name}`);
60+
}
61+
62+
deleteRegionalSecretAnnotation();
63+
// [END secretmanager_delete_regional_secret_annotation]
64+
}
65+
66+
const args = process.argv.slice(2);
67+
main(...args).catch(console.error);

secret-manager/test/secretmanager.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,20 @@ describe('Secret Manager samples', () => {
547547
assert.match(output, new RegExp(`Updated secret ${regionalSecret.name}`));
548548
});
549549

550+
it('deletes a secret annotation', async () => {
551+
const output = execSync(
552+
`node deleteSecretAnnotation.js ${secret.name} ${annotationKey}`
553+
);
554+
assert.match(output, new RegExp(`Updated secret ${secret.name}`));
555+
});
556+
557+
it('deletes a regional secret annotation', async () => {
558+
const output = execSync(
559+
`node regional_samples/deleteRegionalSecretAnnotation.js ${projectId} ${locationId} ${secretId} ${annotationKey}`
560+
);
561+
assert.match(output, new RegExp(`Updated secret ${regionalSecret.name}`));
562+
});
563+
550564
it('deletes a regional secret', async () => {
551565
const output = execSync(
552566
`node regional_samples/deleteRegionalSecret.js ${projectId} ${locationId} ${secretId}-3`

0 commit comments

Comments
 (0)