-
Notifications
You must be signed in to change notification settings - Fork 6.7k
feat(cloudkms): add samples for CryptoKey/CryptoKeyVersion deletion and get/lists RetiredResources #13852
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
iennae
merged 3 commits into
GoogleCloudPlatform:main
from
yasharel:delete-key-samples-python
Feb 25, 2026
Merged
feat(cloudkms): add samples for CryptoKey/CryptoKeyVersion deletion and get/lists RetiredResources #13852
Changes from all commits
Commits
Show all changes
3 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| # 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. | ||
|
|
||
| # [START kms_delete_key] | ||
| from google.cloud import kms | ||
|
|
||
|
|
||
| def delete_key( | ||
| project_id: str, location_id: str, key_ring_id: str, key_id: str | ||
| ) -> None: | ||
| """ | ||
| Delete the given key. This action is permanent and cannot be undone. Once the | ||
| key is deleted, it will no longer exist. | ||
|
|
||
| Args: | ||
| project_id (str): Google Cloud project ID (e.g. 'my-project'). | ||
| location_id (str): Cloud KMS location (e.g. 'us-east1'). | ||
| key_ring_id (str): ID of the Cloud KMS key ring (e.g. 'my-key-ring'). | ||
| key_id (str): ID of the key to use (e.g. 'my-key'). | ||
|
|
||
| Returns: | ||
| None | ||
|
|
||
| """ | ||
|
|
||
| # Create the client. | ||
| client = kms.KeyManagementServiceClient() | ||
|
|
||
| # Build the key name. | ||
| key_name = client.crypto_key_path(project_id, location_id, key_ring_id, key_id) | ||
|
|
||
| # Call the API. | ||
| # Note: delete_crypto_key returns a long-running operation. | ||
| # Warning: This operation is permanent and cannot be undone. | ||
| operation = client.delete_crypto_key(request={"name": key_name}) | ||
|
|
||
| # Wait for the operation to complete. | ||
| operation.result() | ||
|
|
||
| print(f"Deleted key: {key_name}") | ||
|
|
||
|
|
||
| # [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,57 @@ | ||
| # 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. | ||
|
|
||
| # [START kms_delete_key_version] | ||
| from google.cloud import kms | ||
|
|
||
|
|
||
| def delete_key_version( | ||
| project_id: str, location_id: str, key_ring_id: str, key_id: str, version_id: str | ||
| ) -> None: | ||
| """ | ||
| Delete the given key version. This action is permanent and cannot be undone. | ||
| Once the key version is deleted, it will no longer exist. | ||
|
|
||
| Args: | ||
| project_id (str): Google Cloud project ID (e.g. 'my-project'). | ||
| location_id (str): Cloud KMS location (e.g. 'us-east1'). | ||
| key_ring_id (str): ID of the Cloud KMS key ring (e.g. 'my-key-ring'). | ||
| key_id (str): ID of the key to use (e.g. 'my-key'). | ||
| version_id (str): ID of the key version to delete (e.g. '1'). | ||
|
|
||
| Returns: | ||
| None | ||
|
|
||
| """ | ||
|
|
||
| # Create the client. | ||
| client = kms.KeyManagementServiceClient() | ||
|
|
||
| # Build the key version name. | ||
| key_version_name = client.crypto_key_version_path( | ||
| project_id, location_id, key_ring_id, key_id, version_id | ||
| ) | ||
|
|
||
| # Call the API. | ||
| # Note: delete_crypto_key_version returns a long-running operation. | ||
| # Warning: This operation is permanent and cannot be undone. | ||
| operation = client.delete_crypto_key_version(request={"name": key_version_name}) | ||
|
|
||
| # Wait for the operation to complete. | ||
| operation.result() | ||
|
|
||
| print(f"Deleted key version: {key_version_name}") | ||
|
|
||
|
|
||
| # [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,50 @@ | ||
| # 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. | ||
|
|
||
| # [START kms_get_retired_resource] | ||
| from google.cloud import kms | ||
|
|
||
|
|
||
| def get_retired_resource( | ||
| project_id: str, location_id: str, retired_resource_id: str | ||
| ) -> kms.RetiredResource: | ||
| """ | ||
| Get the details of a retired resource. | ||
|
|
||
| Args: | ||
| project_id (str): Google Cloud project ID (e.g. 'my-project'). | ||
| location_id (str): Cloud KMS location (e.g. 'us-east1'). | ||
| resource_id (str): ID of the retired resource to get. | ||
|
|
||
| Returns: | ||
| kms.RetiredResource: The requested retired resource. | ||
|
|
||
| """ | ||
|
|
||
| # Create the client. | ||
| client = kms.KeyManagementServiceClient() | ||
|
|
||
| # Build the retired resource name. | ||
| # Note: Retired resources are tied to a Location, not a KeyRing. | ||
| # The name is like projects/{project}/locations/{location}/retiredResources/{id} | ||
| name = client.retired_resource_path(project_id, location_id, retired_resource_id) | ||
|
|
||
| # Call the API. | ||
| response = client.get_retired_resource(request={"name": name}) | ||
|
|
||
| print(f"Got retired resource: {response.name}") | ||
| return response | ||
|
|
||
|
|
||
| # [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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| # 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. | ||
|
|
||
| # [START kms_list_retired_resources] | ||
| from typing import List | ||
|
|
||
| from google.cloud import kms | ||
|
|
||
|
|
||
| def list_retired_resources(project_id: str, location_id: str) -> List[kms.RetiredResource]: | ||
| """ | ||
| List the retired resources in a location. | ||
|
|
||
| Args: | ||
| project_id (str): Google Cloud project ID (e.g. 'my-project'). | ||
| location_id (str): Cloud KMS location (e.g. 'us-east1'). | ||
|
|
||
| Returns: | ||
| list[kms.RetiredResource]: The list of retired resources. | ||
| """ | ||
|
|
||
| # Create the client. | ||
| client = kms.KeyManagementServiceClient() | ||
|
|
||
| # Build the parent location name. | ||
| parent = client.common_location_path(project_id, location_id) | ||
|
|
||
| # Call the API. | ||
| # The API paginates, but the Python client library handles that for us. | ||
| resources_list = list(client.list_retired_resources(request={"parent": parent})) | ||
|
|
||
| # Iterate over the resources and print them. | ||
| for resource in resources_list: | ||
| print(f"Retired resource: {resource.name}") | ||
|
|
||
| return resources_list | ||
|
|
||
|
|
||
| # [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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| google-cloud-kms==3.2.1 | ||
| google-cloud-kms==3.11.0 | ||
| cryptography==45.0.1 | ||
| crcmod==1.7 | ||
| jwcrypto==1.5.6 |
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
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.