Skip to content

feat(parametermanager): Added global and regional samples for parameter manager #10028

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

Closed
49 changes: 49 additions & 0 deletions parametermanager/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Google Parameter Manager

<a href="https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&page=editor&open_in_editor=secretmanager/README.md">
<img alt="Open in Cloud Shell" src ="http://gstatic.com/cloudssh/images/open-btn.png"></a>

Google [Parameter Manager](https://cloud.google.com/secret-manager/parameter-manager/docs/overview)
provides a centralized storage for all configuration parameters related to your workload deployments.
Parameters are variables, often in the form of key-value pairs, which customize how an application functions.
These sample Java applications demonstrate how to access
the Parameter Manager API using the Google Java API Client Libraries.

## Prerequisites

### Enable the API

You
must [enable the Parameter Manager API](https://console.cloud.google.com/apis/enableflow?apiid=parametermanager.googleapis.com)
for your project in order to use these samples

### Set Environment Variables

You must set your project ID in order to run the tests

```text
$ export GOOGLE_CLOUD_PROJECT=<your-project-id-here>
```

You must set your Location in order to run the regional tests

```text
$ export GOOGLE_CLOUD_PROJECT_LOCATION=<your-location-id-here>
```

### Grant Permissions

You must ensure that
the [user account or service account](https://cloud.google.com/iam/docs/service-accounts#differences_between_a_service_account_and_a_user_account)
you used to authorize your gcloud session has the proper permissions to edit Parameter Manager resources for your project.
In the Cloud Console under IAM, add the following roles to the project whose service account you're using to test:

* Parameter Manager Admin (`roles/parametermanager.admin`)
* Parameter Manager Parameter Accessor (`roles/parametermanager.parameterAccessor`)
* Parameter Manager Parameter Version Adder (`roles/parametermanager.parameterVersionAdder`)

To use the rendering of secret through parameter manager add the following role also:
* Secret Manager Secret Accessor (`roles/secretmanager.secretAccessor`)

More information can be found in
the [Parameter Manager Docs](https://cloud.google.com/secret-manager/parameter-manager/docs/access-control)
116 changes: 116 additions & 0 deletions parametermanager/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<!--
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.
-->

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>parametermanager</groupId>
<artifactId>parametermanager-samples</artifactId>
<packaging>jar</packaging>

<!--
The parent pom defines common style checks and testing strategies for our samples.
Removing or replacing it should not affect the execution of the samples in any way.
-->
<parent>
<groupId>com.google.cloud.samples</groupId>
<artifactId>shared-configuration</artifactId>
<version>1.2.0</version>
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>libraries-bom</artifactId>
<version>26.54.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-parametermanager</artifactId>
</dependency>

<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java-util</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>

<!-- test dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
<version>1.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-secretmanager</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-iam-policy</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.12.1</version>
<configuration>
<source>11</source> <!-- depending on your project -->
<target>11</target> <!-- depending on your project -->
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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]

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;

/** This class demonstrates how to create a parameter using the Parameter Manager SDK for GCP. */
public class CreateParam {

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 create parameter.
createParam(projectId, parameterId);
}

// This is an example snippet for creating a new parameter.
public static Parameter createParam(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 parent name from the project.
LocationName location = LocationName.of(projectId, locationId);

// Build the parameter to create.
Parameter parameter = Parameter.newBuilder().build();

// Create the parameter.
Parameter createdParameter =
client.createParameter(location.toString(), parameter, parameterId);
System.out.printf("Created parameter: %s\n", createdParameter.getName());

return createdParameter;
}
}
}
// [END parametermanager_create_param]
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* 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_version]

import com.google.cloud.parametermanager.v1.ParameterManagerClient;
import com.google.cloud.parametermanager.v1.ParameterName;
import com.google.cloud.parametermanager.v1.ParameterVersion;
import com.google.cloud.parametermanager.v1.ParameterVersionPayload;
import com.google.protobuf.ByteString;
import java.io.IOException;

/**
* This class demonstrates how to create a parameter version with an unformatted payload using the
* Parameter Manager SDK for GCP.
*/
public class CreateParamVersion {

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 versionId = "your-version-id";
String payload = "test123";

// Call the method to create a parameter version with unformatted payload.
createParamVersion(projectId, parameterId, versionId, payload);
}

// This is an example snippet that creates a parameter version with an unformatted payload.
public static ParameterVersion createParamVersion(
String projectId, String parameterId, String versionId, String payload) 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 parameterName = ParameterName.of(projectId, locationId, parameterId);

// Convert the payload string to ByteString.
ByteString byteStringPayload = ByteString.copyFromUtf8(payload);

// Create the parameter version payload.
ParameterVersionPayload parameterVersionPayload =
ParameterVersionPayload.newBuilder().setData(byteStringPayload).build();

// Create the parameter version with the unformatted payload.
ParameterVersion parameterVersion =
ParameterVersion.newBuilder().setPayload(parameterVersionPayload).build();

// Create the parameter version in the Parameter Manager.
ParameterVersion createdParameterVersion =
client.createParameterVersion(parameterName.toString(), parameterVersion, versionId);
System.out.printf("Created parameter version: %s\n", createdParameterVersion.getName());

return createdParameterVersion;
}
}
}
// [END parametermanager_create_param_version]
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* 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_version_with_secret]

import com.google.cloud.parametermanager.v1.ParameterManagerClient;
import com.google.cloud.parametermanager.v1.ParameterName;
import com.google.cloud.parametermanager.v1.ParameterVersion;
import com.google.cloud.parametermanager.v1.ParameterVersionPayload;
import com.google.protobuf.ByteString;
import java.io.IOException;

/**
* This class demonstrates how to create a parameter version with a JSON payload that includes a
* secret reference using the Parameter Manager SDK for GCP.
*/
public class CreateParamVersionWithSecret {

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 versionId = "your-version-id";
String secretId = "projects/your-project-id/secrets/your-secret-id/versions/latest";

// Call the method to create parameter version with JSON payload that includes a secret
// reference.
createParamVersionWithSecret(projectId, parameterId, versionId, secretId);
}

// This is an example snippet that creates a parameter version with a JSON payload that includes a
// secret reference.
public static ParameterVersion createParamVersionWithSecret(
String projectId, String parameterId, String versionId, String secretId) 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 parameterName = ParameterName.of(projectId, locationId, parameterId);

// Convert the JSON payload string to ByteString.
String payload =
String.format(
"{\"username\": \"test-user\", "
+ "\"password\": \"__REF__(//secretmanager.googleapis.com/%s)\"}",
secretId);
ByteString byteStringPayload = ByteString.copyFromUtf8(payload);

// Create the parameter version payload with the secret reference.
ParameterVersionPayload parameterVersionPayload =
ParameterVersionPayload.newBuilder().setData(byteStringPayload).build();

// Create the parameter version with the JSON payload.
ParameterVersion parameterVersion =
ParameterVersion.newBuilder().setPayload(parameterVersionPayload).build();

// Create the parameter version in the Parameter Manager.
ParameterVersion createdParameterVersion =
client.createParameterVersion(parameterName.toString(), parameterVersion, versionId);
System.out.printf("Created parameter version: %s\n", createdParameterVersion.getName());

return createdParameterVersion;
}
}
}
// [END parametermanager_create_param_version_with_secret]
Loading