|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright 2025 Google LLC |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | + |
| 16 | +# [START secretmanager_delete_regional_secret_annotation] |
| 17 | +import argparse |
| 18 | + |
| 19 | +# Import the Secret Manager client library. |
| 20 | +from google.cloud import secretmanager_v1 |
| 21 | + |
| 22 | + |
| 23 | +def delete_regional_secret_annotation( |
| 24 | + project_id: str, location_id: str, secret_id: str, annotation_key: str |
| 25 | +) -> secretmanager_v1.Secret: |
| 26 | + """ |
| 27 | + Delete a annotation on an existing secret. |
| 28 | + """ |
| 29 | + |
| 30 | + # Endpoint to call the regional Secret Manager API. |
| 31 | + api_endpoint = f"secretmanager.{location_id}.rep.googleapis.com" |
| 32 | + |
| 33 | + # Create the Secret Manager client. |
| 34 | + client = secretmanager_v1.SecretManagerServiceClient( |
| 35 | + client_options={"api_endpoint": api_endpoint}, |
| 36 | + ) |
| 37 | + |
| 38 | + # Build the resource name of the parent secret. |
| 39 | + name = f"projects/{project_id}/locations/{location_id}/secrets/{secret_id}" |
| 40 | + |
| 41 | + # Get the secret. |
| 42 | + response = client.get_secret(request={"name": name}) |
| 43 | + |
| 44 | + annotations = response.annotations |
| 45 | + |
| 46 | + # Delete the annotation |
| 47 | + annotations.pop(annotation_key, None) |
| 48 | + |
| 49 | + # Update the secret. |
| 50 | + secret = {"name": name, "annotations": annotations} |
| 51 | + update_mask = {"paths": ["annotations"]} |
| 52 | + response = client.update_secret( |
| 53 | + request={"secret": secret, "update_mask": update_mask} |
| 54 | + ) |
| 55 | + |
| 56 | + # Print the new secret name. |
| 57 | + print(f"Updated secret: {response.name}") |
| 58 | + |
| 59 | + return response |
| 60 | + |
| 61 | + |
| 62 | +# [END secretmanager_delete_regional_secret_annotation] |
| 63 | + |
| 64 | +if __name__ == "__main__": |
| 65 | + parser = argparse.ArgumentParser( |
| 66 | + description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter |
| 67 | + ) |
| 68 | + parser.add_argument("project_id", help="id of the GCP project") |
| 69 | + parser.add_argument( |
| 70 | + "location_id", help="id of the location where secret is to be created" |
| 71 | + ) |
| 72 | + parser.add_argument("secret_id", help="id of the secret to act on") |
| 73 | + parser.add_argument("annotation_key", help="key of the annotation to be deleted") |
| 74 | + args = parser.parse_args() |
| 75 | + |
| 76 | + delete_regional_secret_annotation( |
| 77 | + args.project_id, args.location_id, args.secret_id, args.annotation_key |
| 78 | + ) |
0 commit comments