Skip to content

Commit 3d36933

Browse files
feat(secretmanager): add examples for listing, binding and removing tags from secrets
1 parent 9d6e4ee commit 3d36933

File tree

8 files changed

+472
-0
lines changed

8 files changed

+472
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2026 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package secretmanager;
18+
19+
// [START secretmanager_bind_secret_tag]
20+
import com.google.cloud.resourcemanager.v3.CreateTagBindingRequest;
21+
import com.google.cloud.resourcemanager.v3.TagBinding;
22+
import com.google.cloud.resourcemanager.v3.TagBindingsClient;
23+
import java.io.IOException;
24+
import java.util.concurrent.ExecutionException;
25+
26+
public class BindSecretTag {
27+
28+
public static void main(String[] args) throws Exception {
29+
// TODO(developer): replace these variables before running the sample.
30+
31+
// This is the id of the GCP project
32+
String projectId = "your-project-id";
33+
// This is the id of the secret to act on
34+
String secretId = "your-secret-id";
35+
// Tag value to bind, e.g. "tagValues/123"
36+
String tagValueName = "your-tag-value";
37+
38+
bindSecretTag(projectId, secretId, tagValueName);
39+
}
40+
41+
// Bind a TagValue to a Secret by creating a TagBinding.
42+
public static TagBinding bindSecretTag(String projectId, String secretId, String tagValueName)
43+
throws IOException, InterruptedException, ExecutionException {
44+
45+
String parent = String.format("//secretmanager.googleapis.com/projects/%s/secrets/%s",
46+
projectId, secretId);
47+
48+
try (TagBindingsClient tagBindingsClient = TagBindingsClient.create()) {
49+
TagBinding tagBinding = TagBinding.newBuilder()
50+
.setTagValue(tagValueName)
51+
.setParent(parent)
52+
.build();
53+
54+
CreateTagBindingRequest request = CreateTagBindingRequest.newBuilder()
55+
.setTagBinding(tagBinding)
56+
.build();
57+
58+
TagBinding created = tagBindingsClient.createTagBindingAsync(request).get();
59+
System.out.printf("Created TagBinding: %s\n", created.getName());
60+
return created;
61+
}
62+
}
63+
}
64+
// [END secretmanager_bind_secret_tag]
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2026 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package secretmanager;
18+
19+
// [START secretmanager_list_secret_tag_bindings]
20+
import com.google.cloud.resourcemanager.v3.ListTagBindingsRequest;
21+
import com.google.cloud.resourcemanager.v3.TagBinding;
22+
import com.google.cloud.resourcemanager.v3.TagBindingsClient;
23+
import java.io.IOException;
24+
25+
public class ListSecretTagBindings {
26+
27+
public static void main(String[] args) throws Exception {
28+
// TODO(developer): replace these variables before running the sample.
29+
30+
// This is the id of the GCP project
31+
String projectId = "your-project-id";
32+
// This is the id of the secret to act on
33+
String secretId = "your-secret-id";
34+
35+
listSecretTagBindings(projectId, secretId);
36+
}
37+
38+
// List tag bindings attached to the secret resource.
39+
public static void listSecretTagBindings(String projectId, String secretId)
40+
throws IOException {
41+
42+
// Resource Manager TagBindings are listed under a parent such as the project.
43+
String parent = String.format("//secretmanager.googleapis.com/projects/%s/secrets/%s",
44+
projectId, secretId);
45+
46+
try (TagBindingsClient tagBindingsClient = TagBindingsClient.create()) {
47+
ListTagBindingsRequest request =
48+
ListTagBindingsRequest.newBuilder().setParent(parent).build();
49+
50+
// Iterate over tag bindings
51+
for (TagBinding binding : tagBindingsClient.listTagBindings(request).iterateAll()) {
52+
System.out.printf("Found TagBinding with Name %s and TagValue %s\n",
53+
binding.getName(), binding.getTagValue());
54+
}
55+
}
56+
}
57+
}
58+
// [END secretmanager_list_secret_tag_bindings]
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2026 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package secretmanager;
18+
19+
// [START secretmanager_remove_tag_from_secret]
20+
import com.google.cloud.resourcemanager.v3.ListTagBindingsRequest;
21+
import com.google.cloud.resourcemanager.v3.TagBinding;
22+
import com.google.cloud.resourcemanager.v3.TagBindingsClient;
23+
import java.io.IOException;
24+
import java.util.concurrent.ExecutionException;
25+
26+
public class RemoveTagFromSecret {
27+
28+
public static void main(String[] args) throws Exception {
29+
// TODO(developer): replace these variables before running the sample.
30+
31+
// This is the id of the GCP project
32+
String projectId = "your-project-id";
33+
// This is the id of the secret to act on
34+
String secretId = "your-secret-id";
35+
// Tag value to remove, e.g. "tagValues/123"
36+
String tagValueName = "your-tag-value";
37+
38+
removeTagFromSecret(projectId, secretId, tagValueName);
39+
}
40+
41+
// Remove a TagValue from a Secret by deleting the TagBinding.
42+
public static void removeTagFromSecret(String projectId, String secretId, String tagValueName)
43+
throws IOException, InterruptedException, ExecutionException {
44+
45+
String parent = String.format("//secretmanager.googleapis.com/projects/%s/secrets/%s",
46+
projectId, secretId);
47+
48+
try (TagBindingsClient tagBindingsClient = TagBindingsClient.create()) {
49+
ListTagBindingsRequest request =
50+
ListTagBindingsRequest.newBuilder().setParent(parent).build();
51+
52+
// Iterate over tag bindings
53+
for (TagBinding binding : tagBindingsClient.listTagBindings(request).iterateAll()) {
54+
// Delete the TagBinding if it matches the specified TagValue
55+
if (binding.getTagValue().equals(tagValueName)) {
56+
tagBindingsClient.deleteTagBindingAsync(binding.getName()).get();
57+
System.out.printf("Deleted TagBinding with Name %s and TagValue %s\n",
58+
binding.getName(), binding.getTagValue());
59+
}
60+
}
61+
}
62+
}
63+
}
64+
// [END secretmanager_remove_tag_from_secret]
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2026 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package secretmanager.regionalsamples;
18+
19+
// [START secretmanager_bind_regional_secret_tag]
20+
import com.google.cloud.resourcemanager.v3.CreateTagBindingRequest;
21+
import com.google.cloud.resourcemanager.v3.TagBinding;
22+
import com.google.cloud.resourcemanager.v3.TagBindingsClient;
23+
import com.google.cloud.resourcemanager.v3.TagBindingsSettings;
24+
import java.io.IOException;
25+
import java.util.concurrent.ExecutionException;
26+
27+
public class BindRegionalSecretTag {
28+
29+
public static void main(String[] args) throws Exception {
30+
// TODO(developer): replace these variables before running the sample.
31+
32+
// This is the id of the GCP project
33+
String projectId = "your-project-id";
34+
// Location of the secret.
35+
String locationId = "your-location-id";
36+
// This is the id of the secret to act on
37+
String secretId = "your-secret-id";
38+
// Tag value to bind, e.g. "tagValues/123"
39+
String tagValueName = "your-tag-value";
40+
41+
bindRegionalSecretTag(projectId, locationId, secretId, tagValueName);
42+
}
43+
44+
// Bind a TagValue to a regional Secret by creating a TagBinding.
45+
public static TagBinding bindRegionalSecretTag(
46+
String projectId, String locationId, String secretId, String tagValueName)
47+
throws IOException, InterruptedException, ExecutionException {
48+
49+
String parent = String.format(
50+
"//secretmanager.googleapis.com/projects/%s/locations/%s/secrets/%s",
51+
projectId, locationId, secretId);
52+
53+
// Endpoint to call the regional secret manager server
54+
String apiEndpoint = String.format("%s-cloudresourcemanager.googleapis.com:443", locationId);
55+
TagBindingsSettings tagBindingsSettings =
56+
TagBindingsSettings.newBuilder().setEndpoint(apiEndpoint).build();
57+
58+
try (TagBindingsClient tagBindingsClient = TagBindingsClient.create(tagBindingsSettings)) {
59+
TagBinding tagBinding = TagBinding.newBuilder()
60+
.setTagValue(tagValueName)
61+
.setParent(parent)
62+
.build();
63+
64+
CreateTagBindingRequest request = CreateTagBindingRequest.newBuilder()
65+
.setTagBinding(tagBinding)
66+
.build();
67+
68+
TagBinding created = tagBindingsClient.createTagBindingAsync(request).get();
69+
System.out.printf("Created TagBinding: %s\n", created.getName());
70+
return created;
71+
}
72+
}
73+
}
74+
// [END secretmanager_bind_regional_secret_tag]
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2026 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package secretmanager.regionalsamples;
18+
19+
// [START secretmanager_list_regional_secret_tag_bindings]
20+
import com.google.cloud.resourcemanager.v3.ListTagBindingsRequest;
21+
import com.google.cloud.resourcemanager.v3.TagBinding;
22+
import com.google.cloud.resourcemanager.v3.TagBindingsClient;
23+
import com.google.cloud.resourcemanager.v3.TagBindingsSettings;
24+
import java.io.IOException;
25+
26+
public class ListRegionalSecretTagBindings {
27+
28+
public static void main(String[] args) throws Exception {
29+
// TODO(developer): replace these variables before running the sample.
30+
// This is the id of the GCP project
31+
String projectId = "your-project-id";
32+
// Location of the secret.
33+
String locationId = "your-location-id";
34+
// This is the id of the secret to act on
35+
String secretId = "your-secret-id";
36+
37+
listRegionalSecretTagBindings(projectId, locationId, secretId);
38+
}
39+
40+
// List tag bindings attached to the regional secret resource.
41+
public static void listRegionalSecretTagBindings(
42+
String projectId, String locationId, String secretId) throws IOException {
43+
44+
// Resource Manager TagBindings are listed under a parent such as the project.
45+
String parent = String.format(
46+
"//secretmanager.googleapis.com/projects/%s/locations/%s/secrets/%s",
47+
projectId, locationId, secretId);
48+
49+
// Endpoint to call the regional secret manager server
50+
String apiEndpoint = String.format("%s-cloudresourcemanager.googleapis.com:443", locationId);
51+
TagBindingsSettings tagBindingsSettings =
52+
TagBindingsSettings.newBuilder().setEndpoint(apiEndpoint).build();
53+
54+
try (TagBindingsClient tagBindingsClient = TagBindingsClient.create(tagBindingsSettings)) {
55+
ListTagBindingsRequest request = ListTagBindingsRequest.newBuilder()
56+
.setParent(parent).build();
57+
58+
// Iterate over tag bindings
59+
for (TagBinding binding : tagBindingsClient.listTagBindings(request).iterateAll()) {
60+
System.out.printf("Found TagBinding with Name %s and TagValue %s\n",
61+
binding.getName(), binding.getTagValue());
62+
}
63+
}
64+
}
65+
}
66+
// [END secretmanager_list_regional_secret_tag_bindings]

0 commit comments

Comments
 (0)