Skip to content

Commit 7484bb3

Browse files
Added samples for kms_key field in regional parameter (#10094)
1 parent 33bd13a commit 7484bb3

File tree

5 files changed

+424
-2
lines changed

5 files changed

+424
-2
lines changed

parametermanager/pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
<dependency>
4444
<groupId>com.google.cloud</groupId>
4545
<artifactId>libraries-bom</artifactId>
46-
<version>26.54.0</version>
46+
<version>26.60.0</version>
4747
<type>pom</type>
4848
<scope>import</scope>
4949
</dependency>
@@ -91,6 +91,11 @@
9191
<artifactId>google-iam-policy</artifactId>
9292
<scope>test</scope>
9393
</dependency>
94+
<dependency>
95+
<groupId>com.google.cloud</groupId>
96+
<artifactId>google-cloud-kms</artifactId>
97+
<scope>test</scope>
98+
</dependency>
9499
</dependencies>
95100

96101
<build>
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.regionalsamples;
18+
19+
// [START parametermanager_create_regional_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 com.google.cloud.parametermanager.v1.ParameterManagerSettings;
25+
import java.io.IOException;
26+
27+
/**
28+
* Example class to create a new regional parameter with provided KMS
29+
* key using the Parameter Manager SDK for GCP.
30+
*/
31+
public class CreateRegionalParamWithKmsKey {
32+
33+
public static void main(String[] args) throws IOException {
34+
// TODO(developer): Replace these variables before running the sample.
35+
String projectId = "your-project-id";
36+
String locationId = "your-location-id";
37+
String parameterId = "your-parameter-id";
38+
String kmsKeyName = "your-kms-key";
39+
40+
// Call the method to create a regional parameter with the specified kms key.
41+
createRegionalParameterWithKmsKey(projectId, locationId, parameterId, kmsKeyName);
42+
}
43+
44+
// This is an example snippet for creating a new parameter with a specific format.
45+
public static Parameter createRegionalParameterWithKmsKey(
46+
String projectId, String locationId, String parameterId, String kmsKeyName)
47+
throws IOException {
48+
49+
// Endpoint to call the regional parameter manager server
50+
String apiEndpoint = String.format("parametermanager.%s.rep.googleapis.com:443", locationId);
51+
ParameterManagerSettings parameterManagerSettings =
52+
ParameterManagerSettings.newBuilder().setEndpoint(apiEndpoint).build();
53+
54+
// Initialize the client that will be used to send requests. This client only needs to be
55+
// created once, and can be reused for multiple requests.
56+
try (ParameterManagerClient client = ParameterManagerClient.create(parameterManagerSettings)) {
57+
58+
// Build the parent name from the project.
59+
LocationName location = LocationName.of(projectId, locationId);
60+
61+
// Build the parameter to create with the provided format.
62+
Parameter parameter = Parameter.newBuilder().setKmsKey(kmsKeyName).build();
63+
64+
// Create the parameter.
65+
Parameter createdParameter =
66+
client.createParameter(location.toString(), parameter, parameterId);
67+
System.out.printf(
68+
"Created regional parameter %s with kms key %s\n",
69+
createdParameter.getName(), createdParameter.getKmsKey());
70+
71+
return createdParameter;
72+
}
73+
}
74+
}
75+
// [END parametermanager_create_regional_param_with_kms_key]
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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.regionalsamples;
18+
19+
// [START parametermanager_remove_regional_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 RemoveRegionalParamKmsKey {
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 locationId = "your-location-id";
39+
String parameterId = "your-parameter-id";
40+
41+
// Call the method to remove kms key of a parameter.
42+
removeRegionalParamKmsKey(projectId, locationId, parameterId);
43+
}
44+
45+
// This is an example snippet for updating the kms key of a parameter.
46+
public static Parameter removeRegionalParamKmsKey(
47+
String projectId, String locationId, String parameterId) throws IOException {
48+
49+
// Endpoint to call the regional parameter manager server
50+
String apiEndpoint = String.format("parametermanager.%s.rep.googleapis.com:443", locationId);
51+
ParameterManagerSettings parameterManagerSettings =
52+
ParameterManagerSettings.newBuilder().setEndpoint(apiEndpoint).build();
53+
54+
// Initialize the client that will be used to send requests. This client only needs to be
55+
// created once, and can be reused for multiple requests.
56+
try (ParameterManagerClient client = ParameterManagerClient.create(parameterManagerSettings)) {
57+
58+
// Build the parameter name.
59+
ParameterName name = ParameterName.of(projectId, locationId, parameterId);
60+
61+
// Remove kms key of a parameter .
62+
Parameter parameter = Parameter.newBuilder()
63+
.setName(name.toString())
64+
.clearKmsKey()
65+
.build();
66+
67+
// Build the field mask for the kms_key field.
68+
FieldMask fieldMask = FieldMaskUtil.fromString("kms_key");
69+
70+
// Update the parameter kms key.
71+
Parameter updatedParameter = client.updateParameter(parameter, fieldMask);
72+
System.out.printf(
73+
"Removed kms key for regional parameter %s\n",
74+
updatedParameter.getName());
75+
76+
return updatedParameter;
77+
}
78+
}
79+
}
80+
// [END parametermanager_remove_regional_param_kms_key]
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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.regionalsamples;
18+
19+
// [START parametermanager_update_regional_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 regional
31+
* parameter using the Parameter Manager SDK for GCP.
32+
*/
33+
public class UpdateRegionalParamKmsKey {
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 locationId = "your-location-id";
39+
String parameterId = "your-parameter-id";
40+
String kmsKeyName = "your-kms-key";
41+
42+
// Call the method to update kms key of a parameter.
43+
updateRegionalParamKmsKey(projectId, locationId, parameterId, kmsKeyName);
44+
}
45+
46+
// This is an example snippet for updating the kms key of a parameter.
47+
public static Parameter updateRegionalParamKmsKey(
48+
String projectId, String locationId, String parameterId, String kmsKeyName)
49+
throws IOException {
50+
51+
// Endpoint to call the regional parameter manager server
52+
String apiEndpoint = String.format("parametermanager.%s.rep.googleapis.com:443", locationId);
53+
ParameterManagerSettings parameterManagerSettings =
54+
ParameterManagerSettings.newBuilder().setEndpoint(apiEndpoint).build();
55+
56+
// Initialize the client that will be used to send requests. This client only needs to be
57+
// created once, and can be reused for multiple requests.
58+
try (ParameterManagerClient client = ParameterManagerClient.create(parameterManagerSettings)) {
59+
60+
// Build the parameter name.
61+
ParameterName name = ParameterName.of(projectId, locationId, parameterId);
62+
63+
// Set the parameter kms key to update.
64+
Parameter parameter = Parameter.newBuilder()
65+
.setName(name.toString())
66+
.setKmsKey(kmsKeyName)
67+
.build();
68+
69+
// Build the field mask for the kms_key field.
70+
FieldMask fieldMask = FieldMaskUtil.fromString("kms_key");
71+
72+
// Update the parameter kms key.
73+
Parameter updatedParameter = client.updateParameter(parameter, fieldMask);
74+
System.out.printf(
75+
"Updated regional parameter %s with kms key %s\n",
76+
updatedParameter.getName(), updatedParameter.getKmsKey());
77+
78+
return updatedParameter;
79+
}
80+
}
81+
}
82+
// [END parametermanager_update_regional_param_kms_key]

0 commit comments

Comments
 (0)