Skip to content

Commit 7a89075

Browse files
vipul7499subfuzion
andauthored
feat(secretmanager): add samples for secret annotations and regional labels (#3825)
* feat(secretmanager): add samples for secret annotations * feat: create samples for labels, regional SM * fix: fix linting issue * fix: lint fix * Update viewSecretAnnotations.js * fix: changed filename * fix: fixed tests * fix: fixed tests * fix: fixed linting --------- Co-authored-by: Tony Pujals <[email protected]>
1 parent 196501d commit 7a89075

8 files changed

+491
-1
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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(parent, secretId, annotationKey, annotationValue) {
18+
// [START secretmanager_create_secret_with_annotations]
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 annotationKey = 'exampleannotationkey';
25+
// const annotationValue = 'exampleannotationvalue';
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 createSecretWithAnnotations() {
34+
const [secret] = await client.createSecret({
35+
parent: parent,
36+
secretId: secretId,
37+
secret: {
38+
replication: {
39+
automatic: {},
40+
},
41+
annotations: {
42+
[annotationKey]: annotationValue,
43+
},
44+
},
45+
});
46+
47+
console.log(`Created secret ${secret.name}`);
48+
}
49+
50+
createSecretWithAnnotations();
51+
// [END secretmanager_create_secret_with_annotations]
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 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(name, annotationKey, annotationValue) {
18+
// [START secretmanager_edit_secret_annotations]
19+
/**
20+
* TODO(developer): Uncomment these variables before running the sample.
21+
*/
22+
// const name = 'projects/my-project/secrets/my-secret';
23+
// const annotationKey = 'updatedannotationkey';
24+
// const annotationValue = 'updatedannotationvalue';
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 editSecretAnnotations() {
41+
const oldSecret = await getSecret();
42+
oldSecret.annotations[annotationKey] = annotationValue;
43+
const [secret] = await client.updateSecret({
44+
secret: {
45+
name: name,
46+
annotations: oldSecret.annotations,
47+
},
48+
updateMask: {
49+
paths: ['annotations'],
50+
},
51+
});
52+
53+
console.info(`Updated secret ${secret.name}`);
54+
}
55+
56+
editSecretAnnotations();
57+
// [END secretmanager_edit_secret_annotations]
58+
}
59+
60+
const args = process.argv.slice(2);
61+
main(...args).catch(console.error);
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: 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: 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_edit_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 = '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_edit_regional_secret_label]
65+
}
66+
67+
const args = process.argv.slice(2);
68+
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);

0 commit comments

Comments
 (0)