Skip to content

Commit 4afb60e

Browse files
committed
feat(secretmanager): Adding tags samples
1 parent 455d71e commit 4afb60e

9 files changed

+636
-0
lines changed

secret-manager/detachTagBinding.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright 2026 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, tagValue) {
18+
// [START secretmanager_detach_tag_binding]
19+
/**
20+
* TODO(developer): Uncomment these variables before running the sample.
21+
*/
22+
// const name = 'projects/my-project/secrets/my-secret';
23+
// const tagValue = 'tagValues/123456789012';
24+
25+
// Import the Resource Manager and Secret Manager libraries
26+
const {TagBindingsClient} = require('@google-cloud/resource-manager').v3;
27+
28+
// Create the Resource Manager client
29+
const rmClient = new TagBindingsClient();
30+
31+
// Build the resource name of the parent secret
32+
const parent = `//secretmanager.googleapis.com/${name}`;
33+
34+
async function detachTag() {
35+
// Find the binding name for the given tag value
36+
let bindingName = null;
37+
const iterable = rmClient.listTagBindingsAsync(
38+
{
39+
parent: parent,
40+
pageSize: 50,
41+
},
42+
{autoPaginate: false}
43+
);
44+
45+
for await (const binding of iterable) {
46+
if (binding.tagValue === tagValue) {
47+
bindingName = binding.name;
48+
break;
49+
}
50+
}
51+
52+
if (bindingName === null) {
53+
console.log(`Tag binding for value ${tagValue} not found on ${name}.`);
54+
return;
55+
}
56+
57+
// Delete the tag binding
58+
const [operation] = await rmClient.deleteTagBinding({
59+
name: bindingName,
60+
});
61+
62+
// Wait for the operation to complete
63+
await operation.promise();
64+
console.log(`Detached tag value ${tagValue} from ${name}`);
65+
}
66+
67+
detachTag();
68+
// [END secretmanager_detach_tag_binding]
69+
}
70+
71+
const args = process.argv.slice(2);
72+
main(...args).catch(console.error);
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright 2026 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 = 'projects/my-project/secrets/my-secret') {
18+
// [START secretmanager_list_secret_versions_with_filter]
19+
/**
20+
* TODO(developer): Uncomment these variables before running the sample.
21+
*/
22+
// const parent = 'projects/my-project/secrets/my-secret';
23+
const filterStr = 'state=DISABLED';
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 listSecretVersionsWithFilter() {
32+
const [versions] = await client.listSecretVersions({
33+
parent: parent,
34+
filter: filterStr,
35+
});
36+
37+
versions.forEach(version => {
38+
console.log(`Found version: ${version.name}`);
39+
});
40+
}
41+
42+
listSecretVersionsWithFilter();
43+
// [END secretmanager_list_secret_versions_with_filter]
44+
}
45+
46+
const args = process.argv.slice(2);
47+
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 2026 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) {
18+
// [START secretmanager_list_secrets_with_filter]
19+
/**
20+
* TODO(developer): Uncomment these variables before running the sample.
21+
*/
22+
// const projectId = 'my-project';
23+
const filterStr = 'labels.secretmanager=rocks';
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+
// Build the resource name of the parent project
32+
const parent = `projects/${projectId}`;
33+
34+
// List all secrets
35+
async function listSecretsWithFilter() {
36+
const [secrets] = await client.listSecrets({
37+
parent: parent,
38+
filter: filterStr,
39+
});
40+
41+
// Print each secret
42+
for (const secret of secrets) {
43+
console.log(`Found secret: ${secret.name}`);
44+
}
45+
}
46+
47+
listSecretsWithFilter();
48+
// [END secretmanager_list_secrets_with_filter]
49+
}
50+
51+
const args = process.argv.slice(2);
52+
main(...args).catch(console.error);

