Skip to content
82 changes: 82 additions & 0 deletions parametermanager/snippets/bind_tags_to_param.py
Original file line number Diff line number Diff line change
@@ -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
70 changes: 70 additions & 0 deletions parametermanager/snippets/create_param_template.py
Original file line number Diff line number Diff line change
@@ -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
77 changes: 77 additions & 0 deletions parametermanager/snippets/create_param_template_version.py
Original file line number Diff line number Diff line change
@@ -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
80 changes: 80 additions & 0 deletions parametermanager/snippets/create_param_version_with_checksum.py
Original file line number Diff line number Diff line change
@@ -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
70 changes: 70 additions & 0 deletions parametermanager/snippets/create_param_with_tags.py
Original file line number Diff line number Diff line change
@@ -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
Loading
Loading