Skip to content

Commit ee09630

Browse files
vipul7499graysidesubfuzion
authored
feat: Added samples for secrets with labels (#3728) (#3767)
* feat: Added samples for secrets with labels (#3728) * feat: Added samples for secrets with labels * feat: Added samples for secrets with labels * fix: Linting fix * fix: remoing unwanted change * fix: remove default values for params --------- Co-authored-by: Adam Ross <[email protected]> * fix: updated tests --------- Co-authored-by: Adam Ross <[email protected]> Co-authored-by: Tony Pujals <[email protected]>
1 parent 6216293 commit ee09630

File tree

6 files changed

+259
-1
lines changed

6 files changed

+259
-1
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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(parent, secretId, labelKey, labelValue) {
18+
// [START secretmanager_create_secret_with_labels]
19+
/**
20+
* TODO(developer): Uncomment these variables before running the sample.
21+
*/
22+
// const parent = 'projects/my-project';
23+
// const secretId = 'my-secret';
24+
// const labelKey = 'secretmanager';
25+
// const labelValue = 'rocks';
26+
27+
// Imports the Secret Manager library
28+
const {SecretManagerServiceClient} = require('@google-cloud/secret-manager');
29+
30+
// Instantiates a client
31+
const client = new SecretManagerServiceClient();
32+
33+
async function createSecretWithLabels() {
34+
const [secret] = await client.createSecret({
35+
parent: parent,
36+
secretId: secretId,
37+
secret: {
38+
replication: {
39+
automatic: {},
40+
},
41+
labels: {
42+
[labelKey]: labelValue,
43+
},
44+
},
45+
});
46+
47+
console.log(`Created secret ${secret.name}`);
48+
}
49+
50+
createSecretWithLabels();
51+
// [END secretmanager_create_secret_with_labels]
52+
}
53+
54+
const args = process.argv.slice(2);
55+
main(...args).catch(console.error);
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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, labelKey, labelValue) {
18+
// [START secretmanager_create_update_secret_label]
19+
/**
20+
* TODO(developer): Uncomment these variables before running the sample.
21+
*/
22+
// const name = 'projects/my-project/secrets/my-secret';
23+
// const labelKey = 'gcp';
24+
// const labelValue = 'rocks';
25+
26+
// Imports the Secret Manager library
27+
const {SecretManagerServiceClient} = require('@google-cloud/secret-manager');
28+
29+
// Instantiates a client
30+
const client = new SecretManagerServiceClient();
31+
32+
async function getSecret() {
33+
const [secret] = await client.getSecret({
34+
name: name,
35+
});
36+
37+
return secret;
38+
}
39+
40+
async function createUpdateSecretLabel() {
41+
const oldSecret = await getSecret();
42+
oldSecret.labels[labelKey] = labelValue;
43+
const [secret] = await client.updateSecret({
44+
secret: {
45+
name: name,
46+
labels: oldSecret.labels,
47+
},
48+
updateMask: {
49+
paths: ['labels'],
50+
},
51+
});
52+
53+
console.info(`Updated secret ${secret.name}`);
54+
}
55+
56+
createUpdateSecretLabel();
57+
// [END secretmanager_create_update_secret_label]
58+
}
59+
60+
const args = process.argv.slice(2);
61+
main(...args).catch(console.error);

secret-manager/deleteSecretLabel.js

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, labelKey) {
18+
// [START secretmanager_delete_secret_label]
19+
/**
20+
* TODO(developer): Uncomment these variables before running the sample.
21+
*/
22+
// const name = 'projects/my-project/secrets/my-secret';
23+
// const labelKey = '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 deleteSecretLabel() {
40+
const oldSecret = await getSecret();
41+
delete oldSecret.labels[labelKey];
42+
const [secret] = await client.updateSecret({
43+
secret: {
44+
name: name,
45+
labels: oldSecret.labels,
46+
},
47+
updateMask: {
48+
paths: ['labels'],
49+
},
50+
});
51+
52+
console.info(`Updated secret ${secret.name}`);
53+
}
54+
55+
deleteSecretLabel();
56+
// [END secretmanager_delete_secret_label]
57+
}
58+
59+
const args = process.argv.slice(2);
60+
main(...args).catch(console.error);

secret-manager/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
},
1919
"devDependencies": {
2020
"c8": "^10.0.0",
21-
"chai": "^5.0.0",
21+
"chai": "^4.2.0",
2222
"mocha": "^10.0.0",
2323
"uuid": "^10.0.0"
2424
}