secret-manager/listTagBindings.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright 2026 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 = 'projects/my-project/secrets/my-secret') {
18+
// [START secretmanager_list_tag_bindings]
19+
/**
20+
* TODO(developer): Uncomment these variables before running the sample.
21+
*/
22+
// const name = 'projects/my-project/secrets/my-secret';
23+
24+
// Import the Resource Manager and Secret Manager libraries
25+
const {TagBindingsClient} = require('@google-cloud/resource-manager').v3;
26+
27+
// Create the Resource Manager client
28+
const client = new TagBindingsClient();
29+
30+
// Build the resource name of the parent secret
31+
const parent = `//secretmanager.googleapis.com/${name}`;
32+
33+
async function listTagBindings() {
34+
// List all tag bindings
35+
let foundBindings = false;
36+
37+
// Use paginate to handle any pagination in the response
38+
const iterable = client.listTagBindingsAsync(
39+
{
40+
parent: parent,
41+
pageSize: 10,
42+
},
43+
{autoPaginate: false}
44+
);
45+
46+
console.log(`Tag bindings for ${name}:`);
47+
48+
for await (const binding of iterable) {
49+
console.log(`- Tag Value: ${binding.tagValue}`);
50+
foundBindings = true;
51+
}
52+
53+
if (!foundBindings) {
54+
console.log(`No tag bindings found for ${name}.`);
55+
}
56+
}
57+
58+
listTagBindings();
59+
// [END secretmanager_list_tag_bindings]
60+
}
61+
62+
const args = process.argv.slice(2);
63+
main(...args).catch(console.error);
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Copyright 2026 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_detach_regional_tag]
19+
/**
20+
* TODO(developer): Uncomment these variables before running the sample.
21+
*/
22+
// const projectId = 'my-project';
23+
// const locationId = 'us-central1';
24+
// const secretId = 'my-secret';
25+
// const tagValue = 'tagValues/123456789012';
26+
27+
// Import the Resource Manager library
28+
const {TagBindingsClient} = require('@google-cloud/resource-manager').v3;
29+
30+
// Set up the endpoint for the regional resource manager
31+
const rmEndpoint = `${locationId}-cloudresourcemanager.googleapis.com`;
32+
33+
// Create the Tag Bindings client with the regional endpoint
34+
const tagBindingsClient = new TagBindingsClient({
35+
apiEndpoint: rmEndpoint,
36+
});
37+
38+
// Build the resource name for the regional secret
39+
const secretName = `projects/${projectId}/locations/${locationId}/secrets/${secretId}`;
40+
41+
// Format the parent resource for the tag bindings request
42+
const parent = `//secretmanager.googleapis.com/${secretName}`;
43+
44+
async function detachRegionalTag() {
45+
// Find the binding with the specified tag value
46+
let bindingName = null;
47+
const iterable = tagBindingsClient.listTagBindingsAsync(
48+
{
49+
parent: parent,
50+
pageSize: 50,
51+
},
52+
{autoPaginate: false}
53+
);
54+
55+
for await (const binding of iterable) {
56+
if (binding.tagValue === tagValue) {
57+
bindingName = binding.name;
58+
break;
59+
}
60+
}
61+
62+
if (bindingName === null) {
63+
console.log(
64+
`Tag binding for value ${tagValue} not found on ${secretName}.`
65+
);
66+
return;
67+
}
68+
69+
// Delete the tag binding
70+
const [operation] = await tagBindingsClient.deleteTagBinding({
71+
name: bindingName,
72+
});
73+
74+
// Wait for the operation to complete
75+
await operation.promise();
76+
77+
console.log(`Detached tag value ${tagValue} from ${secretName}`);
78+
}
79+
80+
return detachRegionalTag();
81+
// [END secretmanager_detach_regional_tag]
82+
}
83+
84+
const args = process.argv.slice(2);
85+
main(...args).catch(console.error);
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2026 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_list_regional_secret_versions_with_filter]
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 filterStr = 'state=DISABLED';
26+
27+
const parent = `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 listRegionalSecretVersionsWithFilter() {
40+
const [versions] = await client.listSecretVersions({
41+
parent: parent,
42+
filter: filterStr,
43+
});
44+
45+
versions.forEach(version => {
46+
console.log(`Found version: ${version.name}`);
47+
});
48+
}
49+
50+
listRegionalSecretVersionsWithFilter();
51+
// [END secretmanager_list_regional_secret_versions_with_filter]
52+
}
53+
54+
const args = process.argv.slice(2);
55+
main(...args).catch(console.error);

0 commit comments

Comments
 (0)