Skip to content

Commit a1cbfc0

Browse files
feat(parametermanager): Added quickstart samples for parametermanager (#10048)
* feat(parametermanager): Added quickstart samples for parametermanager * feat(parametermanager): remove README.md file
1 parent 1256f59 commit a1cbfc0

6 files changed

Lines changed: 501 additions & 0 deletions

File tree

parametermanager/README.md

Whitespace-only changes.

parametermanager/pom.xml

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
18+
xmlns="http://maven.apache.org/POM/4.0.0"
19+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
20+
<modelVersion>4.0.0</modelVersion>
21+
<groupId>parametermanager</groupId>
22+
<artifactId>parametermanager-samples</artifactId>
23+
<packaging>jar</packaging>
24+
25+
<!--
26+
The parent pom defines common style checks and testing strategies for our samples.
27+
Removing or replacing it should not affect the execution of the samples in any way.
28+
-->
29+
<parent>
30+
<groupId>com.google.cloud.samples</groupId>
31+
<artifactId>shared-configuration</artifactId>
32+
<version>1.2.0</version>
33+
</parent>
34+
35+
<properties>
36+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
37+
<maven.compiler.source>11</maven.compiler.source>
38+
<maven.compiler.target>11</maven.compiler.target>
39+
</properties>
40+
41+
<dependencyManagement>
42+
<dependencies>
43+
<dependency>
44+
<groupId>com.google.cloud</groupId>
45+
<artifactId>libraries-bom</artifactId>
46+
<version>26.54.0</version>
47+
<type>pom</type>
48+
<scope>import</scope>
49+
</dependency>
50+
</dependencies>
51+
</dependencyManagement>
52+
53+
<dependencies>
54+
<dependency>
55+
<groupId>com.google.cloud</groupId>
56+
<artifactId>google-cloud-parametermanager</artifactId>
57+
</dependency>
58+
59+
<dependency>
60+
<groupId>com.google.protobuf</groupId>
61+
<artifactId>protobuf-java-util</artifactId>
62+
</dependency>
63+
64+
<dependency>
65+
<groupId>org.projectlombok</groupId>
66+
<artifactId>lombok</artifactId>
67+
<version>1.18.30</version>
68+
<scope>provided</scope>
69+
</dependency>
70+
71+
<!-- test dependencies -->
72+
<dependency>
73+
<groupId>junit</groupId>
74+
<artifactId>junit</artifactId>
75+
<version>4.13.2</version>
76+
<scope>test</scope>
77+
</dependency>
78+
<dependency>
79+
<groupId>com.google.truth</groupId>
80+
<artifactId>truth</artifactId>
81+
<version>1.4.0</version>
82+
<scope>test</scope>
83+
</dependency>
84+
<dependency>
85+
<groupId>com.google.cloud</groupId>
86+
<artifactId>google-cloud-secretmanager</artifactId>
87+
<scope>test</scope>
88+
</dependency>
89+
<dependency>
90+
<groupId>com.google.cloud</groupId>
91+
<artifactId>google-iam-policy</artifactId>
92+
<scope>test</scope>
93+
</dependency>
94+
</dependencies>
95+
96+
<build>
97+
<plugins>
98+
<plugin>
99+
<groupId>org.apache.maven.plugins</groupId>
100+
<artifactId>maven-compiler-plugin</artifactId>
101+
<version>3.12.1</version>
102+
<configuration>
103+
<source>11</source> <!-- depending on your project -->
104+
<target>11</target> <!-- depending on your project -->
105+
<annotationProcessorPaths>
106+
<path>
107+
<groupId>org.projectlombok</groupId>
108+
<artifactId>lombok</artifactId>
109+
<version>1.18.30</version>
110+
</path>
111+
</annotationProcessorPaths>
112+
</configuration>
113+
</plugin>
114+
</plugins>
115+
</build>
116+
</project>
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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_quickstart]
20+
21+
import com.google.cloud.parametermanager.v1.LocationName;
22+
import com.google.cloud.parametermanager.v1.Parameter;
23+
import com.google.cloud.parametermanager.v1.ParameterFormat;
24+
import com.google.cloud.parametermanager.v1.ParameterManagerClient;
25+
import com.google.cloud.parametermanager.v1.ParameterName;
26+
import com.google.cloud.parametermanager.v1.ParameterVersion;
27+
import com.google.cloud.parametermanager.v1.ParameterVersionName;
28+
import com.google.cloud.parametermanager.v1.ParameterVersionPayload;
29+
import com.google.protobuf.ByteString;
30+
31+
public class Quickstart {
32+
33+
public static void main(String[] args) throws Exception {
34+
// TODO(developer): Replace these variables before running the sample.
35+
String projectId = "your-project-id";
36+
String parameterId = "your-parameter-id";
37+
String versionId = "your-version-id";
38+
39+
// Run the quickstart method
40+
quickstart(projectId, parameterId, versionId);
41+
}
42+
43+
// This is an example snippet of how to use the basic capabilities in the Parameter Manager API.
44+
public static void quickstart(
45+
String projectId, String parameterId, String versionId) throws Exception {
46+
47+
// Initialize the client that will be used to send requests. This client only needs to be
48+
// created once, and can be reused for multiple requests.
49+
try (ParameterManagerClient client = ParameterManagerClient.create()) {
50+
String locationId = "global";
51+
52+
// Step 1: Create a parameter.
53+
// Build the parent name from the project.
54+
LocationName location = LocationName.of(projectId, locationId);
55+
56+
// Specify the parameter format.
57+
ParameterFormat format = ParameterFormat.JSON;
58+
// Build the parameter to create.
59+
Parameter parameter = Parameter.newBuilder().setFormat(format).build();
60+
61+
// Create the parameter.
62+
Parameter createdParameter =
63+
client.createParameter(location.toString(), parameter, parameterId);
64+
System.out.printf(
65+
"Created parameter %s with format %s\n",
66+
createdParameter.getName(), createdParameter.getFormat());
67+
68+
// Step 2: Create a parameter version with JSON payload containing a secret reference.
69+
// Build the parameter name.
70+
ParameterName parameterName = ParameterName.of(projectId, locationId, parameterId);
71+
72+
String jsonPayload = "{\"username\": \"test-user\", \"host\": \"localhost\"}";
73+
// Convert the JSON payload string to ByteString.
74+
ByteString byteStringPayload = ByteString.copyFromUtf8(jsonPayload);
75+
76+
// Create the parameter version payload.
77+
ParameterVersionPayload parameterVersionPayload =
78+
ParameterVersionPayload.newBuilder().setData(byteStringPayload).build();
79+
80+
// Create the parameter version with the JSON payload.
81+
ParameterVersion parameterVersion =
82+
ParameterVersion.newBuilder().setPayload(parameterVersionPayload).build();
83+
84+
// Create the parameter version in the Parameter Manager.
85+
ParameterVersion createdParameterVersion =
86+
client.createParameterVersion(parameterName.toString(), parameterVersion, versionId);
87+
System.out.printf("Created parameter version %s\n", createdParameterVersion.getName());
88+
89+
// Step 3: Render the parameter version to fetch and print both simple and rendered payloads.
90+
// Build the parameter version name.
91+
ParameterVersionName parameterVersionName =
92+
ParameterVersionName.of(projectId, locationId, parameterId, versionId);
93+
94+
// Render the parameter version.
95+
ParameterVersion response = client.getParameterVersion(parameterVersionName.toString());
96+
System.out.printf(
97+
"Parameter version %s with payload: %s\n",
98+
response.getName(), response.getPayload().getData().toStringUtf8());
99+
}
100+
}
101+
}
102+
// [END parametermanager_quickstart]
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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_regional_quickstart]
20+
import com.google.cloud.parametermanager.v1.LocationName;
21+
import com.google.cloud.parametermanager.v1.Parameter;
22+
import com.google.cloud.parametermanager.v1.ParameterFormat;
23+
import com.google.cloud.parametermanager.v1.ParameterManagerClient;
24+
import com.google.cloud.parametermanager.v1.ParameterManagerSettings;
25+
import com.google.cloud.parametermanager.v1.ParameterName;
26+
import com.google.cloud.parametermanager.v1.ParameterVersion;
27+
import com.google.cloud.parametermanager.v1.ParameterVersionName;
28+
import com.google.cloud.parametermanager.v1.ParameterVersionPayload;
29+
import com.google.protobuf.ByteString;
30+
31+
/** Demonstrates basic capabilities in the regional Parameter Manager API. */
32+
public class RegionalQuickstart {
33+
34+
public static void main(String[] args) throws Exception {
35+
// TODO(developer): Replace these variables before running the sample.
36+
String projectId = "your-project-id";
37+
String locationId = "your-location-id";
38+
String parameterId = "your-parameter-id";
39+
String versionId = "your-version-id";
40+
41+
// Run the quickstart method
42+
regionalQuickstart(projectId, locationId, parameterId, versionId);
43+
}
44+
45+
// This is an example snippet that demonstrates basic capabilities in the regional Parameter
46+
// Manager API
47+
public static void regionalQuickstart(
48+
String projectId, String locationId, String parameterId, String versionId)
49+
throws Exception {
50+
// Endpoint to call the regional parameter manager server
51+
String apiEndpoint = String.format("parametermanager.%s.rep.googleapis.com:443", locationId);
52+
ParameterManagerSettings parameterManagerSettings =
53+
ParameterManagerSettings.newBuilder().setEndpoint(apiEndpoint).build();
54+
55+
// Initialize the client that will be used to send requests. This client only needs to be
56+
// created once, and can be reused for multiple requests.
57+
try (ParameterManagerClient client = ParameterManagerClient.create(parameterManagerSettings)) {
58+
59+
// Step 1: Create a regional parameter.
60+
// Build the parent name from the project.
61+
LocationName location = LocationName.of(projectId, locationId);
62+
63+
// Specify the parameter format.
64+
ParameterFormat format = ParameterFormat.JSON;
65+
// Build the regional parameter to create.
66+
Parameter parameter = Parameter.newBuilder().setFormat(format).build();
67+
68+
// Create the regional parameter.
69+
Parameter createdParameter =
70+
client.createParameter(location.toString(), parameter, parameterId);
71+
System.out.printf(
72+
"Created regional parameter %s with format %s\n",
73+
createdParameter.getName(), createdParameter.getFormat());
74+
75+
// Step 2: Create a parameter version with JSON payload containing a secret reference.
76+
// Build the parameter name.
77+
ParameterName parameterName = ParameterName.of(projectId, locationId, parameterId);
78+
79+
String jsonPayload = "{\"username\": \"test-user\", \"host\": \"localhost\"}";
80+
// Convert the JSON payload string to ByteString.
81+
ByteString byteStringPayload = ByteString.copyFromUtf8(jsonPayload);
82+
83+
// Create the parameter version payload.
84+
ParameterVersionPayload parameterVersionPayload =
85+
ParameterVersionPayload.newBuilder().setData(byteStringPayload).build();
86+
87+
// Create the parameter version with the JSON payload.
88+
ParameterVersion parameterVersion =
89+
ParameterVersion.newBuilder().setPayload(parameterVersionPayload).build();
90+
91+
// Create the parameter version in the Parameter Manager.
92+
ParameterVersion createdParameterVersion =
93+
client.createParameterVersion(parameterName.toString(), parameterVersion, versionId);
94+
System.out.printf(
95+
"Created regional parameter version %s\n", createdParameterVersion.getName());
96+
97+
// Step 3: Render the parameter version to fetch and print both simple and rendered payloads.
98+
// Build the parameter version name.
99+
ParameterVersionName parameterVersionName =
100+
ParameterVersionName.of(projectId, locationId, parameterId, versionId);
101+
102+
// Render the parameter version.
103+
ParameterVersion response = client.getParameterVersion(parameterVersionName.toString());
104+
System.out.printf(
105+
"Retrieved regional parameter version %s with rendered payload: %s\n",
106+
response.getName(), response.getPayload().getData().toStringUtf8());
107+
}
108+
}
109+
}
110+
// [END parametermanager_regional_quickstart]

0 commit comments

Comments
 (0)