Skip to content

Commit 5d5f26f

Browse files
Sm tags (#10119)
* feat(secretmanager): added samples for tags * Testing latest SM version * Testing latest SM version * Testing latest SM version * feat(secretmanager):fixed tests * feat(secretmanager):fixed indentation * fixed tests * fixed tests * fixed tests * fixed tests * Update secretmanager/src/main/java/secretmanager/CreateSecretWithTags.java Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * Update secretmanager/src/main/java/secretmanager/regionalsamples/CreateRegionalSecretWithTags.java Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * fixed pom * fix indentation --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent ae650ee commit 5d5f26f

File tree

5 files changed

+356
-7
lines changed

5 files changed

+356
-7
lines changed

secretmanager/pom.xml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
<groupId>com.google.cloud</groupId>
4646
<scope>import</scope>
4747
<type>pom</type>
48-
<version>26.42.0</version>
48+
<version>26.62.0</version>
4949
</dependency>
5050
</dependencies>
5151
</dependencyManagement>
@@ -54,8 +54,17 @@
5454
<dependency>
5555
<groupId>com.google.cloud</groupId>
5656
<artifactId>google-cloud-secretmanager</artifactId>
57+
<version>2.66.0</version>
58+
</dependency>
59+
<dependency>
60+
<groupId>com.google.api.grpc</groupId>
61+
<artifactId>proto-google-cloud-secretmanager-v1</artifactId>
62+
<version>2.66.0</version>
63+
</dependency>
64+
<dependency>
65+
<groupId>com.google.cloud</groupId>
66+
<artifactId>google-cloud-resourcemanager</artifactId>
5767
</dependency>
58-
5968
<dependency>
6069
<groupId>com.google.protobuf</groupId>
6170
<artifactId>protobuf-java-util</artifactId>
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2025 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_create_secret_with_tags]
20+
import com.google.cloud.secretmanager.v1.ProjectName;
21+
import com.google.cloud.secretmanager.v1.Replication;
22+
import com.google.cloud.secretmanager.v1.Secret;
23+
import com.google.cloud.secretmanager.v1.SecretManagerServiceClient;
24+
import java.io.IOException;
25+
26+
public class CreateSecretWithTags {
27+
28+
public static void createSecretWithTags() throws IOException {
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+
// This is the key of the tag to be added
36+
String tagKey = "your-tag-key";
37+
// This is the value of the tag to be added
38+
String tagValue = "your-tag-value";
39+
createSecretWithTags(projectId, secretId, tagKey, tagValue);
40+
}
41+
42+
// Create a secret with tags.
43+
public static Secret createSecretWithTags(
44+
String projectId, String secretId, String tagKey, String tagValue) throws IOException {
45+
// Initialize client that will be used to send requests. This client only needs to be created
46+
// once, and can be reused for multiple requests.
47+
try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
48+
49+
// Build the name.
50+
ProjectName projectName = ProjectName.of(projectId);
51+
52+
// Build the secret to create with tags.
53+
Secret secret =
54+
Secret.newBuilder()
55+
.setReplication(
56+
Replication.newBuilder()
57+
.setAutomatic(Replication.Automatic.newBuilder().build())
58+
.build())
59+
.putTags(tagKey, tagValue)
60+
.build();
61+
62+
// Create the secret.
63+
Secret createdSecret = client.createSecret(projectName, secretId, secret);
64+
System.out.printf("Created secret with Tags %s\n", createdSecret.getName());
65+
return createdSecret;
66+
}
67+
}
68+
}
69+
// [END secretmanager_create_secret_with_tags]
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2025 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_create_regional_secret_with_tags]
20+
import com.google.cloud.secretmanager.v1.LocationName;
21+
import com.google.cloud.secretmanager.v1.Secret;
22+
import com.google.cloud.secretmanager.v1.SecretManagerServiceClient;
23+
import com.google.cloud.secretmanager.v1.SecretManagerServiceSettings;
24+
import java.io.IOException;
25+
26+
public class CreateRegionalSecretWithTags {
27+
28+
public static void createRegionalSecretWithTags() throws IOException {
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+
// Location of the secret.
34+
String locationId = "your-location-id";
35+
// This is the id of the secret to act on
36+
String secretId = "your-secret-id";
37+
// This is the key of the tag to be added
38+
String tagKey = "your-tag-key";
39+
// This is the value of the tag to be added
40+
String tagValue = "your-tag-value";
41+
createRegionalSecretWithTags(projectId, locationId, secretId, tagKey, tagValue);
42+
}
43+
44+
// Create a secret with tags.
45+
public static Secret createRegionalSecretWithTags(
46+
String projectId,
47+
String locationId,
48+
String secretId,
49+
String tagKey,
50+
String tagValue)
51+
throws IOException {
52+
53+
// Endpoint to call the regional secret manager sever
54+
String apiEndpoint = String.format("secretmanager.%s.rep.googleapis.com:443", locationId);
55+
SecretManagerServiceSettings secretManagerServiceSettings =
56+
SecretManagerServiceSettings.newBuilder().setEndpoint(apiEndpoint).build();
57+
58+
// Initialize client that will be used to send requests. This client only needs to be created
59+
// once, and can be reused for multiple requests.
60+
try (SecretManagerServiceClient client =
61+
SecretManagerServiceClient.create(secretManagerServiceSettings)) {
62+
63+
// Build the parent name from the project.
64+
LocationName location = LocationName.of(projectId, locationId);
65+
66+
// Build the secret to create with tags.
67+
Secret secret =
68+
Secret.newBuilder()
69+
.putTags(tagKey, tagValue)
70+
.build();
71+
72+
// Create the secret.
73+
Secret createdSecret = client.createSecret(location.toString(), secretId, secret);
74+
System.out.printf("Created secret with Tags%s\n", createdSecret.getName());
75+
return createdSecret;
76+
}
77+
}
78+
}
79+
// [END secretmanager_create_regional_secret_with_tags]

