|
| 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 | +command line application and sample code for creating a new secret then |
| 17 | +bind the tag to that secret. |
| 18 | +""" |
| 19 | + |
| 20 | +# [START secretmanager_bind_tags_to_regional_secret] |
| 21 | +import argparse |
| 22 | + |
| 23 | +# Import the Secret Manager and Resource Manager client library. |
| 24 | +from google.cloud import resourcemanager_v3 |
| 25 | +from google.cloud import secretmanager |
| 26 | + |
| 27 | + |
| 28 | +def bind_tags_to_regional_secret( |
| 29 | + project_id: str, |
| 30 | + location_id: str, |
| 31 | + secret_id: str, |
| 32 | + tag_value: str, |
| 33 | +) -> resourcemanager_v3.TagBinding: |
| 34 | + """ |
| 35 | + Create a new regional secret with the given name, and then bind an existing |
| 36 | + tag to it. A secret is a logical wrapper around a collection of secret |
| 37 | + versions. Secret versions hold the actual secret material. |
| 38 | + """ |
| 39 | + |
| 40 | + # Endpoint to call the regional secret manager sever |
| 41 | + api_endpoint = f"secretmanager.{location_id}.rep.googleapis.com" |
| 42 | + |
| 43 | + # Create the Secret Manager client. |
| 44 | + client = secretmanager.SecretManagerServiceClient( |
| 45 | + client_options={"api_endpoint": api_endpoint}, |
| 46 | + ) |
| 47 | + |
| 48 | + # Build the resource name of the parent project. |
| 49 | + parent = f"projects/{project_id}/locations/{location_id}" |
| 50 | + |
| 51 | + # Create the secret. |
| 52 | + secret_response = client.create_secret( |
| 53 | + request={ |
| 54 | + "parent": parent, |
| 55 | + "secret_id": secret_id, |
| 56 | + } |
| 57 | + ) |
| 58 | + |
| 59 | + # Print the new secret name. |
| 60 | + print(f"Created secret: {secret_response.name}") |
| 61 | + |
| 62 | + # Endpoint to call the regional secret manager sever |
| 63 | + resource_manager_api_endpoint = f"{location_id}-cloudresourcemanager.googleapis.com" |
| 64 | + |
| 65 | + # Create the resource manager client |
| 66 | + resource_manager_client = resourcemanager_v3.TagBindingsClient( |
| 67 | + client_options={"api_endpoint": resource_manager_api_endpoint}, |
| 68 | + ) |
| 69 | + |
| 70 | + # Create the tag binding |
| 71 | + request = resourcemanager_v3.CreateTagBindingRequest( |
| 72 | + tag_binding=resourcemanager_v3.TagBinding( |
| 73 | + parent=f"//secretmanager.googleapis.com/{secret_response.name}", |
| 74 | + tag_value=f"{tag_value}", |
| 75 | + ), |
| 76 | + ) |
| 77 | + |
| 78 | + # Create the tag binding |
| 79 | + operation = resource_manager_client.create_tag_binding(request=request) |
| 80 | + |
| 81 | + # Wait for the operation to complete |
| 82 | + response = operation.result() |
| 83 | + |
| 84 | + # Print the tag binding |
| 85 | + print(f"Created tag binding: {response.name}") |
| 86 | + |
| 87 | + return response |
| 88 | + |
| 89 | + |
| 90 | +# [END secretmanager_bind_tags_to_regional_secret] |
| 91 | + |
| 92 | +if __name__ == "__main__": |
| 93 | + parser = argparse.ArgumentParser( |
| 94 | + description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter |
| 95 | + ) |
| 96 | + parser.add_argument("project_id", help="id of the GCP project") |
| 97 | + parser.add_argument( |
| 98 | + "location_id", help="id of the location where secret is to be created" |
| 99 | + ) |
| 100 | + parser.add_argument("secret_id", help="id of the secret to create") |
| 101 | + parser.add_argument("tag_value", help="value of the tag you want to add") |
| 102 | + args = parser.parse_args() |
| 103 | + |
| 104 | + bind_tags_to_regional_secret(args.project_id, args.location_id, args.secret_id, args.tag_value) |
0 commit comments