Skip to content

Commit 56de631

Browse files
authored
feat: add samples for annotations, regional SM (#9566)
* feat: add samples for annotations, regional SM * fix: lint issues
1 parent 81c4eae commit 56de631

File tree

4 files changed

+309
-0
lines changed

4 files changed

+309
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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.regionalsamples;
18+
19+
20+
// [START secretmanager_create_regional_secret_with_annotations]
21+
import com.google.cloud.secretmanager.v1.LocationName;
22+
import com.google.cloud.secretmanager.v1.Secret;
23+
import com.google.cloud.secretmanager.v1.SecretManagerServiceClient;
24+
import com.google.cloud.secretmanager.v1.SecretManagerServiceSettings;
25+
import java.io.IOException;
26+
27+
public class CreateRegionalSecretWithAnnotations {
28+
29+
public static void createRegionalSecretWithAnnotations() throws IOException {
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+
// This is the key of the annotation to be added
39+
String annotationKey = "your-annotation-key";
40+
// This is the value of the annotation to be added
41+
String annotationValue = "your-annotation-value";
42+
createRegionalSecretWithAnnotations(
43+
projectId, locationId, secretId, annotationKey, annotationValue
44+
);
45+
}
46+
47+
// Create a secret with annotations.
48+
public static Secret createRegionalSecretWithAnnotations(
49+
String projectId,
50+
String locationId,
51+
String secretId,
52+
String annotationKey,
53+
String annotationValue
54+
) throws IOException {
55+
56+
// Endpoint to call the regional secret manager sever
57+
String apiEndpoint = String.format("secretmanager.%s.rep.googleapis.com:443", locationId);
58+
SecretManagerServiceSettings secretManagerServiceSettings =
59+
SecretManagerServiceSettings.newBuilder().setEndpoint(apiEndpoint).build();
60+
61+
// Initialize the client that will be used to send requests. This client only needs to be
62+
// created once, and can be reused for multiple requests.
63+
try (SecretManagerServiceClient client =
64+
SecretManagerServiceClient.create(secretManagerServiceSettings)) {
65+
66+
// Build the parent name from the project.
67+
LocationName location = LocationName.of(projectId, locationId);
68+
69+
// Build the secret to create with labels.
70+
Secret secret =
71+
Secret.newBuilder()
72+
.putAnnotations(annotationKey, annotationValue)
73+
.build();
74+
75+
// Create the secret.
76+
Secret createdSecret = client.createSecret(location.toString(), secretId, secret);
77+
System.out.printf("Created secret %s\n", createdSecret.getName());
78+
return createdSecret;
79+
}
80+
}
81+
}
82+
// [END secretmanager_create_regional_secret_with_annotations]
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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.regionalsamples;
18+
19+
// [START secretmanager_edit_regional_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.SecretManagerServiceSettings;
23+
import com.google.cloud.secretmanager.v1.SecretName;
24+
import com.google.protobuf.FieldMask;
25+
import com.google.protobuf.util.FieldMaskUtil;
26+
import java.io.IOException;
27+
import java.util.HashMap;
28+
import java.util.Map;
29+
30+
public class EditRegionalSecretAnnotations {
31+
32+
public static void editRegionalSecretAnnotations() throws IOException {
33+
// TODO(developer): Replace these variables before running the sample.
34+
35+
// This is the id of the GCP project
36+
String projectId = "your-project-id";
37+
// Location of the secret.
38+
String locationId = "your-location-id";
39+
// This is the id of the secret to act on
40+
String secretId = "your-secret-id";
41+
// This is the key of the annotation to be added/updated
42+
String annotationKey = "your-annotation-key";
43+
// This is the value of the annotation to be added/updated
44+
String annotationValue = "your-annotation-value";
45+
editRegionalSecretAnnotations(
46+
projectId, locationId, secretId, annotationKey, annotationValue
47+
);
48+
}
49+
50+
// Update an existing secret, by creating a new annotation or updating an existing annotation.
51+
public static Secret editRegionalSecretAnnotations(
52+
String projectId,
53+
String locationId,
54+
String secretId,
55+
String annotationKey,
56+
String annotationValue
57+
) throws IOException {
58+
59+
// Endpoint to call the regional secret manager sever
60+
String apiEndpoint = String.format("secretmanager.%s.rep.googleapis.com:443", locationId);
61+
SecretManagerServiceSettings secretManagerServiceSettings =
62+
SecretManagerServiceSettings.newBuilder().setEndpoint(apiEndpoint).build();
63+
64+
// Initialize the client that will be used to send requests. This client only needs to be
65+
// created once, and can be reused for multiple requests.
66+
try (SecretManagerServiceClient client =
67+
SecretManagerServiceClient.create(secretManagerServiceSettings)) {
68+
// Build the name.
69+
SecretName secretName =
70+
SecretName.ofProjectLocationSecretName(projectId, locationId, secretId);
71+
72+
// Get the existing secret
73+
Secret existingSecret = client.getSecret(secretName);
74+
75+
Map<String, String> existingAnnotationsMap =
76+
new HashMap<String, String>(existingSecret.getAnnotationsMap());
77+
78+
// Add a new annotation key and value.
79+
existingAnnotationsMap.put(annotationKey, annotationValue);
80+
81+
// Build the updated secret.
82+
Secret secret =
83+
Secret.newBuilder()
84+
.setName(secretName.toString())
85+
.putAllAnnotations(existingAnnotationsMap)
86+
.build();
87+
88+
// Build the field mask.
89+
FieldMask fieldMask = FieldMaskUtil.fromString("annotations");
90+
91+
// Update the secret.
92+
Secret updatedSecret = client.updateSecret(secret, fieldMask);
93+
System.out.printf("Updated secret %s\n", updatedSecret.getName());
94+
95+
return updatedSecret;
96+
}
97+
}
98+
}
99+
// [END secretmanager_edit_regional_secret_annotations]
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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.regionalsamples;
18+
19+
// [START secretmanager_view_regional_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.SecretManagerServiceSettings;
23+
import com.google.cloud.secretmanager.v1.SecretName;
24+
import java.io.IOException;
25+
import java.util.Map;
26+
27+
public class ViewRegionalSecretAnnotations {
28+
29+
public static void viewRegionalSecretAnnotations() throws IOException {
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 whose annotations to view
37+
String secretId = "your-secret-id";
38+
viewRegionalSecretAnnotations(projectId, locationId, secretId);
39+
}
40+
41+
// View the annotations of an existing secret.
42+
public static Map<String, String> viewRegionalSecretAnnotations(
43+
String projectId,
44+
String locationId,
45+
String secretId
46+
) throws IOException {
47+
48+
// Endpoint to call the regional secret manager sever
49+
String apiEndpoint = String.format("secretmanager.%s.rep.googleapis.com:443", locationId);
50+
SecretManagerServiceSettings secretManagerServiceSettings =
51+
SecretManagerServiceSettings.newBuilder().setEndpoint(apiEndpoint).build();
52+
53+
// Initialize the client that will be used to send requests. This client only needs to be
54+
// created once, and can be reused for multiple requests.
55+
try (SecretManagerServiceClient client =
56+
SecretManagerServiceClient.create(secretManagerServiceSettings)) {
57+
58+
// Build the name.
59+
SecretName secretName =
60+
SecretName.ofProjectLocationSecretName(projectId, locationId, secretId);
61+
62+
// Create the secret.
63+
Secret secret = client.getSecret(secretName);
64+
65+
Map<String, String> annotations = secret.getAnnotationsMap();
66+
67+
System.out.printf("Secret %s \n", secret.getName());
68+
69+
for (Map.Entry<String, String> annotation : annotations.entrySet()) {
70+
System.out.printf("Annotation key : %s, Annotation Value : %s\n",
71+
annotation.getKey(), annotation.getValue());
72+
}
73+
74+
return secret.getAnnotationsMap();
75+
}
76+
}
77+
}
78+
// [END secretmanager_view_regional_secret_annotations]
79+

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import java.io.ByteArrayOutputStream;
4747
import java.io.IOException;
4848
import java.io.PrintStream;
49+
import java.util.Map;
4950
import java.util.Random;
5051
import org.junit.After;
5152
import org.junit.AfterClass;
@@ -69,12 +70,17 @@ public class SnippetsIT {
6970
private static final String LOCATION_ID = "us-central1";
7071
private static final String REGIONAL_ENDPOINT =
7172
String.format("secretmanager.%s.rep.googleapis.com:443", LOCATION_ID);
73+
private static final String ANNOTATION_KEY = "exampleannotationkey";
74+
private static final String ANNOTATION_VALUE = "exampleannotationvalue";
75+
private static final String UPDATED_ANNOTATION_KEY = "updatedannotationkey";
76+
private static final String UPDATED_ANNOTATION_VALUE = "updatedannotationvalue";
7277

7378
private static Secret TEST_REGIONAL_SECRET;
7479
private static Secret TEST_REGIONAL_SECRET_TO_DELETE;
7580
private static Secret TEST_REGIONAL_SECRET_TO_DELETE_WITH_ETAG;
7681
private static Secret TEST_REGIONAL_SECRET_WITH_VERSIONS;
7782
private static SecretName TEST_REGIONAL_SECRET_TO_CREATE_NAME;
83+
private static SecretName TEST_REGIONAL_SECRET_WITH_ANNOTATION_TO_CREATE_NAME;
7884
private static SecretVersion TEST_REGIONAL_SECRET_VERSION;
7985
private static SecretVersion TEST_REGIONAL_SECRET_VERSION_TO_DESTROY;
8086
private static SecretVersion TEST_REGIONAL_SECRET_VERSION_TO_DESTROY_WITH_ETAG;
@@ -97,6 +103,8 @@ public static void beforeAll() throws IOException {
97103
TEST_REGIONAL_SECRET_WITH_VERSIONS = createRegionalSecret();
98104
TEST_REGIONAL_SECRET_TO_CREATE_NAME =
99105
SecretName.ofProjectLocationSecretName(PROJECT_ID, LOCATION_ID, randomSecretId());
106+
TEST_REGIONAL_SECRET_WITH_ANNOTATION_TO_CREATE_NAME =
107+
SecretName.ofProjectLocationSecretName(PROJECT_ID, LOCATION_ID, randomSecretId());
100108

101109
TEST_REGIONAL_SECRET_VERSION = addRegionalSecretVersion(TEST_REGIONAL_SECRET_WITH_VERSIONS);
102110
TEST_REGIONAL_SECRET_VERSION_TO_DESTROY =
@@ -134,6 +142,7 @@ public static void afterAll() throws IOException {
134142

135143
deleteRegionalSecret(TEST_REGIONAL_SECRET.getName());
136144
deleteRegionalSecret(TEST_REGIONAL_SECRET_TO_CREATE_NAME.toString());
145+
deleteRegionalSecret(TEST_REGIONAL_SECRET_WITH_ANNOTATION_TO_CREATE_NAME.toString());
137146
deleteRegionalSecret(TEST_REGIONAL_SECRET_TO_DELETE.getName());
138147
deleteRegionalSecret(TEST_REGIONAL_SECRET_TO_DELETE_WITH_ETAG.getName());
139148
deleteRegionalSecret(TEST_REGIONAL_SECRET_WITH_VERSIONS.getName());
@@ -150,6 +159,11 @@ private static Secret createRegionalSecret() throws IOException {
150159
CreateSecretRequest request =
151160
CreateSecretRequest.newBuilder()
152161
.setParent(parent.toString())
162+
.setSecret(
163+
Secret.newBuilder()
164+
.putAnnotations(ANNOTATION_KEY, ANNOTATION_VALUE)
165+
.build()
166+
)
153167
.setSecretId(randomSecretId())
154168
.build();
155169

@@ -243,6 +257,15 @@ public void testCreateRegionalSecret() throws IOException {
243257
assertEquals(name.getSecret(), createdSecretName.getSecret());
244258
}
245259

260+
@Test
261+
public void testCreateRegionalSecretWithAnnotations() throws IOException {
262+
SecretName name = TEST_REGIONAL_SECRET_WITH_ANNOTATION_TO_CREATE_NAME;
263+
Secret secret = CreateRegionalSecretWithAnnotations.createRegionalSecretWithAnnotations(
264+
name.getProject(), name.getLocation(), name.getSecret(), ANNOTATION_KEY, ANNOTATION_VALUE);
265+
SecretName createdSecretName = SecretName.parse(secret.getName());
266+
assertEquals(name.getSecret(), createdSecretName.getSecret());
267+
}
268+
246269
@Test
247270
public void testDeleteRegionalSecret() throws IOException {
248271
SecretName name = SecretName.parse(TEST_REGIONAL_SECRET_TO_DELETE.getName());
@@ -459,6 +482,17 @@ public void testListRegionalSecretsWithFilter() throws IOException {
459482
assertTrue(secretPresentInList);
460483
}
461484

485+
@Test
486+
public void testViewRegionalSecretAnnotations() throws IOException {
487+
SecretName name = SecretName.parse(TEST_REGIONAL_SECRET.getName());
488+
Map<String, String> annotations =
489+
ViewRegionalSecretAnnotations.viewRegionalSecretAnnotations(
490+
name.getProject(), name.getLocation(), name.getSecret()
491+
);
492+
493+
assertThat(annotations).containsEntry(ANNOTATION_KEY, ANNOTATION_VALUE);
494+
}
495+
462496
@Test
463497
public void testUpdateRegionalSecret() throws IOException {
464498
SecretName name = SecretName.parse(TEST_REGIONAL_SECRET.getName());
@@ -476,5 +510,20 @@ public void testUpdateRegionalSecretWithAlias() throws IOException {
476510

477511
assertEquals(1L, (long) updatedSecret.getVersionAliasesMap().get("test"));
478512
}
513+
514+
@Test
515+
public void testEditSecretAnnotations() throws IOException {
516+
SecretName name = SecretName.parse(TEST_REGIONAL_SECRET.getName());
517+
Secret updatedSecret = EditRegionalSecretAnnotations.editRegionalSecretAnnotations(
518+
name.getProject(),
519+
name.getLocation(),
520+
name.getSecret(),
521+
UPDATED_ANNOTATION_KEY,
522+
UPDATED_ANNOTATION_VALUE
523+
);
524+
525+
assertThat(updatedSecret.getAnnotationsMap()).containsEntry(
526+
UPDATED_ANNOTATION_KEY, UPDATED_ANNOTATION_VALUE);
527+
}
479528
}
480529

0 commit comments

Comments
 (0)