Skip to content

Commit 196501d

Browse files
vipul7499kapishps
andauthored
feat: add annotations samples for regional SM (#3896)
* feat: add annotations samples for regional SM * fix: added tests * fix: lint issues * fix: lint issues * fix: lint issues --------- Co-authored-by: Kapish <[email protected]>
1 parent e587ab9 commit 196501d

File tree

4 files changed

+231
-0
lines changed

4 files changed

+231
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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(
18+
projectId,
19+
locationId,
20+
secretId,
21+
annotationKey,
22+
annotationValue
23+
) {
24+
// [START secretmanager_create_regional_secret_with_annotations]
25+
/**
26+
* TODO(developer): Uncomment these variables before running the sample.
27+
*/
28+
// const projectId = 'my-project'
29+
// const locationId = 'locationId';
30+
// const secretId = 'my-secret';
31+
// const annotationKey = 'exampleannotationkey';
32+
// const annotationValue = 'exampleannotationvalue';
33+
34+
const parent = `projects/${projectId}/locations/${locationId}`;
35+
36+
// Imports the Secret Manager library
37+
const {SecretManagerServiceClient} = require('@google-cloud/secret-manager');
38+
39+
// Adding the endpoint to call the regional secret manager sever
40+
const options = {};
41+
options.apiEndpoint = `secretmanager.${locationId}.rep.googleapis.com`;
42+
// Instantiates a client
43+
const client = new SecretManagerServiceClient(options);
44+
45+
async function createRegionalSecretWithAnnotations() {
46+
const [secret] = await client.createSecret({
47+
parent: parent,
48+
secretId: secretId,
49+
secret: {
50+
annotations: {
51+
[annotationKey]: annotationValue,
52+
},
53+
},
54+
});
55+
56+
console.log(`Created secret ${secret.name}`);
57+
}
58+
59+
createRegionalSecretWithAnnotations();
60+
// [END secretmanager_create_regional_secret_with_annotations]
61+
}
62+
63+
const args = process.argv.slice(2);
64+
main(...args).catch(console.error);
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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(
18+
projectId,
19+
locationId,
20+
secretId,
21+
annotationKey,
22+
annotationValue
23+
) {
24+
// [START secretmanager_edit_regional_secret_annotations]
25+
/**
26+
* TODO(developer): Uncomment these variables before running the sample.
27+
*/
28+
// const projectId = 'my-project'
29+
// const locationId = 'locationId';
30+
// const secretId = 'my-secret';
31+
// const annotationKey = 'updatedannotationkey';
32+
// const annotationValue = 'updatedannotationvalue';
33+
34+
const name = `projects/${projectId}/locations/${locationId}/secrets/${secretId}`;
35+
36+
// Imports the Secret Manager library
37+
const {SecretManagerServiceClient} = require('@google-cloud/secret-manager');
38+
39+
// Adding the endpoint to call the regional secret manager sever
40+
const options = {};
41+
options.apiEndpoint = `secretmanager.${locationId}.rep.googleapis.com`;
42+
// Instantiates a client
43+
const client = new SecretManagerServiceClient(options);
44+
45+
async function getSecret() {
46+
const [secret] = await client.getSecret({
47+
name: name,
48+
});
49+
50+
return secret;
51+
}
52+
53+
async function editRegionalSecretAnnotations() {
54+
const oldSecret = await getSecret();
55+
oldSecret.annotations[annotationKey] = annotationValue;
56+
const [secret] = await client.updateSecret({
57+
secret: {
58+
name: name,
59+
annotations: oldSecret.annotations,
60+
},
61+
updateMask: {
62+
paths: ['annotations'],
63+
},
64+
});
65+
66+
console.info(`Updated secret ${secret.name}`);
67+
}
68+
69+
editRegionalSecretAnnotations();
70+
// [END secretmanager_edit_regional_secret_annotations]
71+
}
72+
73+
const args = process.argv.slice(2);
74+
main(...args).catch(console.error);
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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) {
18+
// [START secretmanager_view_regional_secret_annotations]
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+
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+
// Instantiates a client
35+
const client = new SecretManagerServiceClient(options);
36+
37+
async function viewRegionalSecretAnnotations() {
38+
const [secret] = await client.getSecret({
39+
name: name,
40+
});
41+
42+
for (const key in secret.annotations) {
43+
console.log(`${key} : ${secret.annotations[key]}`);
44+
}
45+
}
46+
47+
viewRegionalSecretAnnotations();
48+
// [END secretmanager_view_regional_secret_annotations]
49+
}
50+
51+
const args = process.argv.slice(2);
52+
main(...args).catch(console.error);

