Skip to content

Commit 4fcb198

Browse files
feat(parametermanager): add bind tags and get tags samples
Add global and regional samples that bind a tag value to a parameter through Resource Manager and read the tags of a parameter as tag bindings, with tests.
1 parent c0d67fa commit 4fcb198

6 files changed

Lines changed: 400 additions & 0 deletions

File tree

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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 parameter and binding a tag to it.
18+
"""
19+
20+
from google.cloud import resourcemanager_v3
21+
22+
23+
# [START parametermanager_bind_tags_to_param]
24+
def bind_tags_to_param(
25+
project_id: str, parameter_id: str, tag_value: str
26+
) -> resourcemanager_v3.TagBinding:
27+
"""
28+
Creates a parameter in the global location 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+
parameter_id (str): The ID to assign to the new parameter.
34+
tag_value (str): The tag value resource name, e.g. tagValues/1234567890.
35+
36+
Returns:
37+
resourcemanager_v3.TagBinding: An object representing the tag binding.
38+
39+
Example:
40+
bind_tags_to_param(
41+
"my-project",
42+
"my-parameter",
43+
"tagValues/1234567890"
44+
)
45+
"""
46+
# Import the necessary libraries.
47+
from google.cloud import parametermanager_v1
48+
from google.cloud import resourcemanager_v3
49+
50+
# Create the Parameter Manager client.
51+
client = parametermanager_v1.ParameterManagerClient()
52+
53+
# Create the Resource Manager tag bindings client.
54+
resource_manager_client = resourcemanager_v3.TagBindingsClient()
55+
56+
# Build the resource name of the parent project.
57+
parent = client.common_location_path(project_id, "global")
58+
59+
# Create the parameter.
60+
parameter = client.create_parameter(
61+
request={"parent": parent, "parameter_id": parameter_id}
62+
)
63+
64+
# Print the new parameter name.
65+
print(f"Created parameter: {parameter.name}")
66+
67+
# Define the tag binding request for the new parameter.
68+
request = resourcemanager_v3.CreateTagBindingRequest(
69+
tag_binding=resourcemanager_v3.TagBinding(
70+
parent=f"//parametermanager.googleapis.com/{parameter.name}",
71+
tag_value=tag_value,
72+
),
73+
)
74+
75+
# Create the tag binding and wait for the operation to complete.
76+
operation = resource_manager_client.create_tag_binding(request=request)
77+
response = operation.result()
78+
79+
# Print the tag binding.
80+
print(f"Created tag binding: {response.name}")
81+
# [END parametermanager_bind_tags_to_param]
82+
83+
return response
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
getting the tags of a parameter.
18+
"""
19+
20+
from google.cloud import resourcemanager_v3
21+
22+
23+
# [START parametermanager_get_param_tags]
24+
def get_param_tags(
25+
project_id: str, parameter_id: str
26+
) -> list[resourcemanager_v3.TagBinding]:
27+
"""
28+
Gets the tags bound to an existing parameter in the global location of the
29+
specified project.
30+
Tags are input-only on the parameter itself, so they are read as
31+
tag bindings through the Resource Manager API.
32+
33+
Args:
34+
project_id (str): The ID of the project.
35+
parameter_id (str): The ID of the parameter whose tags are retrieved.
36+
37+
Returns:
38+
list[resourcemanager_v3.TagBinding]: The tag bindings of the parameter.
39+
40+
Example:
41+
get_param_tags(
42+
"my-project",
43+
"my-parameter"
44+
)
45+
"""
46+
# Import the necessary libraries.
47+
from google.cloud import parametermanager_v1
48+
from google.cloud import resourcemanager_v3
49+
50+
# Create the Parameter Manager client.
51+
client = parametermanager_v1.ParameterManagerClient()
52+
53+
# Create the Resource Manager tag bindings client.
54+
resource_manager_client = resourcemanager_v3.TagBindingsClient()
55+
56+
# Build the resource name of the parameter and get it. The parameter name
57+
# contains the project number, which the tag bindings parent requires.
58+
name = client.parameter_path(project_id, "global", parameter_id)
59+
parameter = client.get_parameter(request={"name": name})
60+
61+
# List the tag bindings of the parameter.
62+
request = resourcemanager_v3.ListTagBindingsRequest(
63+
parent=f"//parametermanager.googleapis.com/{parameter.name}"
64+
)
65+
bindings = list(resource_manager_client.list_tag_bindings(request=request))
66+
67+
# Print the tags of the parameter.
68+
for binding in bindings:
69+
print(f"Found parameter tag: {binding.tag_value}")
70+
# [END parametermanager_get_param_tags]
71+
72+
return bindings
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
getting the tags of a regional parameter.
18+
"""
19+
20+
from google.cloud import resourcemanager_v3
21+
22+
23+
# [START parametermanager_get_regional_param_tags]
24+
def get_regional_param_tags(
25+
project_id: str, location_id: str, parameter_id: str
26+
) -> list[resourcemanager_v3.TagBinding]:
27+
"""
28+
Gets the tags bound to an existing parameter in the specified region of the
29+
specified project.
30+
Tags are input-only on the parameter itself, so they are read as
31+
tag bindings through the Resource Manager API.
32+
33+
Args:
34+
project_id (str): The ID of the project.
35+
location_id (str): The region where the parameter is located.
36+
parameter_id (str): The ID of the parameter whose tags are retrieved.
37+
38+
Returns:
39+
list[resourcemanager_v3.TagBinding]: The tag bindings of the parameter.
40+
41+
Example:
42+
get_regional_param_tags(
43+
"my-project",
44+
"us-central1",
45+
"my-parameter"
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 parameter and get it. The parameter name
66+
# contains the project number, which the tag bindings parent requires.
67+
name = client.parameter_path(project_id, location_id, parameter_id)
68+
parameter = client.get_parameter(request={"name": name})
69+
70+
# List the tag bindings of the parameter.
71+
request = resourcemanager_v3.ListTagBindingsRequest(
72+
parent=f"//parametermanager.googleapis.com/{parameter.name}"
73+
)
74+
bindings = list(resource_manager_client.list_tag_bindings(request=request))
75+
76+
# Print the tags of the parameter.
77+
for binding in bindings:
78+
print(f"Found regional parameter tag: {binding.tag_value}")
79+
# [END parametermanager_get_regional_param_tags]
80+
81+
return bindings

‎parametermanager/snippets/regional_samples/snippets_test.py‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import pytest
2323

2424
# Import the methods to be tested
25+
from regional_samples import bind_tags_to_regional_param
2526
from regional_samples import create_regional_param
2627
from regional_samples import create_regional_param_version
2728
from regional_samples import (
@@ -37,6 +38,7 @@
3738
from regional_samples import disable_regional_param_version
3839
from regional_samples import enable_regional_param_version
3940
from regional_samples import get_regional_param
41+
from regional_samples import get_regional_param_tags
4042
from regional_samples import get_regional_param_version
4143
from regional_samples import list_regional_param_versions
4244
from regional_samples import list_regional_params
@@ -1108,3 +1110,37 @@ def test_create_param_version_with_mismatched_checksum(
11081110
getattr(detail, "reason", "") == "CHECKSUM_MISMATCH"
11091111
for detail in exc_info.value.details
11101112
)
1113+
1114+
1115+
def test_bind_tags_to_regional_param(
1116+
location_id: str,
1117+
parameter_id: Tuple[str, str],
1118+
tag_key_value: Tuple[str, str],
1119+
project_id: str,
1120+
) -> None:
1121+
param_id, _ = parameter_id
1122+
_, tag_value = tag_key_value
1123+
binding = bind_tags_to_regional_param.bind_tags_to_regional_param(project_id, location_id, param_id, tag_value)
1124+
assert binding.tag_value == tag_value
1125+
assert param_id in binding.parent
1126+
1127+
1128+
def test_get_regional_param_tags(
1129+
client: parametermanager_v1.ParameterManagerClient,
1130+
location_id: str,
1131+
parameter_id: Tuple[str, str],
1132+
tag_key_value: Tuple[str, str],
1133+
project_id: str,
1134+
) -> None:
1135+
param_id, _ = parameter_id
1136+
tag_key, tag_value = tag_key_value
1137+
_ = retry_client_create_parameter(
1138+
client,
1139+
request={
1140+
"parent": client.common_location_path(project_id, location_id),
1141+
"parameter_id": param_id,
1142+
"parameter": {"tags": {tag_key: tag_value}},
1143+
},
1144+
)
1145+
bindings = get_regional_param_tags.get_regional_param_tags(project_id, location_id, param_id)
1146+
assert tag_value in [binding.tag_value for binding in bindings]

0 commit comments

Comments
 (0)