8000 feat(secretmanager): added samples for annotations (#9513) · ludoch/java-docs-samples@4697d0d · GitHub
[go: up one dir, main page]

Skip to content

Commit 4697d0d

Browse files
authored
feat(secretmanager): added samples for annotations (GoogleCloudPlatform#9513)
* feat(secretmanager): added samples for annotations * fix:resolved comments * fix: lint fix
1 parent b718949 commit 4697d0d

File tree

4 files changed

+287
-14
lines changed

4 files changed

+287
-14
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2024 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_annotations]
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 CreateSecretWithAnnotations {
27+
28+
public static void createSecretWithAnnotations() 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 annotation to be added
36+
String annotationKey = "your-annotation-key";
37+
// This is the value of the annotation to be added
38+
String annotationValue = "your-annotation-value";
39+
createSecretWithAnnotations(projectId, secretId, annotationKey, annotationValue);
40+
}
41+
42+
// Create a secret with annotations.
43+
public static Secret createSecretWithAnnotations(
44+
String projectId,
45+
String secretId,
46+
String annotationKey,
47+
String annotationValue
48+
) throws IOException {
49+
50+
// Initialize client that will be used to send requests. This client only needs to be created
51+
// once, and can be reused for multiple requests.
52+
try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
53+
54+
// Build the name.
55+
ProjectName projectName = ProjectName.of(projectId);
56+
57+
// Build the secret to create with labels.
58+
Secret secret =
59+
Secret.newBuilder()
60+
.setReplication(
61+
Replication.newBuilder()
62+
.setAutomatic(Replication.Automatic.newBuilder().build())
63+
.build())
64< A3E2 /code>+
.putAnnotations(annotationKey, annotationValue)
65+
.build();
66+
67+
// Create the secret.
68+
Secret createdSecret = client.createSecret(projectName, secretId, secret);
69+
System.out.printf("Created secret %s\n", createdSecret.getName());
70+
return createdSecret;
71+
}
72+
}
73+
}
74+
// [END secretmanager_create_secret_with_annotations]
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright 2024 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_edit_secret_annotations]
20+
import com.google.cloud.secretmanager.v1.Secret;
21+
import com.google.cloud.secretmanager.v1.SecretManagerServiceClient;
22+
import com.google.cloud.secretmanager.v1.SecretName;
23+
import com.google.protobuf.FieldMask;
24+
import com.google.protobuf.util.FieldMaskUtil;
25+
import java.io.IOException;
26+
import java.util.HashMap;
27+
import java.util.Map;
28+
29+
public class EditSecretAnnotations {
30+
31+
public static void editSecretAnnotations() throws IOException {
32+
// TODO(developer): Replace these variables before running the sample.
33+
34+
// This is the id of the GCP project
35+
String projectId = "your-project-id";
36+
// This is the id of the secret to act on
37+
String secretId = "your-secret-id";
38+
// This is the key of the annotation to be added/updated
39+
String annotationKey = "your-annotation-key";
40+
// This is the value of the annotation to be added/updated
41+
String annotationValue = "your-annotation-value";
42+
editSecretAnnotations(projectId, secretId, annotationKey, annotationValue);
43+
}
44+
45+
// Update an existing secret, by creating a new annotation or updating an existing annotation.
46+
public static Secret editSecretAnnotations(
47+
String projectId,
48+
String secretId,
49+
String annotationKey,
50+
String annotationValue
51+
) throws IOException {
52+
53+
// Initialize client that will be used to send requests. This client only needs to be created
54+
// once, and can be reused for multiple requests.
55+
try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
56+
// Build the name.
57+
SecretName secretName = SecretName.of(projectId, secretId);
58+
59+
// Get the existing secret
60+
Secret existingSecret = client.getSecret(secretName);
61+
62+
Map<String, String> existingAnnotationsMap =
63+
new HashMap<String, String>(existingSecret.getAnnotationsMap());
64+
65+
// Add a new annotation key and value.
66+
existingAnnotationsMap.put(annotationKey, annotationValue);
67+
68+
// Build the updated secret.
69+
Secret secret =
70+
Secret.newBuilder()
71+
.setName(secretName.toString())
72+
.putAllAnnotations(existingAnnotationsMap)
73+
.build();
74+
75+
// Build the field mask.
76+
FieldMask fieldMask = FieldMaskUtil.fromString("annotations");
77+
78+
// Update the secret.
79+
Secret updatedSecret = client.updateSecret(secret, fieldMask);
80+
System.out.printf("Updated secret %s\n", updatedSecret.getName());
81+
82+
return updatedSecret;
83+
}
84+
}
85+
}
86+
// [END secretmanager_edit_secret_annotations]
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2024 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_view_secret_annotations]
20+
import com.google.cloud.secretmanager.v1.Secret;
21+
import com.google.cloud.secretmanager.v1.SecretManagerServiceClient;
22+
import com.google.cloud.secretmanager.v1.SecretName;
23+
import java.io.IOException;
24+
import java.util.Map;
25+
26+
public class ViewSecretAnnotations {
27+
28+
public static void viewSecretAnnotations() 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 whose annotations to view
34+
String secretId = "your-secret-id";
35+
viewSecretAnnotations(projectId, secretId);
36+
}
37+
38+
// View the annotations of an existing secret.
39+
public static Map<String, String> viewSecretAnnotations(
40+
String projectId,
41+
String secretId
42+
) throws IOException {
43+
// Initialize client that will be used to send requests. This client only needs to be created
44+
// once, and can be reused for multiple requests.
45+
try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
46+
// Build the name.
47+
SecretName secretName = SecretName.of(projectId, secretId);
48+
49+
// Create the secret.
50+
Secret secret = client.getSecret(secretName);
51+
52+
Map<String, String> annotations = secret.getAnnotationsMap();
53+
54+
System.out.printf("Secret %s \n", secret.getName());
55+
56+
for (Map.Entry<String, String> annotation : annotations.entrySet()) {
57+
System.out.printf("Annotation key : %s, Annotation Value : %s\n",
58+
annotation.getKey(), annotation.getValue());
59+
}
60+
61+
return secret.getAnnotationsMap();
62+
}
63+
}
64+
}
65+
// [END secretmanager_view_secret_annotations]

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

