From c0d67fac76c69801e2cc58048efb642a4bf5e182 Mon Sep 17 00:00:00 2001 From: smalaviya-crest Date: Wed, 7 Oct 2026 12:05:36 +0530 Subject: [PATCH 1/7] 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. --- .../snippets/create_param_template.py | 70 +++ .../snippets/create_param_template_version.py | 82 ++++ .../create_param_version_with_checksum.py | 84 ++++ .../snippets/create_param_with_tags.py | 73 ++++ .../snippets/delete_param_template.py | 54 +++ .../snippets/delete_param_template_version.py | 59 +++ .../disable_param_template_version.py | 75 ++++ .../snippets/enable_param_template_version.py | 75 ++++ .../snippets/get_param_template.py | 60 +++ .../snippets/get_param_template_version.py | 65 +++ .../get_param_version_verify_checksum.py | 75 ++++ .../snippets/list_param_template_versions.py | 53 +++ .../snippets/list_param_templates.py | 50 +++ .../create_regional_param_template.py | 80 ++++ .../create_regional_param_template_version.py | 87 ++++ ...te_regional_param_version_with_checksum.py | 89 ++++ .../create_regional_param_with_tags.py | 78 ++++ .../delete_regional_param_template.py | 61 +++ .../delete_regional_param_template_version.py | 66 +++ ...disable_regional_param_template_version.py | 82 ++++ .../enable_regional_param_template_version.py | 82 ++++ .../get_regional_param_template.py | 65 +++ .../get_regional_param_template_version.py | 72 ++++ ..._regional_param_version_verify_checksum.py | 82 ++++ .../list_regional_param_template_versions.py | 60 +++ .../list_regional_param_templates.py | 57 +++ .../regional_template_quickstart.py | 128 ++++++ .../render_regional_param_template_version.py | 93 ++++ .../regional_samples/snippets_test.py | 398 +++++++++++++++++- .../update_regional_param_template_labels.py | 87 ++++ .../snippets/render_param_template_version.py | 85 ++++ .../snippets/requirements-test.txt | 5 +- parametermanager/snippets/requirements.txt | 3 +- parametermanager/snippets/snippets_test.py | 398 +++++++++++++++++- .../snippets/template_quickstart.py | 119 ++++++ .../snippets/update_param_template_labels.py | 80 ++++ 36 files changed, 3227 insertions(+), 5 deletions(-) create mode 100644 parametermanager/snippets/create_param_template.py create mode 100644 parametermanager/snippets/create_param_template_version.py create mode 100644 parametermanager/snippets/create_param_version_with_checksum.py create mode 100644 parametermanager/snippets/create_param_with_tags.py create mode 100644 parametermanager/snippets/delete_param_template.py create mode 100644 parametermanager/snippets/delete_param_template_version.py create mode 100644 parametermanager/snippets/disable_param_template_version.py create mode 100644 parametermanager/snippets/enable_param_template_version.py create mode 100644 parametermanager/snippets/get_param_template.py create mode 100644 parametermanager/snippets/get_param_template_version.py create mode 100644 parametermanager/snippets/get_param_version_verify_checksum.py create mode 100644 parametermanager/snippets/list_param_template_versions.py create mode 100644 parametermanager/snippets/list_param_templates.py create mode 100644 parametermanager/snippets/regional_samples/create_regional_param_template.py create mode 100644 parametermanager/snippets/regional_samples/create_regional_param_template_version.py create mode 100644 parametermanager/snippets/regional_samples/create_regional_param_version_with_checksum.py create mode 100644 parametermanager/snippets/regional_samples/create_regional_param_with_tags.py create mode 100644 parametermanager/snippets/regional_samples/delete_regional_param_template.py create mode 100644 parametermanager/snippets/regional_samples/delete_regional_param_template_version.py create mode 100644 parametermanager/snippets/regional_samples/disable_regional_param_template_version.py create mode 100644 parametermanager/snippets/regional_samples/enable_regional_param_template_version.py create mode 100644 parametermanager/snippets/regional_samples/get_regional_param_template.py create mode 100644 parametermanager/snippets/regional_samples/get_regional_param_template_version.py create mode 100644 parametermanager/snippets/regional_samples/get_regional_param_version_verify_checksum.py create mode 100644 parametermanager/snippets/regional_samples/list_regional_param_template_versions.py create mode 100644 parametermanager/snippets/regional_samples/list_regional_param_templates.py create mode 100644 parametermanager/snippets/regional_samples/regional_template_quickstart.py create mode 100644 parametermanager/snippets/regional_samples/render_regional_param_template_version.py create mode 100644 parametermanager/snippets/regional_samples/update_regional_param_template_labels.py create mode 100644 parametermanager/snippets/render_param_template_version.py create mode 100644 parametermanager/snippets/template_quickstart.py create mode 100644 parametermanager/snippets/update_param_template_labels.py diff --git a/parametermanager/snippets/create_param_template.py b/parametermanager/snippets/create_param_template.py new file mode 100644 index 00000000000..a592b675b94 --- /dev/null +++ b/parametermanager/snippets/create_param_template.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python + +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +""" +command line application and sample code for +creating a new template. +""" + +from google.cloud import parametermanager_v1 + + +# [START parametermanager_create_param_template] +def create_param_template( + project_id: str, template_id: str, format_type: parametermanager_v1.TemplateFormat +) -> parametermanager_v1.Template: + """ + Creates a template in the global location of the specified project with the + specified format using the Google Cloud Parameter Manager SDK. + + Args: + project_id (str): The ID of the project. + template_id (str): The ID to assign to the new template. + format_type (parametermanager_v1.TemplateFormat): The format of the + template (TEMPLATE_FORMAT_YAML or TEMPLATE_FORMAT_JSON). + + Returns: + parametermanager_v1.Template: An object representing the newly created template. + + Example: + create_param_template( + "my-project", + "my-template", + parametermanager_v1.TemplateFormat.TEMPLATE_FORMAT_JSON + ) + """ + # Import the necessary library for Google Cloud Parameter Manager. + from google.cloud import parametermanager_v1 + + # Create the Parameter Manager client. + client = parametermanager_v1.ParameterManagerClient() + + # Build the resource name of the parent project in the global location. + parent = client.common_location_path(project_id, "global") + + # Define the template creation request with the specified format. + request = parametermanager_v1.CreateTemplateRequest( + parent=parent, + template_id=template_id, + template=parametermanager_v1.Template(format_=format_type), + ) + + # Create the template. + response = client.create_template(request=request) + + # Print the newly created template name. + print(f"Created template {response.name} with format {response.format_.name}") + # [END parametermanager_create_param_template] + + return response diff --git a/parametermanager/snippets/create_param_template_version.py b/parametermanager/snippets/create_param_template_version.py new file mode 100644 index 00000000000..f1ed231bbfe --- /dev/null +++ b/parametermanager/snippets/create_param_template_version.py @@ -0,0 +1,82 @@ +#!/usr/bin/env python + +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +""" +command line application and sample code for +creating a new template version. +""" + +from google.cloud import parametermanager_v1 + + +# [START parametermanager_create_param_template_version] +def create_param_template_version( + project_id: str, template_id: str, version_id: str +) -> parametermanager_v1.TemplateVersion: + """ + Creates a new version of an existing template in the global location of the + specified project using the Google Cloud Parameter Manager SDK. The payload + is a JSON document with a placeholder, {{.variableName}}, for each value + that is resolved from a parameter version when the template version is + rendered. + + Args: + project_id (str): The ID of the project. + template_id (str): The ID of the template for which the version is to be + created. + version_id (str): The ID of the template version to be created. + + Returns: + parametermanager_v1.TemplateVersion: An object representing the newly created + template version. + + Example: + create_param_template_version( + "my-project", + "my-template", + "v1" + ) + """ + # Import the necessary library for Google Cloud Parameter Manager. + from google.cloud import parametermanager_v1 + import json + + # Create the Parameter Manager client. + client = parametermanager_v1.ParameterManagerClient() + + # Build the resource name of the template. + parent = client.template_path(project_id, "global", template_id) + + # Define the template payload. Each {{.variableName}} placeholder is + # replaced with the matching key of the parameter version at render time. + payload_data = {"username": "{{.username}}", "host": "{{.host}}"} + payload = parametermanager_v1.TemplateVersionPayload( + data=json.dumps(payload_data).encode("utf-8") + ) + + # Define the template version creation request. + request = parametermanager_v1.CreateTemplateVersionRequest( + parent=parent, + template_version_id=version_id, + template_version=parametermanager_v1.TemplateVersion(payload=payload), + ) + + # Create the template version. + response = client.create_template_version(request=request) + + # Print the newly created template version name. + print(f"Created template version: {response.name}") + # [END parametermanager_create_param_template_version] + + return response diff --git a/parametermanager/snippets/create_param_version_with_checksum.py b/parametermanager/snippets/create_param_version_with_checksum.py new file mode 100644 index 00000000000..ce8386bad8c --- /dev/null +++ b/parametermanager/snippets/create_param_version_with_checksum.py @@ -0,0 +1,84 @@ +#!/usr/bin/env python + +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +""" +command line application and sample code for +creating a new parameter version with a checksum. +""" + +from google.cloud import parametermanager_v1 + + +# [START parametermanager_create_param_version_with_checksum] +def create_param_version_with_checksum( + project_id: str, parameter_id: str, version_id: str +) -> parametermanager_v1.ParameterVersion: + """ + Creates a new version of an existing parameter in the global location of the + specified project, sending a CRC32C checksum of the payload so that the + server can verify the data it received, using the Google Cloud Parameter + Manager SDK. + + Args: + project_id (str): The ID of the project. + parameter_id (str): The ID of the parameter for which the version is to + be created. + version_id (str): The ID of the parameter version to be created. + + Returns: + parametermanager_v1.ParameterVersion: An object representing the newly created + parameter version. + + Example: + create_param_version_with_checksum( + "my-project", + "my-parameter", + "v1" + ) + """ + # Import the necessary library for Google Cloud Parameter Manager. + from google.cloud import parametermanager_v1 + import google_crc32c + + # Create the Parameter Manager client. + client = parametermanager_v1.ParameterManagerClient() + + # Build the resource name of the parameter. + parent = client.parameter_path(project_id, "global", parameter_id) + + # Define the payload and compute its CRC32C checksum (Castagnoli). + payload_data = b"hello world!" + data_crc32c = google_crc32c.value(payload_data) + + # Define the parameter version creation request with the checksum. + request = parametermanager_v1.CreateParameterVersionRequest( + parent=parent, + parameter_version_id=version_id, + parameter_version=parametermanager_v1.ParameterVersion( + payload=parametermanager_v1.ParameterVersionPayload( + data=payload_data, data_crc32c=data_crc32c + ) + ), + ) + + # Create the parameter version. The request fails with a + # CHECKSUM_MISMATCH error if the checksum does not match the payload. + response = client.create_parameter_version(request=request) + + # Print the newly created parameter version name and checksum source. + print(f"Created parameter version: {response.name}") + print(f"Checksum source: {response.checksum_source.name}") + # [END parametermanager_create_param_version_with_checksum] + + return response diff --git a/parametermanager/snippets/create_param_with_tags.py b/parametermanager/snippets/create_param_with_tags.py new file mode 100644 index 00000000000..37500cfb879 --- /dev/null +++ b/parametermanager/snippets/create_param_with_tags.py @@ -0,0 +1,73 @@ +#!/usr/bin/env python + +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +""" +command line application and sample code for +creating a new parameter with tags. +""" + +from google.cloud import parametermanager_v1 + + +# [START parametermanager_create_param_with_tags] +def create_param_with_tags( + project_id: str, parameter_id: str, tag_key: str, tag_value: str +) -> parametermanager_v1.Parameter: + """ + Creates a parameter with a tag in the global location of the specified + project using the Google Cloud Parameter Manager SDK. Tags can only be set + when the parameter is created. They are not returned when the parameter is + retrieved. To view a tag binding, use the Resource Manager API. + + Args: + project_id (str): The ID of the project. + parameter_id (str): The ID to assign to the new parameter. + tag_key (str): The tag key resource name, e.g. tagKeys/1234567890. + tag_value (str): The tag value resource name, e.g. tagValues/1234567890. + + Returns: + parametermanager_v1.Parameter: An object representing the newly created parameter. + + Example: + create_param_with_tags( + "my-project", + "my-parameter", + "tagKeys/1234567890", + "tagValues/1234567890" + ) + """ + # Import the necessary library for Google Cloud Parameter Manager. + from google.cloud import parametermanager_v1 + + # Create the Parameter Manager client. + client = parametermanager_v1.ParameterManagerClient() + + # Build the resource name of the parent project in the global location. + parent = client.common_location_path(project_id, "global") + + # Define the parameter creation request with the tag. + request = parametermanager_v1.CreateParameterRequest( + parent=parent, + parameter_id=parameter_id, + parameter=parametermanager_v1.Parameter(tags={tag_key: tag_value}), + ) + + # Create the parameter. + response = client.create_parameter(request=request) + + # Print the newly created parameter name. + print(f"Created parameter {response.name} with tag {tag_key}: {tag_value}") + # [END parametermanager_create_param_with_tags] + + return response diff --git a/parametermanager/snippets/delete_param_template.py b/parametermanager/snippets/delete_param_template.py new file mode 100644 index 00000000000..b893b9db704 --- /dev/null +++ b/parametermanager/snippets/delete_param_template.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python + +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +""" +command line application and sample code for +deleting a template. +""" + + +# [START parametermanager_delete_param_template] +def delete_param_template(project_id: str, template_id: str) -> None: + """ + Deletes an existing template in the global location of the specified project + using the Google Cloud Parameter Manager SDK. + + Args: + project_id (str): The ID of the project. + template_id (str): The ID of the template to be deleted. + + Returns: + None + + Example: + delete_param_template( + "my-project", + "my-template" + ) + """ + # Import the necessary library for Google Cloud Parameter Manager. + from google.cloud import parametermanager_v1 + + # Create the Parameter Manager client. + client = parametermanager_v1.ParameterManagerClient() + + # Build the resource name of the template. + name = client.template_path(project_id, "global", template_id) + + # Delete the template. + client.delete_template(request={"name": name}) + + # Print confirmation of the deletion. + print(f"Deleted template: {name}") + # [END parametermanager_delete_param_template] diff --git a/parametermanager/snippets/delete_param_template_version.py b/parametermanager/snippets/delete_param_template_version.py new file mode 100644 index 00000000000..de968ef3be2 --- /dev/null +++ b/parametermanager/snippets/delete_param_template_version.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python + +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +""" +command line application and sample code for +deleting a template version. +""" + + +# [START parametermanager_delete_param_template_version] +def delete_param_template_version( + project_id: str, template_id: str, version_id: str +) -> None: + """ + Deletes a specific version of an existing template in the global location of + the specified project using the Google Cloud Parameter Manager SDK. + + Args: + project_id (str): The ID of the project. + template_id (str): The ID of the template whose version is to be + deleted. + version_id (str): The ID of the template version to be deleted. + + Returns: + None + + Example: + delete_param_template_version( + "my-project", + "my-template", + "v1" + ) + """ + # Import the necessary library for Google Cloud Parameter Manager. + from google.cloud import parametermanager_v1 + + # Create the Parameter Manager client. + client = parametermanager_v1.ParameterManagerClient() + + # Build the resource name of the template version. + name = client.template_version_path(project_id, "global", template_id, version_id) + + # Delete the template version. + client.delete_template_version(request={"name": name}) + + # Print confirmation of the deletion. + print(f"Deleted template version: {name}") + # [END parametermanager_delete_param_template_version] diff --git a/parametermanager/snippets/disable_param_template_version.py b/parametermanager/snippets/disable_param_template_version.py new file mode 100644 index 00000000000..8fa62baa56d --- /dev/null +++ b/parametermanager/snippets/disable_param_template_version.py @@ -0,0 +1,75 @@ +#!/usr/bin/env python + +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +""" +command line application and sample code for +disabling the template version. +""" + +from google.cloud import parametermanager_v1 + + +# [START parametermanager_disable_param_template_version] +def disable_param_template_version( + project_id: str, template_id: str, version_id: str +) -> parametermanager_v1.TemplateVersion: + """ + Disables a specific version of an existing template in the global location + of the specified project using the Google Cloud Parameter Manager SDK. + + Args: + project_id (str): The ID of the project. + template_id (str): The ID of the template whose version is to be + disabled. + version_id (str): The ID of the template version to be disabled. + + Returns: + parametermanager_v1.TemplateVersion: An object representing the disabled + template version. + + Example: + disable_param_template_version( + "my-project", + "my-template", + "v1" + ) + """ + # Import the necessary library for Google Cloud Parameter Manager. + from google.cloud import parametermanager_v1 + from google.protobuf import field_mask_pb2 + + # Create the Parameter Manager client. + client = parametermanager_v1.ParameterManagerClient() + + # Build the resource name of the template version. + name = client.template_version_path(project_id, "global", template_id, version_id) + + # Get the current template version and set the disabled field. + template_version = client.get_template_version(request={"name": name}) + template_version.disabled = True + + # Define the update mask for the disabled field. + update_mask = field_mask_pb2.FieldMask(paths=["disabled"]) + + # Update the template version. + request = parametermanager_v1.UpdateTemplateVersionRequest( + template_version=template_version, update_mask=update_mask + ) + response = client.update_template_version(request=request) + + # Print confirmation of the update. + print(f"Disabled template version: {response.name}") + # [END parametermanager_disable_param_template_version] + + return response diff --git a/parametermanager/snippets/enable_param_template_version.py b/parametermanager/snippets/enable_param_template_version.py new file mode 100644 index 00000000000..423b24ee01d --- /dev/null +++ b/parametermanager/snippets/enable_param_template_version.py @@ -0,0 +1,75 @@ +#!/usr/bin/env python + +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +""" +command line application and sample code for +enabling the template version. +""" + +from google.cloud import parametermanager_v1 + + +# [START parametermanager_enable_param_template_version] +def enable_param_template_version( + project_id: str, template_id: str, version_id: str +) -> parametermanager_v1.TemplateVersion: + """ + Enables a specific version of an existing template in the global location of + the specified project using the Google Cloud Parameter Manager SDK. + + Args: + project_id (str): The ID of the project. + template_id (str): The ID of the template whose version is to be + enabled. + version_id (str): The ID of the template version to be enabled. + + Returns: + parametermanager_v1.TemplateVersion: An object representing the enabled + template version. + + Example: + enable_param_template_version( + "my-project", + "my-template", + "v1" + ) + """ + # Import the necessary library for Google Cloud Parameter Manager. + from google.cloud import parametermanager_v1 + from google.protobuf import field_mask_pb2 + + # Create the Parameter Manager client. + client = parametermanager_v1.ParameterManagerClient() + + # Build the resource name of the template version. + name = client.template_version_path(project_id, "global", template_id, version_id) + + # Get the current template version and set the disabled field. + template_version = client.get_template_version(request={"name": name}) + template_version.disabled = False + + # Define the update mask for the disabled field. + update_mask = field_mask_pb2.FieldMask(paths=["disabled"]) + + # Update the template version. + request = parametermanager_v1.UpdateTemplateVersionRequest( + template_version=template_version, update_mask=update_mask + ) + response = client.update_template_version(request=request) + + # Print confirmation of the update. + print(f"Enabled template version: {response.name}") + # [END parametermanager_enable_param_template_version] + + return response diff --git a/parametermanager/snippets/get_param_template.py b/parametermanager/snippets/get_param_template.py new file mode 100644 index 00000000000..d5ea8b88311 --- /dev/null +++ b/parametermanager/snippets/get_param_template.py @@ -0,0 +1,60 @@ +#!/usr/bin/env python + +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +""" +command line application and sample code for +getting a template. +""" + +from google.cloud import parametermanager_v1 + + +# [START parametermanager_get_param_template] +def get_param_template( + project_id: str, template_id: str +) -> parametermanager_v1.Template: + """ + Retrieves the details of an existing template in the global location of the + specified project using the Google Cloud Parameter Manager SDK. + + Args: + project_id (str): The ID of the project. + template_id (str): The ID of the template to be retrieved. + + Returns: + parametermanager_v1.Template: An object representing the template. + + Example: + get_param_template( + "my-project", + "my-template" + ) + """ + # Import the necessary library for Google Cloud Parameter Manager. + from google.cloud import parametermanager_v1 + + # Create the Parameter Manager client. + client = parametermanager_v1.ParameterManagerClient() + + # Build the resource name of the template. + name = client.template_path(project_id, "global", template_id) + + # Get the template. + response = client.get_template(request={"name": name}) + + # Print the template details. + print(f"Got template {response.name} with format {response.format_.name}") + # [END parametermanager_get_param_template] + + return response diff --git a/parametermanager/snippets/get_param_template_version.py b/parametermanager/snippets/get_param_template_version.py new file mode 100644 index 00000000000..de6f12174bb --- /dev/null +++ b/parametermanager/snippets/get_param_template_version.py @@ -0,0 +1,65 @@ +#!/usr/bin/env python + +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +""" +command line application and sample code for +getting a template version. +""" + +from google.cloud import parametermanager_v1 + + +# [START parametermanager_get_param_template_version] +def get_param_template_version( + project_id: str, template_id: str, version_id: str +) -> parametermanager_v1.TemplateVersion: + """ + Retrieves the details of a specific version of an existing template in the + global location of the specified project using the Google Cloud Parameter + Manager SDK. + + Args: + project_id (str): The ID of the project. + template_id (str): The ID of the template for which the version details + are to be retrieved. + version_id (str): The ID of the template version to be retrieved. + + Returns: + parametermanager_v1.TemplateVersion: An object representing the template version. + + Example: + get_param_template_version( + "my-project", + "my-template", + "v1" + ) + """ + # Import the necessary library for Google Cloud Parameter Manager. + from google.cloud import parametermanager_v1 + + # Create the Parameter Manager client. + client = parametermanager_v1.ParameterManagerClient() + + # Build the resource name of the template version. + name = client.template_version_path(project_id, "global", template_id, version_id) + + # Get the template version. + response = client.get_template_version(request={"name": name}) + + # Print the template version details. + print(f"Got template version {response.name}") + print(f"Payload: {response.payload.data.decode('utf-8')}") + # [END parametermanager_get_param_template_version] + + return response diff --git a/parametermanager/snippets/get_param_version_verify_checksum.py b/parametermanager/snippets/get_param_version_verify_checksum.py new file mode 100644 index 00000000000..ff48eff13bf --- /dev/null +++ b/parametermanager/snippets/get_param_version_verify_checksum.py @@ -0,0 +1,75 @@ +#!/usr/bin/env python + +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +""" +command line application and sample code for +getting a parameter version and verifying its checksum. +""" + +from google.cloud import parametermanager_v1 + + +# [START parametermanager_get_param_version_verify_checksum] +def get_param_version_verify_checksum( + project_id: str, parameter_id: str, version_id: str +) -> parametermanager_v1.ParameterVersion: + """ + Retrieves a specific version of an existing parameter in the global location + of the specified project and verifies the returned payload against the + returned CRC32C checksum using the Google Cloud Parameter Manager SDK. + + Args: + project_id (str): The ID of the project. + parameter_id (str): The ID of the parameter for which the version + details are to be retrieved. + version_id (str): The ID of the parameter version to be retrieved. + + Returns: + parametermanager_v1.ParameterVersion: An object representing the parameter version. + + Example: + get_param_version_verify_checksum( + "my-project", + "my-parameter", + "v1" + ) + """ + # Import the necessary library for Google Cloud Parameter Manager. + from google.cloud import parametermanager_v1 + import google_crc32c + + # Create the Parameter Manager client. + client = parametermanager_v1.ParameterManagerClient() + + # Build the resource name of the parameter version. + name = client.parameter_version_path(project_id, "global", parameter_id, version_id) + + # Get the parameter version with the payload and checksum (full view). + request = parametermanager_v1.GetParameterVersionRequest( + name=name, view=parametermanager_v1.View.FULL + ) + response = client.get_parameter_version(request=request) + + # Recompute the CRC32C checksum of the payload and compare it with the + # checksum returned by the server. + computed_crc32c = google_crc32c.value(response.payload.data) + if computed_crc32c != response.payload.data_crc32c: + raise ValueError(f"Checksum mismatch for {response.name}: data corrupted") + + # Print the verification result and the checksum source. + print(f"Verified checksum for parameter version: {response.name}") + print(f"Checksum source: {response.checksum_source.name}") + # [END parametermanager_get_param_version_verify_checksum] + + return response diff --git a/parametermanager/snippets/list_param_template_versions.py b/parametermanager/snippets/list_param_template_versions.py new file mode 100644 index 00000000000..3ae3771cc0b --- /dev/null +++ b/parametermanager/snippets/list_param_template_versions.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python + +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +""" +command line application and sample code for +listing the template versions. +""" + + +# [START parametermanager_list_param_template_versions] +def list_param_template_versions(project_id: str, template_id: str) -> None: + """ + Lists all versions of an existing template in the global location of the + specified project using the Google Cloud Parameter Manager SDK. + + Args: + project_id (str): The ID of the project. + template_id (str): The ID of the template for which versions are to be + listed. + + Returns: + None + + Example: + list_param_template_versions( + "my-project", + "my-template" + ) + """ + # Import the necessary library for Google Cloud Parameter Manager. + from google.cloud import parametermanager_v1 + + # Create the Parameter Manager client. + client = parametermanager_v1.ParameterManagerClient() + + # Build the resource name of the template. + parent = client.template_path(project_id, "global", template_id) + + # List the versions of the template. + for version in client.list_template_versions(parent=parent): + print(f"Found template version: {version.name}") + # [END parametermanager_list_param_template_versions] diff --git a/parametermanager/snippets/list_param_templates.py b/parametermanager/snippets/list_param_templates.py new file mode 100644 index 00000000000..adf9808c307 --- /dev/null +++ b/parametermanager/snippets/list_param_templates.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python + +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +""" +command line application and sample code for +listing templates. +""" + + +# [START parametermanager_list_param_templates] +def list_param_templates(project_id: str) -> None: + """ + Lists all templates in the global location of the specified project using + the Google Cloud Parameter Manager SDK. + + Args: + project_id (str): The ID of the project. + + Returns: + None + + Example: + list_param_templates( + "my-project" + ) + """ + # Import the necessary library for Google Cloud Parameter Manager. + from google.cloud import parametermanager_v1 + + # Create the Parameter Manager client. + client = parametermanager_v1.ParameterManagerClient() + + # Build the resource name of the parent project in the global location. + parent = client.common_location_path(project_id, "global") + + # List all templates in the parent project and location. + for template in client.list_templates(parent=parent): + print(f"Found template {template.name} with format {template.format_.name}") + # [END parametermanager_list_param_templates] diff --git a/parametermanager/snippets/regional_samples/create_regional_param_template.py b/parametermanager/snippets/regional_samples/create_regional_param_template.py new file mode 100644 index 00000000000..009da0eadd0 --- /dev/null +++ b/parametermanager/snippets/regional_samples/create_regional_param_template.py @@ -0,0 +1,80 @@ +#!/usr/bin/env python + +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +""" +command line application and sample code for +creating a new template. +""" + +from google.cloud import parametermanager_v1 + + +# [START parametermanager_create_regional_param_template] +def create_regional_param_template( + project_id: str, + location_id: str, + template_id: str, + format_type: parametermanager_v1.TemplateFormat, +) -> parametermanager_v1.Template: + """ + Creates a template in the specified region of the specified project with the + specified format using the Google Cloud Parameter Manager SDK. + + Args: + project_id (str): The ID of the project. + location_id (str): The region where the resources are located. + template_id (str): The ID to assign to the new template. + format_type (parametermanager_v1.TemplateFormat): The format of the + template (TEMPLATE_FORMAT_YAML or TEMPLATE_FORMAT_JSON). + + Returns: + parametermanager_v1.Template: An object representing the newly created template. + + Example: + create_regional_param_template( + "my-project", + "us-central1", + "my-template", + parametermanager_v1.TemplateFormat.TEMPLATE_FORMAT_JSON + ) + """ + # Import the necessary library for Google Cloud Parameter Manager. + from google.cloud import parametermanager_v1 + + # Create the Parameter Manager client with the regional endpoint. + api_endpoint = f"parametermanager.{location_id}.rep.googleapis.com" + client = parametermanager_v1.ParameterManagerClient( + client_options={"api_endpoint": api_endpoint} + ) + + # Build the resource name of the parent project in the specified region. + parent = client.common_location_path(project_id, location_id) + + # Define the template creation request with the specified format. + request = parametermanager_v1.CreateTemplateRequest( + parent=parent, + template_id=template_id, + template=parametermanager_v1.Template(format_=format_type), + ) + + # Create the template. + response = client.create_template(request=request) + + # Print the newly created template name. + print( + f"Created regional template {response.name} with format {response.format_.name}" + ) + # [END parametermanager_create_regional_param_template] + + return response diff --git a/parametermanager/snippets/regional_samples/create_regional_param_template_version.py b/parametermanager/snippets/regional_samples/create_regional_param_template_version.py new file mode 100644 index 00000000000..8e0fdd926e2 --- /dev/null +++ b/parametermanager/snippets/regional_samples/create_regional_param_template_version.py @@ -0,0 +1,87 @@ +#!/usr/bin/env python + +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +""" +command line application and sample code for +creating a new template version. +""" + +from google.cloud import parametermanager_v1 + + +# [START parametermanager_create_regional_param_template_version] +def create_regional_param_template_version( + project_id: str, location_id: str, template_id: str, version_id: str +) -> parametermanager_v1.TemplateVersion: + """ + Creates a new version of an existing template in the specified region of the + specified project using the Google Cloud Parameter Manager SDK. The payload + is a JSON document with a placeholder, {{.variableName}}, for each value + that is resolved from a parameter version when the template version is + rendered. + + Args: + project_id (str): The ID of the project. + location_id (str): The region where the resources are located. + template_id (str): The ID of the template for which the version is to be + created. + version_id (str): The ID of the template version to be created. + + Returns: + parametermanager_v1.TemplateVersion: An object representing the newly created + template version. + + Example: + create_regional_param_template_version( + "my-project", + "us-central1", + "my-template", + "v1" + ) + """ + # Import the necessary library for Google Cloud Parameter Manager. + from google.cloud import parametermanager_v1 + import json + + # Create the Parameter Manager client with the regional endpoint. + api_endpoint = f"parametermanager.{location_id}.rep.googleapis.com" + client = parametermanager_v1.ParameterManagerClient( + client_options={"api_endpoint": api_endpoint} + ) + + # Build the resource name of the template. + parent = client.template_path(project_id, location_id, template_id) + + # Define the template payload. Each {{.variableName}} placeholder is + # replaced with the matching key of the parameter version at render time. + payload_data = {"username": "{{.username}}", "host": "{{.host}}"} + payload = parametermanager_v1.TemplateVersionPayload( + data=json.dumps(payload_data).encode("utf-8") + ) + + # Define the template version creation request. + request = parametermanager_v1.CreateTemplateVersionRequest( + parent=parent, + template_version_id=version_id, + template_version=parametermanager_v1.TemplateVersion(payload=payload), + ) + + # Create the template version. + response = client.create_template_version(request=request) + + # Print the newly created template version name. + print(f"Created regional template version: {response.name}") + # [END parametermanager_create_regional_param_template_version] + + return response diff --git a/parametermanager/snippets/regional_samples/create_regional_param_version_with_checksum.py b/parametermanager/snippets/regional_samples/create_regional_param_version_with_checksum.py new file mode 100644 index 00000000000..56fd104a9ae --- /dev/null +++ b/parametermanager/snippets/regional_samples/create_regional_param_version_with_checksum.py @@ -0,0 +1,89 @@ +#!/usr/bin/env python + +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +""" +command line application and sample code for +creating a new parameter version with a checksum. +""" + +from google.cloud import parametermanager_v1 + + +# [START parametermanager_create_regional_param_version_with_checksum] +def create_regional_param_version_with_checksum( + project_id: str, location_id: str, parameter_id: str, version_id: str +) -> parametermanager_v1.ParameterVersion: + """ + Creates a new version of an existing parameter in the specified region of + the specified project, sending a CRC32C checksum of the payload so that the + server can verify the data it received, using the Google Cloud Parameter + Manager SDK. + + Args: + project_id (str): The ID of the project. + location_id (str): The region where the resources are located. + parameter_id (str): The ID of the parameter for which the version is to + be created. + version_id (str): The ID of the parameter version to be created. + + Returns: + parametermanager_v1.ParameterVersion: An object representing the newly created + parameter version. + + Example: + create_regional_param_version_with_checksum( + "my-project", + "us-central1", + "my-parameter", + "v1" + ) + """ + # Import the necessary library for Google Cloud Parameter Manager. + from google.cloud import parametermanager_v1 + import google_crc32c + + # Create the Parameter Manager client with the regional endpoint. + api_endpoint = f"parametermanager.{location_id}.rep.googleapis.com" + client = parametermanager_v1.ParameterManagerClient( + client_options={"api_endpoint": api_endpoint} + ) + + # Build the resource name of the parameter. + parent = client.parameter_path(project_id, location_id, parameter_id) + + # Define the payload and compute its CRC32C checksum (Castagnoli). + payload_data = b"hello world!" + data_crc32c = google_crc32c.value(payload_data) + + # Define the parameter version creation request with the checksum. + request = parametermanager_v1.CreateParameterVersionRequest( + parent=parent, + parameter_version_id=version_id, + parameter_version=parametermanager_v1.ParameterVersion( + payload=parametermanager_v1.ParameterVersionPayload( + data=payload_data, data_crc32c=data_crc32c + ) + ), + ) + + # Create the parameter version. The request fails with a + # CHECKSUM_MISMATCH error if the checksum does not match the payload. + response = client.create_parameter_version(request=request) + + # Print the newly created parameter version name and checksum source. + print(f"Created regional parameter version: {response.name}") + print(f"Checksum source: {response.checksum_source.name}") + # [END parametermanager_create_regional_param_version_with_checksum] + + return response diff --git a/parametermanager/snippets/regional_samples/create_regional_param_with_tags.py b/parametermanager/snippets/regional_samples/create_regional_param_with_tags.py new file mode 100644 index 00000000000..c15f51f6e8f --- /dev/null +++ b/parametermanager/snippets/regional_samples/create_regional_param_with_tags.py @@ -0,0 +1,78 @@ +#!/usr/bin/env python + +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +""" +command line application and sample code for +creating a new parameter with tags. +""" + +from google.cloud import parametermanager_v1 + + +# [START parametermanager_create_regional_param_with_tags] +def create_regional_param_with_tags( + project_id: str, location_id: str, parameter_id: str, tag_key: str, tag_value: str +) -> parametermanager_v1.Parameter: + """ + Creates a parameter with a tag in the specified region of the specified + project using the Google Cloud Parameter Manager SDK. Tags can only be set + when the parameter is created. They are not returned when the parameter is + retrieved. To view a tag binding, use the Resource Manager API. + + Args: + project_id (str): The ID of the project. + location_id (str): The region where the resources are located. + parameter_id (str): The ID to assign to the new parameter. + tag_key (str): The tag key resource name, e.g. tagKeys/1234567890. + tag_value (str): The tag value resource name, e.g. tagValues/1234567890. + + Returns: + parametermanager_v1.Parameter: An object representing the newly created parameter. + + Example: + create_regional_param_with_tags( + "my-project", + "us-central1", + "my-parameter", + "tagKeys/1234567890", + "tagValues/1234567890" + ) + """ + # Import the necessary library for Google Cloud Parameter Manager. + from google.cloud import parametermanager_v1 + + # Create the Parameter Manager client with the regional endpoint. + api_endpoint = f"parametermanager.{location_id}.rep.googleapis.com" + client = parametermanager_v1.ParameterManagerClient( + client_options={"api_endpoint": api_endpoint} + ) + + # Build the resource name of the parent project in the specified region. + parent = client.common_location_path(project_id, location_id) + + # Define the parameter creation request with the tag. + request = parametermanager_v1.CreateParameterRequest( + parent=parent, + parameter_id=parameter_id, + parameter=parametermanager_v1.Parameter(tags={tag_key: tag_value}), + ) + + # Create the parameter. + response = client.create_parameter(request=request) + + # Print the newly created parameter name. + print(f"Created regional parameter {response.name} with tag {tag_key}: {tag_value}") + # [END parametermanager_create_regional_param_with_tags] + + return response diff --git a/parametermanager/snippets/regional_samples/delete_regional_param_template.py b/parametermanager/snippets/regional_samples/delete_regional_param_template.py new file mode 100644 index 00000000000..95f0d5b8e69 --- /dev/null +++ b/parametermanager/snippets/regional_samples/delete_regional_param_template.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python + +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +""" +command line application and sample code for +deleting a template. +""" + + +# [START parametermanager_delete_regional_param_template] +def delete_regional_param_template( + project_id: str, location_id: str, template_id: str +) -> None: + """ + Deletes an existing template in the specified region of the specified + project using the Google Cloud Parameter Manager SDK. + + Args: + project_id (str): The ID of the project. + location_id (str): The region where the resources are located. + template_id (str): The ID of the template to be deleted. + + Returns: + None + + Example: + delete_regional_param_template( + "my-project", + "us-central1", + "my-template" + ) + """ + # Import the necessary library for Google Cloud Parameter Manager. + from google.cloud import parametermanager_v1 + + # Create the Parameter Manager client with the regional endpoint. + api_endpoint = f"parametermanager.{location_id}.rep.googleapis.com" + client = parametermanager_v1.ParameterManagerClient( + client_options={"api_endpoint": api_endpoint} + ) + + # Build the resource name of the template. + name = client.template_path(project_id, location_id, template_id) + + # Delete the template. + client.delete_template(request={"name": name}) + + # Print confirmation of the deletion. + print(f"Deleted regional template: {name}") + # [END parametermanager_delete_regional_param_template] diff --git a/parametermanager/snippets/regional_samples/delete_regional_param_template_version.py b/parametermanager/snippets/regional_samples/delete_regional_param_template_version.py new file mode 100644 index 00000000000..ad55601ee32 --- /dev/null +++ b/parametermanager/snippets/regional_samples/delete_regional_param_template_version.py @@ -0,0 +1,66 @@ +#!/usr/bin/env python + +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +""" +command line application and sample code for +deleting a template version. +""" + + +# [START parametermanager_delete_regional_param_template_version] +def delete_regional_param_template_version( + project_id: str, location_id: str, template_id: str, version_id: str +) -> None: + """ + Deletes a specific version of an existing template in the specified region + of the specified project using the Google Cloud Parameter Manager SDK. + + Args: + project_id (str): The ID of the project. + location_id (str): The region where the resources are located. + template_id (str): The ID of the template whose version is to be + deleted. + version_id (str): The ID of the template version to be deleted. + + Returns: + None + + Example: + delete_regional_param_template_version( + "my-project", + "us-central1", + "my-template", + "v1" + ) + """ + # Import the necessary library for Google Cloud Parameter Manager. + from google.cloud import parametermanager_v1 + + # Create the Parameter Manager client with the regional endpoint. + api_endpoint = f"parametermanager.{location_id}.rep.googleapis.com" + client = parametermanager_v1.ParameterManagerClient( + client_options={"api_endpoint": api_endpoint} + ) + + # Build the resource name of the template version. + name = client.template_version_path( + project_id, location_id, template_id, version_id + ) + + # Delete the template version. + client.delete_template_version(request={"name": name}) + + # Print confirmation of the deletion. + print(f"Deleted regional template version: {name}") + # [END parametermanager_delete_regional_param_template_version] diff --git a/parametermanager/snippets/regional_samples/disable_regional_param_template_version.py b/parametermanager/snippets/regional_samples/disable_regional_param_template_version.py new file mode 100644 index 00000000000..a0d141a1ebe --- /dev/null +++ b/parametermanager/snippets/regional_samples/disable_regional_param_template_version.py @@ -0,0 +1,82 @@ +#!/usr/bin/env python + +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +""" +command line application and sample code for +disabling the template version. +""" + +from google.cloud import parametermanager_v1 + + +# [START parametermanager_disable_regional_param_template_version] +def disable_regional_param_template_version( + project_id: str, location_id: str, template_id: str, version_id: str +) -> parametermanager_v1.TemplateVersion: + """ + Disables a specific version of an existing template in the specified region + of the specified project using the Google Cloud Parameter Manager SDK. + + Args: + project_id (str): The ID of the project. + location_id (str): The region where the resources are located. + template_id (str): The ID of the template whose version is to be + disabled. + version_id (str): The ID of the template version to be disabled. + + Returns: + parametermanager_v1.TemplateVersion: An object representing the disabled + template version. + + Example: + disable_regional_param_template_version( + "my-project", + "us-central1", + "my-template", + "v1" + ) + """ + # Import the necessary library for Google Cloud Parameter Manager. + from google.cloud import parametermanager_v1 + from google.protobuf import field_mask_pb2 + + # Create the Parameter Manager client with the regional endpoint. + api_endpoint = f"parametermanager.{location_id}.rep.googleapis.com" + client = parametermanager_v1.ParameterManagerClient( + client_options={"api_endpoint": api_endpoint} + ) + + # Build the resource name of the template version. + name = client.template_version_path( + project_id, location_id, template_id, version_id + ) + + # Get the current template version and set the disabled field. + template_version = client.get_template_version(request={"name": name}) + template_version.disabled = True + + # Define the update mask for the disabled field. + update_mask = field_mask_pb2.FieldMask(paths=["disabled"]) + + # Update the template version. + request = parametermanager_v1.UpdateTemplateVersionRequest( + template_version=template_version, update_mask=update_mask + ) + response = client.update_template_version(request=request) + + # Print confirmation of the update. + print(f"Disabled regional template version: {response.name}") + # [END parametermanager_disable_regional_param_template_version] + + return response diff --git a/parametermanager/snippets/regional_samples/enable_regional_param_template_version.py b/parametermanager/snippets/regional_samples/enable_regional_param_template_version.py new file mode 100644 index 00000000000..ab40e12c2f6 --- /dev/null +++ b/parametermanager/snippets/regional_samples/enable_regional_param_template_version.py @@ -0,0 +1,82 @@ +#!/usr/bin/env python + +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +""" +command line application and sample code for +enabling the template version. +""" + +from google.cloud import parametermanager_v1 + + +# [START parametermanager_enable_regional_param_template_version] +def enable_regional_param_template_version( + project_id: str, location_id: str, template_id: str, version_id: str +) -> parametermanager_v1.TemplateVersion: + """ + Enables a specific version of an existing template in the specified region + of the specified project using the Google Cloud Parameter Manager SDK. + + Args: + project_id (str): The ID of the project. + location_id (str): The region where the resources are located. + template_id (str): The ID of the template whose version is to be + enabled. + version_id (str): The ID of the template version to be enabled. + + Returns: + parametermanager_v1.TemplateVersion: An object representing the enabled + template version. + + Example: + enable_regional_param_template_version( + "my-project", + "us-central1", + "my-template", + "v1" + ) + """ + # Import the necessary library for Google Cloud Parameter Manager. + from google.cloud import parametermanager_v1 + from google.protobuf import field_mask_pb2 + + # Create the Parameter Manager client with the regional endpoint. + api_endpoint = f"parametermanager.{location_id}.rep.googleapis.com" + client = parametermanager_v1.ParameterManagerClient( + client_options={"api_endpoint": api_endpoint} + ) + + # Build the resource name of the template version. + name = client.template_version_path( + project_id, location_id, template_id, version_id + ) + + # Get the current template version and set the disabled field. + template_version = client.get_template_version(request={"name": name}) + template_version.disabled = False + + # Define the update mask for the disabled field. + update_mask = field_mask_pb2.FieldMask(paths=["disabled"]) + + # Update the template version. + request = parametermanager_v1.UpdateTemplateVersionRequest( + template_version=template_version, update_mask=update_mask + ) + response = client.update_template_version(request=request) + + # Print confirmation of the update. + print(f"Enabled regional template version: {response.name}") + # [END parametermanager_enable_regional_param_template_version] + + return response diff --git a/parametermanager/snippets/regional_samples/get_regional_param_template.py b/parametermanager/snippets/regional_samples/get_regional_param_template.py new file mode 100644 index 00000000000..738c45d737f --- /dev/null +++ b/parametermanager/snippets/regional_samples/get_regional_param_template.py @@ -0,0 +1,65 @@ +#!/usr/bin/env python + +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +""" +command line application and sample code for +getting a template. +""" + +from google.cloud import parametermanager_v1 + + +# [START parametermanager_get_regional_param_template] +def get_regional_param_template( + project_id: str, location_id: str, template_id: str +) -> parametermanager_v1.Template: + """ + Retrieves the details of an existing template in the specified region of the + specified project using the Google Cloud Parameter Manager SDK. + + Args: + project_id (str): The ID of the project. + location_id (str): The region where the resources are located. + template_id (str): The ID of the template to be retrieved. + + Returns: + parametermanager_v1.Template: An object representing the template. + + Example: + get_regional_param_template( + "my-project", + "us-central1", + "my-template" + ) + """ + # Import the necessary library for Google Cloud Parameter Manager. + from google.cloud import parametermanager_v1 + + # Create the Parameter Manager client with the regional endpoint. + api_endpoint = f"parametermanager.{location_id}.rep.googleapis.com" + client = parametermanager_v1.ParameterManagerClient( + client_options={"api_endpoint": api_endpoint} + ) + + # Build the resource name of the template. + name = client.template_path(project_id, location_id, template_id) + + # Get the template. + response = client.get_template(request={"name": name}) + + # Print the template details. + print(f"Got regional template {response.name} with format {response.format_.name}") + # [END parametermanager_get_regional_param_template] + + return response diff --git a/parametermanager/snippets/regional_samples/get_regional_param_template_version.py b/parametermanager/snippets/regional_samples/get_regional_param_template_version.py new file mode 100644 index 00000000000..27edcef1293 --- /dev/null +++ b/parametermanager/snippets/regional_samples/get_regional_param_template_version.py @@ -0,0 +1,72 @@ +#!/usr/bin/env python + +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +""" +command line application and sample code for +getting a template version. +""" + +from google.cloud import parametermanager_v1 + + +# [START parametermanager_get_regional_param_template_version] +def get_regional_param_template_version( + project_id: str, location_id: str, template_id: str, version_id: str +) -> parametermanager_v1.TemplateVersion: + """ + Retrieves the details of a specific version of an existing template in the + specified region of the specified project using the Google Cloud Parameter + Manager SDK. + + Args: + project_id (str): The ID of the project. + location_id (str): The region where the resources are located. + template_id (str): The ID of the template for which the version details + are to be retrieved. + version_id (str): The ID of the template version to be retrieved. + + Returns: + parametermanager_v1.TemplateVersion: An object representing the template version. + + Example: + get_regional_param_template_version( + "my-project", + "us-central1", + "my-template", + "v1" + ) + """ + # Import the necessary library for Google Cloud Parameter Manager. + from google.cloud import parametermanager_v1 + + # Create the Parameter Manager client with the regional endpoint. + api_endpoint = f"parametermanager.{location_id}.rep.googleapis.com" + client = parametermanager_v1.ParameterManagerClient( + client_options={"api_endpoint": api_endpoint} + ) + + # Build the resource name of the template version. + name = client.template_version_path( + project_id, location_id, template_id, version_id + ) + + # Get the template version. + response = client.get_template_version(request={"name": name}) + + # Print the template version details. + print(f"Got regional template version {response.name}") + print(f"Payload: {response.payload.data.decode('utf-8')}") + # [END parametermanager_get_regional_param_template_version] + + return response diff --git a/parametermanager/snippets/regional_samples/get_regional_param_version_verify_checksum.py b/parametermanager/snippets/regional_samples/get_regional_param_version_verify_checksum.py new file mode 100644 index 00000000000..96c9babd5ba --- /dev/null +++ b/parametermanager/snippets/regional_samples/get_regional_param_version_verify_checksum.py @@ -0,0 +1,82 @@ +#!/usr/bin/env python + +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +""" +command line application and sample code for +getting a parameter version and verifying its checksum. +""" + +from google.cloud import parametermanager_v1 + + +# [START parametermanager_get_regional_param_version_verify_checksum] +def get_regional_param_version_verify_checksum( + project_id: str, location_id: str, parameter_id: str, version_id: str +) -> parametermanager_v1.ParameterVersion: + """ + Retrieves a specific version of an existing parameter in the specified + region of the specified project and verifies the returned payload against + the returned CRC32C checksum using the Google Cloud Parameter Manager SDK. + + Args: + project_id (str): The ID of the project. + location_id (str): The region where the resources are located. + parameter_id (str): The ID of the parameter for which the version + details are to be retrieved. + version_id (str): The ID of the parameter version to be retrieved. + + Returns: + parametermanager_v1.ParameterVersion: An object representing the parameter version. + + Example: + get_regional_param_version_verify_checksum( + "my-project", + "us-central1", + "my-parameter", + "v1" + ) + """ + # Import the necessary library for Google Cloud Parameter Manager. + from google.cloud import parametermanager_v1 + import google_crc32c + + # Create the Parameter Manager client with the regional endpoint. + api_endpoint = f"parametermanager.{location_id}.rep.googleapis.com" + client = parametermanager_v1.ParameterManagerClient( + client_options={"api_endpoint": api_endpoint} + ) + + # Build the resource name of the parameter version. + name = client.parameter_version_path( + project_id, location_id, parameter_id, version_id + ) + + # Get the parameter version with the payload and checksum (full view). + request = parametermanager_v1.GetParameterVersionRequest( + name=name, view=parametermanager_v1.View.FULL + ) + response = client.get_parameter_version(request=request) + + # Recompute the CRC32C checksum of the payload and compare it with the + # checksum returned by the server. + computed_crc32c = google_crc32c.value(response.payload.data) + if computed_crc32c != response.payload.data_crc32c: + raise ValueError(f"Checksum mismatch for {response.name}: data corrupted") + + # Print the verification result and the checksum source. + print(f"Verified checksum for regional parameter version: {response.name}") + print(f"Checksum source: {response.checksum_source.name}") + # [END parametermanager_get_regional_param_version_verify_checksum] + + return response diff --git a/parametermanager/snippets/regional_samples/list_regional_param_template_versions.py b/parametermanager/snippets/regional_samples/list_regional_param_template_versions.py new file mode 100644 index 00000000000..9a2887bece5 --- /dev/null +++ b/parametermanager/snippets/regional_samples/list_regional_param_template_versions.py @@ -0,0 +1,60 @@ +#!/usr/bin/env python + +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +""" +command line application and sample code for +listing the template versions. +""" + + +# [START parametermanager_list_regional_param_template_versions] +def list_regional_param_template_versions( + project_id: str, location_id: str, template_id: str +) -> None: + """ + Lists all versions of an existing template in the specified region of the + specified project using the Google Cloud Parameter Manager SDK. + + Args: + project_id (str): The ID of the project. + location_id (str): The region where the resources are located. + template_id (str): The ID of the template for which versions are to be + listed. + + Returns: + None + + Example: + list_regional_param_template_versions( + "my-project", + "us-central1", + "my-template" + ) + """ + # Import the necessary library for Google Cloud Parameter Manager. + from google.cloud import parametermanager_v1 + + # Create the Parameter Manager client with the regional endpoint. + api_endpoint = f"parametermanager.{location_id}.rep.googleapis.com" + client = parametermanager_v1.ParameterManagerClient( + client_options={"api_endpoint": api_endpoint} + ) + + # Build the resource name of the template. + parent = client.template_path(project_id, location_id, template_id) + + # List the versions of the template. + for version in client.list_template_versions(parent=parent): + print(f"Found regional template version: {version.name}") + # [END parametermanager_list_regional_param_template_versions] diff --git a/parametermanager/snippets/regional_samples/list_regional_param_templates.py b/parametermanager/snippets/regional_samples/list_regional_param_templates.py new file mode 100644 index 00000000000..bbba1988e80 --- /dev/null +++ b/parametermanager/snippets/regional_samples/list_regional_param_templates.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python + +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +""" +command line application and sample code for +listing templates. +""" + + +# [START parametermanager_list_regional_param_templates] +def list_regional_param_templates(project_id: str, location_id: str) -> None: + """ + Lists all templates in the specified region of the specified project using + the Google Cloud Parameter Manager SDK. + + Args: + project_id (str): The ID of the project. + location_id (str): The region where the resources are located. + + Returns: + None + + Example: + list_regional_param_templates( + "my-project", + "us-central1" + ) + """ + # Import the necessary library for Google Cloud Parameter Manager. + from google.cloud import parametermanager_v1 + + # Create the Parameter Manager client with the regional endpoint. + api_endpoint = f"parametermanager.{location_id}.rep.googleapis.com" + client = parametermanager_v1.ParameterManagerClient( + client_options={"api_endpoint": api_endpoint} + ) + + # Build the resource name of the parent project in the specified region. + parent = client.common_location_path(project_id, location_id) + + # List all templates in the parent project and location. + for template in client.list_templates(parent=parent): + print( + f"Found regional template {template.name} with format {template.format_.name}" + ) + # [END parametermanager_list_regional_param_templates] diff --git a/parametermanager/snippets/regional_samples/regional_template_quickstart.py b/parametermanager/snippets/regional_samples/regional_template_quickstart.py new file mode 100644 index 00000000000..45656d27a9d --- /dev/null +++ b/parametermanager/snippets/regional_samples/regional_template_quickstart.py @@ -0,0 +1,128 @@ +#!/usr/bin/env python + +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +""" +command line application and sample code for +quickstart with regional parameter manager templates. +""" + + +# [START parametermanager_regional_template_quickstart] +def regional_template_quickstart( + project_id: str, + location_id: str, + template_id: str, + parameter_id: str, + version_id: str, +) -> None: + """ + Quickstart example for using Google Cloud Parameter Manager templates to + create a regional template and a template version with placeholders, create a + parameter version that holds the values, and render the template version. + + Args: + project_id (str): The ID of the project. + location_id (str): The region where the resources are created. + template_id (str): The ID to assign to the new template. + parameter_id (str): The ID to assign to the new parameter. + version_id (str): The ID used for both the template version and + the parameter version. + + Returns: + None + + Example: + regional_template_quickstart( + "my-project", + "us-central1", + "my-template", + "my-parameter", + "v1" + ) + """ + # Import the necessary libraries. + from google.cloud import parametermanager_v1 + import json + + # Create the Parameter Manager client with the regional endpoint. + api_endpoint = f"parametermanager.{location_id}.rep.googleapis.com" + client = parametermanager_v1.ParameterManagerClient( + client_options={"api_endpoint": api_endpoint} + ) + + # Build the resource name of the parent project in the specified region. + parent = client.common_location_path(project_id, location_id) + + # Create a template in JSON format. + template = client.create_template( + request=parametermanager_v1.CreateTemplateRequest( + parent=parent, + template_id=template_id, + template=parametermanager_v1.Template( + format_=parametermanager_v1.TemplateFormat.TEMPLATE_FORMAT_JSON + ), + ) + ) + print(f"Created regional template: {template.name}") + + # Create a template version with {{.variableName}} placeholders. + template_payload = {"username": "{{.username}}", "host": "{{.host}}"} + template_version = client.create_template_version( + request=parametermanager_v1.CreateTemplateVersionRequest( + parent=template.name, + template_version_id=version_id, + template_version=parametermanager_v1.TemplateVersion( + payload=parametermanager_v1.TemplateVersionPayload( + data=json.dumps(template_payload).encode("utf-8") + ) + ), + ) + ) + print(f"Created regional template version: {template_version.name}") + + # Create a JSON parameter. + parameter = client.create_parameter( + request=parametermanager_v1.CreateParameterRequest( + parent=parent, + parameter_id=parameter_id, + parameter=parametermanager_v1.Parameter( + format_=parametermanager_v1.ParameterFormat.JSON + ), + ) + ) + print(f"Created regional parameter: {parameter.name}") + + # Create a parameter version that holds the values for the placeholders. + values = {"username": "test-user", "host": "localhost"} + parameter_version = client.create_parameter_version( + request=parametermanager_v1.CreateParameterVersionRequest( + parent=parameter.name, + parameter_version_id=version_id, + parameter_version=parametermanager_v1.ParameterVersion( + payload=parametermanager_v1.ParameterVersionPayload( + data=json.dumps(values).encode("utf-8") + ) + ), + ) + ) + print(f"Created regional parameter version: {parameter_version.name}") + + # Render the template version with the parameter version's values. + response = client.render_template_version( + request=parametermanager_v1.RenderTemplateVersionRequest( + name=template_version.name, parameter_version=parameter_version.name + ) + ) + print(f"Rendered regional payload: {response.rendered_payload.decode('utf-8')}") + # [END parametermanager_regional_template_quickstart] diff --git a/parametermanager/snippets/regional_samples/render_regional_param_template_version.py b/parametermanager/snippets/regional_samples/render_regional_param_template_version.py new file mode 100644 index 00000000000..41eedd7eaf9 --- /dev/null +++ b/parametermanager/snippets/regional_samples/render_regional_param_template_version.py @@ -0,0 +1,93 @@ +#!/usr/bin/env python + +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +""" +command line application and sample code for +rendering a template version. +""" + +from google.cloud import parametermanager_v1 + + +# [START parametermanager_render_regional_param_template_version] +def render_regional_param_template_version( + project_id: str, + location_id: str, + template_id: str, + version_id: str, + parameter_id: str, + parameter_version_id: str, +) -> parametermanager_v1.RenderTemplateVersionResponse: + """ + Renders a version of an existing template in the specified region of the + specified project, resolving its placeholders with the values of a parameter + version, using the Google Cloud Parameter Manager SDK. + + Args: + project_id (str): The ID of the project. + location_id (str): The region where the resources are located. + template_id (str): The ID of the template to be rendered. + version_id (str): The ID of the template version to be rendered. + parameter_id (str): The ID of the parameter whose version supplies the + values. + parameter_version_id (str): The ID of the parameter version that + supplies the values. + + Returns: + parametermanager_v1.RenderTemplateVersionResponse: An object representing the rendered + template version. + + Example: + render_regional_param_template_version( + "my-project", + "us-central1", + "my-template", + "v1", + "my-parameter", + "v1" + ) + """ + # Import the necessary library for Google Cloud Parameter Manager. + from google.cloud import parametermanager_v1 + + # Create the Parameter Manager client with the regional endpoint. + api_endpoint = f"parametermanager.{location_id}.rep.googleapis.com" + client = parametermanager_v1.ParameterManagerClient( + client_options={"api_endpoint": api_endpoint} + ) + + # Build the resource name of the template version. + name = client.template_version_path( + project_id, location_id, template_id, version_id + ) + + # Build the resource name of the parameter version that supplies the values. + parameter_version = client.parameter_version_path( + project_id, location_id, parameter_id, parameter_version_id + ) + + # Define the request to render the template version. + request = parametermanager_v1.RenderTemplateVersionRequest( + name=name, parameter_version=parameter_version + ) + + # Render the template version. + response = client.render_template_version(request=request) + + # Print the template payload and the rendered payload. + print(f"Template payload: {response.payload.data.decode('utf-8')}") + print(f"Rendered regional payload: {response.rendered_payload.decode('utf-8')}") + # [END parametermanager_render_regional_param_template_version] + + return response diff --git a/parametermanager/snippets/regional_samples/snippets_test.py b/parametermanager/snippets/regional_samples/snippets_test.py index aaf3d10aa22..37abfa91dcb 100644 --- a/parametermanager/snippets/regional_samples/snippets_test.py +++ b/parametermanager/snippets/regional_samples/snippets_test.py @@ -17,7 +17,8 @@ import uuid from google.api_core import exceptions, retry -from google.cloud import kms, parametermanager_v1, secretmanager +from google.cloud import kms, parametermanager_v1, resourcemanager_v3, secretmanager +import google_crc32c import pytest # Import the methods to be tested @@ -44,6 +45,23 @@ from regional_samples import render_regional_param_version from regional_samples import update_regional_param_kms_key +from regional_samples import create_regional_param_template +from regional_samples import create_regional_param_template_version +from regional_samples import create_regional_param_version_with_checksum +from regional_samples import create_regional_param_with_tags +from regional_samples import delete_regional_param_template +from regional_samples import delete_regional_param_template_version +from regional_samples import disable_regional_param_template_version +from regional_samples import enable_regional_param_template_version +from regional_samples import get_regional_param_template +from regional_samples import get_regional_param_template_version +from regional_samples import get_regional_param_version_verify_checksum +from regional_samples import list_regional_param_template_versions +from regional_samples import list_regional_param_templates +from regional_samples import regional_template_quickstart +from regional_samples import render_regional_param_template_version +from regional_samples import update_regional_param_template_labels + @pytest.fixture() def client(location_id: str) -> parametermanager_v1.ParameterManagerClient: @@ -712,3 +730,381 @@ def wait_for_ready( return time.sleep((i + 1) ** 2) pytest.fail(f"{key_version_name} not ready") + + +@retry.Retry() +def retry_client_delete_template( + client: parametermanager_v1.ParameterManagerClient, + request: Optional[Union[parametermanager_v1.DeleteTemplateRequest, dict]], +) -> None: + # Retry to avoid 503 error & flaky issues + return client.delete_template(request=request) + + +@retry.Retry() +def retry_client_delete_template_version( + client: parametermanager_v1.ParameterManagerClient, + request: Optional[Union[parametermanager_v1.DeleteTemplateVersionRequest, dict]], +) -> None: + # Retry to avoid 503 error & flaky issues + return client.delete_template_version(request=request) + + +@retry.Retry() +def retry_client_create_template( + client: parametermanager_v1.ParameterManagerClient, + request: Optional[Union[parametermanager_v1.CreateTemplateRequest, dict]], +) -> parametermanager_v1.Template: + # Retry to avoid 503 error & flaky issues + return client.create_template(request=request) + + +@pytest.fixture() +def template_id( + client: parametermanager_v1.ParameterManagerClient, + project_id: str, + location_id: str, +) -> Iterator[Tuple[str, str]]: + tmpl_id = f"python-template-{uuid.uuid4()}" + tmpl_version_id = f"python-template-version-{uuid.uuid4()}" + + yield tmpl_id, tmpl_version_id + tmpl_path = client.template_path(project_id, location_id, tmpl_id) + print(f"Deleting template {tmpl_id}") + try: + time.sleep(5) + for version in client.list_template_versions(request={"parent": tmpl_path}): + print(f"Deleting template version {version.name}") + retry_client_delete_template_version(client, request={"name": version.name}) + retry_client_delete_template(client, request={"name": tmpl_path}) + except exceptions.NotFound: + # Template was already deleted, probably in the test + print(f"Template {tmpl_id} was not found.") + + +@pytest.fixture() +def template( + client: parametermanager_v1.ParameterManagerClient, + project_id: str, + location_id: str, + template_id: Tuple[str, str], +) -> Iterator[Tuple[str, str, str]]: + tmpl_id, version_id = template_id + print(f"Creating template {tmpl_id}") + + parent = client.common_location_path(project_id, location_id) + time.sleep(5) + _ = retry_client_create_template( + client, + request={ + "parent": parent, + "template_id": tmpl_id, + "template": {"format": parametermanager_v1.TemplateFormat.TEMPLATE_FORMAT_JSON}, + }, + ) + + yield project_id, tmpl_id, version_id + + +@pytest.fixture() +def template_version( + client: parametermanager_v1.ParameterManagerClient, + location_id: str, + template: Tuple[str, str, str], +) -> Iterator[Tuple[str, str, str, bytes]]: + project_id, tmpl_id, version_id = template + + print(f"Adding template version to {tmpl_id}") + parent = client.template_path(project_id, location_id, tmpl_id) + payload = b'{"username": "{{.username}}", "host": "{{.host}}"}' + time.sleep(5) + _ = client.create_template_version( + request={ + "parent": parent, + "template_version_id": version_id, + "template_version": {"payload": {"data": payload}}, + } + ) + + yield project_id, tmpl_id, version_id, payload + + +@pytest.fixture() +def json_parameter_version( + client: parametermanager_v1.ParameterManagerClient, + project_id: str, + location_id: str, + parameter_id: Tuple[str, str], +) -> Iterator[Tuple[str, str, str, dict]]: + param_id, version_id = parameter_id + print(f"Creating JSON parameter {param_id}") + + parent = client.common_location_path(project_id, location_id) + time.sleep(5) + _ = retry_client_create_parameter( + client, + request={ + "parent": parent, + "parameter_id": param_id, + "parameter": {"format": parametermanager_v1.ParameterFormat.JSON.name}, + }, + ) + values = {"username": "test-user", "host": "localhost"} + _ = client.create_parameter_version( + request={ + "parent": client.parameter_path(project_id, location_id, param_id), + "parameter_version_id": version_id, + "parameter_version": {"payload": {"data": json.dumps(values).encode("utf-8")}}, + } + ) + + yield project_id, param_id, version_id, values + + +@pytest.fixture() +def tag_key_value() -> Tuple[str, str]: + # Tags are created outside of the tests (they require Resource Manager + # permissions), so the IDs are supplied through the environment. + tag_key = os.environ.get("PARAMETER_MANAGER_TAG_KEY") + tag_value = os.environ.get("PARAMETER_MANAGER_TAG_VALUE") + if not tag_key or not tag_value: + pytest.skip("PARAMETER_MANAGER_TAG_KEY and PARAMETER_MANAGER_TAG_VALUE are not set") + return tag_key, tag_value + + +def test_regional_template_quickstart( + project_id: str, location_id: str, template_id: Tuple[str, str], parameter_id: Tuple[str, str] +) -> None: + tmpl_id, version_id = template_id + param_id, _ = parameter_id + regional_template_quickstart.regional_template_quickstart(project_id, location_id, tmpl_id, param_id, version_id) + + +def test_create_regional_param_template( + project_id: str, location_id: str, template_id: Tuple[str, str] +) -> None: + tmpl_id, _ = template_id + tmpl = create_regional_param_template.create_regional_param_template( + project_id, location_id, tmpl_id, parametermanager_v1.TemplateFormat.TEMPLATE_FORMAT_JSON + ) + assert tmpl_id in tmpl.name + assert tmpl.format_ == parametermanager_v1.TemplateFormat.TEMPLATE_FORMAT_JSON + + +def test_create_regional_param_template_version( + client: parametermanager_v1.ParameterManagerClient, + location_id: str, + template: Tuple[str, str, str], +) -> None: + project_id, tmpl_id, version_id = template + version = create_regional_param_template_version.create_regional_param_template_version(project_id, location_id, tmpl_id, version_id) + assert tmpl_id in version.name + assert version_id in version.name + got = client.get_template_version(request={"name": version.name}) + assert b"{{.username}}" in got.payload.data + + +def test_get_regional_param_template(location_id: str, template: Tuple[str, str, str]) -> None: + project_id, tmpl_id, _ = template + tmpl = get_regional_param_template.get_regional_param_template(project_id, location_id, tmpl_id) + assert tmpl_id in tmpl.name + + +def test_list_regional_param_templates( + capsys: pytest.LogCaptureFixture, + location_id: str, + template: Tuple[str, str, str], +) -> None: + project_id, tmpl_id, _ = template + got = get_regional_param_template.get_regional_param_template(project_id, location_id, tmpl_id) + list_regional_param_templates.list_regional_param_templates(project_id, location_id) + + out, _ = capsys.readouterr() + assert f"Found regional template {got.name} with format {got.format_.name}" in out + + +def test_get_regional_param_template_version( + location_id: str, template_version: Tuple[str, str, str, bytes] +) -> None: + project_id, tmpl_id, version_id, payload = template_version + version = get_regional_param_template_version.get_regional_param_template_version(project_id, location_id, tmpl_id, version_id) + assert tmpl_id in version.name + assert version_id in version.name + assert version.payload.data == payload + + +def test_list_regional_param_template_versions( + capsys: pytest.LogCaptureFixture, + location_id: str, + template_version: Tuple[str, str, str, bytes], +) -> None: + project_id, tmpl_id, version_id, _ = template_version + version = get_regional_param_template_version.get_regional_param_template_version(project_id, location_id, tmpl_id, version_id) + list_regional_param_template_versions.list_regional_param_template_versions(project_id, location_id, tmpl_id) + + out, _ = capsys.readouterr() + assert f"Found regional template version: {version.name}" in out + + +def test_update_regional_param_template_labels( + location_id: str, template: Tuple[str, str, str], label_key: str, label_value: str +) -> None: + project_id, tmpl_id, _ = template + tmpl = update_regional_param_template_labels.update_regional_param_template_labels( + project_id, location_id, tmpl_id, label_key, label_value + ) + assert tmpl.labels[label_key] == label_value + got = get_regional_param_template.get_regional_param_template(project_id, location_id, tmpl_id) + assert got.labels[label_key] == label_value + + +def test_delete_regional_param_template( + client: parametermanager_v1.ParameterManagerClient, + location_id: str, + template: Tuple[str, str, str], +) -> None: + project_id, tmpl_id, _ = template + delete_regional_param_template.delete_regional_param_template(project_id, location_id, tmpl_id) + with pytest.raises(exceptions.NotFound): + client.get_template( + request={"name": client.template_path(project_id, location_id, tmpl_id)} + ) + + +def test_disable_regional_param_template_version( + location_id: str, template_version: Tuple[str, str, str, bytes] +) -> None: + project_id, tmpl_id, version_id, _ = template_version + version = disable_regional_param_template_version.disable_regional_param_template_version(project_id, location_id, tmpl_id, version_id) + assert version.disabled is True + + +def test_enable_regional_param_template_version( + client: parametermanager_v1.ParameterManagerClient, + location_id: str, + template_version: Tuple[str, str, str, bytes], +) -> None: + project_id, tmpl_id, version_id, _ = template_version + name = client.template_version_path(project_id, location_id, tmpl_id, version_id) + version = client.get_template_version(request={"name": name}) + version.disabled = True + client.update_template_version( + request={"template_version": version, "update_mask": {"paths": ["disabled"]}} + ) + version = enable_regional_param_template_version.enable_regional_param_template_version(project_id, location_id, tmpl_id, version_id) + assert version.disabled is False + + +def test_delete_regional_param_template_version( + client: parametermanager_v1.ParameterManagerClient, + location_id: str, + template_version: Tuple[str, str, str, bytes], +) -> None: + project_id, tmpl_id, version_id, _ = template_version + delete_regional_param_template_version.delete_regional_param_template_version(project_id, location_id, tmpl_id, version_id) + with pytest.raises(exceptions.NotFound): + client.get_template_version( + request={ + "name": client.template_version_path( + project_id, location_id, tmpl_id, version_id + ) + } + ) + + +def test_render_regional_param_template_version( + location_id: str, + template_version: Tuple[str, str, str, bytes], + json_parameter_version: Tuple[str, str, str, dict], +) -> None: + project_id, tmpl_id, version_id, payload = template_version + _, param_id, param_version_id, values = json_parameter_version + response = render_regional_param_template_version.render_regional_param_template_version( + project_id, location_id, tmpl_id, version_id, param_id, param_version_id + ) + assert response.payload.data == payload + assert param_id in response.parameter_version + assert json.loads(response.rendered_payload.decode("utf-8")) == values + + +def test_create_regional_param_with_tags( + location_id: str, + parameter_id: Tuple[str, str], + tag_key_value: Tuple[str, str], + project_id: str, +) -> None: + param_id, _ = parameter_id + tag_key, tag_value = tag_key_value + parameter = create_regional_param_with_tags.create_regional_param_with_tags( + project_id, location_id, param_id, tag_key, tag_value + ) + assert param_id in parameter.name + + # Tags are input-only, so confirm them through Resource Manager tag bindings. + bindings_client = resourcemanager_v3.TagBindingsClient(client_options={"api_endpoint": f"{location_id}-cloudresourcemanager.googleapis.com"}) + bindings = bindings_client.list_tag_bindings( + request={"parent": f"//parametermanager.googleapis.com/{parameter.name}"} + ) + assert tag_value in [binding.tag_value for binding in bindings] + + +def test_create_regional_param_version_with_checksum( + client: parametermanager_v1.ParameterManagerClient, + location_id: str, + parameter: Tuple[str, str, str], +) -> None: + project_id, param_id, version_id = parameter + version = create_regional_param_version_with_checksum.create_regional_param_version_with_checksum( + project_id, location_id, param_id, version_id + ) + assert param_id in version.name + assert ( + version.checksum_source + == parametermanager_v1.ParameterVersion.ChecksumSource.USER_SPECIFIED + ) + got = client.get_parameter_version( + request={"name": version.name, "view": parametermanager_v1.View.FULL} + ) + assert got.payload.data_crc32c == google_crc32c.value(got.payload.data) + + +def test_get_regional_param_version_verify_checksum( + capsys: pytest.LogCaptureFixture, + location_id: str, + parameter_version: Tuple[str, str, str, bytes], +) -> None: + project_id, param_id, version_id, payload = parameter_version + version = get_regional_param_version_verify_checksum.get_regional_param_version_verify_checksum( + project_id, location_id, param_id, version_id + ) + assert version.payload.data == payload + assert version.payload.data_crc32c == google_crc32c.value(payload) + + out, _ = capsys.readouterr() + assert "Checksum source: SERVER_GENERATED" in out + + +def test_create_param_version_with_mismatched_checksum( + client: parametermanager_v1.ParameterManagerClient, + location_id: str, + parameter: Tuple[str, str, str], +) -> None: + project_id, param_id, version_id = parameter + payload = b"hello world!" + wrong_crc32c = (google_crc32c.value(payload) + 1) % (2**32) + parent = client.parameter_path(project_id, location_id, param_id) + + with pytest.raises(exceptions.InvalidArgument) as exc_info: + client.create_parameter_version( + request={ + "parent": parent, + "parameter_version_id": version_id, + "parameter_version": { + "payload": {"data": payload, "data_crc32c": wrong_crc32c} + }, + } + ) + assert "CHECKSUM_MISMATCH" in str(exc_info.value) or any( + getattr(detail, "reason", "") == "CHECKSUM_MISMATCH" + for detail in exc_info.value.details + ) diff --git a/parametermanager/snippets/regional_samples/update_regional_param_template_labels.py b/parametermanager/snippets/regional_samples/update_regional_param_template_labels.py new file mode 100644 index 00000000000..38b102e34e8 --- /dev/null +++ b/parametermanager/snippets/regional_samples/update_regional_param_template_labels.py @@ -0,0 +1,87 @@ +#!/usr/bin/env python + +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +""" +command line application and sample code for +updating the labels of a template. +""" + +from google.cloud import parametermanager_v1 + + +# [START parametermanager_update_regional_param_template_labels] +def update_regional_param_template_labels( + project_id: str, location_id: str, template_id: str, key: str, value: str +) -> parametermanager_v1.Template: + """ + Adds or updates a label on an existing template in the specified region of + the specified project using the Google Cloud Parameter Manager SDK. + + Args: + project_id (str): The ID of the project. + location_id (str): The region where the resources are located. + template_id (str): The ID of the template whose labels are to be + updated. + key (str): The label key. + value (str): The label value. + + Returns: + parametermanager_v1.Template: An object representing the updated template. + + Example: + update_regional_param_template_labels( + "my-project", + "us-central1", + "my-template", + "environment", + "production" + ) + """ + # Import the necessary library for Google Cloud Parameter Manager. + from google.cloud import parametermanager_v1 + from google.protobuf import field_mask_pb2 + + # Create the Parameter Manager client with the regional endpoint. + api_endpoint = f"parametermanager.{location_id}.rep.googleapis.com" + client = parametermanager_v1.ParameterManagerClient( + client_options={"api_endpoint": api_endpoint} + ) + + # Build the resource name of the template. + name = client.template_path(project_id, location_id, template_id) + + # Get the current template and set the label. + template = client.get_template(request={"name": name}) + template.labels[key] = value + + # Define the update mask for the labels field. + update_mask = field_mask_pb2.FieldMask(paths=["labels"]) + + # Update the template. + request = parametermanager_v1.UpdateTemplateRequest( + template=template, update_mask=update_mask + ) + client.update_template(request=request) + + # The update response does not include the labels, so get the template + # again to read the stored labels. + response = client.get_template(request={"name": name}) + + # Print the updated labels. + print( + f"Updated regional template {response.name} with labels {dict(response.labels)}" + ) + # [END parametermanager_update_regional_param_template_labels] + + return response diff --git a/parametermanager/snippets/render_param_template_version.py b/parametermanager/snippets/render_param_template_version.py new file mode 100644 index 00000000000..3739b9f707d --- /dev/null +++ b/parametermanager/snippets/render_param_template_version.py @@ -0,0 +1,85 @@ +#!/usr/bin/env python + +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +""" +command line application and sample code for +rendering a template version. +""" + +from google.cloud import parametermanager_v1 + + +# [START parametermanager_render_param_template_version] +def render_param_template_version( + project_id: str, + template_id: str, + version_id: str, + parameter_id: str, + parameter_version_id: str, +) -> parametermanager_v1.RenderTemplateVersionResponse: + """ + Renders a version of an existing template in the global location of the + specified project, resolving its placeholders with the values of a parameter + version, using the Google Cloud Parameter Manager SDK. + + Args: + project_id (str): The ID of the project. + template_id (str): The ID of the template to be rendered. + version_id (str): The ID of the template version to be rendered. + parameter_id (str): The ID of the parameter whose version supplies the + values. + parameter_version_id (str): The ID of the parameter version that + supplies the values. + + Returns: + parametermanager_v1.RenderTemplateVersionResponse: An object representing the rendered + template version. + + Example: + render_param_template_version( + "my-project", + "my-template", + "v1", + "my-parameter", + "v1" + ) + """ + # Import the necessary library for Google Cloud Parameter Manager. + from google.cloud import parametermanager_v1 + + # Create the Parameter Manager client. + client = parametermanager_v1.ParameterManagerClient() + + # Build the resource name of the template version. + name = client.template_version_path(project_id, "global", template_id, version_id) + + # Build the resource name of the parameter version that supplies the values. + parameter_version = client.parameter_version_path( + project_id, "global", parameter_id, parameter_version_id + ) + + # Define the request to render the template version. + request = parametermanager_v1.RenderTemplateVersionRequest( + name=name, parameter_version=parameter_version + ) + + # Render the template version. + response = client.render_template_version(request=request) + + # Print the template payload and the rendered payload. + print(f"Template payload: {response.payload.data.decode('utf-8')}") + print(f"Rendered payload: {response.rendered_payload.decode('utf-8')}") + # [END parametermanager_render_param_template_version] + + return response diff --git a/parametermanager/snippets/requirements-test.txt b/parametermanager/snippets/requirements-test.txt index f70a9b5f321..0ea0e9d6e90 100644 --- a/parametermanager/snippets/requirements-test.txt +++ b/parametermanager/snippets/requirements-test.txt @@ -1,3 +1,4 @@ pytest==9.0.3; python_version >= "3.10" -google-cloud-secret-manager==2.21.1 -google-cloud-kms==3.2.1 +google-cloud-secret-manager==2.31.0 +google-cloud-kms==3.18.0 +google-cloud-resource-manager==1.19.0 diff --git a/parametermanager/snippets/requirements.txt b/parametermanager/snippets/requirements.txt index 0919a6ec653..5dcdeb2ee18 100644 --- a/parametermanager/snippets/requirements.txt +++ b/parametermanager/snippets/requirements.txt @@ -1 +1,2 @@ -google-cloud-parametermanager==0.1.5 +google-cloud-parametermanager==0.4.3 +google-crc32c==1.9.0 diff --git a/parametermanager/snippets/snippets_test.py b/parametermanager/snippets/snippets_test.py index bf464cf2020..4f310525371 100644 --- a/parametermanager/snippets/snippets_test.py +++ b/parametermanager/snippets/snippets_test.py @@ -17,7 +17,8 @@ import uuid from google.api_core import exceptions, retry -from google.cloud import kms, parametermanager_v1, secretmanager +from google.cloud import kms, parametermanager_v1, resourcemanager_v3, secretmanager +import google_crc32c import pytest # Import the methods to be tested @@ -40,6 +41,23 @@ from render_param_version import render_param_version from update_param_kms_key import update_param_kms_key +from create_param_template import create_param_template +from create_param_template_version import create_param_template_version +from create_param_version_with_checksum import create_param_version_with_checksum +from create_param_with_tags import create_param_with_tags +from delete_param_template import delete_param_template +from delete_param_template_version import delete_param_template_version +from disable_param_template_version import disable_param_template_version +from enable_param_template_version import enable_param_template_version +from get_param_template import get_param_template +from get_param_template_version import get_param_template_version +from get_param_version_verify_checksum import get_param_version_verify_checksum +from list_param_template_versions import list_param_template_versions +from list_param_templates import list_param_templates +from render_param_template_version import render_param_template_version +from template_quickstart import template_quickstart +from update_param_template_labels import update_param_template_labels + @pytest.fixture() def client() -> parametermanager_v1.ParameterManagerClient: @@ -654,3 +672,381 @@ def wait_for_ready( return time.sleep((i + 1) ** 2) pytest.fail(f"{key_version_name} not ready") + + +@retry.Retry() +def retry_client_delete_template( + client: parametermanager_v1.ParameterManagerClient, + request: Optional[Union[parametermanager_v1.DeleteTemplateRequest, dict]], +) -> None: + # Retry to avoid 503 error & flaky issues + return client.delete_template(request=request) + + +@retry.Retry() +def retry_client_delete_template_version( + client: parametermanager_v1.ParameterManagerClient, + request: Optional[Union[parametermanager_v1.DeleteTemplateVersionRequest, dict]], +) -> None: + # Retry to avoid 503 error & flaky issues + return client.delete_template_version(request=request) + + +@retry.Retry() +def retry_client_create_template( + client: parametermanager_v1.ParameterManagerClient, + request: Optional[Union[parametermanager_v1.CreateTemplateRequest, dict]], +) -> parametermanager_v1.Template: + # Retry to avoid 503 error & flaky issues + return client.create_template(request=request) + + +@pytest.fixture() +def template_id( + client: parametermanager_v1.ParameterManagerClient, + project_id: str, + location_id: str, +) -> Iterator[Tuple[str, str]]: + tmpl_id = f"python-template-{uuid.uuid4()}" + tmpl_version_id = f"python-template-version-{uuid.uuid4()}" + + yield tmpl_id, tmpl_version_id + tmpl_path = client.template_path(project_id, location_id, tmpl_id) + print(f"Deleting template {tmpl_id}") + try: + time.sleep(5) + for version in client.list_template_versions(request={"parent": tmpl_path}): + print(f"Deleting template version {version.name}") + retry_client_delete_template_version(client, request={"name": version.name}) + retry_client_delete_template(client, request={"name": tmpl_path}) + except exceptions.NotFound: + # Template was already deleted, probably in the test + print(f"Template {tmpl_id} was not found.") + + +@pytest.fixture() +def template( + client: parametermanager_v1.ParameterManagerClient, + project_id: str, + location_id: str, + template_id: Tuple[str, str], +) -> Iterator[Tuple[str, str, str]]: + tmpl_id, version_id = template_id + print(f"Creating template {tmpl_id}") + + parent = client.common_location_path(project_id, location_id) + time.sleep(5) + _ = retry_client_create_template( + client, + request={ + "parent": parent, + "template_id": tmpl_id, + "template": {"format": parametermanager_v1.TemplateFormat.TEMPLATE_FORMAT_JSON}, + }, + ) + + yield project_id, tmpl_id, version_id + + +@pytest.fixture() +def template_version( + client: parametermanager_v1.ParameterManagerClient, + location_id: str, + template: Tuple[str, str, str], +) -> Iterator[Tuple[str, str, str, bytes]]: + project_id, tmpl_id, version_id = template + + print(f"Adding template version to {tmpl_id}") + parent = client.template_path(project_id, location_id, tmpl_id) + payload = b'{"username": "{{.username}}", "host": "{{.host}}"}' + time.sleep(5) + _ = client.create_template_version( + request={ + "parent": parent, + "template_version_id": version_id, + "template_version": {"payload": {"data": payload}}, + } + ) + + yield project_id, tmpl_id, version_id, payload + + +@pytest.fixture() +def json_parameter_version( + client: parametermanager_v1.ParameterManagerClient, + project_id: str, + location_id: str, + parameter_id: Tuple[str, str], +) -> Iterator[Tuple[str, str, str, dict]]: + param_id, version_id = parameter_id + print(f"Creating JSON parameter {param_id}") + + parent = client.common_location_path(project_id, location_id) + time.sleep(5) + _ = retry_client_create_parameter( + client, + request={ + "parent": parent, + "parameter_id": param_id, + "parameter": {"format": parametermanager_v1.ParameterFormat.JSON.name}, + }, + ) + values = {"username": "test-user", "host": "localhost"} + _ = client.create_parameter_version( + request={ + "parent": client.parameter_path(project_id, location_id, param_id), + "parameter_version_id": version_id, + "parameter_version": {"payload": {"data": json.dumps(values).encode("utf-8")}}, + } + ) + + yield project_id, param_id, version_id, values + + +@pytest.fixture() +def tag_key_value() -> Tuple[str, str]: + # Tags are created outside of the tests (they require Resource Manager + # permissions), so the IDs are supplied through the environment. + tag_key = os.environ.get("PARAMETER_MANAGER_TAG_KEY") + tag_value = os.environ.get("PARAMETER_MANAGER_TAG_VALUE") + if not tag_key or not tag_value: + pytest.skip("PARAMETER_MANAGER_TAG_KEY and PARAMETER_MANAGER_TAG_VALUE are not set") + return tag_key, tag_value + + +def test_template_quickstart( + project_id: str, location_id: str, template_id: Tuple[str, str], parameter_id: Tuple[str, str] +) -> None: + tmpl_id, version_id = template_id + param_id, _ = parameter_id + template_quickstart(project_id, tmpl_id, param_id, version_id) + + +def test_create_param_template( + project_id: str, location_id: str, template_id: Tuple[str, str] +) -> None: + tmpl_id, _ = template_id + tmpl = create_param_template( + project_id, tmpl_id, parametermanager_v1.TemplateFormat.TEMPLATE_FORMAT_JSON + ) + assert tmpl_id in tmpl.name + assert tmpl.format_ == parametermanager_v1.TemplateFormat.TEMPLATE_FORMAT_JSON + + +def test_create_param_template_version( + client: parametermanager_v1.ParameterManagerClient, + location_id: str, + template: Tuple[str, str, str], +) -> None: + project_id, tmpl_id, version_id = template + version = create_param_template_version(project_id, tmpl_id, version_id) + assert tmpl_id in version.name + assert version_id in version.name + got = client.get_template_version(request={"name": version.name}) + assert b"{{.username}}" in got.payload.data + + +def test_get_param_template(location_id: str, template: Tuple[str, str, str]) -> None: + project_id, tmpl_id, _ = template + tmpl = get_param_template(project_id, tmpl_id) + assert tmpl_id in tmpl.name + + +def test_list_param_templates( + capsys: pytest.LogCaptureFixture, + location_id: str, + template: Tuple[str, str, str], +) -> None: + project_id, tmpl_id, _ = template + got = get_param_template(project_id, tmpl_id) + list_param_templates(project_id) + + out, _ = capsys.readouterr() + assert f"Found template {got.name} with format {got.format_.name}" in out + + +def test_get_param_template_version( + location_id: str, template_version: Tuple[str, str, str, bytes] +) -> None: + project_id, tmpl_id, version_id, payload = template_version + version = get_param_template_version(project_id, tmpl_id, version_id) + assert tmpl_id in version.name + assert version_id in version.name + assert version.payload.data == payload + + +def test_list_param_template_versions( + capsys: pytest.LogCaptureFixture, + location_id: str, + template_version: Tuple[str, str, str, bytes], +) -> None: + project_id, tmpl_id, version_id, _ = template_version + version = get_param_template_version(project_id, tmpl_id, version_id) + list_param_template_versions(project_id, tmpl_id) + + out, _ = capsys.readouterr() + assert f"Found template version: {version.name}" in out + + +def test_update_param_template_labels( + location_id: str, template: Tuple[str, str, str], label_key: str, label_value: str +) -> None: + project_id, tmpl_id, _ = template + tmpl = update_param_template_labels( + project_id, tmpl_id, label_key, label_value + ) + assert tmpl.labels[label_key] == label_value + got = get_param_template(project_id, tmpl_id) + assert got.labels[label_key] == label_value + + +def test_delete_param_template( + client: parametermanager_v1.ParameterManagerClient, + location_id: str, + template: Tuple[str, str, str], +) -> None: + project_id, tmpl_id, _ = template + delete_param_template(project_id, tmpl_id) + with pytest.raises(exceptions.NotFound): + client.get_template( + request={"name": client.template_path(project_id, location_id, tmpl_id)} + ) + + +def test_disable_param_template_version( + location_id: str, template_version: Tuple[str, str, str, bytes] +) -> None: + project_id, tmpl_id, version_id, _ = template_version + version = disable_param_template_version(project_id, tmpl_id, version_id) + assert version.disabled is True + + +def test_enable_param_template_version( + client: parametermanager_v1.ParameterManagerClient, + location_id: str, + template_version: Tuple[str, str, str, bytes], +) -> None: + project_id, tmpl_id, version_id, _ = template_version + name = client.template_version_path(project_id, location_id, tmpl_id, version_id) + version = client.get_template_version(request={"name": name}) + version.disabled = True + client.update_template_version( + request={"template_version": version, "update_mask": {"paths": ["disabled"]}} + ) + version = enable_param_template_version(project_id, tmpl_id, version_id) + assert version.disabled is False + + +def test_delete_param_template_version( + client: parametermanager_v1.ParameterManagerClient, + location_id: str, + template_version: Tuple[str, str, str, bytes], +) -> None: + project_id, tmpl_id, version_id, _ = template_version + delete_param_template_version(project_id, tmpl_id, version_id) + with pytest.raises(exceptions.NotFound): + client.get_template_version( + request={ + "name": client.template_version_path( + project_id, location_id, tmpl_id, version_id + ) + } + ) + + +def test_render_param_template_version( + location_id: str, + template_version: Tuple[str, str, str, bytes], + json_parameter_version: Tuple[str, str, str, dict], +) -> None: + project_id, tmpl_id, version_id, payload = template_version + _, param_id, param_version_id, values = json_parameter_version + response = render_param_template_version( + project_id, tmpl_id, version_id, param_id, param_version_id + ) + assert response.payload.data == payload + assert param_id in response.parameter_version + assert json.loads(response.rendered_payload.decode("utf-8")) == values + + +def test_create_param_with_tags( + location_id: str, + parameter_id: Tuple[str, str], + tag_key_value: Tuple[str, str], + project_id: str, +) -> None: + param_id, _ = parameter_id + tag_key, tag_value = tag_key_value + parameter = create_param_with_tags( + project_id, param_id, tag_key, tag_value + ) + assert param_id in parameter.name + + # Tags are input-only, so confirm them through Resource Manager tag bindings. + bindings_client = resourcemanager_v3.TagBindingsClient() + bindings = bindings_client.list_tag_bindings( + request={"parent": f"//parametermanager.googleapis.com/{parameter.name}"} + ) + assert tag_value in [binding.tag_value for binding in bindings] + + +def test_create_param_version_with_checksum( + client: parametermanager_v1.ParameterManagerClient, + location_id: str, + parameter: Tuple[str, str, str], +) -> None: + project_id, param_id, version_id = parameter + version = create_param_version_with_checksum( + project_id, param_id, version_id + ) + assert param_id in version.name + assert ( + version.checksum_source + == parametermanager_v1.ParameterVersion.ChecksumSource.USER_SPECIFIED + ) + got = client.get_parameter_version( + request={"name": version.name, "view": parametermanager_v1.View.FULL} + ) + assert got.payload.data_crc32c == google_crc32c.value(got.payload.data) + + +def test_get_param_version_verify_checksum( + capsys: pytest.LogCaptureFixture, + location_id: str, + parameter_version: Tuple[str, str, str, bytes], +) -> None: + project_id, param_id, version_id, payload = parameter_version + version = get_param_version_verify_checksum( + project_id, param_id, version_id + ) + assert version.payload.data == payload + assert version.payload.data_crc32c == google_crc32c.value(payload) + + out, _ = capsys.readouterr() + assert "Checksum source: SERVER_GENERATED" in out + + +def test_create_param_version_with_mismatched_checksum( + client: parametermanager_v1.ParameterManagerClient, + location_id: str, + parameter: Tuple[str, str, str], +) -> None: + project_id, param_id, version_id = parameter + payload = b"hello world!" + wrong_crc32c = (google_crc32c.value(payload) + 1) % (2**32) + parent = client.parameter_path(project_id, location_id, param_id) + + with pytest.raises(exceptions.InvalidArgument) as exc_info: + client.create_parameter_version( + request={ + "parent": parent, + "parameter_version_id": version_id, + "parameter_version": { + "payload": {"data": payload, "data_crc32c": wrong_crc32c} + }, + } + ) + assert "CHECKSUM_MISMATCH" in str(exc_info.value) or any( + getattr(detail, "reason", "") == "CHECKSUM_MISMATCH" + for detail in exc_info.value.details + ) diff --git a/parametermanager/snippets/template_quickstart.py b/parametermanager/snippets/template_quickstart.py new file mode 100644 index 00000000000..f7a81a2a5b6 --- /dev/null +++ b/parametermanager/snippets/template_quickstart.py @@ -0,0 +1,119 @@ +#!/usr/bin/env python + +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +""" +command line application and sample code for +quickstart with parameter manager templates. +""" + + +# [START parametermanager_template_quickstart] +def template_quickstart( + project_id: str, template_id: str, parameter_id: str, version_id: str +) -> None: + """ + Quickstart example for using Google Cloud Parameter Manager templates to + create a template and a template version with placeholders, create a + parameter version that holds the values, and render the template version. + + Args: + project_id (str): The ID of the project. + template_id (str): The ID to assign to the new template. + parameter_id (str): The ID to assign to the new parameter. + version_id (str): The ID used for both the template version and + the parameter version. + + Returns: + None + + Example: + template_quickstart( + "my-project", + "my-template", + "my-parameter", + "v1" + ) + """ + # Import the necessary libraries. + from google.cloud import parametermanager_v1 + import json + + # Create the Parameter Manager client. + client = parametermanager_v1.ParameterManagerClient() + + # Build the resource name of the parent project in the global location. + parent = client.common_location_path(project_id, "global") + + # Create a template in JSON format. + template = client.create_template( + request=parametermanager_v1.CreateTemplateRequest( + parent=parent, + template_id=template_id, + template=parametermanager_v1.Template( + format_=parametermanager_v1.TemplateFormat.TEMPLATE_FORMAT_JSON + ), + ) + ) + print(f"Created template: {template.name}") + + # Create a template version with {{.variableName}} placeholders. + template_payload = {"username": "{{.username}}", "host": "{{.host}}"} + template_version = client.create_template_version( + request=parametermanager_v1.CreateTemplateVersionRequest( + parent=template.name, + template_version_id=version_id, + template_version=parametermanager_v1.TemplateVersion( + payload=parametermanager_v1.TemplateVersionPayload( + data=json.dumps(template_payload).encode("utf-8") + ) + ), + ) + ) + print(f"Created template version: {template_version.name}") + + # Create a JSON parameter. + parameter = client.create_parameter( + request=parametermanager_v1.CreateParameterRequest( + parent=parent, + parameter_id=parameter_id, + parameter=parametermanager_v1.Parameter( + format_=parametermanager_v1.ParameterFormat.JSON + ), + ) + ) + print(f"Created parameter: {parameter.name}") + + # Create a parameter version that holds the values for the placeholders. + values = {"username": "test-user", "host": "localhost"} + parameter_version = client.create_parameter_version( + request=parametermanager_v1.CreateParameterVersionRequest( + parent=parameter.name, + parameter_version_id=version_id, + parameter_version=parametermanager_v1.ParameterVersion( + payload=parametermanager_v1.ParameterVersionPayload( + data=json.dumps(values).encode("utf-8") + ) + ), + ) + ) + print(f"Created parameter version: {parameter_version.name}") + + # Render the template version with the parameter version's values. + response = client.render_template_version( + request=parametermanager_v1.RenderTemplateVersionRequest( + name=template_version.name, parameter_version=parameter_version.name + ) + ) + print(f"Rendered payload: {response.rendered_payload.decode('utf-8')}") + # [END parametermanager_template_quickstart] diff --git a/parametermanager/snippets/update_param_template_labels.py b/parametermanager/snippets/update_param_template_labels.py new file mode 100644 index 00000000000..460fe24ca6a --- /dev/null +++ b/parametermanager/snippets/update_param_template_labels.py @@ -0,0 +1,80 @@ +#!/usr/bin/env python + +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +""" +command line application and sample code for +updating the labels of a template. +""" + +from google.cloud import parametermanager_v1 + + +# [START parametermanager_update_param_template_labels] +def update_param_template_labels( + project_id: str, template_id: str, key: str, value: str +) -> parametermanager_v1.Template: + """ + Adds or updates a label on an existing template in the global location of + the specified project using the Google Cloud Parameter Manager SDK. + + Args: + project_id (str): The ID of the project. + template_id (str): The ID of the template whose labels are to be + updated. + key (str): The label key. + value (str): The label value. + + Returns: + parametermanager_v1.Template: An object representing the updated template. + + Example: + update_param_template_labels( + "my-project", + "my-template", + "environment", + "production" + ) + """ + # Import the necessary library for Google Cloud Parameter Manager. + from google.cloud import parametermanager_v1 + from google.protobuf import field_mask_pb2 + + # Create the Parameter Manager client. + client = parametermanager_v1.ParameterManagerClient() + + # Build the resource name of the template. + name = client.template_path(project_id, "global", template_id) + + # Get the current template and set the label. + template = client.get_template(request={"name": name}) + template.labels[key] = value + + # Define the update mask for the labels field. + update_mask = field_mask_pb2.FieldMask(paths=["labels"]) + + # Update the template. + request = parametermanager_v1.UpdateTemplateRequest( + template=template, update_mask=update_mask + ) + client.update_template(request=request) + + # The update response does not include the labels, so get the template + # again to read the stored labels. + response = client.get_template(request={"name": name}) + + # Print the updated labels. + print(f"Updated template {response.name} with labels {dict(response.labels)}") + # [END parametermanager_update_param_template_labels] + + return response From 4fcb1980d016bb1a8a2d43b196bbe49cac92767b Mon Sep 17 00:00:00 2001 From: smalaviya-crest Date: Wed, 7 Oct 2026 14:27:40 +0530 Subject: [PATCH 2/7] 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. --- .../snippets/bind_tags_to_param.py | 83 +++++++++++++++++ parametermanager/snippets/get_param_tags.py | 72 +++++++++++++++ .../bind_tags_to_regional_param.py | 92 +++++++++++++++++++ .../get_regional_param_tags.py | 81 ++++++++++++++++ .../regional_samples/snippets_test.py | 36 ++++++++ parametermanager/snippets/snippets_test.py | 36 ++++++++ 6 files changed, 400 insertions(+) create mode 100644 parametermanager/snippets/bind_tags_to_param.py create mode 100644 parametermanager/snippets/get_param_tags.py create mode 100644 parametermanager/snippets/regional_samples/bind_tags_to_regional_param.py create mode 100644 parametermanager/snippets/regional_samples/get_regional_param_tags.py diff --git a/parametermanager/snippets/bind_tags_to_param.py b/parametermanager/snippets/bind_tags_to_param.py new file mode 100644 index 00000000000..085d71f0afa --- /dev/null +++ b/parametermanager/snippets/bind_tags_to_param.py @@ -0,0 +1,83 @@ +#!/usr/bin/env python + +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +""" +command line application and sample code for +creating a parameter and binding a tag to it. +""" + +from google.cloud import resourcemanager_v3 + + +# [START parametermanager_bind_tags_to_param] +def bind_tags_to_param( + project_id: str, parameter_id: str, tag_value: str +) -> resourcemanager_v3.TagBinding: + """ + Creates a parameter in the global location of the specified project and then + binds an existing tag value to it through the Resource Manager API. + + Args: + project_id (str): The ID of the project. + parameter_id (str): The ID to assign to the new parameter. + tag_value (str): The tag value resource name, e.g. tagValues/1234567890. + + Returns: + resourcemanager_v3.TagBinding: An object representing the tag binding. + + Example: + bind_tags_to_param( + "my-project", + "my-parameter", + "tagValues/1234567890" + ) + """ + # Import the necessary libraries. + from google.cloud import parametermanager_v1 + from google.cloud import resourcemanager_v3 + + # Create the Parameter Manager client. + client = parametermanager_v1.ParameterManagerClient() + + # Create the Resource Manager tag bindings client. + resource_manager_client = resourcemanager_v3.TagBindingsClient() + + # Build the resource name of the parent project. + parent = client.common_location_path(project_id, "global") + + # Create the parameter. + parameter = client.create_parameter( + request={"parent": parent, "parameter_id": parameter_id} + ) + + # Print the new parameter name. + print(f"Created parameter: {parameter.name}") + + # Define the tag binding request for the new parameter. + request = resourcemanager_v3.CreateTagBindingRequest( + tag_binding=resourcemanager_v3.TagBinding( + parent=f"//parametermanager.googleapis.com/{parameter.name}", + tag_value=tag_value, + ), + ) + + # Create the tag binding and wait for the operation to complete. + operation = resource_manager_client.create_tag_binding(request=request) + response = operation.result() + + # Print the tag binding. + print(f"Created tag binding: {response.name}") + # [END parametermanager_bind_tags_to_param] + + return response diff --git a/parametermanager/snippets/get_param_tags.py b/parametermanager/snippets/get_param_tags.py new file mode 100644 index 00000000000..5b803b7852f --- /dev/null +++ b/parametermanager/snippets/get_param_tags.py @@ -0,0 +1,72 @@ +#!/usr/bin/env python + +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +""" +command line application and sample code for +getting the tags of a parameter. +""" + +from google.cloud import resourcemanager_v3 + + +# [START parametermanager_get_param_tags] +def get_param_tags( + project_id: str, parameter_id: str +) -> list[resourcemanager_v3.TagBinding]: + """ + Gets the tags bound to an existing parameter in the global location of the + specified project. + Tags are input-only on the parameter itself, so they are read as + tag bindings through the Resource Manager API. + + Args: + project_id (str): The ID of the project. + parameter_id (str): The ID of the parameter whose tags are retrieved. + + Returns: + list[resourcemanager_v3.TagBinding]: The tag bindings of the parameter. + + Example: + get_param_tags( + "my-project", + "my-parameter" + ) + """ + # Import the necessary libraries. + from google.cloud import parametermanager_v1 + from google.cloud import resourcemanager_v3 + + # Create the Parameter Manager client. + client = parametermanager_v1.ParameterManagerClient() + + # Create the Resource Manager tag bindings client. + resource_manager_client = resourcemanager_v3.TagBindingsClient() + + # Build the resource name of the parameter and get it. The parameter name + # contains the project number, which the tag bindings parent requires. + name = client.parameter_path(project_id, "global", parameter_id) + parameter = client.get_parameter(request={"name": name}) + + # List the tag bindings of the parameter. + request = resourcemanager_v3.ListTagBindingsRequest( + parent=f"//parametermanager.googleapis.com/{parameter.name}" + ) + bindings = list(resource_manager_client.list_tag_bindings(request=request)) + + # Print the tags of the parameter. + for binding in bindings: + print(f"Found parameter tag: {binding.tag_value}") + # [END parametermanager_get_param_tags] + + return bindings diff --git a/parametermanager/snippets/regional_samples/bind_tags_to_regional_param.py b/parametermanager/snippets/regional_samples/bind_tags_to_regional_param.py new file mode 100644 index 00000000000..421d5cf1220 --- /dev/null +++ b/parametermanager/snippets/regional_samples/bind_tags_to_regional_param.py @@ -0,0 +1,92 @@ +#!/usr/bin/env python + +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +""" +command line application and sample code for +creating a regional parameter and binding a tag to it. +""" + +from google.cloud import resourcemanager_v3 + + +# [START parametermanager_bind_tags_to_regional_param] +def bind_tags_to_regional_param( + project_id: str, location_id: str, parameter_id: str, tag_value: str +) -> resourcemanager_v3.TagBinding: + """ + Creates a parameter in the specified region of the specified project and then + binds an existing tag value to it through the Resource Manager API. + + Args: + project_id (str): The ID of the project. + location_id (str): The region where the parameter is located. + parameter_id (str): The ID to assign to the new parameter. + tag_value (str): The tag value resource name, e.g. tagValues/1234567890. + + Returns: + resourcemanager_v3.TagBinding: An object representing the tag binding. + + Example: + bind_tags_to_regional_param( + "my-project", + "us-central1", + "my-parameter", + "tagValues/1234567890" + ) + """ + # Import the necessary libraries. + from google.cloud import parametermanager_v1 + from google.cloud import resourcemanager_v3 + + # Create the Parameter Manager client with the regional endpoint. + api_endpoint = f"parametermanager.{location_id}.rep.googleapis.com" + client = parametermanager_v1.ParameterManagerClient( + client_options={"api_endpoint": api_endpoint} + ) + + # Create the Resource Manager tag bindings client with the regional endpoint. + resource_manager_client = resourcemanager_v3.TagBindingsClient( + client_options={ + "api_endpoint": f"{location_id}-cloudresourcemanager.googleapis.com" + } + ) + + # Build the resource name of the parent project. + parent = client.common_location_path(project_id, location_id) + + # Create the parameter. + parameter = client.create_parameter( + request={"parent": parent, "parameter_id": parameter_id} + ) + + # Print the new parameter name. + print(f"Created regional parameter: {parameter.name}") + + # Define the tag binding request for the new parameter. + request = resourcemanager_v3.CreateTagBindingRequest( + tag_binding=resourcemanager_v3.TagBinding( + parent=f"//parametermanager.googleapis.com/{parameter.name}", + tag_value=tag_value, + ), + ) + + # Create the tag binding and wait for the operation to complete. + operation = resource_manager_client.create_tag_binding(request=request) + response = operation.result() + + # Print the tag binding. + print(f"Created tag binding: {response.name}") + # [END parametermanager_bind_tags_to_regional_param] + + return response diff --git a/parametermanager/snippets/regional_samples/get_regional_param_tags.py b/parametermanager/snippets/regional_samples/get_regional_param_tags.py new file mode 100644 index 00000000000..cd7530cbb48 --- /dev/null +++ b/parametermanager/snippets/regional_samples/get_regional_param_tags.py @@ -0,0 +1,81 @@ +#!/usr/bin/env python + +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +""" +command line application and sample code for +getting the tags of a regional parameter. +""" + +from google.cloud import resourcemanager_v3 + + +# [START parametermanager_get_regional_param_tags] +def get_regional_param_tags( + project_id: str, location_id: str, parameter_id: str +) -> list[resourcemanager_v3.TagBinding]: + """ + Gets the tags bound to an existing parameter in the specified region of the + specified project. + Tags are input-only on the parameter itself, so they are read as + tag bindings through the Resource Manager API. + + Args: + project_id (str): The ID of the project. + location_id (str): The region where the parameter is located. + parameter_id (str): The ID of the parameter whose tags are retrieved. + + Returns: + list[resourcemanager_v3.TagBinding]: The tag bindings of the parameter. + + Example: + get_regional_param_tags( + "my-project", + "us-central1", + "my-parameter" + ) + """ + # Import the necessary libraries. + from google.cloud import parametermanager_v1 + from google.cloud import resourcemanager_v3 + + # Create the Parameter Manager client with the regional endpoint. + api_endpoint = f"parametermanager.{location_id}.rep.googleapis.com" + client = parametermanager_v1.ParameterManagerClient( + client_options={"api_endpoint": api_endpoint} + ) + + # Create the Resource Manager tag bindings client with the regional endpoint. + resource_manager_client = resourcemanager_v3.TagBindingsClient( + client_options={ + "api_endpoint": f"{location_id}-cloudresourcemanager.googleapis.com" + } + ) + + # Build the resource name of the parameter and get it. The parameter name + # contains the project number, which the tag bindings parent requires. + name = client.parameter_path(project_id, location_id, parameter_id) + parameter = client.get_parameter(request={"name": name}) + + # List the tag bindings of the parameter. + request = resourcemanager_v3.ListTagBindingsRequest( + parent=f"//parametermanager.googleapis.com/{parameter.name}" + ) + bindings = list(resource_manager_client.list_tag_bindings(request=request)) + + # Print the tags of the parameter. + for binding in bindings: + print(f"Found regional parameter tag: {binding.tag_value}") + # [END parametermanager_get_regional_param_tags] + + return bindings diff --git a/parametermanager/snippets/regional_samples/snippets_test.py b/parametermanager/snippets/regional_samples/snippets_test.py index 37abfa91dcb..76328854926 100644 --- a/parametermanager/snippets/regional_samples/snippets_test.py +++ b/parametermanager/snippets/regional_samples/snippets_test.py @@ -22,6 +22,7 @@ import pytest # Import the methods to be tested +from regional_samples import bind_tags_to_regional_param from regional_samples import create_regional_param from regional_samples import create_regional_param_version from regional_samples import ( @@ -37,6 +38,7 @@ from regional_samples import disable_regional_param_version from regional_samples import enable_regional_param_version from regional_samples import get_regional_param +from regional_samples import get_regional_param_tags from regional_samples import get_regional_param_version from regional_samples import list_regional_param_versions from regional_samples import list_regional_params @@ -1108,3 +1110,37 @@ def test_create_param_version_with_mismatched_checksum( getattr(detail, "reason", "") == "CHECKSUM_MISMATCH" for detail in exc_info.value.details ) + + +def test_bind_tags_to_regional_param( + location_id: str, + parameter_id: Tuple[str, str], + tag_key_value: Tuple[str, str], + project_id: str, +) -> None: + param_id, _ = parameter_id + _, tag_value = tag_key_value + binding = bind_tags_to_regional_param.bind_tags_to_regional_param(project_id, location_id, param_id, tag_value) + assert binding.tag_value == tag_value + assert param_id in binding.parent + + +def test_get_regional_param_tags( + client: parametermanager_v1.ParameterManagerClient, + location_id: str, + parameter_id: Tuple[str, str], + tag_key_value: Tuple[str, str], + project_id: str, +) -> None: + param_id, _ = parameter_id + tag_key, tag_value = tag_key_value + _ = retry_client_create_parameter( + client, + request={ + "parent": client.common_location_path(project_id, location_id), + "parameter_id": param_id, + "parameter": {"tags": {tag_key: tag_value}}, + }, + ) + bindings = get_regional_param_tags.get_regional_param_tags(project_id, location_id, param_id) + assert tag_value in [binding.tag_value for binding in bindings] diff --git a/parametermanager/snippets/snippets_test.py b/parametermanager/snippets/snippets_test.py index 4f310525371..cb0712511c0 100644 --- a/parametermanager/snippets/snippets_test.py +++ b/parametermanager/snippets/snippets_test.py @@ -22,6 +22,7 @@ import pytest # Import the methods to be tested +from bind_tags_to_param import bind_tags_to_param from create_param import create_param from create_param_version import create_param_version from create_param_version_with_secret import create_param_version_with_secret @@ -33,6 +34,7 @@ from disable_param_version import disable_param_version from enable_param_version import enable_param_version from get_param import get_param +from get_param_tags import get_param_tags from get_param_version import get_param_version from list_param_versions import list_param_versions from list_params import list_params @@ -1050,3 +1052,37 @@ def test_create_param_version_with_mismatched_checksum( getattr(detail, "reason", "") == "CHECKSUM_MISMATCH" for detail in exc_info.value.details ) + + +def test_bind_tags_to_param( + location_id: str, + parameter_id: Tuple[str, str], + tag_key_value: Tuple[str, str], + project_id: str, +) -> None: + param_id, _ = parameter_id + _, tag_value = tag_key_value + binding = bind_tags_to_param(project_id, param_id, tag_value) + assert binding.tag_value == tag_value + assert param_id in binding.parent + + +def test_get_param_tags( + client: parametermanager_v1.ParameterManagerClient, + location_id: str, + parameter_id: Tuple[str, str], + tag_key_value: Tuple[str, str], + project_id: str, +) -> None: + param_id, _ = parameter_id + tag_key, tag_value = tag_key_value + _ = retry_client_create_parameter( + client, + request={ + "parent": client.common_location_path(project_id, location_id), + "parameter_id": param_id, + "parameter": {"tags": {tag_key: tag_value}}, + }, + ) + bindings = get_param_tags(project_id, param_id) + assert tag_value in [binding.tag_value for binding in bindings] From 12b5773674678df1301deddefcf81ad2358f77da Mon Sep 17 00:00:00 2001 From: smalaviya-crest Date: Wed, 7 Oct 2026 15:47:31 +0530 Subject: [PATCH 3/7] style(parametermanager): fix import order in snippet tests --- .../regional_samples/snippets_test.py | 31 +++++++++---------- parametermanager/snippets/snippets_test.py | 31 +++++++++---------- 2 files changed, 30 insertions(+), 32 deletions(-) diff --git a/parametermanager/snippets/regional_samples/snippets_test.py b/parametermanager/snippets/regional_samples/snippets_test.py index 76328854926..630ffcb766e 100644 --- a/parametermanager/snippets/regional_samples/snippets_test.py +++ b/parametermanager/snippets/regional_samples/snippets_test.py @@ -24,44 +24,43 @@ # Import the methods to be tested from regional_samples import bind_tags_to_regional_param from regional_samples import create_regional_param +from regional_samples import create_regional_param_template +from regional_samples import create_regional_param_template_version from regional_samples import create_regional_param_version +from regional_samples import create_regional_param_version_with_checksum from regional_samples import ( create_regional_param_version_with_secret, ) from regional_samples import create_regional_param_with_kms_key +from regional_samples import create_regional_param_with_tags from regional_samples import create_structured_regional_param from regional_samples import ( create_structured_regional_param_version, ) from regional_samples import delete_regional_param +from regional_samples import delete_regional_param_template +from regional_samples import delete_regional_param_template_version from regional_samples import delete_regional_param_version +from regional_samples import disable_regional_param_template_version from regional_samples import disable_regional_param_version +from regional_samples import enable_regional_param_template_version from regional_samples import enable_regional_param_version from regional_samples import get_regional_param from regional_samples import get_regional_param_tags -from regional_samples import get_regional_param_version -from regional_samples import list_regional_param_versions -from regional_samples import list_regional_params -from regional_samples import regional_quickstart -from regional_samples import remove_regional_param_kms_key -from regional_samples import render_regional_param_version -from regional_samples import update_regional_param_kms_key - -from regional_samples import create_regional_param_template -from regional_samples import create_regional_param_template_version -from regional_samples import create_regional_param_version_with_checksum -from regional_samples import create_regional_param_with_tags -from regional_samples import delete_regional_param_template -from regional_samples import delete_regional_param_template_version -from regional_samples import disable_regional_param_template_version -from regional_samples import enable_regional_param_template_version from regional_samples import get_regional_param_template from regional_samples import get_regional_param_template_version +from regional_samples import get_regional_param_version from regional_samples import get_regional_param_version_verify_checksum from regional_samples import list_regional_param_template_versions from regional_samples import list_regional_param_templates +from regional_samples import list_regional_param_versions +from regional_samples import list_regional_params +from regional_samples import regional_quickstart from regional_samples import regional_template_quickstart +from regional_samples import remove_regional_param_kms_key from regional_samples import render_regional_param_template_version +from regional_samples import render_regional_param_version +from regional_samples import update_regional_param_kms_key from regional_samples import update_regional_param_template_labels diff --git a/parametermanager/snippets/snippets_test.py b/parametermanager/snippets/snippets_test.py index cb0712511c0..de9c3e6c993 100644 --- a/parametermanager/snippets/snippets_test.py +++ b/parametermanager/snippets/snippets_test.py @@ -24,40 +24,39 @@ # Import the methods to be tested from bind_tags_to_param import bind_tags_to_param from create_param import create_param +from create_param_template import create_param_template +from create_param_template_version import create_param_template_version from create_param_version import create_param_version +from create_param_version_with_checksum import create_param_version_with_checksum from create_param_version_with_secret import create_param_version_with_secret from create_param_with_kms_key import create_param_with_kms_key +from create_param_with_tags import create_param_with_tags from create_structured_param import create_structured_param from create_structured_param_version import create_structured_param_version from delete_param import delete_param +from delete_param_template import delete_param_template +from delete_param_template_version import delete_param_template_version from delete_param_version import delete_param_version +from disable_param_template_version import disable_param_template_version from disable_param_version import disable_param_version +from enable_param_template_version import enable_param_template_version from enable_param_version import enable_param_version from get_param import get_param from get_param_tags import get_param_tags -from get_param_version import get_param_version -from list_param_versions import list_param_versions -from list_params import list_params -from quickstart import quickstart -from remove_param_kms_key import remove_param_kms_key -from render_param_version import render_param_version -from update_param_kms_key import update_param_kms_key - -from create_param_template import create_param_template -from create_param_template_version import create_param_template_version -from create_param_version_with_checksum import create_param_version_with_checksum -from create_param_with_tags import create_param_with_tags -from delete_param_template import delete_param_template -from delete_param_template_version import delete_param_template_version -from disable_param_template_version import disable_param_template_version -from enable_param_template_version import enable_param_template_version from get_param_template import get_param_template from get_param_template_version import get_param_template_version +from get_param_version import get_param_version from get_param_version_verify_checksum import get_param_version_verify_checksum from list_param_template_versions import list_param_template_versions from list_param_templates import list_param_templates +from list_param_versions import list_param_versions +from list_params import list_params +from quickstart import quickstart +from remove_param_kms_key import remove_param_kms_key from render_param_template_version import render_param_template_version +from render_param_version import render_param_version from template_quickstart import template_quickstart +from update_param_kms_key import update_param_kms_key from update_param_template_labels import update_param_template_labels From 13cba49b43f6995d07b62a41e1d3de658b6e8b2c Mon Sep 17 00:00:00 2001 From: smalaviya-crest Date: Wed, 7 Oct 2026 15:58:46 +0530 Subject: [PATCH 4/7] style(parametermanager): group imports in snippets_test for CI lint --- parametermanager/snippets/snippets_test.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/parametermanager/snippets/snippets_test.py b/parametermanager/snippets/snippets_test.py index de9c3e6c993..94fd8118644 100644 --- a/parametermanager/snippets/snippets_test.py +++ b/parametermanager/snippets/snippets_test.py @@ -16,11 +16,6 @@ from typing import Iterator, Optional, Tuple, Union import uuid -from google.api_core import exceptions, retry -from google.cloud import kms, parametermanager_v1, resourcemanager_v3, secretmanager -import google_crc32c -import pytest - # Import the methods to be tested from bind_tags_to_param import bind_tags_to_param from create_param import create_param @@ -47,10 +42,14 @@ from get_param_template_version import get_param_template_version from get_param_version import get_param_version from get_param_version_verify_checksum import get_param_version_verify_checksum +from google.api_core import exceptions, retry +from google.cloud import kms, parametermanager_v1, resourcemanager_v3, secretmanager +import google_crc32c from list_param_template_versions import list_param_template_versions from list_param_templates import list_param_templates from list_param_versions import list_param_versions from list_params import list_params +import pytest from quickstart import quickstart from remove_param_kms_key import remove_param_kms_key from render_param_template_version import render_param_template_version From 654a76e12ec041e507454a30cc70815a1b4255dc Mon Sep 17 00:00:00 2001 From: smalaviya-crest Date: Wed, 7 Oct 2026 16:01:25 +0530 Subject: [PATCH 5/7] style(parametermanager): apply black formatting --- .../snippets/get_param_version.py | 5 +- .../regional_samples/get_regional_param.py | 4 +- .../get_regional_param_version.py | 4 +- .../regional_samples/list_regional_params.py | 4 +- .../regional_samples/snippets_test.py | 172 +++++++++++++----- .../snippets/remove_param_kms_key.py | 1 + parametermanager/snippets/snippets_test.py | 33 ++-- 7 files changed, 155 insertions(+), 68 deletions(-) diff --git a/parametermanager/snippets/get_param_version.py b/parametermanager/snippets/get_param_version.py index aaf75a4936a..64a016927d7 100644 --- a/parametermanager/snippets/get_param_version.py +++ b/parametermanager/snippets/get_param_version.py @@ -18,7 +18,6 @@ from google.cloud import parametermanager_v1 - # [START parametermanager_get_param_version] # The GetParameterVersion operation retrieves parameter metadata and doesn't @@ -69,7 +68,9 @@ def get_param_version( # Show parameter version details. # Find more details for the Parameter Version object here: # https://cloud.google.com/secret-manager/parameter-manager/docs/reference/rest/v1/projects.locations.parameters.versions#ParameterVersion - print(f"Found parameter version {response.name} with state {'disabled' if response.disabled else 'enabled'}") + print( + f"Found parameter version {response.name} with state {'disabled' if response.disabled else 'enabled'}" + ) if not response.disabled: print(f"Payload: {response.payload.data.decode('utf-8')}") # [END parametermanager_get_param_version] diff --git a/parametermanager/snippets/regional_samples/get_regional_param.py b/parametermanager/snippets/regional_samples/get_regional_param.py index c5f25fb2432..f7686d55e42 100644 --- a/parametermanager/snippets/regional_samples/get_regional_param.py +++ b/parametermanager/snippets/regional_samples/get_regional_param.py @@ -60,7 +60,9 @@ def get_regional_param( # Show parameter details. # Find more details for the Parameter object here: # https://cloud.google.com/secret-manager/parameter-manager/docs/reference/rest/v1/projects.locations.parameters#Parameter - print(f"Found the regional parameter {parameter.name} with format {parameter.format_.name}") + print( + f"Found the regional parameter {parameter.name} with format {parameter.format_.name}" + ) # [END parametermanager_get_regional_param] return parameter diff --git a/parametermanager/snippets/regional_samples/get_regional_param_version.py b/parametermanager/snippets/regional_samples/get_regional_param_version.py index c29e08264e9..b45a0e937e0 100644 --- a/parametermanager/snippets/regional_samples/get_regional_param_version.py +++ b/parametermanager/snippets/regional_samples/get_regional_param_version.py @@ -71,7 +71,9 @@ def get_regional_param_version( # Show parameter version details. # Find more details for the Parameter Version object here: # https://cloud.google.com/secret-manager/parameter-manager/docs/reference/rest/v1/projects.locations.parameters.versions#ParameterVersion - print(f"Found regional parameter version {response.name} with state {'disabled' if response.disabled else 'enabled'}") + print( + f"Found regional parameter version {response.name} with state {'disabled' if response.disabled else 'enabled'}" + ) if not response.disabled: print(f"Payload: {response.payload.data.decode('utf-8')}") # [END parametermanager_get_regional_param_version] diff --git a/parametermanager/snippets/regional_samples/list_regional_params.py b/parametermanager/snippets/regional_samples/list_regional_params.py index 90df45e3254..47d5695dfe9 100644 --- a/parametermanager/snippets/regional_samples/list_regional_params.py +++ b/parametermanager/snippets/regional_samples/list_regional_params.py @@ -52,6 +52,8 @@ def list_regional_params(project_id: str, location_id: str) -> None: # List all parameters in the specified parent project and region. for parameter in client.list_parameters(parent=parent): - print(f"Found regional parameter {parameter.name} with format {parameter.format_.name}") + print( + f"Found regional parameter {parameter.name} with format {parameter.format_.name}" + ) # [END parametermanager_list_regional_params] diff --git a/parametermanager/snippets/regional_samples/snippets_test.py b/parametermanager/snippets/regional_samples/snippets_test.py index 630ffcb766e..e926ac61bc5 100644 --- a/parametermanager/snippets/regional_samples/snippets_test.py +++ b/parametermanager/snippets/regional_samples/snippets_test.py @@ -230,7 +230,7 @@ def parameter_with_kms( location_id: str, project_id: str, parameter_id: str, - hsm_key_id: str + hsm_key_id: str, ) -> Iterator[Tuple[str, str, str, parametermanager_v1.Parameter]]: param_id, version_id = parameter_id print(f"Creating parameter {param_id} with kms {hsm_key_id}") @@ -412,7 +412,11 @@ def key_ring_id( key_id = f"{uuid.uuid4()}" try: key_ring = kms_key_client.create_key_ring( - request={"parent": location_name, "key_ring_id": key_ring_id, "key_ring": {}} + request={ + "parent": location_name, + "key_ring_id": key_ring_id, + "key_ring": {}, + } ) yield key_ring.name, key_id except exceptions.AlreadyExists: @@ -436,8 +440,7 @@ def hsm_key_id( "crypto_key": { "purpose": kms.CryptoKey.CryptoKeyPurpose.ENCRYPT_DECRYPT, "version_template": { - "algorithm": - kms.CryptoKeyVersion.CryptoKeyVersionAlgorithm.GOOGLE_SYMMETRIC_ENCRYPTION, + "algorithm": kms.CryptoKeyVersion.CryptoKeyVersionAlgorithm.GOOGLE_SYMMETRIC_ENCRYPTION, "protection_level": kms.ProtectionLevel.HSM, }, "labels": {"foo": "bar", "zip": "zap"}, @@ -449,9 +452,13 @@ def hsm_key_id( print(f"Destroying the key version {key.name}") try: time.sleep(5) - for key_version in kms_key_client.list_crypto_key_versions(request={"parent": key.name}): + for key_version in kms_key_client.list_crypto_key_versions( + request={"parent": key.name} + ): if key_version.state == key_version.state.ENABLED: - retry_client_destroy_crypto_key(kms_key_client, request={"name": key_version.name}) + retry_client_destroy_crypto_key( + kms_key_client, request={"name": key_version.name} + ) except exceptions.NotFound: # KMS key was already deleted, probably in the test print(f"KMS Key {key.name} was not found.") @@ -473,8 +480,7 @@ def updated_hsm_key_id( "crypto_key": { "purpose": kms.CryptoKey.CryptoKeyPurpose.ENCRYPT_DECRYPT, "version_template": { - "algorithm": - kms.CryptoKeyVersion.CryptoKeyVersionAlgorithm.GOOGLE_SYMMETRIC_ENCRYPTION, + "algorithm": kms.CryptoKeyVersion.CryptoKeyVersionAlgorithm.GOOGLE_SYMMETRIC_ENCRYPTION, "protection_level": kms.ProtectionLevel.HSM, }, "labels": {"foo": "bar", "zip": "zap"}, @@ -486,9 +492,13 @@ def updated_hsm_key_id( print(f"Destroying the key version {key.name}") try: time.sleep(5) - for key_version in kms_key_client.list_crypto_key_versions(request={"parent": key.name}): + for key_version in kms_key_client.list_crypto_key_versions( + request={"parent": key.name} + ): if key_version.state == key_version.state.ENABLED: - retry_client_destroy_crypto_key(kms_key_client, request={"name": key_version.name}) + retry_client_destroy_crypto_key( + kms_key_client, request={"name": key_version.name} + ) except exceptions.NotFound: # KMS key was already deleted, probably in the test print(f"KMS Key {key.name} was not found.") @@ -498,7 +508,9 @@ def test_regional_quickstart( project_id: str, location_id: str, parameter_id: Tuple[str, str] ) -> None: param_id, version_id = parameter_id - regional_quickstart.regional_quickstart(project_id, location_id, param_id, version_id) + regional_quickstart.regional_quickstart( + project_id, location_id, param_id, version_id + ) def test_create_regional_param( @@ -507,15 +519,14 @@ def test_create_regional_param( parameter_id: str, ) -> None: param_id, _ = parameter_id - parameter = create_regional_param.create_regional_param(project_id, location_id, param_id) + parameter = create_regional_param.create_regional_param( + project_id, location_id, param_id + ) assert param_id in parameter.name def test_create_regional_param_with_kms_key( - project_id: str, - location_id: str, - parameter_id: str, - hsm_key_id: str + project_id: str, location_id: str, parameter_id: str, hsm_key_id: str ) -> None: param_id, _ = parameter_id parameter = create_regional_param_with_kms_key.create_regional_param_with_kms_key( @@ -529,7 +540,7 @@ def test_update_regional_param_kms_key( project_id: str, location_id: str, parameter_with_kms: Tuple[str, str, str, str], - updated_hsm_key_id: str + updated_hsm_key_id: str, ) -> None: project_id, param_id, _, kms_key = parameter_with_kms parameter = update_regional_param_kms_key.update_regional_param_kms_key( @@ -544,7 +555,7 @@ def test_remove_regional_param_kms_key( project_id: str, location_id: str, parameter_with_kms: Tuple[str, str, str, str], - hsm_key_id: str + hsm_key_id: str, ) -> None: project_id, param_id, _, kms_key = parameter_with_kms parameter = remove_regional_param_kms_key.remove_regional_param_kms_key( @@ -625,7 +636,9 @@ def test_delete_regional_param_version( parameter_version: Tuple[str, str, str, str], ) -> None: project_id, param_id, version_id, _ = parameter_version - delete_regional_param_version.delete_regional_param_version(project_id, location_id, param_id, version_id) + delete_regional_param_version.delete_regional_param_version( + project_id, location_id, param_id, version_id + ) with pytest.raises(exceptions.NotFound): print(f"{client}") name = client.parameter_version_path( @@ -656,7 +669,9 @@ def test_enable_regional_param_version( def test_get_regional_param(parameter: Tuple[str, str, str], location_id: str) -> None: project_id, param_id, _ = parameter - snippet_param = get_regional_param.get_regional_param(project_id, location_id, param_id) + snippet_param = get_regional_param.get_regional_param( + project_id, location_id, param_id + ) assert param_id in snippet_param.name @@ -664,7 +679,9 @@ def test_get_regional_param_version( parameter_version: Tuple[str, str, str, str], location_id: str ) -> None: project_id, param_id, version_id, payload = parameter_version - version = get_regional_param_version.get_regional_param_version(project_id, location_id, param_id, version_id) + version = get_regional_param_version.get_regional_param_version( + project_id, location_id, param_id, version_id + ) assert param_id in version.name assert version_id in version.name assert version.payload.data == payload @@ -680,7 +697,10 @@ def test_list_regional_params( list_regional_params.list_regional_params(project_id, location_id) out, _ = capsys.readouterr() - assert f"Found regional parameter {got_param.name} with format {got_param.format_.name}" in out + assert ( + f"Found regional parameter {got_param.name} with format {got_param.format_.name}" + in out + ) def test_list_param_regional_versions( @@ -692,7 +712,9 @@ def test_list_param_regional_versions( version_1 = get_regional_param_version.get_regional_param_version( project_id, location_id, param_id, version_id ) - list_regional_param_versions.list_regional_param_versions(project_id, location_id, param_id) + list_regional_param_versions.list_regional_param_versions( + project_id, location_id, param_id + ) out, _ = capsys.readouterr() assert param_id in out @@ -726,7 +748,9 @@ def wait_for_ready( kms_key_client: kms.KeyManagementServiceClient, key_version_name: str ) -> None: for i in range(4): - key_version = kms_key_client.get_crypto_key_version(request={"name": key_version_name}) + key_version = kms_key_client.get_crypto_key_version( + request={"name": key_version_name} + ) if key_version.state == kms.CryptoKeyVersion.CryptoKeyVersionState.ENABLED: return time.sleep((i + 1) ** 2) @@ -800,7 +824,9 @@ def template( request={ "parent": parent, "template_id": tmpl_id, - "template": {"format": parametermanager_v1.TemplateFormat.TEMPLATE_FORMAT_JSON}, + "template": { + "format": parametermanager_v1.TemplateFormat.TEMPLATE_FORMAT_JSON + }, }, ) @@ -855,7 +881,9 @@ def json_parameter_version( request={ "parent": client.parameter_path(project_id, location_id, param_id), "parameter_version_id": version_id, - "parameter_version": {"payload": {"data": json.dumps(values).encode("utf-8")}}, + "parameter_version": { + "payload": {"data": json.dumps(values).encode("utf-8")} + }, } ) @@ -869,16 +897,23 @@ def tag_key_value() -> Tuple[str, str]: tag_key = os.environ.get("PARAMETER_MANAGER_TAG_KEY") tag_value = os.environ.get("PARAMETER_MANAGER_TAG_VALUE") if not tag_key or not tag_value: - pytest.skip("PARAMETER_MANAGER_TAG_KEY and PARAMETER_MANAGER_TAG_VALUE are not set") + pytest.skip( + "PARAMETER_MANAGER_TAG_KEY and PARAMETER_MANAGER_TAG_VALUE are not set" + ) return tag_key, tag_value def test_regional_template_quickstart( - project_id: str, location_id: str, template_id: Tuple[str, str], parameter_id: Tuple[str, str] + project_id: str, + location_id: str, + template_id: Tuple[str, str], + parameter_id: Tuple[str, str], ) -> None: tmpl_id, version_id = template_id param_id, _ = parameter_id - regional_template_quickstart.regional_template_quickstart(project_id, location_id, tmpl_id, param_id, version_id) + regional_template_quickstart.regional_template_quickstart( + project_id, location_id, tmpl_id, param_id, version_id + ) def test_create_regional_param_template( @@ -886,7 +921,10 @@ def test_create_regional_param_template( ) -> None: tmpl_id, _ = template_id tmpl = create_regional_param_template.create_regional_param_template( - project_id, location_id, tmpl_id, parametermanager_v1.TemplateFormat.TEMPLATE_FORMAT_JSON + project_id, + location_id, + tmpl_id, + parametermanager_v1.TemplateFormat.TEMPLATE_FORMAT_JSON, ) assert tmpl_id in tmpl.name assert tmpl.format_ == parametermanager_v1.TemplateFormat.TEMPLATE_FORMAT_JSON @@ -898,16 +936,24 @@ def test_create_regional_param_template_version( template: Tuple[str, str, str], ) -> None: project_id, tmpl_id, version_id = template - version = create_regional_param_template_version.create_regional_param_template_version(project_id, location_id, tmpl_id, version_id) + version = ( + create_regional_param_template_version.create_regional_param_template_version( + project_id, location_id, tmpl_id, version_id + ) + ) assert tmpl_id in version.name assert version_id in version.name got = client.get_template_version(request={"name": version.name}) assert b"{{.username}}" in got.payload.data -def test_get_regional_param_template(location_id: str, template: Tuple[str, str, str]) -> None: +def test_get_regional_param_template( + location_id: str, template: Tuple[str, str, str] +) -> None: project_id, tmpl_id, _ = template - tmpl = get_regional_param_template.get_regional_param_template(project_id, location_id, tmpl_id) + tmpl = get_regional_param_template.get_regional_param_template( + project_id, location_id, tmpl_id + ) assert tmpl_id in tmpl.name @@ -917,7 +963,9 @@ def test_list_regional_param_templates( template: Tuple[str, str, str], ) -> None: project_id, tmpl_id, _ = template - got = get_regional_param_template.get_regional_param_template(project_id, location_id, tmpl_id) + got = get_regional_param_template.get_regional_param_template( + project_id, location_id, tmpl_id + ) list_regional_param_templates.list_regional_param_templates(project_id, location_id) out, _ = capsys.readouterr() @@ -928,7 +976,9 @@ def test_get_regional_param_template_version( location_id: str, template_version: Tuple[str, str, str, bytes] ) -> None: project_id, tmpl_id, version_id, payload = template_version - version = get_regional_param_template_version.get_regional_param_template_version(project_id, location_id, tmpl_id, version_id) + version = get_regional_param_template_version.get_regional_param_template_version( + project_id, location_id, tmpl_id, version_id + ) assert tmpl_id in version.name assert version_id in version.name assert version.payload.data == payload @@ -940,8 +990,12 @@ def test_list_regional_param_template_versions( template_version: Tuple[str, str, str, bytes], ) -> None: project_id, tmpl_id, version_id, _ = template_version - version = get_regional_param_template_version.get_regional_param_template_version(project_id, location_id, tmpl_id, version_id) - list_regional_param_template_versions.list_regional_param_template_versions(project_id, location_id, tmpl_id) + version = get_regional_param_template_version.get_regional_param_template_version( + project_id, location_id, tmpl_id, version_id + ) + list_regional_param_template_versions.list_regional_param_template_versions( + project_id, location_id, tmpl_id + ) out, _ = capsys.readouterr() assert f"Found regional template version: {version.name}" in out @@ -955,7 +1009,9 @@ def test_update_regional_param_template_labels( project_id, location_id, tmpl_id, label_key, label_value ) assert tmpl.labels[label_key] == label_value - got = get_regional_param_template.get_regional_param_template(project_id, location_id, tmpl_id) + got = get_regional_param_template.get_regional_param_template( + project_id, location_id, tmpl_id + ) assert got.labels[label_key] == label_value @@ -965,7 +1021,9 @@ def test_delete_regional_param_template( template: Tuple[str, str, str], ) -> None: project_id, tmpl_id, _ = template - delete_regional_param_template.delete_regional_param_template(project_id, location_id, tmpl_id) + delete_regional_param_template.delete_regional_param_template( + project_id, location_id, tmpl_id + ) with pytest.raises(exceptions.NotFound): client.get_template( request={"name": client.template_path(project_id, location_id, tmpl_id)} @@ -976,7 +1034,11 @@ def test_disable_regional_param_template_version( location_id: str, template_version: Tuple[str, str, str, bytes] ) -> None: project_id, tmpl_id, version_id, _ = template_version - version = disable_regional_param_template_version.disable_regional_param_template_version(project_id, location_id, tmpl_id, version_id) + version = ( + disable_regional_param_template_version.disable_regional_param_template_version( + project_id, location_id, tmpl_id, version_id + ) + ) assert version.disabled is True @@ -992,7 +1054,11 @@ def test_enable_regional_param_template_version( client.update_template_version( request={"template_version": version, "update_mask": {"paths": ["disabled"]}} ) - version = enable_regional_param_template_version.enable_regional_param_template_version(project_id, location_id, tmpl_id, version_id) + version = ( + enable_regional_param_template_version.enable_regional_param_template_version( + project_id, location_id, tmpl_id, version_id + ) + ) assert version.disabled is False @@ -1002,7 +1068,9 @@ def test_delete_regional_param_template_version( template_version: Tuple[str, str, str, bytes], ) -> None: project_id, tmpl_id, version_id, _ = template_version - delete_regional_param_template_version.delete_regional_param_template_version(project_id, location_id, tmpl_id, version_id) + delete_regional_param_template_version.delete_regional_param_template_version( + project_id, location_id, tmpl_id, version_id + ) with pytest.raises(exceptions.NotFound): client.get_template_version( request={ @@ -1020,8 +1088,10 @@ def test_render_regional_param_template_version( ) -> None: project_id, tmpl_id, version_id, payload = template_version _, param_id, param_version_id, values = json_parameter_version - response = render_regional_param_template_version.render_regional_param_template_version( - project_id, location_id, tmpl_id, version_id, param_id, param_version_id + response = ( + render_regional_param_template_version.render_regional_param_template_version( + project_id, location_id, tmpl_id, version_id, param_id, param_version_id + ) ) assert response.payload.data == payload assert param_id in response.parameter_version @@ -1042,7 +1112,11 @@ def test_create_regional_param_with_tags( assert param_id in parameter.name # Tags are input-only, so confirm them through Resource Manager tag bindings. - bindings_client = resourcemanager_v3.TagBindingsClient(client_options={"api_endpoint": f"{location_id}-cloudresourcemanager.googleapis.com"}) + bindings_client = resourcemanager_v3.TagBindingsClient( + client_options={ + "api_endpoint": f"{location_id}-cloudresourcemanager.googleapis.com" + } + ) bindings = bindings_client.list_tag_bindings( request={"parent": f"//parametermanager.googleapis.com/{parameter.name}"} ) @@ -1119,7 +1193,9 @@ def test_bind_tags_to_regional_param( ) -> None: param_id, _ = parameter_id _, tag_value = tag_key_value - binding = bind_tags_to_regional_param.bind_tags_to_regional_param(project_id, location_id, param_id, tag_value) + binding = bind_tags_to_regional_param.bind_tags_to_regional_param( + project_id, location_id, param_id, tag_value + ) assert binding.tag_value == tag_value assert param_id in binding.parent @@ -1141,5 +1217,7 @@ def test_get_regional_param_tags( "parameter": {"tags": {tag_key: tag_value}}, }, ) - bindings = get_regional_param_tags.get_regional_param_tags(project_id, location_id, param_id) + bindings = get_regional_param_tags.get_regional_param_tags( + project_id, location_id, param_id + ) assert tag_value in [binding.tag_value for binding in bindings] diff --git a/parametermanager/snippets/remove_param_kms_key.py b/parametermanager/snippets/remove_param_kms_key.py index 64db832afcd..c5308bed46a 100644 --- a/parametermanager/snippets/remove_param_kms_key.py +++ b/parametermanager/snippets/remove_param_kms_key.py @@ -15,6 +15,7 @@ """ command line application and sample code for removing the kms key of the parameter. """ + from google.cloud import parametermanager_v1 diff --git a/parametermanager/snippets/snippets_test.py b/parametermanager/snippets/snippets_test.py index 94fd8118644..7571850a200 100644 --- a/parametermanager/snippets/snippets_test.py +++ b/parametermanager/snippets/snippets_test.py @@ -741,7 +741,9 @@ def template( request={ "parent": parent, "template_id": tmpl_id, - "template": {"format": parametermanager_v1.TemplateFormat.TEMPLATE_FORMAT_JSON}, + "template": { + "format": parametermanager_v1.TemplateFormat.TEMPLATE_FORMAT_JSON + }, }, ) @@ -796,7 +798,9 @@ def json_parameter_version( request={ "parent": client.parameter_path(project_id, location_id, param_id), "parameter_version_id": version_id, - "parameter_version": {"payload": {"data": json.dumps(values).encode("utf-8")}}, + "parameter_version": { + "payload": {"data": json.dumps(values).encode("utf-8")} + }, } ) @@ -810,12 +814,17 @@ def tag_key_value() -> Tuple[str, str]: tag_key = os.environ.get("PARAMETER_MANAGER_TAG_KEY") tag_value = os.environ.get("PARAMETER_MANAGER_TAG_VALUE") if not tag_key or not tag_value: - pytest.skip("PARAMETER_MANAGER_TAG_KEY and PARAMETER_MANAGER_TAG_VALUE are not set") + pytest.skip( + "PARAMETER_MANAGER_TAG_KEY and PARAMETER_MANAGER_TAG_VALUE are not set" + ) return tag_key, tag_value def test_template_quickstart( - project_id: str, location_id: str, template_id: Tuple[str, str], parameter_id: Tuple[str, str] + project_id: str, + location_id: str, + template_id: Tuple[str, str], + parameter_id: Tuple[str, str], ) -> None: tmpl_id, version_id = template_id param_id, _ = parameter_id @@ -892,9 +901,7 @@ def test_update_param_template_labels( location_id: str, template: Tuple[str, str, str], label_key: str, label_value: str ) -> None: project_id, tmpl_id, _ = template - tmpl = update_param_template_labels( - project_id, tmpl_id, label_key, label_value - ) + tmpl = update_param_template_labels(project_id, tmpl_id, label_key, label_value) assert tmpl.labels[label_key] == label_value got = get_param_template(project_id, tmpl_id) assert got.labels[label_key] == label_value @@ -977,9 +984,7 @@ def test_create_param_with_tags( ) -> None: param_id, _ = parameter_id tag_key, tag_value = tag_key_value - parameter = create_param_with_tags( - project_id, param_id, tag_key, tag_value - ) + parameter = create_param_with_tags(project_id, param_id, tag_key, tag_value) assert param_id in parameter.name # Tags are input-only, so confirm them through Resource Manager tag bindings. @@ -996,9 +1001,7 @@ def test_create_param_version_with_checksum( parameter: Tuple[str, str, str], ) -> None: project_id, param_id, version_id = parameter - version = create_param_version_with_checksum( - project_id, param_id, version_id - ) + version = create_param_version_with_checksum(project_id, param_id, version_id) assert param_id in version.name assert ( version.checksum_source @@ -1016,9 +1019,7 @@ def test_get_param_version_verify_checksum( parameter_version: Tuple[str, str, str, bytes], ) -> None: project_id, param_id, version_id, payload = parameter_version - version = get_param_version_verify_checksum( - project_id, param_id, version_id - ) + version = get_param_version_verify_checksum(project_id, param_id, version_id) assert version.payload.data == payload assert version.payload.data_crc32c == google_crc32c.value(payload) From 1fc0c34b58373681fbb26fed065a0bacf2c021c7 Mon Sep 17 00:00:00 2001 From: smalaviya-crest Date: Wed, 7 Oct 2026 17:26:35 +0530 Subject: [PATCH 6/7] style(parametermanager): restore local import grouping in snippets_test --- parametermanager/snippets/snippets_test.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/parametermanager/snippets/snippets_test.py b/parametermanager/snippets/snippets_test.py index 7571850a200..941757aa546 100644 --- a/parametermanager/snippets/snippets_test.py +++ b/parametermanager/snippets/snippets_test.py @@ -16,8 +16,13 @@ from typing import Iterator, Optional, Tuple, Union import uuid +from google.api_core import exceptions, retry +from google.cloud import kms, parametermanager_v1, resourcemanager_v3, secretmanager +import google_crc32c +import pytest + # Import the methods to be tested -from bind_tags_to_param import bind_tags_to_param +from bind_tags_to_param import bind_tags_to_param # noqa: I100 from create_param import create_param from create_param_template import create_param_template from create_param_template_version import create_param_template_version @@ -42,14 +47,10 @@ from get_param_template_version import get_param_template_version from get_param_version import get_param_version from get_param_version_verify_checksum import get_param_version_verify_checksum -from google.api_core import exceptions, retry -from google.cloud import kms, parametermanager_v1, resourcemanager_v3, secretmanager -import google_crc32c from list_param_template_versions import list_param_template_versions from list_param_templates import list_param_templates from list_param_versions import list_param_versions from list_params import list_params -import pytest from quickstart import quickstart from remove_param_kms_key import remove_param_kms_key from render_param_template_version import render_param_template_version From 5238d5dd7955a33c02cb384fb4438de9065e7353 Mon Sep 17 00:00:00 2001 From: smalaviya-crest Date: Fri, 9 Oct 2026 16:30:27 +0530 Subject: [PATCH 7/7] docs(parametermanager): shorten docstrings and comments in templates, tags and checksum samples Align docstrings and comments with the other samples. --- parametermanager/snippets/bind_tags_to_param.py | 3 +-- .../snippets/create_param_template_version.py | 9 ++------- .../snippets/create_param_version_with_checksum.py | 10 +++------- parametermanager/snippets/create_param_with_tags.py | 5 +---- parametermanager/snippets/get_param_tags.py | 8 ++------ .../snippets/get_param_version_verify_checksum.py | 7 ++----- .../regional_samples/bind_tags_to_regional_param.py | 3 +-- .../create_regional_param_template_version.py | 9 ++------- .../create_regional_param_version_with_checksum.py | 10 +++------- .../create_regional_param_with_tags.py | 5 +---- .../regional_samples/get_regional_param_tags.py | 8 ++------ .../get_regional_param_version_verify_checksum.py | 7 ++----- .../regional_samples/regional_template_quickstart.py | 4 +--- .../snippets/regional_samples/snippets_test.py | 5 ++--- .../update_regional_param_template_labels.py | 3 +-- parametermanager/snippets/snippets_test.py | 5 ++--- parametermanager/snippets/template_quickstart.py | 4 +--- .../snippets/update_param_template_labels.py | 3 +-- 18 files changed, 30 insertions(+), 78 deletions(-) diff --git a/parametermanager/snippets/bind_tags_to_param.py b/parametermanager/snippets/bind_tags_to_param.py index 085d71f0afa..0b66ff8cca6 100644 --- a/parametermanager/snippets/bind_tags_to_param.py +++ b/parametermanager/snippets/bind_tags_to_param.py @@ -25,8 +25,7 @@ def bind_tags_to_param( project_id: str, parameter_id: str, tag_value: str ) -> resourcemanager_v3.TagBinding: """ - Creates a parameter in the global location of the specified project and then - binds an existing tag value to it through the Resource Manager API. + Creates a parameter and binds a tag value to it. Args: project_id (str): The ID of the project. diff --git a/parametermanager/snippets/create_param_template_version.py b/parametermanager/snippets/create_param_template_version.py index f1ed231bbfe..40ffe4a5ef4 100644 --- a/parametermanager/snippets/create_param_template_version.py +++ b/parametermanager/snippets/create_param_template_version.py @@ -25,11 +25,7 @@ def create_param_template_version( project_id: str, template_id: str, version_id: str ) -> parametermanager_v1.TemplateVersion: """ - Creates a new version of an existing template in the global location of the - specified project using the Google Cloud Parameter Manager SDK. The payload - is a JSON document with a placeholder, {{.variableName}}, for each value - that is resolved from a parameter version when the template version is - rendered. + Creates a new template version with placeholders in its payload. Args: project_id (str): The ID of the project. @@ -58,8 +54,7 @@ def create_param_template_version( # Build the resource name of the template. parent = client.template_path(project_id, "global", template_id) - # Define the template payload. Each {{.variableName}} placeholder is - # replaced with the matching key of the parameter version at render time. + # Define the template payload. payload_data = {"username": "{{.username}}", "host": "{{.host}}"} payload = parametermanager_v1.TemplateVersionPayload( data=json.dumps(payload_data).encode("utf-8") diff --git a/parametermanager/snippets/create_param_version_with_checksum.py b/parametermanager/snippets/create_param_version_with_checksum.py index ce8386bad8c..910cbc04a73 100644 --- a/parametermanager/snippets/create_param_version_with_checksum.py +++ b/parametermanager/snippets/create_param_version_with_checksum.py @@ -25,10 +25,7 @@ def create_param_version_with_checksum( project_id: str, parameter_id: str, version_id: str ) -> parametermanager_v1.ParameterVersion: """ - Creates a new version of an existing parameter in the global location of the - specified project, sending a CRC32C checksum of the payload so that the - server can verify the data it received, using the Google Cloud Parameter - Manager SDK. + Creates a parameter version with a CRC32C checksum of the payload. Args: project_id (str): The ID of the project. @@ -57,7 +54,7 @@ def create_param_version_with_checksum( # Build the resource name of the parameter. parent = client.parameter_path(project_id, "global", parameter_id) - # Define the payload and compute its CRC32C checksum (Castagnoli). + # Define the payload and compute its checksum. payload_data = b"hello world!" data_crc32c = google_crc32c.value(payload_data) @@ -72,8 +69,7 @@ def create_param_version_with_checksum( ), ) - # Create the parameter version. The request fails with a - # CHECKSUM_MISMATCH error if the checksum does not match the payload. + # Create the parameter version. response = client.create_parameter_version(request=request) # Print the newly created parameter version name and checksum source. diff --git a/parametermanager/snippets/create_param_with_tags.py b/parametermanager/snippets/create_param_with_tags.py index 37500cfb879..501d369b1ce 100644 --- a/parametermanager/snippets/create_param_with_tags.py +++ b/parametermanager/snippets/create_param_with_tags.py @@ -25,10 +25,7 @@ def create_param_with_tags( project_id: str, parameter_id: str, tag_key: str, tag_value: str ) -> parametermanager_v1.Parameter: """ - Creates a parameter with a tag in the global location of the specified - project using the Google Cloud Parameter Manager SDK. Tags can only be set - when the parameter is created. They are not returned when the parameter is - retrieved. To view a tag binding, use the Resource Manager API. + Creates a parameter with a tag. Args: project_id (str): The ID of the project. diff --git a/parametermanager/snippets/get_param_tags.py b/parametermanager/snippets/get_param_tags.py index 5b803b7852f..2097cb294fe 100644 --- a/parametermanager/snippets/get_param_tags.py +++ b/parametermanager/snippets/get_param_tags.py @@ -25,10 +25,7 @@ def get_param_tags( project_id: str, parameter_id: str ) -> list[resourcemanager_v3.TagBinding]: """ - Gets the tags bound to an existing parameter in the global location of the - specified project. - Tags are input-only on the parameter itself, so they are read as - tag bindings through the Resource Manager API. + Gets the tag bindings of a parameter. Args: project_id (str): The ID of the project. @@ -53,8 +50,7 @@ def get_param_tags( # Create the Resource Manager tag bindings client. resource_manager_client = resourcemanager_v3.TagBindingsClient() - # Build the resource name of the parameter and get it. The parameter name - # contains the project number, which the tag bindings parent requires. + # Get the parameter. name = client.parameter_path(project_id, "global", parameter_id) parameter = client.get_parameter(request={"name": name}) diff --git a/parametermanager/snippets/get_param_version_verify_checksum.py b/parametermanager/snippets/get_param_version_verify_checksum.py index ff48eff13bf..cf8e5eb100f 100644 --- a/parametermanager/snippets/get_param_version_verify_checksum.py +++ b/parametermanager/snippets/get_param_version_verify_checksum.py @@ -25,9 +25,7 @@ def get_param_version_verify_checksum( project_id: str, parameter_id: str, version_id: str ) -> parametermanager_v1.ParameterVersion: """ - Retrieves a specific version of an existing parameter in the global location - of the specified project and verifies the returned payload against the - returned CRC32C checksum using the Google Cloud Parameter Manager SDK. + Gets a parameter version and verifies its payload checksum. Args: project_id (str): The ID of the project. @@ -61,8 +59,7 @@ def get_param_version_verify_checksum( ) response = client.get_parameter_version(request=request) - # Recompute the CRC32C checksum of the payload and compare it with the - # checksum returned by the server. + # Verify the payload checksum. computed_crc32c = google_crc32c.value(response.payload.data) if computed_crc32c != response.payload.data_crc32c: raise ValueError(f"Checksum mismatch for {response.name}: data corrupted") diff --git a/parametermanager/snippets/regional_samples/bind_tags_to_regional_param.py b/parametermanager/snippets/regional_samples/bind_tags_to_regional_param.py index 421d5cf1220..f76f699c0ec 100644 --- a/parametermanager/snippets/regional_samples/bind_tags_to_regional_param.py +++ b/parametermanager/snippets/regional_samples/bind_tags_to_regional_param.py @@ -25,8 +25,7 @@ def bind_tags_to_regional_param( project_id: str, location_id: str, parameter_id: str, tag_value: str ) -> resourcemanager_v3.TagBinding: """ - Creates a parameter in the specified region of the specified project and then - binds an existing tag value to it through the Resource Manager API. + Creates a parameter and binds a tag value to it. Args: project_id (str): The ID of the project. diff --git a/parametermanager/snippets/regional_samples/create_regional_param_template_version.py b/parametermanager/snippets/regional_samples/create_regional_param_template_version.py index 8e0fdd926e2..992f1eee1fa 100644 --- a/parametermanager/snippets/regional_samples/create_regional_param_template_version.py +++ b/parametermanager/snippets/regional_samples/create_regional_param_template_version.py @@ -25,11 +25,7 @@ def create_regional_param_template_version( project_id: str, location_id: str, template_id: str, version_id: str ) -> parametermanager_v1.TemplateVersion: """ - Creates a new version of an existing template in the specified region of the - specified project using the Google Cloud Parameter Manager SDK. The payload - is a JSON document with a placeholder, {{.variableName}}, for each value - that is resolved from a parameter version when the template version is - rendered. + Creates a new template version with placeholders in its payload. Args: project_id (str): The ID of the project. @@ -63,8 +59,7 @@ def create_regional_param_template_version( # Build the resource name of the template. parent = client.template_path(project_id, location_id, template_id) - # Define the template payload. Each {{.variableName}} placeholder is - # replaced with the matching key of the parameter version at render time. + # Define the template payload. payload_data = {"username": "{{.username}}", "host": "{{.host}}"} payload = parametermanager_v1.TemplateVersionPayload( data=json.dumps(payload_data).encode("utf-8") diff --git a/parametermanager/snippets/regional_samples/create_regional_param_version_with_checksum.py b/parametermanager/snippets/regional_samples/create_regional_param_version_with_checksum.py index 56fd104a9ae..ae324eab3b7 100644 --- a/parametermanager/snippets/regional_samples/create_regional_param_version_with_checksum.py +++ b/parametermanager/snippets/regional_samples/create_regional_param_version_with_checksum.py @@ -25,10 +25,7 @@ def create_regional_param_version_with_checksum( project_id: str, location_id: str, parameter_id: str, version_id: str ) -> parametermanager_v1.ParameterVersion: """ - Creates a new version of an existing parameter in the specified region of - the specified project, sending a CRC32C checksum of the payload so that the - server can verify the data it received, using the Google Cloud Parameter - Manager SDK. + Creates a parameter version with a CRC32C checksum of the payload. Args: project_id (str): The ID of the project. @@ -62,7 +59,7 @@ def create_regional_param_version_with_checksum( # Build the resource name of the parameter. parent = client.parameter_path(project_id, location_id, parameter_id) - # Define the payload and compute its CRC32C checksum (Castagnoli). + # Define the payload and compute its checksum. payload_data = b"hello world!" data_crc32c = google_crc32c.value(payload_data) @@ -77,8 +74,7 @@ def create_regional_param_version_with_checksum( ), ) - # Create the parameter version. The request fails with a - # CHECKSUM_MISMATCH error if the checksum does not match the payload. + # Create the parameter version. response = client.create_parameter_version(request=request) # Print the newly created parameter version name and checksum source. diff --git a/parametermanager/snippets/regional_samples/create_regional_param_with_tags.py b/parametermanager/snippets/regional_samples/create_regional_param_with_tags.py index c15f51f6e8f..e44f68902d1 100644 --- a/parametermanager/snippets/regional_samples/create_regional_param_with_tags.py +++ b/parametermanager/snippets/regional_samples/create_regional_param_with_tags.py @@ -25,10 +25,7 @@ def create_regional_param_with_tags( project_id: str, location_id: str, parameter_id: str, tag_key: str, tag_value: str ) -> parametermanager_v1.Parameter: """ - Creates a parameter with a tag in the specified region of the specified - project using the Google Cloud Parameter Manager SDK. Tags can only be set - when the parameter is created. They are not returned when the parameter is - retrieved. To view a tag binding, use the Resource Manager API. + Creates a parameter with a tag. Args: project_id (str): The ID of the project. diff --git a/parametermanager/snippets/regional_samples/get_regional_param_tags.py b/parametermanager/snippets/regional_samples/get_regional_param_tags.py index cd7530cbb48..937f465b9dd 100644 --- a/parametermanager/snippets/regional_samples/get_regional_param_tags.py +++ b/parametermanager/snippets/regional_samples/get_regional_param_tags.py @@ -25,10 +25,7 @@ def get_regional_param_tags( project_id: str, location_id: str, parameter_id: str ) -> list[resourcemanager_v3.TagBinding]: """ - Gets the tags bound to an existing parameter in the specified region of the - specified project. - Tags are input-only on the parameter itself, so they are read as - tag bindings through the Resource Manager API. + Gets the tag bindings of a parameter. Args: project_id (str): The ID of the project. @@ -62,8 +59,7 @@ def get_regional_param_tags( } ) - # Build the resource name of the parameter and get it. The parameter name - # contains the project number, which the tag bindings parent requires. + # Get the parameter. name = client.parameter_path(project_id, location_id, parameter_id) parameter = client.get_parameter(request={"name": name}) diff --git a/parametermanager/snippets/regional_samples/get_regional_param_version_verify_checksum.py b/parametermanager/snippets/regional_samples/get_regional_param_version_verify_checksum.py index 96c9babd5ba..b27801d1f68 100644 --- a/parametermanager/snippets/regional_samples/get_regional_param_version_verify_checksum.py +++ b/parametermanager/snippets/regional_samples/get_regional_param_version_verify_checksum.py @@ -25,9 +25,7 @@ def get_regional_param_version_verify_checksum( project_id: str, location_id: str, parameter_id: str, version_id: str ) -> parametermanager_v1.ParameterVersion: """ - Retrieves a specific version of an existing parameter in the specified - region of the specified project and verifies the returned payload against - the returned CRC32C checksum using the Google Cloud Parameter Manager SDK. + Gets a parameter version and verifies its payload checksum. Args: project_id (str): The ID of the project. @@ -68,8 +66,7 @@ def get_regional_param_version_verify_checksum( ) response = client.get_parameter_version(request=request) - # Recompute the CRC32C checksum of the payload and compare it with the - # checksum returned by the server. + # Verify the payload checksum. computed_crc32c = google_crc32c.value(response.payload.data) if computed_crc32c != response.payload.data_crc32c: raise ValueError(f"Checksum mismatch for {response.name}: data corrupted") diff --git a/parametermanager/snippets/regional_samples/regional_template_quickstart.py b/parametermanager/snippets/regional_samples/regional_template_quickstart.py index 45656d27a9d..ec2b6e48e9e 100644 --- a/parametermanager/snippets/regional_samples/regional_template_quickstart.py +++ b/parametermanager/snippets/regional_samples/regional_template_quickstart.py @@ -27,9 +27,7 @@ def regional_template_quickstart( version_id: str, ) -> None: """ - Quickstart example for using Google Cloud Parameter Manager templates to - create a regional template and a template version with placeholders, create a - parameter version that holds the values, and render the template version. + Creates a template, a parameter version and renders the template version. Args: project_id (str): The ID of the project. diff --git a/parametermanager/snippets/regional_samples/snippets_test.py b/parametermanager/snippets/regional_samples/snippets_test.py index e926ac61bc5..0382fc31c81 100644 --- a/parametermanager/snippets/regional_samples/snippets_test.py +++ b/parametermanager/snippets/regional_samples/snippets_test.py @@ -892,8 +892,7 @@ def json_parameter_version( @pytest.fixture() def tag_key_value() -> Tuple[str, str]: - # Tags are created outside of the tests (they require Resource Manager - # permissions), so the IDs are supplied through the environment. + # Read the tag key and value from the environment. tag_key = os.environ.get("PARAMETER_MANAGER_TAG_KEY") tag_value = os.environ.get("PARAMETER_MANAGER_TAG_VALUE") if not tag_key or not tag_value: @@ -1111,7 +1110,7 @@ def test_create_regional_param_with_tags( ) assert param_id in parameter.name - # Tags are input-only, so confirm them through Resource Manager tag bindings. + # Check the tag bindings of the parameter. bindings_client = resourcemanager_v3.TagBindingsClient( client_options={ "api_endpoint": f"{location_id}-cloudresourcemanager.googleapis.com" diff --git a/parametermanager/snippets/regional_samples/update_regional_param_template_labels.py b/parametermanager/snippets/regional_samples/update_regional_param_template_labels.py index 38b102e34e8..4c7e850c0ae 100644 --- a/parametermanager/snippets/regional_samples/update_regional_param_template_labels.py +++ b/parametermanager/snippets/regional_samples/update_regional_param_template_labels.py @@ -74,8 +74,7 @@ def update_regional_param_template_labels( ) client.update_template(request=request) - # The update response does not include the labels, so get the template - # again to read the stored labels. + # Get the updated template. response = client.get_template(request={"name": name}) # Print the updated labels. diff --git a/parametermanager/snippets/snippets_test.py b/parametermanager/snippets/snippets_test.py index 941757aa546..d23e4266a9b 100644 --- a/parametermanager/snippets/snippets_test.py +++ b/parametermanager/snippets/snippets_test.py @@ -810,8 +810,7 @@ def json_parameter_version( @pytest.fixture() def tag_key_value() -> Tuple[str, str]: - # Tags are created outside of the tests (they require Resource Manager - # permissions), so the IDs are supplied through the environment. + # Read the tag key and value from the environment. tag_key = os.environ.get("PARAMETER_MANAGER_TAG_KEY") tag_value = os.environ.get("PARAMETER_MANAGER_TAG_VALUE") if not tag_key or not tag_value: @@ -988,7 +987,7 @@ def test_create_param_with_tags( parameter = create_param_with_tags(project_id, param_id, tag_key, tag_value) assert param_id in parameter.name - # Tags are input-only, so confirm them through Resource Manager tag bindings. + # Check the tag bindings of the parameter. bindings_client = resourcemanager_v3.TagBindingsClient() bindings = bindings_client.list_tag_bindings( request={"parent": f"//parametermanager.googleapis.com/{parameter.name}"} diff --git a/parametermanager/snippets/template_quickstart.py b/parametermanager/snippets/template_quickstart.py index f7a81a2a5b6..3464f0fbadb 100644 --- a/parametermanager/snippets/template_quickstart.py +++ b/parametermanager/snippets/template_quickstart.py @@ -23,9 +23,7 @@ def template_quickstart( project_id: str, template_id: str, parameter_id: str, version_id: str ) -> None: """ - Quickstart example for using Google Cloud Parameter Manager templates to - create a template and a template version with placeholders, create a - parameter version that holds the values, and render the template version. + Creates a template, a parameter version and renders the template version. Args: project_id (str): The ID of the project. diff --git a/parametermanager/snippets/update_param_template_labels.py b/parametermanager/snippets/update_param_template_labels.py index 460fe24ca6a..9fbf6b13e33 100644 --- a/parametermanager/snippets/update_param_template_labels.py +++ b/parametermanager/snippets/update_param_template_labels.py @@ -69,8 +69,7 @@ def update_param_template_labels( ) client.update_template(request=request) - # The update response does not include the labels, so get the template - # again to read the stored labels. + # Get the updated template. response = client.get_template(request={"name": name}) # Print the updated labels.