Skip to content

Commit aec5ef9

Browse files
feat(parametermanager): created samples for global and regional parameters (#13171)
* feat(parametermanager): created samples for global and regional parameter manager api * feat(parametermanager): removed cli entrypoints * feat(parametermanager): resolved the header-check issue * feat(parametermanager): updated readme file * feat(parametermanager): updated blunderbuss file * feat(parametermanager): created samples for global and regional parameter manager api * feat(parametermanager): removed cli entrypoints * feat(parametermanager): resolved the header-check issue * feat(parametermanager): updated readme file * feat(parametermanager): updated regional package init file and fix linting * feat(parametermanager): fixed printing in test cases * feat(parametermanager): updated print statement * feat(parametermanager): updated method names according to secret manager service * feat(parametermanager): updated method names according to secret manager service * feat(parametermanager): updated print statement
1 parent 1d4f38a commit aec5ef9

37 files changed

+3327
-0
lines changed

parametermanager/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Sample Snippets for Parameter Manager API
2+
======================================
3+
4+
Quick Start
5+
-----------
6+
7+
In order to run these samples, you first need to go through the following steps:
8+
9+
1. `Select or create a Cloud Platform project.`_
10+
2. `Enable billing for your project.`_
11+
3. `Enable the Parameter Manager API.`_
12+
4. `Setup Authentication.`_
13+
14+
.. _Select or create a Cloud Platform project.: https://console.cloud.google.com/project
15+
.. _Enable billing for your project.: https://cloud.google.com/billing/docs/how-to/modify-project#enable_billing_for_a_project
16+
.. _Enable the Parameter Manager API.: https://cloud.google.com/secret-manager/parameter-manager/docs/prepare-environment
17+
.. _Setup Authentication.: https://googleapis.dev/python/google-api-core/latest/auth.html
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2025 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 default format parameter.
18+
"""
19+
20+
from google.cloud import parametermanager_v1
21+
22+
23+
# [START parametermanager_create_param]
24+
def create_param(project_id: str, parameter_id: str) -> parametermanager_v1.Parameter:
25+
"""
26+
Creates a parameter with default format (Unformatted)
27+
in the global location of the specified
28+
project using the Google Cloud Parameter Manager SDK.
29+
30+
Args:
31+
project_id (str): The ID of the project where
32+
the parameter is to be created.
33+
parameter_id (str): The ID to assign to the new parameter.
34+
This ID must be unique within the project.
35+
36+
Returns:
37+
parametermanager_v1.Parameter: An object representing
38+
the newly created parameter.
39+
40+
Example:
41+
create_param(
42+
"my-project",
43+
"my-global-parameter"
44+
)
45+
"""
46+
# Import the necessary library for Google Cloud Parameter Manager.
47+
from google.cloud import parametermanager_v1
48+
49+
# Create the Parameter Manager client.
50+
client = parametermanager_v1.ParameterManagerClient()
51+
52+
# Build the resource name of the parent project in the global location.
53+
parent = client.common_location_path(project_id, "global")
54+
55+
# Define the parameter creation request.
56+
request = parametermanager_v1.CreateParameterRequest(
57+
parent=parent,
58+
parameter_id=parameter_id,
59+
)
60+
61+
# Create the parameter.
62+
response = client.create_parameter(request=request)
63+
64+
# Print the newly created parameter name.
65+
print(f"Created parameter: {response.name}")
66+
# [END parametermanager_create_param]
67+
68+
return response
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2025 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 unformatted parameter version.
18+
"""
19+
20+
from google.cloud import parametermanager_v1
21+
22+
23+
# [START parametermanager_create_param_version]
24+
def create_param_version(
25+
project_id: str, parameter_id: str, version_id: str, payload: str
26+
) -> parametermanager_v1.ParameterVersion:
27+
"""
28+
Creates a new version of an existing parameter in the global location
29+
of the specified project using the Google Cloud Parameter Manager SDK.
30+
The payload is specified as an unformatted string.
31+
32+
Args:
33+
project_id (str): The ID of the project where the parameter is located.
34+
parameter_id (str): The ID of the parameter for which
35+
the version is to be created.
36+
version_id (str): The ID of the version to be created.
37+
payload (str): The unformatted string payload
38+
to be stored in the new parameter version.
39+
40+
Returns:
41+
parametermanager_v1.ParameterVersion: An object representing the
42+
newly created parameter version.
43+
44+
Example:
45+
create_param_version(
46+
"my-project",
47+
"my-global-parameter",
48+
"v1",
49+
"my-unformatted-payload"
50+
)
51+
"""
52+
# Import the necessary library for Google Cloud Parameter Manager.
53+
from google.cloud import parametermanager_v1
54+
55+
# Create the Parameter Manager client.
56+
client = parametermanager_v1.ParameterManagerClient()
57+
58+
# Build the resource name of the parameter.
59+
parent = client.parameter_path(project_id, "global", parameter_id)
60+
61+
# Define the parameter version creation request with an unformatted payload.
62+
request = parametermanager_v1.CreateParameterVersionRequest(
63+
parent=parent,
64+
parameter_version_id=version_id,
65+
parameter_version=parametermanager_v1.ParameterVersion(
66+
payload=parametermanager_v1.ParameterVersionPayload(
67+
data=payload.encode("utf-8") # Encoding the payload to bytes.
68+
)
69+
),
70+
)
71+
72+
# Create the parameter version.
73+
response = client.create_parameter_version(request=request)
74+
75+
# Print the newly created parameter version name.
76+
print(f"Created parameter version: {response.name}")
77+
# [END parametermanager_create_param_version]
78+
79+
return response
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2025 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 secret reference.
18+
"""
19+
20+
from google.cloud import parametermanager_v1
21+
22+
23+
# [START parametermanager_create_param_version_with_secret]
24+
def create_param_version_with_secret(
25+
project_id: str, parameter_id: str, version_id: str, secret_id: str
26+
) -> parametermanager_v1.ParameterVersion:
27+
"""
28+
Creates a new version of an existing parameter in the global location
29+
of the specified project using the Google Cloud Parameter Manager SDK.
30+
The payload is specified as a JSON string and
31+
includes a reference to a secret.
32+
33+
Args:
34+
project_id (str): The ID of the project where the parameter is located.
35+
parameter_id (str): The ID of the parameter for
36+
which the version is to be created.
37+
version_id (str): The ID of the version to be created.
38+
secret_id (str): The ID of the secret to be referenced.
39+
40+
Returns:
41+
parametermanager_v1.ParameterVersion: An object representing the
42+
newly created parameter version.
43+
44+
Example:
45+
create_param_version_with_secret(
46+
"my-project",
47+
"my-global-parameter",
48+
"v1",
49+
"projects/my-project/secrets/application-secret/version/latest"
50+
)
51+
"""
52+
# Import the necessary library for Google Cloud Parameter Manager.
53+
from google.cloud import parametermanager_v1
54+
import json
55+
56+
# Create the Parameter Manager client.
57+
client = parametermanager_v1.ParameterManagerClient()
58+
59+
# Build the resource name of the parameter.
60+
parent = client.parameter_path(project_id, "global", parameter_id)
61+
62+
# Create the JSON payload with a secret reference.
63+
payload_dict = {
64+
"username": "test-user",
65+
"password": f"__REF__('//secretmanager.googleapis.com/{secret_id}')",
66+
}
67+
payload_json = json.dumps(payload_dict)
68+
69+
# Define the parameter version creation request with the JSON payload.
70+
request = parametermanager_v1.CreateParameterVersionRequest(
71+
parent=parent,
72+
parameter_version_id=version_id,
73+
parameter_version=parametermanager_v1.ParameterVersion(
74+
payload=parametermanager_v1.ParameterVersionPayload(
75+
data=payload_json.encode("utf-8")
76+
)
77+
),
78+
)
79+
80+
# Create the parameter version.
81+
response = client.create_parameter_version(request=request)
82+
83+
# Print the newly created parameter version name.
84+
print(f"Created parameter version: {response.name}")
85+
# [END parametermanager_create_param_version_with_secret]
86+
87+
return response
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2025 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 formatted parameter.
18+
"""
19+
20+
from google.cloud import parametermanager_v1
21+
22+
23+
# [START parametermanager_create_structured_param]
24+
def create_structured_param(
25+
project_id: str, parameter_id: str, format_type: parametermanager_v1.ParameterFormat
26+
) -> parametermanager_v1.Parameter:
27+
"""
28+
Creates a parameter in the global location of the specified
29+
project with specified format using the Google Cloud Parameter Manager SDK.
30+
31+
Args:
32+
project_id (str): The ID of the project where
33+
the parameter is to be created.
34+
parameter_id (str): The ID to assign to the new parameter.
35+
This ID must be unique within the project.
36+
format_type (parametermanager_v1.ParameterFormat): The format type of
37+
the parameter (UNFORMATTED, YAML, JSON).
38+
39+
Returns:
40+
parametermanager_v1.Parameter: An object representing the
41+
newly created parameter.
42+
43+
Example:
44+
create_structured_param(
45+
"my-project",
46+
"my-global-parameter",
47+
parametermanager_v1.ParameterFormat.JSON
48+
)
49+
"""
50+
# Import the necessary library for Google Cloud Parameter Manager.
51+
from google.cloud import parametermanager_v1
52+
53+
# Create the Parameter Manager client.
54+
client = parametermanager_v1.ParameterManagerClient()
55+
56+
# Build the resource name of the parent project in the global location.
57+
parent = client.common_location_path(project_id, "global")
58+
59+
# Define the parameter creation request with the specified format.
60+
request = parametermanager_v1.CreateParameterRequest(
61+
parent=parent,
62+
parameter_id=parameter_id,
63+
parameter=parametermanager_v1.Parameter(format_=format_type),
64+
)
65+
66+
# Create the parameter.
67+
response = client.create_parameter(request=request)
68+
69+
# Print the newly created parameter name.
70+
print(f"Created parameter {response.name} with format {response.format_.name}")
71+
# [END parametermanager_create_structured_param]
72+
73+
return response

0 commit comments

Comments
 (0)