Skip to content

Commit 222169e

Browse files
committed
feat: create samples for labels, regional SM
1 parent 1825bd2 commit 222169e

File tree

5 files changed

+289
-1
lines changed

5 files changed

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

secret-manager/test/secretmanager.test.js

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ describe('Secret Manager samples', () => {
7171
[regionalSecret] = await regionalClient.createSecret({
7272
parent: `projects/${projectId}/locations/${locationId}`,
7373
secretId: secretId,
74+
secret: {
75+
labels: {
76+
[labelKey]: labelValue,
77+
},
78+
}
7479
});
7580

7681
[version] = await client.addSecretVersion({
@@ -187,6 +192,16 @@ describe('Secret Manager samples', () => {
187192
throw err;
188193
}
189194
}
195+
196+
try {
197+
await client.deleteSecret({
198+
name: `${secret.name}-6`,
199+
});
200+
} catch (err) {
201+
if (!err.message.includes('NOT_FOUND')) {
202+
throw err;
203+
}
204+
}
190205
});
191206

192207
it('runs the quickstart', async () => {
@@ -235,9 +250,16 @@ describe('Secret Manager samples', () => {
235250
assert.match(output, new RegExp('Created secret'));
236251
});
237252

253+
it('creates a regional secret with labels', async () => {
254+
const output = execSync(
255+
`node regional_samples/createRegionalSecretWithLabels.js ${projectId} ${locationId} ${secretId}-5 ${labelKey} ${labelValue}`
256+
);
257+
assert.match(output, new RegExp('Created secret'));
258+
});
259+
238260
it('creates a secret with annotations', async () => {
239261
const output = execSync(
240-
`node createSecretWithAnnotations.js projects/${projectId} ${secretId}-5 ${annotationKey} ${annotationValue}`
262+
`node createSecretWithAnnotations.js projects/${projectId} ${secretId}-6 ${annotationKey} ${annotationValue}`
241263
);
242264
assert.match(output, new RegExp('Created secret'));
243265
});
@@ -264,6 +286,13 @@ describe('Secret Manager samples', () => {
264286
assert.match(output, new RegExp(`${labelKey}`));
265287
});
266288

289+
it('view a regional secret labels', async () => {
290+
const output = execSync(
291+
`node regional_samples/viewRegionalSecretLabels.js ${projectId} ${locationId} ${secretId}
292+
`);
293+
assert.match(output, new RegExp(`${labelKey}`));
294+
});
295+
267296
it('view a secret annotations', async () => {
268297
const output = execSync(`node viewSecretAnnotations.js ${secret.name}`);
269298
assert.match(output, new RegExp(`${annotationKey}`));
@@ -300,6 +329,13 @@ describe('Secret Manager samples', () => {
300329
assert.match(output, new RegExp(`Updated secret ${secret.name}`));
301330
});
302331

332+
it('create or updates a regional secret labels', async () => {
333+
const output = execSync(
334+
`node regional_samples/createUpdateRegionalSecretLabel.js ${projectId} ${locationId} ${secretId} ${labelKeyUpdated} ${labelValueUpdated}`
335+
);
336+
assert.match(output, new RegExp(`Updated secret ${regionalSecret.name}`));
337+
});
338+
303339
it('edits a secret annotation', async () => {
304340
const output = execSync(
305341
`node editSecretAnnotations.js ${secret.name} ${annotationKeyUpdated} ${annotationValueUpdated}`
@@ -328,6 +364,13 @@ describe('Secret Manager samples', () => {
328364
assert.match(output, new RegExp(`Updated secret ${secret.name}`));
329365
});
330366

367+
it('deletes a regional secret label', async () => {
368+
const output = execSync(
369+
`node regional_samples/deleteRegionalSecretLabel.js ${projectId} ${locationId} ${secretId} ${labelKey}`
370+
);
371+
assert.match(output, new RegExp(`Updated secret ${regionalSecret.name}`));
372+
});
373+
331374
it('deletes a regional secret', async () => {
332375
const output = execSync(
333376
`node regional_samples/deleteRegionalSecret.js ${projectId} ${locationId} ${secretId}-3`

0 commit comments

Comments
 (0)