Skip to content

Commit 721bac6

Browse files
authored
feat:add samples for labels, regional SM (#9565)
* feat:add samples for labels, regional SM * fix: resolved comments * fix: lint issues * fix: lint issues * fix: lint issues * fix: fixed tests
1 parent 56de631 commit 721bac6

File tree

5 files changed

+394
-6
lines changed

5 files changed

+394
-6
lines changed
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_create_regional_secret_with_labels]
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 CreateRegionalSecretWithLabels {
27+
28+
public static void createRegionalSecretWithLabels() 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 label to be added
38+
String labelKey = "your-label-key";
39+
// This is the value of the label to be added
40+
String labelValue = "your-label-value";
41+
createRegionalSecretWithLabels(projectId, locationId, secretId, labelKey, labelValue);
42+
}
43+
44+
// Create a secret with labels.
45+
public static Secret createRegionalSecretWithLabels(
46+
String projectId,
47+
String locationId,
48+
String secretId,
49+
String labelKey,
50+
String labelValue)
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+
.putLabels(labelKey, labelValue)
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_labels]
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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_delete_regional_secret_label]
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 DeleteRegionalSecretLabel {
31+
32+
public static void deleteRegionalSecretLabel() 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 label to be deleted
42+
String labelKey = "your-label-key";
43+
deleteRegionalSecretLabel(projectId, locationId, secretId, labelKey);
44+
}
45+
46+
// Update an existing secret, by deleting a label.
47+
public static Secret deleteRegionalSecretLabel(
48+
String projectId, String locationId, String secretId, String labelKey) throws IOException {
49+
50+
// Endpoint to call the regional secret manager sever
51+
String apiEndpoint = String.format("secretmanager.%s.rep.googleapis.com:443", locationId);
52+
SecretManagerServiceSettings secretManagerServiceSettings =
53+
SecretManagerServiceSettings.newBuilder().setEndpoint(apiEndpoint).build();
54+
55+
// Initialize client that will be used to send requests. This client only needs to be created
56+
// once, and can be reused for multiple requests.
57+
try (SecretManagerServiceClient client =
58+
SecretManagerServiceClient.create(secretManagerServiceSettings)) {
59+
// Build the secret name.
60+
SecretName secretName =
61+
SecretName.ofProjectLocationSecretName(projectId, locationId, secretId);
62+
63+
// Get the existing secret
64+
Secret existingSecret = client.getSecret(secretName);
65+
66+
Map<String, String> existingLabelsMap =
67+
new HashMap<String, String>(existingSecret.getLabels());
68+
existingLabelsMap.remove(labelKey);
69+
70+
// Build the updated secret.
71+
Secret secret =
72+
Secret.newBuilder()
73+
.setName(secretName.toString())
74+
.putAllLabels(existingLabelsMap)
75+
.build();
76+
77+
// Build the field mask.
78+
FieldMask fieldMask = FieldMaskUtil.fromString("labels");
79+
80+
// Update the secret.
81+
Secret updatedSecret = client.updateSecret(secret, fieldMask);
82+
System.out.printf("Updated secret %s\n", updatedSecret.getName());
83+
84+
return updatedSecret;
85+
}
86+
}
87+
}
88+
// [END secretmanager_delete_regional_secret_label]
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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_label]
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 EditRegionalSecretLabel {
31+
32+
public static void editRegionalSecretLabel() 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 label to be added/updated
42+
String labelKey = "your-label-key";
43+
// This is the value of the label to be added/updated
44+
String labelValue = "your-label-value";
45+
editRegionalSecretLabel(projectId, locationId, secretId, labelKey, labelValue);
46+
}
47+
48+
// Update an existing secret, by creating a new label or updating an existing label.
49+
public static Secret editRegionalSecretLabel(
50+
String projectId, String locationId, String secretId, String labelKey, String labelValue)
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+
// Build the secret name.
63+
SecretName secretName =
64+
SecretName.ofProjectLocationSecretName(projectId, locationId, secretId);
65+
66+
// Get the existing secret
67+
Secret existingSecret = client.getSecret(secretName);
68+
69+
Map<String, String> existingLabelsMap =
70+
new HashMap<String, String>(existingSecret.getLabels());
71+
72+
// Add a new label key and value.
73+
existingLabelsMap.put(labelKey, labelValue);
74+
75+
// Build the updated secret.
76+
Secret secret =
77+
Secret.newBuilder()
78+
.setName(secretName.toString())
79+
.putAllLabels(existingLabelsMap)
80+
.build();
81+
82+
// Build the field mask.
83+
FieldMask fieldMask = FieldMaskUtil.fromString("labels");
84+
85+
// Update the secret.
86+
Secret updatedSecret = client.updateSecret(secret, fieldMask);
87+
System.out.printf("Updated secret %s\n", updatedSecret.getName());
88+
89+
return updatedSecret;
90+
}
91+
}
92+
}
93+
// [END secretmanager_edit_regional_secret_label]
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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_labels]
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 ViewRegionalSecretLabels {
28+
29+
public static void viewRegionalSecretLabels() 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 labels to view
37+
String secretId = "your-secret-id";
38+
viewRegionalSecretLabels(projectId, locationId, secretId);
39+
}
40+
41+
// View the labels of an existing secret.
42+
public static Map<String, String> viewRegionalSecretLabels(
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 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 =
56+
SecretManagerServiceClient.create(secretManagerServiceSettings)) {
57+
// Build the name.
58+
SecretName secretName =
59+
SecretName.ofProjectLocationSecretName(projectId, locationId, secretId);
60+
61+
// Create the secret.
62+
Secret secret = client.getSecret(secretName);
63+
64+
Map<String, String> labels = secret.getLabels();
65+
66+
System.out.printf("Secret %s \n", secret.getName());
67+
68+
for (Map.Entry<String, String> label : labels.entrySet()) {
69+
System.out.printf("Label key : %s, Label Value : %s\n", label.getKey(), label.getValue());
70+
}
71+
72+
return secret.getLabels();
73+
}
74+
}
75+
}
76+
// [END secretmanager_view_regional_secret_labels]

0 commit comments

Comments
 (0)