Skip to content

Commit db1741b

Browse files
committed
feat(secretmanager): added samples for tags
1 parent ae650ee commit db1741b

File tree

4 files changed

+221
-4
lines changed

4 files changed

+221
-4
lines changed

secretmanager/pom.xml

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,22 @@
5050
</dependencies>
5151
</dependencyManagement>
5252

53-
<dependencies>
53+
<dependencies>
5454
<dependency>
5555
<groupId>com.google.cloud</groupId>
5656
<artifactId>google-cloud-secretmanager</artifactId>
57-
</dependency>
58-
59-
<dependency>
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>
67+
</dependency>
68+
<dependency>
6069
<groupId>com.google.protobuf</groupId>
6170
<artifactId>protobuf-java-util</artifactId>
6271
</dependency>
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 labels.
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 %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 labels.
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 %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: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@
1919
import static com.google.common.truth.Truth.assertThat;
2020
import static org.junit.Assert.assertFalse;
2121

22+
import com.google.cloud.resourcemanager.v3.CreateTagKeyMetadata;
23+
import com.google.cloud.resourcemanager.v3.CreateTagKeyRequest;
24+
import com.google.cloud.resourcemanager.v3.CreateTagValueMetadata;
25+
import com.google.cloud.resourcemanager.v3.CreateTagValueRequest;
26+
import com.google.api.gax.longrunning.OperationFuture;
27+
import com.google.cloud.resourcemanager.v3.TagKey;
28+
import com.google.cloud.resourcemanager.v3.TagKeysClient;
29+
import com.google.cloud.resourcemanager.v3.TagValue;
30+
import com.google.cloud.resourcemanager.v3.TagValuesClient;
2231
import com.google.cloud.secretmanager.v1.AddSecretVersionRequest;
2332
import com.google.cloud.secretmanager.v1.CreateSecretRequest;
2433
import com.google.cloud.secretmanager.v1.DeleteSecretRequest;
@@ -33,6 +42,7 @@
3342
import com.google.cloud.secretmanager.v1.SecretVersionName;
3443
import com.google.common.base.Strings;
3544
import com.google.protobuf.ByteString;
45+
import java.lang.Exception;
3646
import java.io.ByteArrayOutputStream;
3747
import java.io.IOException;
3848
import java.io.PrintStream;
@@ -78,6 +88,7 @@ public class SnippetsIT {
7888
private static Secret TEST_SECRET_WITH_VERSIONS;
7989
private static SecretName TEST_SECRET_TO_CREATE_NAME;
8090
private static SecretName TEST_SECRET_WITH_LABEL_TO_CREATE_NAME;
91+
private static SecretName TEST_SECRET_WITH_TAGS_TO_CREATE_NAME;
8192
private static SecretName TEST_SECRET_WITH_ANNOTATION_TO_CREATE_NAME;
8293
private static SecretName TEST_UMMR_SECRET_TO_CREATE_NAME;
8394
private static SecretVersion TEST_SECRET_VERSION;
@@ -88,6 +99,9 @@ public class SnippetsIT {
8899
private static SecretVersion TEST_SECRET_VERSION_TO_ENABLE;
89100
private static SecretVersion TEST_SECRET_VERSION_TO_ENABLE_WITH_ETAG;
90101

102+
private static TagKey TAG_KEY;
103+
private static TagValue TAG_VALUE;
104+
91105
private ByteArrayOutputStream stdOut;
92106

93107
@BeforeClass
@@ -100,6 +114,7 @@ public static void beforeAll() throws IOException {
100114
TEST_SECRET_WITH_VERSIONS = createSecret(false);
101115
TEST_SECRET_TO_CREATE_NAME = SecretName.of(PROJECT_ID, randomSecretId());
102116
TEST_UMMR_SECRET_TO_CREATE_NAME = SecretName.of(PROJECT_ID, randomSecretId());
117+
TEST_SECRET_WITH_TAGS_TO_CREATE_NAME = SecretName.of(PROJECT_ID, randomSecretId());
103118
TEST_SECRET_WITH_LABEL_TO_CREATE_NAME = SecretName.of(PROJECT_ID, randomSecretId());
104119
TEST_SECRET_WITH_ANNOTATION_TO_CREATE_NAME = SecretName.of(PROJECT_ID, randomSecretId());
105120

@@ -113,6 +128,7 @@ public static void beforeAll() throws IOException {
113128
disableSecretVersion(TEST_SECRET_VERSION_TO_ENABLE);
114129
TEST_SECRET_VERSION_TO_ENABLE_WITH_ETAG = disableSecretVersion(
115130
TEST_SECRET_VERSION_TO_ENABLE_WITH_ETAG);
131+
createTags();
116132
}
117133

118134
@Before
@@ -133,19 +149,50 @@ public static void afterAll() throws IOException {
133149

134150
deleteSecret(TEST_SECRET.getName());
135151
deleteSecret(TEST_SECRET_TO_CREATE_NAME.toString());
152+
deleteSecret(TEST_SECRET_WITH_TAGS_TO_CREATE_NAME.toString());
136153
deleteSecret(TEST_SECRET_WITH_LABEL_TO_CREATE_NAME.toString());
137154
deleteSecret(TEST_SECRET_WITH_ANNOTATION_TO_CREATE_NAME.toString());
138155
deleteSecret(TEST_UMMR_SECRET_TO_CREATE_NAME.toString());
139156
deleteSecret(TEST_SECRET_TO_DELETE.getName());
140157
deleteSecret(TEST_SECRET_TO_DELETE_WITH_ETAG.getName());
141158
deleteSecret(TEST_SECRET_WITH_VERSIONS.getName());
159+
deleteTags();
142160
}
143161

144162
private static String randomSecretId() {
145163
Random random = new Random();
146164
return "java-" + random.nextLong();
147165
}
148166

167+
private static void createTags() throws IOException{
168+
try (TagKeysClient tagKeysClient = TagKeysClient.create()) {
169+
CreateTagKeyRequest request =
170+
CreateTagKeyRequest.newBuilder()
171+
.setTagKey(TagKey.newBuilder().build())
172+
.build();
173+
OperationFuture<TagKey, CreateTagKeyMetadata> future =
174+
tagKeysClient.createTagKeyOperationCallable().futureCall(request);
175+
TagKey response = future.get();
176+
TAG_KEY = response;
177+
}catch(Exception e){
178+
}
179+
try (TagValuesClient tagValuesClient = TagValuesClient.create()) {
180+
CreateTagValueRequest request =
181+
CreateTagValueRequest.newBuilder()
182+
.setTagValue(TagValue.newBuilder().setParent(TAG_KEY.getName()).build())
183+
.build();
184+
OperationFuture<TagValue, CreateTagValueMetadata> future =
185+
tagValuesClient.createTagValueOperationCallable().futureCall(request);
186+
TagValue response = future.get();
187+
TAG_VALUE = response;
188+
}catch(Exception e){
189+
}
190+
191+
}
192+
193+
private static void deleteTags() throws IOException{
194+
}
195+
149196
private static Secret createSecret(boolean addAnnotation) throws IOException {
150197
ProjectName parent = ProjectName.of(PROJECT_ID);
151198

@@ -257,6 +304,19 @@ public void testCreateSecretWithLabel() throws IOException {
257304
assertThat(secret.getLabelsMap()).containsEntry(LABEL_KEY, LABEL_VALUE);
258305
}
259306

307+
@Test
308+
public void testCreateSecretWithTag() throws IOException {
309+
SecretName name = TEST_SECRET_WITH_TAGS_TO_CREATE_NAME;
310+
Secret secret = CreateSecretWithTags.createSecretWithTags(
311+
name.getProject(),
312+
name.getSecret(),
313+
TAG_KEY.getName(),
314+
TAG_VALUE.getName()
315+
);
316+
317+
assertThat(secret.getTagsMap()).containsEntry(TAG_KEY, TAG_VALUE);
318+
}
319+
260320
@Test
261321
public void testCreateSecretWithAnnotations() throws IOException {
262322
SecretName name = TEST_SECRET_WITH_ANNOTATION_TO_CREATE_NAME;

0 commit comments

Comments
 (0)