Skip to content

Commit 9b64b2e

Browse files
feat(parametermanager): Added list and render samples for parametermanager (#10054)
1 parent f2d0081 commit 9b64b2e

File tree

6 files changed

+467
-0
lines changed

6 files changed

+467
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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_get_param]
20+
21+
import com.google.cloud.parametermanager.v1.Parameter;
22+
import com.google.cloud.parametermanager.v1.ParameterManagerClient;
23+
import com.google.cloud.parametermanager.v1.ParameterName;
24+
import java.io.IOException;
25+
26+
/** This class demonstrates how to get a parameter using the Parameter Manager SDK for GCP. */
27+
public class GetParam {
28+
29+
public static void main(String[] args) throws IOException {
30+
// TODO(developer): Replace these variables before running the sample.
31+
String projectId = "your-project-id";
32+
String parameterId = "your-parameter-id";
33+
34+
// Call the method to get a parameter.
35+
getParam(projectId, parameterId);
36+
}
37+
38+
// This is an example snippet for getting a parameter.
39+
public static Parameter getParam(String projectId, String parameterId) throws IOException {
40+
// Initialize the client that will be used to send requests. This client only
41+
// needs to be created once, and can be reused for multiple requests.
42+
try (ParameterManagerClient client = ParameterManagerClient.create()) {
43+
String locationId = "global";
44+
45+
// Build the parameter name.
46+
ParameterName parameterName = ParameterName.of(projectId, locationId, parameterId);
47+
48+
// Get the parameter.
49+
Parameter parameter = client.getParameter(parameterName.toString());
50+
// Find more details for the Parameter object here:
51+
// https://cloud.google.com/secret-manager/parameter-manager/docs/reference/rest/v1/projects.locations.parameters#Parameter
52+
System.out.printf(
53+
"Found the parameter %s with format: %s\n", parameter.getName(), parameter.getFormat());
54+
55+
return parameter;
56+
}
57+
}
58+
}
59+
// [END parametermanager_get_param]
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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_get_param_version]
20+
21+
import com.google.cloud.parametermanager.v1.ParameterManagerClient;
22+
import com.google.cloud.parametermanager.v1.ParameterVersion;
23+
import com.google.cloud.parametermanager.v1.ParameterVersionName;
24+
import java.io.IOException;
25+
26+
/**
27+
* This class demonstrates how to get a parameter version using the Parameter Manager SDK for GCP.
28+
*/
29+
public class GetParamVersion {
30+
public static void main(String[] args) throws IOException {
31+
// TODO(developer): Replace these variables before running the sample.
32+
String projectId = "your-project-id";
33+
String parameterId = "your-parameter-id";
34+
String versionId = "your-version-id";
35+
36+
// Call the method to get a parameter version.
37+
getParamVersion(projectId, parameterId, versionId);
38+
}
39+
40+
// This is an example snippet for getting a parameter version.
41+
public static ParameterVersion getParamVersion(
42+
String projectId, String parameterId, String versionId) throws IOException {
43+
// Initialize the client that will be used to send requests. This client only
44+
// needs to be created once, and can be reused for multiple requests.
45+
try (ParameterManagerClient client = ParameterManagerClient.create()) {
46+
String locationId = "global";
47+
48+
// Build the parameter version name.
49+
ParameterVersionName parameterVersionName =
50+
ParameterVersionName.of(projectId, locationId, parameterId, versionId);
51+
52+
// Get the parameter version.
53+
ParameterVersion parameterVersion =
54+
client.getParameterVersion(parameterVersionName.toString());
55+
// Find more details for the Parameter Version object here:
56+
// https://cloud.google.com/secret-manager/parameter-manager/docs/reference/rest/v1/projects.locations.parameters.versions#ParameterVersion
57+
System.out.printf(
58+
"Found parameter version %s with state %s\n",
59+
parameterVersion.getName(), (parameterVersion.getDisabled() ? "disabled" : "enabled"));
60+
if (!parameterVersion.getDisabled()) {
61+
System.out.printf("Payload: %s\n", parameterVersion.getPayload().getData().toStringUtf8());
62+
}
63+
return parameterVersion;
64+
}
65+
}
66+
}
67+
// [END parametermanager_get_param_version]
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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_list_param_versions]
20+
21+
import com.google.cloud.parametermanager.v1.ListParameterVersionsRequest;
22+
import com.google.cloud.parametermanager.v1.ParameterManagerClient;
23+
import com.google.cloud.parametermanager.v1.ParameterManagerClient.ListParameterVersionsPagedResponse;
24+
import com.google.cloud.parametermanager.v1.ParameterName;
25+
import java.io.IOException;
26+
27+
/** Class to list parameter versions using the Parameter Manager SDK for GCP. */
28+
public class ListParamVersions {
29+
30+
public static void main(String[] args) throws IOException {
31+
// TODO(developer): Replace these variables before running the sample.
32+
String projectId = "your-project-id";
33+
String parameterId = "your-parameter-id";
34+
35+
// Call the method to list parameter versions.
36+
listParamVersions(projectId, parameterId);
37+
}
38+
39+
// This is an example snippet that list all parameter versions
40+
public static ListParameterVersionsPagedResponse listParamVersions(
41+
String projectId, String parameterId) throws IOException {
42+
// Initialize the client that will be used to send requests. This client only needs to be
43+
// created once,
44+
// and can be reused for multiple requests.
45+
try (ParameterManagerClient client = ParameterManagerClient.create()) {
46+
String locationId = "global";
47+
48+
// Build the parameter name from the project and parameter ID.
49+
ParameterName parameterName = ParameterName.of(projectId, locationId, parameterId);
50+
51+
// Build the request to list parameter versions.
52+
ListParameterVersionsRequest request =
53+
ListParameterVersionsRequest.newBuilder().setParent(parameterName.toString()).build();
54+
55+
// Send the request and get the response.
56+
ListParameterVersionsPagedResponse response = client.listParameterVersions(request);
57+
58+
// Iterate through all versions and print their details.
59+
response
60+
.iterateAll()
61+
.forEach(
62+
version ->
63+
System.out.printf(
64+
"Found parameter version %s with state %s\n",
65+
version.getName(), (version.getDisabled() ? "disabled" : "enabled")));
66+
67+
return response;
68+
}
69+
}
70+
}
71+
// [END parametermanager_list_param_versions]
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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_list_params]
20+
21+
import com.google.cloud.parametermanager.v1.LocationName;
22+
import com.google.cloud.parametermanager.v1.ParameterManagerClient;
23+
import com.google.cloud.parametermanager.v1.ParameterManagerClient.ListParametersPagedResponse;
24+
import java.io.IOException;
25+
26+
/** Class to demonstrate listing parameter using the parameter manager SDK for GCP. */
27+
public class ListParams {
28+
29+
public static void main(String[] args) throws IOException {
30+
// TODO(developer): Replace these variables before running the sample.
31+
String projectId = "your-project-id";
32+
33+
// Call the method to list parameters.
34+
listParams(projectId);
35+
}
36+
37+
// This is an example snippet for listing all parameters in given project.
38+
public static ListParametersPagedResponse listParams(String projectId) throws IOException {
39+
// Initialize the client that will be used to send requests. This client only
40+
// needs to be created once, and can be reused for multiple requests.
41+
try (ParameterManagerClient client = ParameterManagerClient.create()) {
42+
String locationId = "global";
43+
44+
// Build the parent name from the project.
45+
LocationName location = LocationName.of(projectId, locationId);
46+
47+
// Get all parameters.
48+
ListParametersPagedResponse response = client.listParameters(location.toString());
49+
50+
// List all parameters.
51+
response
52+
.iterateAll()
53+
.forEach(parameter ->
54+
System.out.printf("Found parameter %s with format %s\n",
55+
parameter.getName(), parameter.getFormat()));
56+
57+
return response;
58+
}
59+
}
60+
}
61+
// [END parametermanager_list_params]
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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_render_param_version]
20+
import com.google.cloud.parametermanager.v1.ParameterManagerClient;
21+
import com.google.cloud.parametermanager.v1.ParameterVersionName;
22+
import com.google.cloud.parametermanager.v1.RenderParameterVersionResponse;
23+
import java.io.IOException;
24+
25+
/**
26+
* This class demonstrates how to render a parameter version using the Parameter Manager SDK for
27+
* GCP.
28+
*/
29+
public class RenderParamVersion {
30+
31+
public static void main(String[] args) throws IOException {
32+
// TODO(developer): Replace these variables before running the sample.
33+
String projectId = "your-project-id";
34+
String parameterId = "your-parameter-id";
35+
String versionId = "your-version-id";
36+
37+
// Call the method to render a parameter version.
38+
renderParamVersion(projectId, parameterId, versionId);
39+
}
40+
41+
// This is an example snippet to render a parameter version.
42+
public static RenderParameterVersionResponse renderParamVersion(
43+
String projectId, String parameterId, String versionId) throws IOException {
44+
// Initialize the client that will be used to send requests. This client only
45+
// needs to be created once, and can be reused for multiple requests.
46+
try (ParameterManagerClient client = ParameterManagerClient.create()) {
47+
String locationId = "global";
48+
49+
// Build the parameter version name.
50+
ParameterVersionName parameterVersionName =
51+
ParameterVersionName.of(projectId, locationId, parameterId, versionId);
52+
53+
// Render the parameter version.
54+
RenderParameterVersionResponse response =
55+
client.renderParameterVersion(parameterVersionName.toString());
56+
System.out.printf(
57+
"Rendered parameter version payload: %s\n",
58+
response.getRenderedPayload().toStringUtf8());
59+
60+
return response;
61+
}
62+
}
63+
}
64+
// [END parametermanager_render_param_version]

0 commit comments

Comments
 (0)