|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright 2026 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 |
| 17 | +creating a regional parameter and binding a tag to it. |
| 18 | +""" |
| 19 | + |
| 20 | +from google.cloud import resourcemanager_v3 |
| 21 | + |
| 22 | + |
| 23 | +# [START parametermanager_bind_tags_to_regional_param] |
| 24 | +def bind_tags_to_regional_param( |
| 25 | + project_id: str, location_id: str, parameter_id: str, tag_value: str |
| 26 | +) -> resourcemanager_v3.TagBinding: |
| 27 | + """ |
| 28 | + Creates a parameter in the specified region of the specified project and then |
| 29 | + binds an existing tag value to it through the Resource Manager API. |
| 30 | +
|
| 31 | + Args: |
| 32 | + project_id (str): The ID of the project. |
| 33 | + location_id (str): The region where the parameter is located. |
| 34 | + parameter_id (str): The ID to assign to the new parameter. |
| 35 | + tag_value (str): The tag value resource name, e.g. tagValues/1234567890. |
| 36 | +
|
| 37 | + Returns: |
| 38 | + resourcemanager_v3.TagBinding: An object representing the tag binding. |
| 39 | +
|
| 40 | + Example: |
| 41 | + bind_tags_to_regional_param( |
| 42 | + "my-project", |
| 43 | + "us-central1", |
| 44 | + "my-parameter", |
| 45 | + "tagValues/1234567890" |
| 46 | + ) |
| 47 | + """ |
| 48 | + # Import the necessary libraries. |
| 49 | + from google.cloud import parametermanager_v1 |
| 50 | + from google.cloud import resourcemanager_v3 |
| 51 | + |
| 52 | + # Create the Parameter Manager client with the regional endpoint. |
| 53 | + api_endpoint = f"parametermanager.{location_id}.rep.googleapis.com" |
| 54 | + client = parametermanager_v1.ParameterManagerClient( |
| 55 | + client_options={"api_endpoint": api_endpoint} |
| 56 | + ) |
| 57 | + |
| 58 | + # Create the Resource Manager tag bindings client with the regional endpoint. |
| 59 | + resource_manager_client = resourcemanager_v3.TagBindingsClient( |
| 60 | + client_options={ |
| 61 | + "api_endpoint": f"{location_id}-cloudresourcemanager.googleapis.com" |
| 62 | + } |
| 63 | + ) |
| 64 | + |
| 65 | + # Build the resource name of the parent project. |
| 66 | + parent = client.common_location_path(project_id, location_id) |
| 67 | + |
| 68 | + # Create the parameter. |
| 69 | + parameter = client.create_parameter( |
| 70 | + request={"parent": parent, "parameter_id": parameter_id} |
| 71 | + ) |
| 72 | + |
| 73 | + # Print the new parameter name. |
| 74 | + print(f"Created regional parameter: {parameter.name}") |
| 75 | + |
| 76 | + # Define the tag binding request for the new parameter. |
| 77 | + request = resourcemanager_v3.CreateTagBindingRequest( |
| 78 | + tag_binding=resourcemanager_v3.TagBinding( |
| 79 | + parent=f"//parametermanager.googleapis.com/{parameter.name}", |
| 80 | + tag_value=tag_value, |
| 81 | + ), |
| 82 | + ) |
| 83 | + |
| 84 | + # Create the tag binding and wait for the operation to complete. |
| 85 | + operation = resource_manager_client.create_tag_binding(request=request) |
| 86 | + response = operation.result() |
| 87 | + |
| 88 | + # Print the tag binding. |
| 89 | + print(f"Created tag binding: {response.name}") |
| 90 | + # [END parametermanager_bind_tags_to_regional_param] |
| 91 | + |
| 92 | + return response |
0 commit comments