-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat(parametermanager): Added samples for kms_key field in global parameter #10093
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
telpirion
merged 3 commits into
GoogleCloudPlatform:main
from
durgesh-ninave-crest:parametermanager-global-kms_key-samples
Jun 17, 2025
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
ac72f71
Added samples for kms_key field in global parameter
durgesh-ninave-crest e99ccfb
Merge branch 'main' into parametermanager-global-kms_key-samples
durgesh-ninave-crest 3956f2e
fix(parametermanager): update sleep time for render regional paramete…
durgesh-ninave-crest File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
parametermanager/src/main/java/parametermanager/CreateParamWithKmsKey.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright 2025 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package parametermanager; | ||
|
||
// [START parametermanager_create_param_with_kms_key] | ||
|
||
import com.google.cloud.parametermanager.v1.LocationName; | ||
import com.google.cloud.parametermanager.v1.Parameter; | ||
import com.google.cloud.parametermanager.v1.ParameterManagerClient; | ||
import java.io.IOException; | ||
|
||
/** | ||
* Example class to create a new parameter with provided KMS key | ||
* using the Parameter Manager SDK for GCP. | ||
*/ | ||
public class CreateParamWithKmsKey { | ||
|
||
public static void main(String[] args) throws IOException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "your-project-id"; | ||
String parameterId = "your-parameter-id"; | ||
String kmsKeyName = "your-kms-key"; | ||
|
||
// Call the method to create a parameter with the specified kms key. | ||
createParameterWithKmsKey(projectId, parameterId, kmsKeyName); | ||
} | ||
|
||
// This is an example snippet for creating a new parameter with a specific format. | ||
public static Parameter createParameterWithKmsKey( | ||
String projectId, String parameterId, String kmsKeyName) throws IOException { | ||
// Initialize the client that will be used to send requests. | ||
try (ParameterManagerClient client = ParameterManagerClient.create()) { | ||
String locationId = "global"; | ||
|
||
// Build the parent name from the project. | ||
LocationName location = LocationName.of(projectId, locationId); | ||
|
||
// Build the parameter to create with the provided format. | ||
Parameter parameter = Parameter.newBuilder().setKmsKey(kmsKeyName).build(); | ||
|
||
// Create the parameter. | ||
Parameter createdParameter = | ||
client.createParameter(location.toString(), parameter, parameterId); | ||
System.out.printf( | ||
"Created parameter %s with kms key %s\n", | ||
createdParameter.getName(), createdParameter.getKmsKey()); | ||
|
||
return createdParameter; | ||
} | ||
} | ||
} | ||
// [END parametermanager_create_param_with_kms_key] |
74 changes: 74 additions & 0 deletions
74
parametermanager/src/main/java/parametermanager/RemoveParamKmsKey.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright 2025 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package parametermanager; | ||
|
||
// [START parametermanager_remove_param_kms_key] | ||
|
||
import com.google.cloud.parametermanager.v1.Parameter; | ||
import com.google.cloud.parametermanager.v1.ParameterManagerClient; | ||
import com.google.cloud.parametermanager.v1.ParameterManagerSettings; | ||
import com.google.cloud.parametermanager.v1.ParameterName; | ||
import com.google.protobuf.FieldMask; | ||
import com.google.protobuf.util.FieldMaskUtil; | ||
import java.io.IOException; | ||
|
||
/** | ||
* This class demonstrates how to change the kms key of a parameter | ||
* using the Parameter Manager SDK for GCP. | ||
*/ | ||
public class RemoveParamKmsKey { | ||
|
||
public static void main(String[] args) throws IOException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "your-project-id"; | ||
String parameterId = "your-parameter-id"; | ||
|
||
// Call the method to remove kms key of a parameter. | ||
removeParamKmsKey(projectId, parameterId); | ||
} | ||
|
||
// This is an example snippet for updating the kms key of a parameter. | ||
public static Parameter removeParamKmsKey( | ||
String projectId, String parameterId) throws IOException { | ||
// Initialize the client that will be used to send requests. This client only | ||
// needs to be created once, and can be reused for multiple requests. | ||
try (ParameterManagerClient client = ParameterManagerClient.create()) { | ||
String locationId = "global"; | ||
|
||
// Build the parameter name. | ||
ParameterName name = ParameterName.of(projectId, locationId, parameterId); | ||
|
||
// Remove kms key of a parameter . | ||
Parameter parameter = Parameter.newBuilder() | ||
.setName(name.toString()) | ||
.clearKmsKey() | ||
.build(); | ||
|
||
// Build the field mask for the kms_key field. | ||
FieldMask fieldMask = FieldMaskUtil.fromString("kms_key"); | ||
|
||
// Update the parameter kms key. | ||
Parameter updatedParameter = client.updateParameter(parameter, fieldMask); | ||
System.out.printf( | ||
"Removed kms key for parameter %s\n", | ||
updatedParameter.getName()); | ||
|
||
return updatedParameter; | ||
} | ||
} | ||
} | ||
// [END parametermanager_remove_param_kms_key] |
75 changes: 75 additions & 0 deletions
75
parametermanager/src/main/java/parametermanager/UpdateParamKmsKey.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright 2025 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package parametermanager; | ||
|
||
// [START parametermanager_update_param_kms_key] | ||
|
||
import com.google.cloud.parametermanager.v1.Parameter; | ||
import com.google.cloud.parametermanager.v1.ParameterManagerClient; | ||
import com.google.cloud.parametermanager.v1.ParameterManagerSettings; | ||
import com.google.cloud.parametermanager.v1.ParameterName; | ||
import com.google.protobuf.FieldMask; | ||
import com.google.protobuf.util.FieldMaskUtil; | ||
import java.io.IOException; | ||
|
||
/** | ||
* This class demonstrates how to change the kms key of a parameter | ||
* using theParameter Manager SDK for GCP. | ||
*/ | ||
public class UpdateParamKmsKey { | ||
|
||
public static void main(String[] args) throws IOException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "your-project-id"; | ||
String parameterId = "your-parameter-id"; | ||
String kmsKeyName = "your-kms-key"; | ||
|
||
// Call the method to update kms key of a parameter. | ||
updateParamKmsKey(projectId, parameterId, kmsKeyName); | ||
} | ||
|
||
// This is an example snippet for updating the kms key of a parameter. | ||
public static Parameter updateParamKmsKey( | ||
String projectId, String parameterId, String kmsKeyName) throws IOException { | ||
// Initialize the client that will be used to send requests. This client only | ||
// needs to be created once, and can be reused for multiple requests. | ||
try (ParameterManagerClient client = ParameterManagerClient.create()) { | ||
String locationId = "global"; | ||
|
||
// Build the parameter name. | ||
ParameterName name = ParameterName.of(projectId, locationId, parameterId); | ||
|
||
// Set the parameter kms key to update. | ||
Parameter parameter = Parameter.newBuilder() | ||
.setName(name.toString()) | ||
.setKmsKey(kmsKeyName) | ||
.build(); | ||
|
||
// Build the field mask for the kms_key field. | ||
FieldMask fieldMask = FieldMaskUtil.fromString("kms_key"); | ||
|
||
// Update the parameter kms key. | ||
Parameter updatedParameter = client.updateParameter(parameter, fieldMask); | ||
System.out.printf( | ||
"Updated parameter %s with kms key %s\n", | ||
updatedParameter.getName(), updatedParameter.getKmsKey()); | ||
|
||
return updatedParameter; | ||
} | ||
} | ||
} | ||
// [END parametermanager_update_param_kms_key] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The copyright year is set to 2025. Could you please update this to the current year of creation (e.g., 2024)? This should also be checked for the other new sample files (
UpdateParamKmsKey.java
,RemoveParamKmsKey.java
).