secretmanager/src/test/java/secretmanager/SnippetsIT.java

Lines changed: 97 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@
1919
import static com.google.common.truth.Truth.assertThat;
2020
import static org.junit.Assert.assertFalse;
2121

22+
import com.google.api.gax.longrunning.OperationFuture;
23+
import com.google.cloud.resourcemanager.v3.CreateTagKeyMetadata;
24+
import com.google.cloud.resourcemanager.v3.CreateTagKeyRequest;
25+
import com.google.cloud.resourcemanager.v3.CreateTagValueMetadata;
26+
import com.google.cloud.resourcemanager.v3.CreateTagValueRequest;
27+
import com.google.cloud.resourcemanager.v3.DeleteTagKeyMetadata;
28+
import com.google.cloud.resourcemanager.v3.DeleteTagKeyRequest;
29+
import com.google.cloud.resourcemanager.v3.DeleteTagValueMetadata;
30+
import com.google.cloud.resourcemanager.v3.DeleteTagValueRequest;
31+
import com.google.cloud.resourcemanager.v3.TagKey;
32+
import com.google.cloud.resourcemanager.v3.TagKeysClient;
33+
import com.google.cloud.resourcemanager.v3.TagValue;
34+
import com.google.cloud.resourcemanager.v3.TagValuesClient;
2235
import com.google.cloud.secretmanager.v1.AddSecretVersionRequest;
2336
import com.google.cloud.secretmanager.v1.CreateSecretRequest;
2437
import com.google.cloud.secretmanager.v1.DeleteSecretRequest;
@@ -36,6 +49,7 @@
3649
import java.io.ByteArrayOutputStream;
3750
import java.io.IOException;
3851
import java.io.PrintStream;
52+
import java.lang.Exception;
3953
import java.nio.charset.StandardCharsets;
4054
import java.util.Arrays;
4155
import java.util.Base64;
@@ -78,6 +92,7 @@ public class SnippetsIT {
7892
private static Secret TEST_SECRET_WITH_VERSIONS;
7993
private static SecretName TEST_SECRET_TO_CREATE_NAME;
8094
private static SecretName TEST_SECRET_WITH_LABEL_TO_CREATE_NAME;
95+
private static SecretName TEST_SECRET_WITH_TAGS_TO_CREATE_NAME;
8196
private static SecretName TEST_SECRET_WITH_ANNOTATION_TO_CREATE_NAME;
8297
private static SecretName TEST_UMMR_SECRET_TO_CREATE_NAME;
8398
private static SecretVersion TEST_SECRET_VERSION;
@@ -88,10 +103,13 @@ public class SnippetsIT {
88103
private static SecretVersion TEST_SECRET_VERSION_TO_ENABLE;
89104
private static SecretVersion TEST_SECRET_VERSION_TO_ENABLE_WITH_ETAG;
90105

106+
private static TagKey TAG_KEY;
107+
private static TagValue TAG_VALUE;
108+
91109
private ByteArrayOutputStream stdOut;
92110

93111
@BeforeClass
94-
public static void beforeAll() throws IOException {
112+
public static void beforeAll() throws Exception {
95113
Assert.assertFalse("missing GOOGLE_CLOUD_PROJECT", Strings.isNullOrEmpty(PROJECT_ID));
96114

97115
TEST_SECRET = createSecret(true);
@@ -100,6 +118,7 @@ public static void beforeAll() throws IOException {
100118
TEST_SECRET_WITH_VERSIONS = createSecret(false);
101119
TEST_SECRET_TO_CREATE_NAME = SecretName.of(PROJECT_ID, randomSecretId());
102120
TEST_UMMR_SECRET_TO_CREATE_NAME = SecretName.of(PROJECT_ID, randomSecretId());
121+
TEST_SECRET_WITH_TAGS_TO_CREATE_NAME = SecretName.of(PROJECT_ID, randomSecretId());
103122
TEST_SECRET_WITH_LABEL_TO_CREATE_NAME = SecretName.of(PROJECT_ID, randomSecretId());
104123
TEST_SECRET_WITH_ANNOTATION_TO_CREATE_NAME = SecretName.of(PROJECT_ID, randomSecretId());
105124

@@ -113,6 +132,7 @@ public static void beforeAll() throws IOException {
113132
disableSecretVersion(TEST_SECRET_VERSION_TO_ENABLE);
114133
TEST_SECRET_VERSION_TO_ENABLE_WITH_ETAG = disableSecretVersion(
115134
TEST_SECRET_VERSION_TO_ENABLE_WITH_ETAG);
135+
createTags();
116136
}
117137

118138
@Before
@@ -128,24 +148,86 @@ public void afterEach() {
128148
}
129149

130150
@AfterClass
131-
public static void afterAll() throws IOException {
151+
public static void afterAll() throws Exception {
132152
Assert.assertFalse("missing GOOGLE_CLOUD_PROJECT", Strings.isNullOrEmpty(PROJECT_ID));
133153

134154
deleteSecret(TEST_SECRET.getName());
135155
deleteSecret(TEST_SECRET_TO_CREATE_NAME.toString());
156+
deleteSecret(TEST_SECRET_WITH_TAGS_TO_CREATE_NAME.toString());
136157
deleteSecret(TEST_SECRET_WITH_LABEL_TO_CREATE_NAME.toString());
137158
deleteSecret(TEST_SECRET_WITH_ANNOTATION_TO_CREATE_NAME.toString());
138159
deleteSecret(TEST_UMMR_SECRET_TO_CREATE_NAME.toString());
139160
deleteSecret(TEST_SECRET_TO_DELETE.getName());
140161
deleteSecret(TEST_SECRET_TO_DELETE_WITH_ETAG.getName());
141162
deleteSecret(TEST_SECRET_WITH_VERSIONS.getName());
163+
deleteTags();
142164
}
143165

144166
private static String randomSecretId() {
145167
Random random = new Random();
146168
return "java-" + random.nextLong();
147169
}
148170

171+
private static void createTags() throws Exception {
172+
try (TagKeysClient tagKeysClient = TagKeysClient.create()) {
173+
Random random = new Random();
174+
ProjectName parent = ProjectName.of(PROJECT_ID);
175+
CreateTagKeyRequest request =
176+
CreateTagKeyRequest.newBuilder()
177+
.setTagKey(
178+
TagKey
179+
.newBuilder()
180+
.setParent(parent.toString())
181+
.setShortName("java-" + random.nextLong())
182+
.build())
183+
.build();
184+
OperationFuture<TagKey, CreateTagKeyMetadata> future =
185+
tagKeysClient.createTagKeyOperationCallable().futureCall(request);
186+
TagKey response = future.get();
187+
TAG_KEY = response;
188+
}
189+
190+
try (TagValuesClient tagValuesClient = TagValuesClient.create()) {
191+
Random random = new Random();
192+
CreateTagValueRequest request =
193+
CreateTagValueRequest.newBuilder()
194+
.setTagValue(
195+
TagValue
196+
.newBuilder()
197+
.setParent(TAG_KEY.getName())
198+
.setShortName("java-" + random.nextLong())
199+
.build())
200+
.build();
201+
OperationFuture<TagValue, CreateTagValueMetadata> future =
202+
tagValuesClient.createTagValueOperationCallable().futureCall(request);
203+
TagValue response = future.get();
204+
TAG_VALUE = response;
205+
}
206+
}
207+
208+
private static void deleteTags() throws Exception {
209+
Thread.sleep(60000);
210+
try (TagValuesClient tagValuesClient = TagValuesClient.create()) {
211+
DeleteTagValueRequest request =
212+
DeleteTagValueRequest.newBuilder()
213+
.setName(TAG_VALUE.getName())
214+
.build();
215+
OperationFuture<TagValue, DeleteTagValueMetadata> future =
216+
tagValuesClient.deleteTagValueOperationCallable().futureCall(request);
217+
TagValue response = future.get();
218+
}
219+
220+
try (TagKeysClient tagKeysClient = TagKeysClient.create()) {
221+
DeleteTagKeyRequest request =
222+
DeleteTagKeyRequest.newBuilder()
223+
.setName(TAG_KEY.getName())
224+
.build();
225+
OperationFuture<TagKey, DeleteTagKeyMetadata> future =
226+
tagKeysClient.deleteTagKeyOperationCallable().futureCall(request);
227+
TagKey response = future.get();
228+
}
229+
}
230+
149231
private static Secret createSecret(boolean addAnnotation) throws IOException {
150232
ProjectName parent = ProjectName.of(PROJECT_ID);
151233

@@ -257,6 +339,19 @@ public void testCreateSecretWithLabel() throws IOException {
257339
assertThat(secret.getLabelsMap()).containsEntry(LABEL_KEY, LABEL_VALUE);
258340
}
259341

342+
@Test
343+
public void testCreateSecretWithTag() throws IOException {
344+
SecretName name = TEST_SECRET_WITH_TAGS_TO_CREATE_NAME;
345+
Secret secret = CreateSecretWithTags.createSecretWithTags(
346+
name.getProject(),
347+
name.getSecret(),
348+
TAG_KEY.getName(),
349+
TAG_VALUE.getName()
350+
);
351+
352+
assertThat(stdOut.toString()).contains("Created secret with Tags");
353+
}
354+
260355
@Test
261356
public void testCreateSecretWithAnnotations() throws IOException {
262357
SecretName name = TEST_SECRET_WITH_ANNOTATION_TO_CREATE_NAME;

0 commit comments

Comments
 (0)