Skip to content

Commit 3bd50b1

Browse files
authored
feat: added labels samples for secret manager (GoogleCloudPlatform#9401)
* feat: Created Samples for labels in SM * fix:lint checks * fix: updated copyright year * fix: resolved comments * fix:resolved comments
1 parent 300f3b9 commit 3bd50b1

File tree

5 files changed

+340
-0
lines changed

5 files changed

+340
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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_labels]
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 CreateSecretWithLabels {
27+
28+
public static void createSecretWithLabels() 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 label to be added
36+
String labelKey = "your-label-key";
37+
// This is the value of the label to be added
38+
String labelValue = "your-label-value";
39+
createSecretWithLabels(projectId, secretId, labelKey, labelValue);
40+
}
41+
42+
// Create a secret with labels.
43+
public static Secret createSecretWithLabels(
44+
String projectId, String secretId, String labelKey, String labelValue) 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+
.putLabels(labelKey, labelValue)
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_labels]
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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_update_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.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 CreateUpdateSecretLabel {
30+
31+
public static void createUpdateSecretLabel() 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 label to be added/updated
39+
String labelKey = "your-label-key";
40+
// This is the value of the label to be added/updated
41+
String labelValue = "your-label-value";
42+
createUpdateSecretLabel(projectId, secretId, labelKey, labelValue);
43+
}
44+
45+
// Update an existing secret, by creating a new label or updating an existing label.
46+
public static Secret createUpdateSecretLabel(
47+
String projectId, String secretId, String labelKey, String labelValue) throws IOException {
48+
// Initialize client that will be used to send requests. This client only needs to be created
49+
// once, and can be reused for multiple requests.
50+
try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
51+
// Build the name.
52+
SecretName secretName = SecretName.of(projectId, secretId);
53+
54+
// Get the existing secret
55+
Secret existingSecret = client.getSecret(secretName);
56+
57+
Map<String, String> existingLabelsMap =
58+
new HashMap<String, String>(existingSecret.getLabels());
59+
60+
// Add a new label key and value.
61+
existingLabelsMap.put(labelKey, labelValue);
62+
63+
// Build the updated secret.
64+
Secret secret =
65+
Secret.newBuilder()
66+
.setName(secretName.toString())
67+
.putAllLabels(existingLabelsMap)
68+
.build();
69+
70+
// Build the field mask.
71+
FieldMask fieldMask = FieldMaskUtil.fromString("labels");
72+
73+
// Update the secret.
74+
Secret updatedSecret = client.updateSecret(secret, fieldMask);
75+
System.out.printf("Updated secret %s\n", updatedSecret.getName());
76+
77+
return updatedSecret;
78+
}
79+
}
80+
}
81+
// [END secretmanager_create_update_secret_label]
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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_delete_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.SecretName;
23+
import com.google.protobuf.FieldMask;
24+
import com.google.protobuf.FieldMaskOrBuilder;
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 DeleteSecretLabel {
31+
32+
public static void deleteSecretLabel() 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+
// This is the id of the secret to act on
38+
String secretId = "your-secret-id";
39+
// This is the key of the label to be deleted
40+
String labelKey = "your-label-key";
41+
deleteSecretLabel(projectId, secretId, labelKey);
42+
}
43+
44+
// Update an existing secret, by deleting a label.
45+
public static Secret deleteSecretLabel(
46+
String projectId, String secretId, String labelKey) throws IOException {
47+
// Initialize client that will be used to send requests. This client only needs to be created
48+
// once, and can be reused for multiple requests.
49+
try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
50+
// Build the name.
51+
SecretName secretName = SecretName.of(projectId, secretId);
52+
53+
// Get the existing secret
54+
Secret existingSecret = client.getSecret(secretName);
55+
56+
Map<String, String> existingLabelsMap =
57+
new HashMap<String, String>(existingSecret.getLabels());
58+
existingLabelsMap.remove(labelKey);
59+
60+
// Build the updated secret.
61+
Secret secret =
62+
Secret.newBuilder()
63+
.setName(secretName.toString())
64+
.putAllLabels(existingLabelsMap)
65+
.build();
66+
67+
// Build the field mask.
68+
FieldMask fieldMask = FieldMaskUtil.fromString("labels");
69+
70+
// Update the secret.
71+
Secret updatedSecret = client.updateSecret(secret, fieldMask);
72+
System.out.printf("Updated secret %s\n", updatedSecret.getName());
73+
74+
return updatedSecret;
75+
}
76+
}
77+
}
78+
// [END secretmanager_delete_secret_label]
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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_labels]
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 ViewSecretLabels {
27+
28+
public static void viewSecretLabels() 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 labels to view
34+
String secretId = "your-secret-id";
35+
viewSecretLabels(projectId, secretId);
36+
}
37+
38+
// View the labels of an existing secret.
39+
public static Map<String, String> viewSecretLabels(
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> labels = secret.getLabels();
53+
54+
System.out.printf("Secret %s \n", secret.getName());
55+
56+
for (Map.Entry<String, String> label : labels.entrySet()) {
57+
System.out.printf("Label key : %s, Label Value : %s\n", label.getKey(), label.getValue());
58+
}
59+
60+
return secret.getLabels();
61+
}
62+
}
63+
}
64+
// [END secretmanager_view_secret_labels]

0 commit comments

Comments
 (0)