Skip to content

Commit 5e096f2

Browse files
committed
feat: add delete and retired resources samples for java
1 parent 1407c26 commit 5e096f2

7 files changed

Lines changed: 392 additions & 21 deletions

File tree

kms/pom.xml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
<dependency>
3232
<groupId>com.google.cloud</groupId>
3333
<artifactId>libraries-bom</artifactId>
34-
<version>26.32.0</version>
34+
<version>26.50.0</version>
3535
<type>pom</type>
3636
<scope>import</scope>
3737
</dependency>
@@ -42,16 +42,28 @@
4242
<dependency>
4343
<groupId>com.google.cloud</groupId>
4444
<artifactId>google-cloud-kms</artifactId>
45+
<version>2.88.0</version>
46+
</dependency>
47+
<dependency>
48+
<groupId>com.google.api.grpc</groupId>
49+
<artifactId>proto-google-cloud-kms-v1</artifactId>
50+
<version>0.179.0</version>
4551
</dependency>
4652
<dependency>
4753
<groupId>com.google.crypto.tink</groupId>
4854
<artifactId>tink</artifactId>
4955
<version>1.12.0</version>
5056
</dependency>
5157
<!-- [START_EXCLUDE] -->
58+
<dependency>
59+
<groupId>com.google.protobuf</groupId>
60+
<artifactId>protobuf-java</artifactId>
61+
<version>4.33.2</version>
62+
</dependency>
5263
<dependency>
5364
<groupId>com.google.protobuf</groupId>
5465
<artifactId>protobuf-java-util</artifactId>
66+
<version>4.33.2</version>
5567
</dependency>
5668
<dependency>
5769
<groupId>junit</groupId>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2026 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 kms;
18+
19+
// [START kms_delete_key]
20+
import com.google.cloud.kms.v1.CryptoKeyName;
21+
import com.google.cloud.kms.v1.KeyManagementServiceClient;
22+
import java.io.IOException;
23+
24+
public class DeleteKey {
25+
26+
public void deleteKey() throws IOException {
27+
// TODO(developer): Replace these variables before running the sample.
28+
String projectId = "your-project-id";
29+
String locationId = "us-east1";
30+
String keyRingId = "my-key-ring";
31+
String keyId = "my-key";
32+
deleteKey(projectId, locationId, keyRingId, keyId);
33+
}
34+
35+
// Schedule deletion of the given key.
36+
public void deleteKey(String projectId, String locationId, String keyRingId, String keyId)
37+
throws IOException {
38+
// Initialize client that will be used to send requests. This client only
39+
// needs to be created once, and can be reused for multiple requests. After
40+
// completing all of your requests, call the "close" method on the client to
41+
// safely clean up any remaining background resources.
42+
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
43+
// Build the key name from the project, location, key ring, and key.
44+
CryptoKeyName keyName = CryptoKeyName.of(projectId, locationId, keyRingId, keyId);
45+
46+
// Delete the key.
47+
// Wait for the operation to complete.
48+
client.deleteCryptoKeyAsync(keyName).get();
49+
System.out.printf("Deleted key: %s%n", keyName.toString());
50+
} catch (Exception e) {
51+
System.err.printf("Failed to delete key: %s%n", e.getMessage());
52+
}
53+
}
54+
}
55+
// [END kms_delete_key]
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2026 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 kms;
18+
19+
// [START kms_delete_key_version]
20+
import com.google.cloud.kms.v1.CryptoKeyVersionName;
21+
import com.google.cloud.kms.v1.KeyManagementServiceClient;
22+
import java.io.IOException;
23+
24+
public class DeleteKeyVersion {
25+
26+
public void deleteKeyVersion() throws IOException {
27+
// TODO(developer): Replace these variables before running the sample.
28+
String projectId = "your-project-id";
29+
String locationId = "us-east1";
30+
String keyRingId = "my-key-ring";
31+
String keyId = "my-key";
32+
String keyVersionId = "123";
33+
deleteKeyVersion(projectId, locationId, keyRingId, keyId, keyVersionId);
34+
}
35+
36+
// Schedule deletion of the given key version.
37+
public void deleteKeyVersion(
38+
String projectId, String locationId, String keyRingId, String keyId, String keyVersionId)
39+
throws IOException {
40+
// Initialize client that will be used to send requests. This client only
41+
// needs to be created once, and can be reused for multiple requests. After
42+
// completing all of your requests, call the "close" method on the client to
43+
// safely clean up any remaining background resources.
44+
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
45+
// Build the key version name from the project, location, key ring, key,
46+
// and key version.
47+
CryptoKeyVersionName keyVersionName =
48+
CryptoKeyVersionName.of(projectId, locationId, keyRingId, keyId, keyVersionId);
49+
50+
// Delete the key version.
51+
// Delete the key version.
52+
// Wait for the operation to complete.
53+
client.deleteCryptoKeyVersionAsync(keyVersionName).get();
54+
System.out.printf("Deleted key version: %s%n", keyVersionName.toString());
55+
System.out.printf("Deleted key version: %s%n", keyVersionName.toString());
56+
} catch (Exception e) {
57+
System.err.printf("Failed to delete key version: %s%n", e.getMessage());
58+
}
59+
}
60+
}
61+
// [END kms_delete_key_version]
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2026 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 kms;
18+
19+
// [START kms_get_retired_resource]
20+
import com.google.cloud.kms.v1.KeyManagementServiceClient;
21+
import com.google.cloud.kms.v1.RetiredResource;
22+
import com.google.cloud.kms.v1.RetiredResourceName;
23+
import java.io.IOException;
24+
25+
public class GetRetiredResource {
26+
27+
public void getRetiredResource() throws IOException {
28+
// TODO(developer): Replace these variables before running the sample.
29+
String projectId = "your-project-id";
30+
String locationId = "us-east1";
31+
String retiredResourceId = "my-retired-resource-id";
32+
getRetiredResource(projectId, locationId, retiredResourceId);
33+
}
34+
35+
// Get the retired resource.
36+
public void getRetiredResource(
37+
String projectId, String locationId, String retiredResourceId)
38+
throws IOException {
39+
// Initialize client that will be used to send requests. This client only
40+
// needs to be created once, and can be reused for multiple requests. After
41+
// completing all of your requests, call the "close" method on the client to
42+
// safely clean up any remaining background resources.
43+
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
44+
// Build the retired resource name from the project, location, and retired resource id.
45+
RetiredResourceName name = RetiredResourceName.of(projectId, locationId, retiredResourceId);
46+
47+
// Get the retired resource.
48+
RetiredResource response = client.getRetiredResource(name);
49+
System.out.printf("Retired resource: %s%n", response.getName());
50+
}
51+
}
52+
}
53+
// [END kms_get_retired_resource]

