-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat(cloudkms): add samples for CryptoKey/CryptoKeyVersion deletion and get/lists RetiredResources #10237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat(cloudkms): add samples for CryptoKey/CryptoKeyVersion deletion and get/lists RetiredResources #10237
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package kms; | ||
|
|
||
| // [START kms_delete_key] | ||
| import com.google.cloud.kms.v1.CryptoKeyName; | ||
| import com.google.cloud.kms.v1.DeleteCryptoKeyMetadata; | ||
| import com.google.cloud.kms.v1.KeyManagementServiceClient; | ||
| import java.io.IOException; | ||
| import java.util.concurrent.ExecutionException; | ||
|
|
||
| public class DeleteKey { | ||
|
|
||
| public void deleteKey() throws IOException { | ||
| // TODO(developer): Replace these variables before running the sample. | ||
| String projectId = "your-project-id"; | ||
| String locationId = "us-east1"; | ||
| String keyRingId = "my-key-ring"; | ||
| String keyId = "my-key"; | ||
| deleteKey(projectId, locationId, keyRingId, keyId); | ||
| } | ||
|
|
||
| // deleteKey deletes a crypto key. This action is permanent and cannot be undone. Once the key | ||
| // is deleted, it will no longer exist. | ||
| public void deleteKey(String projectId, String locationId, String keyRingId, String keyId) | ||
| throws IOException { | ||
| // Initialize client that will be used to send requests. This client only | ||
| // needs to be created once, and can be reused for multiple requests. After | ||
| // completing all of your requests, call the "close" method on the client to | ||
| // safely clean up any remaining background resources. | ||
| try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) { | ||
| // Build the key name from the project, location, key ring, and key. | ||
| CryptoKeyName keyName = CryptoKeyName.of(projectId, locationId, keyRingId, keyId); | ||
|
|
||
| // Delete the key. | ||
| // Warning: This operation is permanent and cannot be undone. | ||
| // Wait for the operation to complete. | ||
| client.deleteCryptoKeyAsync(keyName).get(); | ||
| System.out.printf("Deleted key: %s%n", keyName.toString()); | ||
| } catch (Exception e) { | ||
| System.err.printf("Failed to delete key: %s%n", e.getMessage()); | ||
| } | ||
| } | ||
| } | ||
| // [END kms_delete_key] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package kms; | ||
|
|
||
| // [START kms_delete_key_version] | ||
| import com.google.cloud.kms.v1.CryptoKeyVersionName; | ||
| import com.google.cloud.kms.v1.KeyManagementServiceClient; | ||
| import java.io.IOException; | ||
|
|
||
| public class DeleteKeyVersion { | ||
|
|
||
| public void deleteKeyVersion() throws IOException { | ||
| // TODO(developer): Replace these variables before running the sample. | ||
| String projectId = "your-project-id"; | ||
| String locationId = "us-east1"; | ||
| String keyRingId = "my-key-ring"; | ||
| String keyId = "my-key"; | ||
| String keyVersionId = "123"; | ||
| deleteKeyVersion(projectId, locationId, keyRingId, keyId, keyVersionId); | ||
| } | ||
|
|
||
| // deleteKeyVersion deletes a key version. This action is permanent and cannot be undone. Once the | ||
| // key version is deleted, it will no longer exist. | ||
| public void deleteKeyVersion( | ||
| String projectId, String locationId, String keyRingId, String keyId, String keyVersionId) | ||
| throws IOException { | ||
| // Initialize client that will be used to send requests. This client only | ||
| // needs to be created once, and can be reused for multiple requests. After | ||
| // completing all of your requests, call the "close" method on the client to | ||
| // safely clean up any remaining background resources. | ||
| try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) { | ||
| // Build the key version name from the project, location, key ring, key, | ||
| // and key version. | ||
| CryptoKeyVersionName keyVersionName = | ||
| CryptoKeyVersionName.of(projectId, locationId, keyRingId, keyId, keyVersionId); | ||
|
|
||
| // Delete the key version. | ||
| // Warning: This operation is permanent and cannot be undone. | ||
| // Wait for the operation to complete. | ||
| client.deleteCryptoKeyVersionAsync(keyVersionName).get(); | ||
| System.out.printf("Deleted key version: %s%n", keyVersionName.toString()); | ||
| } catch (Exception e) { | ||
| System.err.printf("Failed to delete key version: %s%n", e.getMessage()); | ||
| } | ||
| } | ||
| } | ||
| // [END kms_delete_key_version] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package kms; | ||
|
|
||
| // [START kms_get_retired_resource] | ||
| import com.google.cloud.kms.v1.KeyManagementServiceClient; | ||
| import com.google.cloud.kms.v1.RetiredResource; | ||
| import com.google.cloud.kms.v1.RetiredResourceName; | ||
| import java.io.IOException; | ||
|
|
||
| public class GetRetiredResource { | ||
|
|
||
| public void getRetiredResource() throws IOException { | ||
| // TODO(developer): Replace these variables before running the sample. | ||
| String projectId = "your-project-id"; | ||
| String locationId = "us-east1"; | ||
| String retiredResourceId = "my-retired-resource-id"; | ||
| getRetiredResource(projectId, locationId, retiredResourceId); | ||
| } | ||
|
|
||
| // Get the retired resource. | ||
| public void getRetiredResource( | ||
| String projectId, String locationId, String retiredResourceId) | ||
| throws IOException { | ||
| // Initialize client that will be used to send requests. This client only | ||
| // needs to be created once, and can be reused for multiple requests. After | ||
| // completing all of your requests, call the "close" method on the client to | ||
| // safely clean up any remaining background resources. | ||
| try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) { | ||
| // Build the retired resource name from the project, location, and retired resource id. | ||
| RetiredResourceName name = RetiredResourceName.of(projectId, locationId, retiredResourceId); | ||
|
|
||
| // Get the retired resource. | ||
| RetiredResource response = client.getRetiredResource(name); | ||
| System.out.printf("Retired resource: %s%n", response.getName()); | ||
| } | ||
| } | ||
| } | ||
| // [END kms_get_retired_resource] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package kms; | ||
|
|
||
| // [START kms_list_retired_resources] | ||
| import com.google.cloud.kms.v1.KeyManagementServiceClient; | ||
| import com.google.cloud.kms.v1.LocationName; | ||
| import com.google.cloud.kms.v1.RetiredResource; | ||
| import java.io.IOException; | ||
|
|
||
| public class ListRetiredResources { | ||
|
|
||
| public void listRetiredResources() throws IOException { | ||
| // TODO(developer): Replace these variables before running the sample. | ||
| String projectId = "your-project-id"; | ||
| String locationId = "us-east1"; | ||
| listRetiredResources(projectId, locationId); | ||
| } | ||
|
|
||
| // List retired resources in a specific project and location. | ||
| public void listRetiredResources(String projectId, String locationId) | ||
| throws IOException { | ||
| // Initialize client that will be used to send requests. This client only | ||
| // needs to be created once, and can be reused for multiple requests. After | ||
| // completing all of your requests, call the "close" method on the client to | ||
| // safely clean up any remaining background resources. | ||
| try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) { | ||
| // Build the location name from the project and location. | ||
| LocationName locationName = LocationName.of(projectId, locationId); | ||
|
|
||
| // List the retired resources. | ||
| for (RetiredResource resource : client.listRetiredResources(locationName).iterateAll()) { | ||
| System.out.printf("Retired resource: %s%n", resource.getName()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| // [END kms_list_retired_resources] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.