Lines changed: 62 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import static com.google.common.truth.Truth.assertThat;
2020
import static org.junit.Assert.assertFalse;
21-
import static org.junit.Assert.assertNull;
2221

2322
import com.google.cloud.secretmanager.v1.AddSecretVersionRequest;
2423
import com.google.cloud.secretmanager.v1.CreateSecretRequest;
@@ -68,13 +67,18 @@ public class SnippetsIT {
6867
private static final String LABEL_VALUE = "examplelabelvalue";
6968
private static final String UPDATED_LABEL_KEY = "updatedlabelkey";
7069
private static final String UPDATED_LABEL_VALUE = "updatedlabelvalue";
70+
private static final String ANNOTATION_KEY = "exampleannotationkey";
71+
private static final String ANNOTATION_VALUE = "exampleannotationvalue";
72+
private static final String UPDATED_ANNOTATION_KEY = "updatedannotationkey";
73+
private static final String UPDATED_ANNOTATION_VALUE = "updatedannotationvalue";
7174

7275
private static Secret TEST_SECRET;
7376
private static Secret TEST_SECRET_TO_DELETE;
7477
private static Secret TEST_SECRET_TO_DELETE_WITH_ETAG;
7578
private static Secret TEST_SECRET_WITH_VERSIONS;
7679
private static SecretName TEST_SECRET_TO_CREATE_NAME;
7780
private static SecretName TEST_SECRET_WITH_LABEL_TO_CREATE_NAME;
81+
private static SecretName TEST_SECRET_WITH_ANNOTATION_TO_CREATE_NAME;
7882
private static SecretName TEST_UMMR_SECRET_TO_CREATE_NAME;
7983
private static SecretVersion TEST_SECRET_VERSION;
8084
private static SecretVersion TEST_SECRET_VERSION_TO_DESTROY;
@@ -90,13 +94,14 @@ public class SnippetsIT {
9094
public static void beforeAll() throws IOException {
9195
Assert.assertFalse("missing GOOGLE_CLOUD_PROJECT", Strings.isNullOrEmpty(PROJECT_ID));
9296

93-
TEST_SECRET = createSecret();
94-
TEST_SECRET_TO_DELETE = createSecret();
95-
TEST_SECRET_TO_DELETE_WITH_ETAG = createSecret();
96-
TEST_SECRET_WITH_VERSIONS = createSecret();
97+
TEST_SECRET = createSecret(true);
98+
TEST_SECRET_TO_DELETE = createSecret(false);
99+
TEST_SECRET_TO_DELETE_WITH_ETAG = createSecret(false);
100+
TEST_SECRET_WITH_VERSIONS = createSecret(false);
97101
TEST_SECRET_TO_CREATE_NAME = SecretName.of(PROJECT_ID, randomSecretId());
98102
TEST_UMMR_SECRET_TO_CREATE_NAME = SecretName.of(PROJECT_ID, randomSecretId());
99103
TEST_SECRET_WITH_LABEL_TO_CREATE_NAME = SecretName.of(PROJECT_ID, randomSecretId());
104+
TEST_SECRET_WITH_ANNOTATION_TO_CREATE_NAME = SecretName.of(PROJECT_ID, randomSecretId());
100105

101106
TEST_SECRET_VERSION = addSecretVersion(TEST_SECRET_WITH_VERSIONS);
102107
TEST_SECRET_VERSION_TO_DESTROY = addSecretVersion(TEST_SECRET_WITH_VERSIONS);
@@ -129,6 +134,7 @@ public static void afterAll() throws IOException {
129134
deleteSecret(TEST_SECRET.getName());
130135
deleteSecret(TEST_SECRET_TO_CREATE_NAME.toString());
131136
deleteSecret(TEST_SECRET_WITH_LABEL_TO_CREATE_NAME.toString());
137+
deleteSecret(TEST_SECRET_WITH_ANNOTATION_TO_CREATE_NAME.toString());
132138
deleteSecret(TEST_UMMR_SECRET_TO_CREATE_NAME.toString());
133139
deleteSecret(TEST_SECRET_TO_DELETE.getName());
134140
deleteSecret(TEST_SECRET_TO_DELETE_WITH_ETAG.getName());
@@ -140,28 +146,42 @@ private static String randomSecretId() {
140146
return "java-" + random.nextLong();
141147
}
142148

143-
private static Secret createSecret() throws IOException {
149+
private static Secret createSecret(boolean addAnnotation) throws IOException {
144150
ProjectName parent = ProjectName.of(PROJECT_ID);
145151

152+
Secret secret;
153+
if (addAnnotation) {
154+
secret = Secret.newBuilder()
155+
.setReplication(
156+
Replication.newBuilder()
157+
.setAutomatic(Replication.Automatic.newBuilder().build())
158+
.build())
159+
.putLabels(LABEL_KEY, LABEL_VALUE)
160+
.putAnnotations(ANNOTATION_KEY, ANNOTATION_VALUE)
161+
.build();
162+
} else {
163+
secret = Secret.newBuilder()
164+
.setReplication(
165+
Replication.newBuilder()
166+
.setAutomatic(Replication.Automatic.newBuilder().build())
167+
.build())
168+
.putLabels(LABEL_KEY, LABEL_VALUE)
169+
.build();
170+
}
171+
146172
CreateSecretRequest request =
147173
CreateSecretRequest.newBuilder()
148174
.setParent(parent.toString())
149175
.setSecretId(randomSecretId())
150-
.setSecret(
151-
Secret.newBuilder()
152-
.setReplication(
153-
Replication.newBuilder()
154-
.setAutomatic(Replication.Automatic.newBuilder().build())
155-
.build())
156-
.putLabels(LABEL_KEY, LABEL_VALUE)
157-
.build())
176+
.setSecret(secret)
158177
.build();
159178

160179
try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
161180
return client.createSecret(request);
162181
}
163182
}
164183

184+
165185
private static SecretVersion addSecretVersion(Secret secret) throws IOException {
166186
SecretName parent = SecretName.parse(secret.getName());
167187

@@ -237,6 +257,15 @@ public void testCreateSecretWithLabel() throws IOException {
237257
assertThat(secret.getLabelsMap()).containsEntry(LABEL_KEY, LABEL_VALUE);
238258
}
239259

260+
@Test
261+
public void testCreateSecretWithAnnotations() throws IOException {
262+
SecretName name = TEST_SECRET_WITH_ANNOTATION_TO_CREATE_NAME;
263+
Secret secret = CreateSecretWithAnnotations.createSecretWithAnnotations(
264+
name.getProject(), name.getSecret(), ANNOTATION_KEY, ANNOTATION_VALUE);
265+
266+
assertThat(secret.getAnnotationsMap()).containsEntry(ANNOTATION_KEY, ANNOTATION_VALUE);
267+
}
268+
240269
@Test
241270
public void testCreateSecretWithUserManagedReplication() throws IOException {
242271
SecretName name = TEST_UMMR_SECRET_TO_CREATE_NAME;
@@ -361,6 +390,15 @@ public void testViewSecretLabels() throws IOException {
361390
assertThat(labels).containsEntry(LABEL_KEY, LABEL_VALUE);
362391
}
363392

393+
@Test
394+
public void testViewSecretAnnotations() throws IOException {
395+
SecretName name = SecretName.parse(TEST_SECRET.getName());
396+
Map<String, String> annotations =
397+
ViewSecretAnnotations.viewSecretAnnotations(name.getProject(), name.getSecret());
398+
399+
assertThat(annotations).containsEntry(ANNOTATION_KEY, ANNOTATION_VALUE);
400+
}
401+
364402

365403
@Test
366404
public void testIamGrantAccess() throws IOException {
@@ -432,6 +470,16 @@ public void testCreateUpdateSecretLabel() throws IOException {
432470
UPDATED_LABEL_KEY, UPDATED_LABEL_VALUE);
433471
}
434472

473+
@Test
474+
public void testEditSecretAnnotations() throws IOException {
475+
SecretName name = SecretName.parse(TEST_SECRET.getName());
476+
Secret updatedSecret = EditSecretAnnotations.editSecretAnnotations(
477+
name.getProject(), name.getSecret(), UPDATED_ANNOTATION_KEY, UPDATED_ANNOTATION_VALUE);
478+
479+
assertThat(updatedSecret.getAnnotationsMap()).containsEntry(
480+
UPDATED_ANNOTATION_KEY, UPDATED_ANNOTATION_VALUE);
481+
}
482+
435483
@Test
436484
public void testUpdateSecretWithAlias() throws IOException {
437485
SecretName name = SecretName.parse(TEST_SECRET_WITH_VERSIONS.getName());

0 commit comments

Comments
 (0)
0