diff --git a/parametermanager/snippets/bind_tags_to_param.py b/parametermanager/snippets/bind_tags_to_param.py new file mode 100644 index 00000000000..0b66ff8cca6 --- /dev/null +++ b/parametermanager/snippets/bind_tags_to_param.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 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 and binds a tag value to it. + + 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/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..40ffe4a5ef4 --- /dev/null +++ b/parametermanager/snippets/create_param_template_version.py @@ -0,0 +1,77 @@ +#!/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 template version with placeholders in its payload. + + 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. + 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..910cbc04a73 --- /dev/null +++ b/parametermanager/snippets/create_param_version_with_checksum.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 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 parameter version with a CRC32C checksum of the payload. + + 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 checksum. + 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. + 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..501d369b1ce --- /dev/null +++ b/parametermanager/snippets/create_param_with_tags.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 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. + + 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_tags.py b/parametermanager/snippets/get_param_tags.py new file mode 100644 index 00000000000..2097cb294fe --- /dev/null +++ b/parametermanager/snippets/get_param_tags.py @@ -0,0 +1,68 @@ +#!/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 tag bindings of a parameter. + + 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() + + # Get the parameter. + 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/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.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/get_param_version_verify_checksum.py b/parametermanager/snippets/get_param_version_verify_checksum.py new file mode 100644 index 00000000000..cf8e5eb100f --- /dev/null +++ b/parametermanager/snippets/get_param_version_verify_checksum.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 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: + """ + Gets a parameter version and verifies its payload checksum. + + 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) + + # 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") + + # 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/bind_tags_to_regional_param.py b/parametermanager/snippets/regional_samples/bind_tags_to_regional_param.py new file mode 100644 index 00000000000..f76f699c0ec --- /dev/null +++ b/parametermanager/snippets/regional_samples/bind_tags_to_regional_param.py @@ -0,0 +1,91 @@ +#!/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 and binds a tag value to it. + + 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/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..992f1eee1fa --- /dev/null +++ b/parametermanager/snippets/regional_samples/create_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 +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 template version with placeholders in its payload. + + 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. + 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..ae324eab3b7 --- /dev/null +++ b/parametermanager/snippets/regional_samples/create_regional_param_version_with_checksum.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 +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 parameter version with a CRC32C checksum of the payload. + + 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 checksum. + 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. + 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..e44f68902d1 --- /dev/null +++ b/parametermanager/snippets/regional_samples/create_regional_param_with_tags.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 +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. + + 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.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_tags.py b/parametermanager/snippets/regional_samples/get_regional_param_tags.py new file mode 100644 index 00000000000..937f465b9dd --- /dev/null +++ b/parametermanager/snippets/regional_samples/get_regional_param_tags.py @@ -0,0 +1,77 @@ +#!/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 tag bindings of a parameter. + + 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" + } + ) + + # Get the parameter. + 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/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.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/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..b27801d1f68 --- /dev/null +++ b/parametermanager/snippets/regional_samples/get_regional_param_version_verify_checksum.py @@ -0,0 +1,79 @@ +#!/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: + """ + Gets a parameter version and verifies its payload checksum. + + 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) + + # 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") + + # 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/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/regional_template_quickstart.py b/parametermanager/snippets/regional_samples/regional_template_quickstart.py new file mode 100644 index 00000000000..ec2b6e48e9e --- /dev/null +++ b/parametermanager/snippets/regional_samples/regional_template_quickstart.py @@ -0,0 +1,126 @@ +#!/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: + """ + Creates a template, a parameter version and renders 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..0382fc31c81 100644 --- a/parametermanager/snippets/regional_samples/snippets_test.py +++ b/parametermanager/snippets/regional_samples/snippets_test.py @@ -17,32 +17,51 @@ 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 +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_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 @pytest.fixture() @@ -211,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}") @@ -393,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: @@ -417,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"}, @@ -430,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.") @@ -454,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"}, @@ -467,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.") @@ -479,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( @@ -488,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( @@ -510,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( @@ -525,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( @@ -606,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( @@ -637,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 @@ -645,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 @@ -661,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( @@ -673,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 @@ -707,8 +748,475 @@ 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) 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]: + # 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: + 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 + + # Check the tag bindings of the parameter. + 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 + ) + + +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/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..4c7e850c0ae --- /dev/null +++ b/parametermanager/snippets/regional_samples/update_regional_param_template_labels.py @@ -0,0 +1,86 @@ +#!/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) + + # Get the updated template. + 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/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/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..d23e4266a9b 100644 --- a/parametermanager/snippets/snippets_test.py +++ b/parametermanager/snippets/snippets_test.py @@ -17,28 +17,47 @@ 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 +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 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_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 @pytest.fixture() @@ -654,3 +673,415 @@ 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]: + # 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: + 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 + + # 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}"} + ) + 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 + ) + + +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] diff --git a/parametermanager/snippets/template_quickstart.py b/parametermanager/snippets/template_quickstart.py new file mode 100644 index 00000000000..3464f0fbadb --- /dev/null +++ b/parametermanager/snippets/template_quickstart.py @@ -0,0 +1,117 @@ +#!/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: + """ + Creates a template, a parameter version and renders 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..9fbf6b13e33 --- /dev/null +++ b/parametermanager/snippets/update_param_template_labels.py @@ -0,0 +1,79 @@ +#!/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) + + # Get the updated template. + 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