Skip to content

Commit 68c79f9

Browse files
feat(secretmanager): Added samples for tags field (#4134)
1 parent dbd7b1d commit 68c79f9

File tree

6 files changed

+400
-1
lines changed

6 files changed

+400
-1
lines changed

secret-manager/bindTagsToSecret.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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, secretId, tagValue) {
18+
// [START secretmanager_bind_tags_to_secret]
19+
/**
20+
* TODO(developer): Uncomment these variables before running the sample.
21+
*/
22+
// const projectId = 'my-project';
23+
// const secretId = 'my-secret';
24+
// const tagValue = 'tagValues/281476592621530';
25+
const parent = `projects/${projectId}`;
26+
27+
// Imports the library
28+
const {SecretManagerServiceClient} = require('@google-cloud/secret-manager');
29+
const {TagBindingsClient} = require('@google-cloud/resource-manager').v3;
30+
31+
// Instantiates a client
32+
const client = new SecretManagerServiceClient();
33+
const resourcemanagerClient = new TagBindingsClient();
34+
35+
async function bindTagsToSecret() {
36+
const [secret] = await client.createSecret({
37+
parent: parent,
38+
secretId: secretId,
39+
secret: {
40+
replication: {
41+
automatic: {},
42+
},
43+
},
44+
});
45+
46+
console.log(`Created secret ${secret.name}`);
47+
48+
const [operation] = await resourcemanagerClient.createTagBinding({
49+
tagBinding: {
50+
parent: `//secretmanager.googleapis.com/${secret.name}`,
51+
tagValue: tagValue,
52+
},
53+
});
54+
const [response] = await operation.promise();
55+
console.log('Created Tag Binding', response.name);
56+
}
57+
58+
bindTagsToSecret();
59+
// [END secretmanager_bind_tags_to_secret]
60+
}
61+
62+
const args = process.argv.slice(2);
63+
main(...args).catch(console.error);
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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, secretId, tagKey, tagValue) {
18+
// [START secretmanager_create_secret_with_tags]
19+
/**
20+
* TODO(developer): Uncomment these variables before running the sample.
21+
*/
22+
// const projectId = 'my-project';
23+
// const secretId = 'my-secret';
24+
// const tagKey = 'tagKeys/281475012216835';
25+
// const tagValue = 'tagValues/281476592621530';
26+
const parent = `projects/${projectId}`;
27+
28+
// Imports the library
29+
const {SecretManagerServiceClient} = require('@google-cloud/secret-manager');
30+
31+
// Instantiates a client
32+
const client = new SecretManagerServiceClient();
33+
34+
async function createSecretWithTags() {
35+
const [secret] = await client.createSecret({
36+
parent: parent,
37+
secretId: secretId,
38+
secret: {
39+
replication: {
40+
automatic: {},
41+
},
42+
tags: {
43+
[tagKey]: tagValue,
44+
},
45+
},
46+
});
47+
48+
console.log(`Created secret ${secret.name}`);
49+
}
50+
51+
createSecretWithTags();
52+
// [END secretmanager_create_secret_with_tags]
53+
}
54+
55+
const args = process.argv.slice(2);
56+
main(...args).catch(console.error);

secret-manager/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"test": "c8 mocha -p -j 2 --recursive test/ --timeout=800000"
1515
},
1616
"dependencies": {
17-
"@google-cloud/secret-manager": "^5.6.0"
17+
"@google-cloud/secret-manager": "^6.1.0",
18+
"@google-cloud/resource-manager": "^6.2.0"
1819
},
1920
"devDependencies": {
2021
"c8": "^10.0.0",
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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, tagValue) {
18+
// [START secretmanager_bind_tags_to_regional_secret]
19+
/**
20+
* TODO(developer): Uncomment these variables before running the sample.
21+
*/
22+
// const projectId = 'my-project';
23+
// const locationId = 'my-location';
24+
// const secretId = 'my-secret';
25+
// const tagValue = 'tagValues/281476592621530';
26+
const parent = `projects/${projectId}/locations/${locationId}`;
27+
28+
// Imports the library
29+
const {SecretManagerServiceClient} = require('@google-cloud/secret-manager');
30+
const {TagBindingsClient} = require('@google-cloud/resource-manager').v3;
31+
32+
// Adding the endpoint to call the regional
33+
const options = {};
34+
const bindingOptions = {};
35+
options.apiEndpoint = `secretmanager.${locationId}.rep.googleapis.com`;
36+
bindingOptions.apiEndpoint = `${locationId}-cloudresourcemanager.googleapis.com`;
37+
38+
// Instantiates a client
39+
const client = new SecretManagerServiceClient(options);
40+
const resourcemanagerClient = new TagBindingsClient(bindingOptions);
41+
42+
async function bindTagsToRegionalSecret() {
43+
const [secret] = await client.createSecret({
44+
parent: parent,
45+
secretId: secretId,
46+
});
47+
48+
console.log(`Created secret ${secret.name}`);
49+
50+
const [operation] = await resourcemanagerClient.createTagBinding({
51+
tagBinding: {
52+
parent: `//secretmanager.googleapis.com/${secret.name}`,
53+
tagValue: tagValue,
54+
},
55+
});
56+
const [response] = await operation.promise();
57+
console.log('Created Tag Binding', response.name);
58+
}
59+
60+
bindTagsToRegionalSecret();
61+
// [END secretmanager_bind_tags_to_regional_secret]
62+
}
63+
64+
const args = process.argv.slice(2);
65+
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 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, tagKey, tagValue) {
18+
// [START secretmanager_create_regional_secret_with_tags]
19+
/**
20+
* TODO(developer): Uncomment these variables before running the sample.
21+
*/
22+
// const projectId = 'my-project';
23+
// const locationId = 'my-location';
24+
// const secretId = 'my-secret';
25+
// const tagKey = 'tagKeys/281475012216835';
26+
// const tagValue = 'tagValues/281476592621530';
27+
const parent = `projects/${projectId}/locations/${locationId}`;
28+
29+
// Imports the 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 createRegionalSecretWithTags() {
40+
const [secret] = await client.createSecret({
41+
parent: parent,
42+
secretId: secretId,
43+
secret: {
44+
tags: {
45+
[tagKey]: tagValue,
46+
},
47+
},
48+
});
49+
50+
console.log(`Created secret ${secret.name}`);
51+
}
52+
53+
createRegionalSecretWithTags();
54+
// [END secretmanager_create_regional_secret_with_tags]
55+
}
56+
57+
const args = process.argv.slice(2);
58+
main(...args).catch(console.error);

0 commit comments

Comments
 (0)