kms/src/main/java/kms/IamRemoveMember.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,28 @@ public void iamRemoveMember(
5656

5757
// Search through the bindings and remove matches.
5858
String roleToFind = "roles/cloudkms.cryptoKeyEncrypterDecrypter";
59+
// Create a new list of bindings, removing the member from the role.
60+
java.util.List<Binding> newBindings = new java.util.ArrayList<>();
5961
for (Binding binding : policy.getBindingsList()) {
6062
if (binding.getRole().equals(roleToFind) && binding.getMembersList().contains(member)) {
61-
binding.getMembersList().remove(member);
63+
Binding.Builder bindingBuilder = binding.toBuilder();
64+
// Remove the member.
65+
// Note: ProtocolStringList is immutable, so we need to rebuild the members list.
66+
java.util.List<String> validMembers = new java.util.ArrayList<>(binding.getMembersList());
67+
validMembers.remove(member);
68+
69+
bindingBuilder.clearMembers().addAllMembers(validMembers);
70+
if (!validMembers.isEmpty()) {
71+
newBindings.add(bindingBuilder.build());
72+
}
73+
// If no members left, we can just omit the binding (effective removal).
74+
} else {
75+
newBindings.add(binding);
6276
}
6377
}
6478

79+
Policy newPolicy = policy.toBuilder().clearBindings().addAllBindings(newBindings).build();
80+
6581
client.setIamPolicy(resourceName, policy);
6682
System.out.printf("Updated IAM policy for %s%n", resourceName.toString());
6783
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2026 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 kms;
18+
19+
// [START kms_list_retired_resources]
20+
import com.google.cloud.kms.v1.KeyManagementServiceClient;
21+
import com.google.cloud.kms.v1.LocationName;
22+
import com.google.cloud.kms.v1.RetiredResource;
23+
import java.io.IOException;
24+
25+
public class ListRetiredResources {
26+
27+
public void listRetiredResources() throws IOException {
28+
// TODO(developer): Replace these variables before running the sample.
29+
String projectId = "your-project-id";
30+
String locationId = "us-east1";
31+
listRetiredResources(projectId, locationId);
32+
}
33+
34+
// List retired resources in a specific project and location.
35+
public void listRetiredResources(String projectId, String locationId)
36+
throws IOException {
37+
// Initialize client that will be used to send requests. This client only
38+
// needs to be created once, and can be reused for multiple requests. After
39+
// completing all of your requests, call the "close" method on the client to
40+
// safely clean up any remaining background resources.
41+
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
42+
// Build the location name from the project and location.
43+
LocationName locationName = LocationName.of(projectId, locationId);
44+
45+
// List the retired resources.
46+
for (RetiredResource resource : client.listRetiredResources(locationName).iterateAll()) {
47+
System.out.printf("Retired resource: %s%n", resource.getName());
48+
}
49+
}
50+
}
51+
}
52+
// [END kms_list_retired_resources]

0 commit comments

Comments
 (0)