secret-manager/test/secretmanager.test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ const labelKey = 'secretmanager';
3030
const labelValue = 'rocks';
3131
const labelKeyUpdated = 'gcp';
3232
const labelValueUpdated = 'rock';
33+
const annotationKey = 'annotationkey';
34+
const annotationValue = 'annotationvalue';
35+
const annotationKeyUpdated = 'updatedannotationekey';
36+
const annotationValueUpdated = 'updatedannotationvalue';
3337

3438
let secret;
3539
let regionalSecret;
@@ -64,6 +68,11 @@ describe('Secret Manager samples', () => {
6468
[regionalSecret] = await regionalClient.createSecret({
6569
parent: `projects/${projectId}/locations/${locationId}`,
6670
secretId: secretId,
71+
secret: {
72+
annotations: {
73+
[annotationKey]: annotationValue,
74+
},
75+
},
6776
});
6877

6978
[version] = await client.addSecretVersion({
@@ -170,6 +179,16 @@ describe('Secret Manager samples', () => {
170179
throw err;
171180
}
172181
}
182+
183+
try {
184+
await regionalClient.deleteSecret({
185+
name: `${regionalSecret.name}-4`,
186+
});
187+
} catch (err) {
188+
if (!err.message.includes('NOT_FOUND')) {
189+
throw err;
190+
}
191+
}
173192
});
174193

175194
it('runs the quickstart', async () => {
@@ -218,6 +237,13 @@ describe('Secret Manager samples', () => {
218237
assert.match(output, new RegExp('Created secret'));
219238
});
220239

240+
it('creates a regional secret with annotations', async () => {
241+
const output = execSync(
242+
`node regional_samples/createRegionalSecretWithAnnotations.js ${projectId} ${locationId} ${secretId}-5 ${annotationKey} ${annotationValue}`
243+
);
244+
assert.match(output, new RegExp('Created secret'));
245+
});
246+
221247
it('lists secrets', async () => {
222248
const output = execSync(`node listSecrets.js projects/${projectId}`);
223249
assert.match(output, new RegExp(`${secret.name}`));
@@ -240,6 +266,14 @@ describe('Secret Manager samples', () => {
240266
assert.match(output, new RegExp(`${labelKey}`));
241267
});
242268

269+
it('view a regional secret annotations', async () => {
270+
const output = execSync(
271+
`node regional_samples/viewRegionalSecretAnnotations.js ${projectId} ${locationId} ${secretId}`
272+
);
273+
274+
assert.match(output, new RegExp(`${annotationKey}`));
275+
});
276+
243277
it('gets a regional secret', async () => {
244278
const output = execSync(
245279
`node regional_samples/getRegionalSecret.js ${projectId} ${locationId} ${secretId}`
@@ -278,6 +312,13 @@ describe('Secret Manager samples', () => {
278312
assert.match(output, new RegExp(`Updated secret ${regionalSecret.name}`));
279313
});
280314

315+
it('edits a regional secret annotations', async () => {
316+
const output = execSync(
317+
`node regional_samples/editRegionalSecretAnnotations.js ${projectId} ${locationId} ${secretId} ${annotationKeyUpdated} ${annotationValueUpdated}`
318+
);
319+
assert.match(output, new RegExp(`Updated secret ${regionalSecret.name}`));
320+
});
321+
281322
it('deletes a secret', async () => {
282323
const output = execSync(
283324
`node deleteSecret.js projects/${projectId}/secrets/${secretId}-2`

0 commit comments

Comments
 (0)