Skip to content

Commit 165b528

Browse files
chore(secretmanager): Added samples for delete secret annotation (#4139)
* feat(secretmanager): Added samples for delete secret annotation * fix(secretmanager): update code samples * fix(secretmanager): address gemini review comments
1 parent 8b72f8f commit 165b528

File tree

3 files changed

+153
-0
lines changed

3 files changed

+153
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright 2025 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+
if (!oldSecret.annotations) {
42+
console.info(
43+
`Secret ${oldSecret.name} has no annotations, skipping update.`
44+
);
45+
return;
46+
}
47+
delete oldSecret.annotations[annotationKey];
48+
const [secret] = await client.updateSecret({
49+
secret: {
50+
name: name,
51+
annotations: oldSecret.annotations,
52+
},
53+
updateMask: {
54+
paths: ['annotations'],
55+
},
56+
});
57+
58+
console.info(`Updated secret ${secret.name}`);
59+
}
60+
61+
deleteSecretAnnotation();
62+
// [END secretmanager_delete_secret_annotation]
63+
}
64+
65+
const args = process.argv.slice(2);
66+
main(...args).catch(console.error);
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright 2025 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+
if (!oldSecret.annotations) {
49+
console.info(
50+
`Secret ${oldSecret.name} has no annotations, skipping update.`
51+
);
52+
return;
53+
}
54+
delete oldSecret.annotations[annotationKey];
55+
const [secret] = await client.updateSecret({
56+
secret: {
57+
name: name,
58+
annotations: oldSecret.annotations,
59+
},
60+
updateMask: {
61+
paths: ['annotations'],
62+
},
63+
});
64+
65+
console.info(`Updated secret ${secret.name}`);
66+
}
67+
68+
deleteRegionalSecretAnnotation();
69+
// [END secretmanager_delete_regional_secret_annotation]
70+
}
71+
72+
const args = process.argv.slice(2);
73+
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)