|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright 2026 Google LLC |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +""" |
| 16 | +command line application and sample code for |
| 17 | +creating a new parameter version with a checksum. |
| 18 | +""" |
| 19 | + |
| 20 | +from google.cloud import parametermanager_v1 |
| 21 | + |
| 22 | + |
| 23 | +# [START parametermanager_create_param_version_with_checksum] |
| 24 | +def create_param_version_with_checksum( |
| 25 | + project_id: str, parameter_id: str, version_id: str |
| 26 | +) -> parametermanager_v1.ParameterVersion: |
| 27 | + """ |
| 28 | + Creates a new version of an existing parameter in the global location of the |
| 29 | + specified project, sending a CRC32C checksum of the payload so that the |
| 30 | + server can verify the data it received, using the Google Cloud Parameter |
| 31 | + Manager SDK. |
| 32 | +
|
| 33 | + Args: |
| 34 | + project_id (str): The ID of the project. |
| 35 | + parameter_id (str): The ID of the parameter for which the version is to |
| 36 | + be created. |
| 37 | + version_id (str): The ID of the parameter version to be created. |
| 38 | +
|
| 39 | + Returns: |
| 40 | + parametermanager_v1.ParameterVersion: An object representing the newly created |
| 41 | + parameter version. |
| 42 | +
|
| 43 | + Example: |
| 44 | + create_param_version_with_checksum( |
| 45 | + "my-project", |
| 46 | + "my-parameter", |
| 47 | + "v1" |
| 48 | + ) |
| 49 | + """ |
| 50 | + # Import the necessary library for Google Cloud Parameter Manager. |
| 51 | + from google.cloud import parametermanager_v1 |
| 52 | + import google_crc32c |
| 53 | + |
| 54 | + # Create the Parameter Manager client. |
| 55 | + client = parametermanager_v1.ParameterManagerClient() |
| 56 | + |
| 57 | + # Build the resource name of the parameter. |
| 58 | + parent = client.parameter_path(project_id, "global", parameter_id) |
| 59 | + |
| 60 | + # Define the payload and compute its CRC32C checksum (Castagnoli). |
| 61 | + payload_data = b"hello world!" |
| 62 | + data_crc32c = google_crc32c.value(payload_data) |
| 63 | + |
| 64 | + # Define the parameter version creation request with the checksum. |
| 65 | + request = parametermanager_v1.CreateParameterVersionRequest( |
| 66 | + parent=parent, |
| 67 | + parameter_version_id=version_id, |
| 68 | + parameter_version=parametermanager_v1.ParameterVersion( |
| 69 | + payload=parametermanager_v1.ParameterVersionPayload( |
| 70 | + data=payload_data, data_crc32c=data_crc32c |
| 71 | + ) |
| 72 | + ), |
| 73 | + ) |
| 74 | + |
| 75 | + # Create the parameter version. The request fails with a |
| 76 | + # CHECKSUM_MISMATCH error if the checksum does not match the payload. |
| 77 | + response = client.create_parameter_version(request=request) |
| 78 | + |
| 79 | + # Print the newly created parameter version name and checksum source. |
| 80 | + print(f"Created parameter version: {response.name}") |
| 81 | + print(f"Checksum source: {response.checksum_source.name}") |
| 82 | + # [END parametermanager_create_param_version_with_checksum] |
| 83 | + |
| 84 | + return response |
0 commit comments