Skip to content

Commit c0d67fa

Browse files
feat(parametermanager): add templates, tags and checksum samples
Add global and regional Python samples and tests for Parameter Manager templates (create, get, list, update labels, enable/disable, delete, render, quickstart), creating a parameter with tags, and CRC32C checksum create/verify. Bump google-cloud-parametermanager to 0.4.3.
1 parent 123b291 commit c0d67fa

36 files changed

Lines changed: 3227 additions & 5 deletions
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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 new template.
18+
"""
19+
20+
from google.cloud import parametermanager_v1
21+
22+
23+
# [START parametermanager_create_param_template]
24+
def create_param_template(
25+
project_id: str, template_id: str, format_type: parametermanager_v1.TemplateFormat
26+
) -> parametermanager_v1.Template:
27+
"""
28+
Creates a template in the global location of the specified project with the
29+
specified format using the Google Cloud Parameter Manager SDK.
30+
31+
Args:
32+
project_id (str): The ID of the project.
33+
template_id (str): The ID to assign to the new template.
34+
format_type (parametermanager_v1.TemplateFormat): The format of the
35+
template (TEMPLATE_FORMAT_YAML or TEMPLATE_FORMAT_JSON).
36+
37+
Returns:
38+
parametermanager_v1.Template: An object representing the newly created template.
39+
40+
Example:
41+
create_param_template(
42+
"my-project",
43+
"my-template",
44+
parametermanager_v1.TemplateFormat.TEMPLATE_FORMAT_JSON
45+
)
46+
"""
47+
# Import the necessary library for Google Cloud Parameter Manager.
48+
from google.cloud import parametermanager_v1
49+
50+
# Create the Parameter Manager client.
51+
client = parametermanager_v1.ParameterManagerClient()
52+
53+
# Build the resource name of the parent project in the global location.
54+
parent = client.common_location_path(project_id, "global")
55+
56+
# Define the template creation request with the specified format.
57+
request = parametermanager_v1.CreateTemplateRequest(
58+
parent=parent,
59+
template_id=template_id,
60+
template=parametermanager_v1.Template(format_=format_type),
61+
)
62+
63+
# Create the template.
64+
response = client.create_template(request=request)
65+
66+
# Print the newly created template name.
67+
print(f"Created template {response.name} with format {response.format_.name}")
68+
# [END parametermanager_create_param_template]
69+
70+
return response
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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 new template version.
18+
"""
19+
20+
from google.cloud import parametermanager_v1
21+
22+
23+
# [START parametermanager_create_param_template_version]
24+
def create_param_template_version(
25+
project_id: str, template_id: str, version_id: str
26+
) -> parametermanager_v1.TemplateVersion:
27+
"""
28+
Creates a new version of an existing template in the global location of the
29+
specified project using the Google Cloud Parameter Manager SDK. The payload
30+
is a JSON document with a placeholder, {{.variableName}}, for each value
31+
that is resolved from a parameter version when the template version is
32+
rendered.
33+
34+
Args:
35+
project_id (str): The ID of the project.
36+
template_id (str): The ID of the template for which the version is to be
37+
created.
38+
version_id (str): The ID of the template version to be created.
39+
40+
Returns:
41+
parametermanager_v1.TemplateVersion: An object representing the newly created
42+
template version.
43+
44+
Example:
45+
create_param_template_version(
46+
"my-project",
47+
"my-template",
48+
"v1"
49+
)
50+
"""
51+
# Import the necessary library for Google Cloud Parameter Manager.
52+
from google.cloud import parametermanager_v1
53+
import json
54+
55+
# Create the Parameter Manager client.
56+
client = parametermanager_v1.ParameterManagerClient()
57+
58+
# Build the resource name of the template.
59+
parent = client.template_path(project_id, "global", template_id)
60+
61+
# Define the template payload. Each {{.variableName}} placeholder is
62+
# replaced with the matching key of the parameter version at render time.
63+
payload_data = {"username": "{{.username}}", "host": "{{.host}}"}
64+
payload = parametermanager_v1.TemplateVersionPayload(
65+
data=json.dumps(payload_data).encode("utf-8")
66+
)
67+
68+
# Define the template version creation request.
69+
request = parametermanager_v1.CreateTemplateVersionRequest(
70+
parent=parent,
71+
template_version_id=version_id,
72+
template_version=parametermanager_v1.TemplateVersion(payload=payload),
73+
)
74+
75+
# Create the template version.
76+
response = client.create_template_version(request=request)
77+
78+
# Print the newly created template version name.
79+
print(f"Created template version: {response.name}")
80+
# [END parametermanager_create_param_template_version]
81+
82+
return response
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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 new parameter version with a checksum.
18+
"""
19+
20+
from google.cloud import parametermanager_v1
21+
22+
23+
# [START parametermanager_create_param_version_with_checksum]
24+
def create_param_version_with_checksum(
25+
project_id: str, parameter_id: str, version_id: str
26+
) -> parametermanager_v1.ParameterVersion:
27+
"""
28+
Creates a new version of an existing parameter in the global location of the
29+
specified project, sending a CRC32C checksum of the payload so that the
30+
server can verify the data it received, using the Google Cloud Parameter
31+
Manager SDK.
32+
33+
Args:
34+
project_id (str): The ID of the project.
35+
parameter_id (str): The ID of the parameter for which the version is to
36+
be created.
37+
version_id (str): The ID of the parameter version to be created.
38+
39+
Returns:
40+
parametermanager_v1.ParameterVersion: An object representing the newly created
41+
parameter version.
42+
43+
Example:
44+
create_param_version_with_checksum(
45+
"my-project",
46+
"my-parameter",
47+
"v1"
48+
)
49+
"""
50+
# Import the necessary library for Google Cloud Parameter Manager.
51+
from google.cloud import parametermanager_v1
52+
import google_crc32c
53+
54+
# Create the Parameter Manager client.
55+
client = parametermanager_v1.ParameterManagerClient()
56+
57+
# Build the resource name of the parameter.
58+
parent = client.parameter_path(project_id, "global", parameter_id)
59+
60+
# Define the payload and compute its CRC32C checksum (Castagnoli).
61+
payload_data = b"hello world!"
62+
data_crc32c = google_crc32c.value(payload_data)
63+
64+
# Define the parameter version creation request with the checksum.
65+
request = parametermanager_v1.CreateParameterVersionRequest(
66+
parent=parent,
67+
parameter_version_id=version_id,
68+
parameter_version=parametermanager_v1.ParameterVersion(
69+
payload=parametermanager_v1.ParameterVersionPayload(
70+
data=payload_data, data_crc32c=data_crc32c
71+
)
72+
),
73+
)
74+
75+
# Create the parameter version. The request fails with a
76+
# CHECKSUM_MISMATCH error if the checksum does not match the payload.
77+
response = client.create_parameter_version(request=request)
78+
79+
# Print the newly created parameter version name and checksum source.
80+
print(f"Created parameter version: {response.name}")
81+
print(f"Checksum source: {response.checksum_source.name}")
82+
# [END parametermanager_create_param_version_with_checksum]
83+
84+
return response
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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 new parameter with tags.
18+
"""
19+
20+
from google.cloud import parametermanager_v1
21+
22+
23+
# [START parametermanager_create_param_with_tags]
24+
def create_param_with_tags(
25+
project_id: str, parameter_id: str, tag_key: str, tag_value: str
26+
) -> parametermanager_v1.Parameter:
27+
"""
28+
Creates a parameter with a tag in the global location of the specified
29+
project using the Google Cloud Parameter Manager SDK. Tags can only be set
30+
when the parameter is created. They are not returned when the parameter is
31+
retrieved. To view a tag binding, use the Resource Manager API.
32+
33+
Args:
34+
project_id (str): The ID of the project.
35+
parameter_id (str): The ID to assign to the new parameter.
36+
tag_key (str): The tag key resource name, e.g. tagKeys/1234567890.
37+
tag_value (str): The tag value resource name, e.g. tagValues/1234567890.
38+
39+
Returns:
40+
parametermanager_v1.Parameter: An object representing the newly created parameter.
41+
42+
Example:
43+
create_param_with_tags(
44+
"my-project",
45+
"my-parameter",
46+
"tagKeys/1234567890",
47+
"tagValues/1234567890"
48+
)
49+
"""
50+
# Import the necessary library for Google Cloud Parameter Manager.
51+
from google.cloud import parametermanager_v1
52+
53+
# Create the Parameter Manager client.
54+
client = parametermanager_v1.ParameterManagerClient()
55+
56+
# Build the resource name of the parent project in the global location.
57+
parent = client.common_location_path(project_id, "global")
58+
59+
# Define the parameter creation request with the tag.
60+
request = parametermanager_v1.CreateParameterRequest(
61+
parent=parent,
62+
parameter_id=parameter_id,
63+
parameter=parametermanager_v1.Parameter(tags={tag_key: tag_value}),
64+
)
65+
66+
# Create the parameter.
67+
response = client.create_parameter(request=request)
68+
69+
# Print the newly created parameter name.
70+
print(f"Created parameter {response.name} with tag {tag_key}: {tag_value}")
71+
# [END parametermanager_create_param_with_tags]
72+
73+
return response
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
deleting a template.
18+
"""
19+
20+
21+
# [START parametermanager_delete_param_template]
22+
def delete_param_template(project_id: str, template_id: str) -> None:
23+
"""
24+
Deletes an existing template in the global location of the specified project
25+
using the Google Cloud Parameter Manager SDK.
26+
27+
Args:
28+
project_id (str): The ID of the project.
29+
template_id (str): The ID of the template to be deleted.
30+
31+
Returns:
32+
None
33+
34+
Example:
35+
delete_param_template(
36+
"my-project",
37+
"my-template"
38+
)
39+
"""
40+
# Import the necessary library for Google Cloud Parameter Manager.
41+
from google.cloud import parametermanager_v1
42+
43+
# Create the Parameter Manager client.
44+
client = parametermanager_v1.ParameterManagerClient()
45+
46+
# Build the resource name of the template.
47+
name = client.template_path(project_id, "global", template_id)
48+
49+
# Delete the template.
50+
client.delete_template(request={"name": name})
51+
52+
# Print confirmation of the deletion.
53+
print(f"Deleted template: {name}")
54+
# [END parametermanager_delete_param_template]

0 commit comments

Comments
 (0)