8000 Added samples for get, list and render regional parameter & parameter… · rebeccaellis/java-docs-samples@47fd761 · GitHub
[go: up one dir, main page]

Skip to content

Commit 47fd761

Browse files
Added samples for get, list and render regional parameter & parameter version (GoogleCloudPlatform#10061)
1 parent 73728d6 commit 47fd761

File tree

6 files changed

+526
-0
lines changed

6 files changed

+526
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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_get_regional_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.ParameterManagerSettings;
24+
import com.google.cloud.parametermanager.v1.ParameterName;
25+
import java.io.IOException;
26+
27+
/**
28+
* This class demonstrates how to get a regional parameter using the Parameter Manager SDK for GCP.
29+
*/
30+
public class GetRegionalParam {
31+
32+
public static void main(String[] args) throws IOException {
33+
// TODO(developer): Replace these variables before running the sample.
34+
String projectId = "your-project-id";
35+
String locationId = "your-location-id";
36+
String parameterId = "your-parameter-id";
37+
38+
// Call the method to get a regional parameter.
39+
getRegionalParam(projectId, locationId, parameterId);
40+
}
41+
42+
// This is an example snippet that gets a regional parameter.
43+
public static Parameter getRegionalParam(String projectId, String locationId, String parameterId)
44+
throws IOException {
45+
// Endpoint to call the regional parameter manager server
46+
String apiEndpoint = String.format("parametermanager.%s.rep.googleapis.com:443", locationId);
47+
ParameterManagerSettings parameterManagerSettings =
48+
ParameterManagerSettings.newBuilder().setEndpoint(apiEndpoint).build();
49+
50+
// Initialize the client that will be used to send requests. This client only
51+
// needs to be created once, and can be reused for multiple requests.
52+
try (ParameterManagerClient client = ParameterManagerClient.create(parameterManagerSettings)) {
53+
// Build the parameter name.
54+
ParameterName parameterName = ParameterName.of(projectId, locationId, parameterId);
55+
56+
// Get the parameter.
57+
Parameter parameter = client.getParameter(parameterName.toString());
58+
// Find more details for the Parameter object here:
59+
// https://cloud.google.com/secret-manager/parameter-manager/docs/reference/rest/v1/projects.locations.parameters#Parameter
60+
System.out.printf(
61+
"Found the regional parameter %s with format %s\n",
62+
parameter.getName(), parameter.getFormat());
63+
64+
return parameter;
65+
}
66+
}
67+
}
68+
// [END parametermanager_get_regional_param]
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_get_regional_param_version]
20+
21+
import com.google.cloud.parametermanager.v1.ParameterManagerClient;
22+
import com.google.cloud.parametermanager.v1.ParameterManagerSettings;
23+
import com.google.cloud.parametermanager.v1.ParameterVersion;
24+
import com.google.cloud.parametermanager.v1.ParameterVersionName;
25+
import java.io.IOException;
26+
27+
/**
28+
* This class demonstrates how to get a regional parameter version using the Parameter Manager SDK
29+
* for GCP.
30+
*/
31+
public class GetRegionalParamVersion {
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 versionId = "your-version-id";
39+
40+
// Call the method to get a regional parameter version.
41+
getRegionalParamVersion(projectId, locationId, parameterId, versionId);
42+
}
43+
44+
// This is an example snippet that gets a regional parameter version.
45+
public static ParameterVersion getRegionalParamVersion(
46+
String projectId, String locationId, String parameterId, String versionId)
47+
throws IOException {
48+
// Endpoint to call the regional parameter manager server
49+
String apiEndpoint = String.format("parametermanager.%s.rep.googleapis.com:443", locationId);
50+
ParameterManagerSettings parameterManagerSettings =
51+
ParameterManagerSettings.newBuilder().setEndpoint(apiEndpoint).build();
52+
53+
// Initialize the client that will be used to send requests. This client only
54+
// needs to be created once, and can be reused for multiple requests.
55+
try (ParameterManagerClient client = ParameterManagerClient.create(parameterManagerSettings)) {
56+
// Build the parameter version name.
57+
ParameterVersionName parameterVersionName =
58+
ParameterVersionName.of(projectId, locationId, parameterId, versionId);
59+
60+
// Get the parameter version.
61+
ParameterVersion parameterVersion =
62+
client.getParameterVersion(parameterVersionName.toString());
63+
// Find more details for the Parameter Version object here:
64+
// https://cloud.google.com/secret-manager/parameter-manager/docs/reference/rest/v1/projects.locations.parameters.versions#ParameterVersion
65+
System.out.printf(
66+
"Found regional parameter version %s with state %s\n",
67+
parameterVersion.getName(), (parameterVersion.getDisabled() ? "disabled" : "enabled"));
68+
if (!parameterVersion.getDisabled()) {
69+
System.out.printf("Payload: %s", parameterVersion.getPayload().getData().toStringUtf8());
70+
}
71+
return parameterVersion;
72+
}
73+
}
74+
}
75+
// [END parametermanager_get_regional_param_version]
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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_list_regional_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.ParameterManagerSettings;
25+
import com.google.cloud.parametermanager.v1.ParameterName;
26+
import java.io.IOException;
27+
28+
/**
29+
* Class to list parameter versions for a specified region using the Parameter Manager SDK
30+
* for GCP.
31+
*/
32+
public class ListRegionalParamVersions {
33+
34+
public static void main(String[] args) throws IOException {
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+
40+
// Call the method to list parameter versions regionally.
41+
listRegionalParamVersions(projectId, locationId, parameterId);
42+
}
43+
44+
// This is an example snippet that list all parameter versions regionally
45+
public static ListParameterVersionsPagedResponse listRegionalParamVersions(
46+
String projectId, String locationId, String parameterId) throws IOException {
47+
// Endpoint to call the regional parameter manager server
48+
String apiEndpoint = String.format("parametermanager.%s.rep.googleapis.com:443", locationId);
49+
ParameterManagerSettings parameterManagerSettings =
50+
ParameterManagerSettings.newBuilder().setEndpoint(apiEndpoint).build();
51+
52+
// Initialize the client that will be used to send requests. This client only needs to be
53+
// created once,
54+
// and can be reused for multiple requests.
55+
try (ParameterManagerClient client = ParameterManagerClient.create(parameterManagerSettings)) {
56+
// Build the parameter name from the project and parameter ID.
57+
ParameterName parameterName = ParameterName.of(projectId, locationId, parameterId);
58+
59+
// Build the request to list parameter versions.
60+
ListParameterVersionsRequest request =
61+
ListParameterVersionsRequest.newBuilder().setParent(parameterName.toString()).build();
62+
63+
// Send the request and get the response.
64+
ListParameterVersionsPagedResponse response = client.listParameterVersions(request);
65+
66+
// Iterate through all versions and print their details.
67+
response
68+
.iterateAll()
69+
.forEach(
70+
version ->
71+
System.out.printf(
72+
"Found regional parameter version %s with state %s\n",
73+
version.getName(), (version.getDisabled() ? "disabled" : "enabled")));
74+
75+
return response;
76+
}
77+
}
78+
}
79+
// [END parametermanager_list_regional_param_versions]
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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_list_regional_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 com.google.cloud.parametermanager.v1.ParameterManagerSettings;
25+
import java.io.IOException;
26+
27+
/**
28+
* Class to demonstrate listing parameters for a specified region using the Parameter Manager SDK
29+
* for GCP.
30+
*/
31+
public class ListRegionalParams {
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+
38+
// Call the method to list parameters regionally.
39+
listRegionalParams(projectId, locationId);
40+
}
41+
42+
// This is an example snippet that list all parameters in a given region.
43+
public static ListParametersPagedResponse listRegionalParams(String projectId, String locationId)
44+
throws IOException {
45+
// Endpoint to call the regional parameter manager server
46+
String apiEndpoint = String.format("parametermanager.%s.rep.googleapis.com:443", locationId);
47+
ParameterManagerSettings parameterManagerSettings =
48+
ParameterManagerSettings.newBuilder().setEndpoint(apiEndpoint).build();
49+
50+
// Initialize the client that will be used to send requests. This client only
51+
// needs to be created once, and can be reused for multiple requests.
52+
try (ParameterManagerClient client = ParameterManagerClient.create(parameterManagerSettings)) {
53+
// Build the parent name from the project.
54+
LocationName location = LocationName.of(projectId, locationId);
55+
56+
// Get all parameters.
57+
ListParametersPagedResponse response = client.listParameters(location.toString());
58+
59+
// List all parameters.
60+
response
61+
.iterateAll()
62+
.forEach(parameter ->
63+
System.out.printf("Found regional parameter %s with format %s\n",
64+
parameter.getName(), parameter.getFormat()));
65+
66+
return response;
67+
}
68+
}
69+
}
70+
// [END parametermanager_list_regional_params]
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.regionalsamples;
18+
19+
// [START parametermanager_render_regional_param_version]
20+
21+
import com.google.cloud.parametermanager.v1.ParameterManagerClient;
22+
import com.google.cloud.parametermanager.v1.ParameterManagerSettings;
23+
import com.google.cloud.parametermanager.v1.ParameterVersionName;
24+
import com.google.cloud.parametermanager.v1.RenderParameterVersionResponse;
25+
import java.io.IOException;
26+
27+
/**
28+
* This class demonstrates how to render a regional parameter version using the Parameter Manager
29+
* SDK for GCP.
30+
*/
31+
public class RenderRegionalParamVersion {
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 versionId = "your-version-id";
39+
40+
// Call the method to render a regional parameter version.
41+
renderRegionalParamVersion(projectId, locationId, parameterId, versionId);
42+
}
43+
44+
// This is an example snippet to render a regional parameter version.
45+
public static RenderParameterVersionResponse renderRegionalParamVersion(
46+
String projectId, String locationId, String parameterId, String versionId)
47+
throws IOException {
48+
// Endpoint to call the regional parameter manager server
49+
String apiEndpoint = String.format("parametermanager.%s.rep.googleapis.com:443", locationId);
50+
ParameterManagerSettings parameterManagerSettings =
51+
ParameterManagerSettings.newBuilder().setEndpoint(apiEndpoint).build();
52+
53+
// Initialize the client that will be used to send requests. This client only
54+
// needs to be created once, and can be reused for multiple requests.
55+
try (ParameterManagerClient client = ParameterManagerClient.create(parameterManagerSettings)) {
56+
// Build the parameter version name.
57+
ParameterVersionName parameterVersionName =
58+
ParameterVersionName.of(projectId, locationId, parameterId, versionId);
59+
60+
// Render the parameter version.
61+
RenderParameterVersionResponse response =
62+
client.renderParameterVersion(parameterVersionName.toString());
63+
System.out.printf(
64+
"Rendered regional parameter version payload: %s\n",
65+
response.getRenderedPayload().toStringUtf8());
66+
67+
return response;
68+
}
69+
}
70+
}
71+
// [END parametermanager_render_regional_param_version]

0 commit comments

Comments
 (0)
0