Skip to content

Commit 46a0c73

Browse files
feat(parametermanager): added samples for handling cmek in parameter (#13259)
1 parent 9d1ab23 commit 46a0c73

10 files changed

+851
-3
lines changed
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 default format parameter with kms key.
18+
"""
19+
20+
from google.cloud import parametermanager_v1
21+
22+
23+
# [START parametermanager_create_param_with_kms_key]
24+
def create_param_with_kms_key(
25+
project_id: str, parameter_id: str, kms_key: str
26+
) -> parametermanager_v1.Parameter:
27+
"""
28+
Creates a parameter with default format (Unformatted)
29+
in the global location of the specified
30+
project and kms key using the Google Cloud Parameter Manager SDK.
31+
32+
Args:
33+
project_id (str): The ID of the project where
34+
the parameter is to be created.
35+
parameter_id (str): The ID to assign to the new parameter.
36+
This ID must be unique within the project.
37+
kms_key (str): The KMS key used to encrypt the parameter.
38+
39+
Returns:
40+
parametermanager_v1.Parameter: An object representing
41+
the newly created parameter.
42+
43+
Example:
44+
create_param_with_kms_key(
45+
"my-project",
46+
"my-global-parameter",
47+
"projects/my-project/locations/global/keyRings/test/cryptoKeys/test-key"
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.
60+
request = parametermanager_v1.CreateParameterRequest(
61+
parent=parent,
62+
parameter_id=parameter_id,
63+
parameter=parametermanager_v1.Parameter(kms_key=kms_key),
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 kms key {kms_key}")
71+
# [END parametermanager_create_param_with_kms_key]
72+
73+
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 default format regional parameter with kms key.
18+
"""
19+
20+
from google.cloud import parametermanager_v1
21+
22+
23+
# [START parametermanager_create_regional_param_with_kms_key]
24+
def create_regional_param_with_kms_key(
25+
project_id: str, location_id: str, parameter_id: str, kms_key: str
26+
) -> parametermanager_v1.Parameter:
27+
"""
28+
Creates a regional parameter with default format (Unformatted)
29+
in the specified location, project and with kms key
30+
using the Google Cloud Parameter Manager SDK.
31+
32+
Args:
33+
project_id (str): The ID of the project where
34+
the regional parameter is to be created.
35+
location_id (str): The region where the parameter is to be created.
36+
parameter_id (str): The ID to assign to the new parameter.
37+
This ID must be unique within the project.
38+
kms_key (str): The KMS key used to encrypt the parameter.
39+
40+
Returns:
41+
parametermanager_v1.Parameter: An object representing
42+
the newly created regional parameter.
43+
44+
Example:
45+
create_regional_param_with_kms_key(
46+
"my-project",
47+
"us-central1",
48+
"my-regional-parameter",
49+
"projects/my-project/locations/us-central1/keyRings/test/cryptoKeys/test-key"
50+
)
51+
"""
52+
53+
# Import the Parameter Manager client library.
54+
from google.cloud import parametermanager_v1
55+
56+
api_endpoint = f"parametermanager.{location_id}.rep.googleapis.com"
57+
# Create the Parameter Manager client for the specified region.
58+
client = parametermanager_v1.ParameterManagerClient(
59+
client_options={"api_endpoint": api_endpoint}
60+
)
61+
62+
# Build the resource name of the parent project for the specified region.
63+
parent = client.common_location_path(project_id, location_id)
64+
65+
# Define the parameter creation request.
66+
request = parametermanager_v1.CreateParameterRequest(
67+
parent=parent,
68+
parameter_id=parameter_id,
69+
parameter=parametermanager_v1.Parameter(kms_key=kms_key),
70+
)
71+
72+
# Create the parameter.
73+
response = client.create_parameter(request=request)
74+
75+
# Print the newly created parameter name.
76+
print(f"Created regional parameter {response.name} with kms key {kms_key}")
77+
# [END parametermanager_create_regional_param_with_kms_key]
78+
79+
return response
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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 removing the kms key from the regional parameter.
17+
"""
18+
19+
from google.cloud import parametermanager_v1
20+
21+
22+
# [START parametermanager_remove_regional_param_kms_key]
23+
def remove_regional_param_kms_key(
24+
project_id: str, location_id: str, parameter_id: str
25+
) -> parametermanager_v1.Parameter:
26+
"""
27+
Remove the kms key of a specified regional parameter
28+
in the specified project using the Google Cloud Parameter Manager SDK.
29+
30+
Args:
31+
project_id (str): The ID of the project where the parameter is to be created.
32+
location_id (str): The region where the parameter is to be created.
33+
parameter_id (str): The ID of the regional parameter for
34+
which kms key is to be updated.
35+
36+
Returns:
37+
parametermanager_v1.Parameter: An object representing the
38+
updated regional parameter.
39+
40+
Example:
41+
remove_regional_param_kms_key(
42+
"my-project",
43+
"us-central1",
44+
"my-global-parameter"
45+
)
46+
"""
47+
# Import the necessary library for Google Cloud Parameter Manager.
48+
from google.cloud import parametermanager_v1
49+
from google.protobuf import field_mask_pb2
50+
51+
# Create the Parameter Manager client.
52+
api_endpoint = f"parametermanager.{location_id}.rep.googleapis.com"
53+
# Create the Parameter Manager client for the specified region.
54+
client = parametermanager_v1.ParameterManagerClient(
55+
client_options={"api_endpoint": api_endpoint}
56+
)
57+
58+
# Build the resource name of the regional parameter.
59+
name = client.parameter_path(project_id, location_id, parameter_id)
60+
61+
# Get the current regional parameter details.
62+
parameter = client.get_parameter(name=name)
63+
64+
# Set the kms key field of the regional parameter.
65+
parameter.kms_key = None
66+
67+
# Define the update mask for the kms_key field.
68+
update_mask = field_mask_pb2.FieldMask(paths=["kms_key"])
69+
70+
# Define the request to update the parameter.
71+
request = parametermanager_v1.UpdateParameterRequest(
72+
parameter=parameter, update_mask=update_mask
73+
)
74+
75+
# Call the API to update (kms_key) the parameter.
76+
response = client.update_parameter(request=request)
77+
78+
# Print the parameter ID that was updated.
79+
print(f"Removed kms key for regional parameter {parameter_id}")
80+
# [END parametermanager_remove_regional_param_kms_key]
81+
82+
return response

0 commit comments

Comments
 (0)