Skip to content

Commit b0b638a

Browse files
feat(parametermanager): Added samples for kms_key field in global parameter (#10093)
* Added samples for kms_key field in global parameter * fix(parametermanager): update sleep time for render regional parameter version
1 parent ff40eb3 commit b0b638a

File tree

5 files changed

+383
-2
lines changed

5 files changed

+383
-2
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package parametermanager;
18+
19+
// [START parametermanager_create_param_with_kms_key]
20+
21+
import com.google.cloud.parametermanager.v1.LocationName;
22+
import com.google.cloud.parametermanager.v1.Parameter;
23+
import com.google.cloud.parametermanager.v1.ParameterManagerClient;
24+
import java.io.IOException;
25+
26+
/**
27+
* Example class to create a new parameter with provided KMS key
28+
* using the Parameter Manager SDK for GCP.
29+
*/
30+
public class CreateParamWithKmsKey {
31+
32+
public static void main(String[] args) throws IOException {
33+
// TODO(developer): Replace these variables before running the sample.
34+
String projectId = "your-project-id";
35+
String parameterId = "your-parameter-id";
36+
String kmsKeyName = "your-kms-key";
37+
38+
// Call the method to create a parameter with the specified kms key.
39+
createParameterWithKmsKey(projectId, parameterId, kmsKeyName);
40+
}
41+
42+
// This is an example snippet for creating a new parameter with a specific format.
43+
public static Parameter createParameterWithKmsKey(
44+
String projectId, String parameterId, String kmsKeyName) throws IOException {
45+
// Initialize the client that will be used to send requests.
46+
try (ParameterManagerClient client = ParameterManagerClient.create()) {
47+
String locationId = "global";
48+
49+
// Build the parent name from the project.
50+
LocationName location = LocationName.of(projectId, locationId);
51+
52+
// Build the parameter to create with the provided format.
53+
Parameter parameter = Parameter.newBuilder().setKmsKey(kmsKeyName).build();
54+
55+
// Create the parameter.
56+
Parameter createdParameter =
57+
client.createParameter(location.toString(), parameter, parameterId);
58+
System.out.printf(
59+
"Created parameter %s with kms key %s\n",
60+
createdParameter.getName(), createdParameter.getKmsKey());
61+
62+
return createdParameter;
63+
}
64+
}
65+
}
66+
// [END parametermanager_create_param_with_kms_key]
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package parametermanager;
18+
19+
// [START parametermanager_remove_param_kms_key]
20+
21+
import com.google.cloud.parametermanager.v1.Parameter;
22+
import com.google.cloud.parametermanager.v1.ParameterManagerClient;
23+
import com.google.cloud.parametermanager.v1.ParameterManagerSettings;
24+
import com.google.cloud.parametermanager.v1.ParameterName;
25+
import com.google.protobuf.FieldMask;
26+
import com.google.protobuf.util.FieldMaskUtil;
27+
import java.io.IOException;
28+
29+
/**
30+
* This class demonstrates how to change the kms key of a parameter
31+
* using the Parameter Manager SDK for GCP.
32+
*/
33+
public class RemoveParamKmsKey {
34+
35+
public static void main(String[] args) throws IOException {
36+
// TODO(developer): Replace these variables before running the sample.
37+
String projectId = "your-project-id";
38+
String parameterId = "your-parameter-id";
39+
40+
// Call the method to remove kms key of a parameter.
41+
removeParamKmsKey(projectId, parameterId);
42+
}
43+
44+
// This is an example snippet for updating the kms key of a parameter.
45+
public static Parameter removeParamKmsKey(
46+
String projectId, String parameterId) throws IOException {
47+
// Initialize the client that will be used to send requests. This client only
48+
// needs to be created once, and can be reused for multiple requests.
49+
try (ParameterManagerClient client = ParameterManagerClient.create()) {
50+
String locationId = "global";
51+
52+
// Build the parameter name.
53+
ParameterName name = ParameterName.of(projectId, locationId, parameterId);
54+
55+
// Remove kms key of a parameter .
56+
Parameter parameter = Parameter.newBuilder()
57+
.setName(name.toString())
58+
.clearKmsKey()
59+
.build();
60+
61+
// Build the field mask for the kms_key field.
62+
FieldMask fieldMask = FieldMaskUtil.fromString("kms_key");
63+
64+
// Update the parameter kms key.
65+
Parameter updatedParameter = client.updateParameter(parameter, fieldMask);
66+
System.out.printf(
67+
"Removed kms key for parameter %s\n",
68+
updatedParameter.getName());
69+
70+
return updatedParameter;
71+
}
72+
}
73+
}
74+
// [END parametermanager_remove_param_kms_key]
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package parametermanager;
18+
19+
// [START parametermanager_update_param_kms_key]
20+
21+
import com.google.cloud.parametermanager.v1.Parameter;
22+
import com.google.cloud.parametermanager.v1.ParameterManagerClient;
23+
import com.google.cloud.parametermanager.v1.ParameterManagerSettings;
24+
import com.google.cloud.parametermanager.v1.ParameterName;
25+
import com.google.protobuf.FieldMask;
26+
import com.google.protobuf.util.FieldMaskUtil;
27+
import java.io.IOException;
28+
29+
/**
30+
* This class demonstrates how to change the kms key of a parameter
31+
* using theParameter Manager SDK for GCP.
32+
*/
33+
public class UpdateParamKmsKey {
34+
35+
public static void main(String[] args) throws IOException {
36+
// TODO(developer): Replace these variables before running the sample.
37+
String projectId = "your-project-id";
38+
String parameterId = "your-parameter-id";
39+
String kmsKeyName = "your-kms-key";
40+
41+
// Call the method to update kms key of a parameter.
42+
updateParamKmsKey(projectId, parameterId, kmsKeyName);
43+
}
44+
45+
// This is an example snippet for updating the kms key of a parameter.
46+
public static Parameter updateParamKmsKey(
47+
String projectId, String parameterId, String kmsKeyName) throws IOException {
48+
// Initialize the client that will be used to send requests. This client only
49+
// needs to be created once, and can be reused for multiple requests.
50+
try (ParameterManagerClient client = ParameterManagerClient.create()) {
51+
String locationId = "global";
52+
53+
// Build the parameter name.
54+
ParameterName name = ParameterName.of(projectId, locationId, parameterId);
55+
56+
// Set the parameter kms key to update.
57+
Parameter parameter = Parameter.newBuilder()
58+
.setName(name.toString())
59+
.setKmsKey(kmsKeyName)
60+
.build();
61+
62+
// Build the field mask for the kms_key field.
63+
FieldMask fieldMask = FieldMaskUtil.fromString("kms_key");
64+
65+
// Update the parameter kms key.
66+
Parameter updatedParameter = client.updateParameter(parameter, fieldMask);
67+
System.out.printf(
68+
"Updated parameter %s with kms key %s\n",
69+
updatedParameter.getName(), updatedParameter.getKmsKey());
70+
71+
return updatedParameter;
72+
}
73+
}
74+
}
75+
// [END parametermanager_update_param_kms_key]

0 commit comments

Comments
 (0)