secret-manager/test/secretmanager.test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ const locationId = process.env.GCLOUD_LOCATION || 'us-central1';
2626
const secretId = v4();
2727
const payload = 'my super secret data';
2828
const iamUser = 'user:[email protected]';
29+
const labelKey = 'secretmanager';
30+
const labelValue = 'rocks';
31+
const labelKeyUpdated = 'gcp';
32+
const labelValueUpdated = 'rock';
2933

3034
let secret;
3135
let regionalSecret;
@@ -51,6 +55,9 @@ describe('Secret Manager samples', () => {
5155
replication: {
5256
automatic: {},
5357
},
58+
labels: {
59+
[labelKey]: labelValue,
60+
},
5461
},
5562
});
5663

@@ -151,6 +158,10 @@ describe('Secret Manager samples', () => {
151158
}
152159

153160
try {
161+
await client.deleteSecret({
162+
name: `${secret.name}-4`,
163+
});
164+
154165
await regionalClient.deleteSecret({
155166
name: `${regionalSecret.name}-3`,
156167
});
@@ -200,6 +211,13 @@ describe('Secret Manager samples', () => {
200211
assert.match(output, new RegExp('Created secret'));
201212
});
202213

214+
it('creates a secret with labels', async () => {
215+
const output = execSync(
216+
`node createSecretWithLabels.js projects/${projectId} ${secretId}-4 ${labelKey} ${labelValue}`
217+
);
218+
assert.match(output, new RegExp('Created secret'));
219+
});
220+
203221
it('lists secrets', async () => {
204222
const output = execSync(`node listSecrets.js projects/${projectId}`);
205223
assert.match(output, new RegExp(`${secret.name}`));
@@ -217,6 +235,11 @@ describe('Secret Manager samples', () => {
217235
assert.match(output, new RegExp(`Found secret ${secret.name}`));
218236
});
219237

238+
it('view a secret labels', async () => {
239+
const output = execSync(`node viewSecretLabels.js ${secret.name}`);
240+
assert.match(output, new RegExp(`${labelKey}`));
241+
});
242+
220243
it('gets a regional secret', async () => {
221244
const output = execSync(
222245
`node regional_samples/getRegionalSecret.js ${projectId} ${locationId} ${secretId}`
@@ -241,6 +264,13 @@ describe('Secret Manager samples', () => {
241264
assert.match(output, new RegExp(`Updated secret ${secret.name}`));
242265
});
243266

267+
it('create or updates a secret labels', async () => {
268+
const output = execSync(
269+
`node createUpdateSecretLabel.js ${secret.name} ${labelKeyUpdated} ${labelValueUpdated}`
270+
);
271+
assert.match(output, new RegExp(`Updated secret ${secret.name}`));
272+
});
273+
244274
it('updates a regional secret with an alias', async () => {
245275
const output = execSync(
246276
`node regional_samples/updateRegionalSecretWithAlias.js ${projectId} ${locationId} ${secretId}`
@@ -255,6 +285,13 @@ describe('Secret Manager samples', () => {
255285
assert.match(output, new RegExp('Deleted secret'));
256286
});
257287

288+
it('deletes a secret label', async () => {
289+
const output = execSync(
290+
`node deleteSecretLabel.js ${secret.name} ${labelKey}`
291+
);
292+
assert.match(output, new RegExp(`Updated secret ${secret.name}`));
293+
});
294+
258295
it('deletes a regional secret', async () => {
259296
const output = execSync(
260297
`node regional_samples/deleteRegionalSecret.js ${projectId} ${locationId} ${secretId}-3`

secret-manager/viewSecretLabels.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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) {
18+
// [START secretmanager_view_secret_labels]
19+
/**
20+
* TODO(developer): Uncomment these variables before running the sample.
21+
*/
22+
// const parent = 'projects/my-project/secrets/my-secret';
23+
24+
// Imports the Secret Manager library
25+
const {SecretManagerServiceClient} = require('@google-cloud/secret-manager');
26+
27+
// Instantiates a client
28+
const client = new SecretManagerServiceClient();
29+
30+
async function getSecretLabels() {
31+
const [secret] = await client.getSecret({
32+
name: name,
33+
});
34+
35+
for (const key in secret.labels) {
36+
console.log(`${key} : ${secret.labels[key]}`);
37+
}
38+
}
39+
40+
getSecretLabels();
41+
// [END secretmanager_view_secret_labels]
42+
}
43+
44+
const args = process.argv.slice(2);
45+
main(...args).catch(console.error);

0 commit comments

Comments
 (0)