Skip to content

Commit f2d0081

Browse files
feat(parametermanager): Added create samples for parametermanager (#10053)
1 parent 99907fe commit f2d0081

File tree

6 files changed

+473
-0
lines changed

6 files changed

+473
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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_create_regional_param]
20+
21+
import com.google.cloud.parametermanager.v1.LocationName;
22+
import com.google.cloud.parametermanager.v1.Parameter;
23+
import com.google.cloud.parametermanager.v1.ParameterManagerClient;
24+
import com.google.cloud.parametermanager.v1.ParameterManagerSettings;
25+
import java.io.IOException;
26+
27+
/**
28+
* This class demonstrates how to create a regional parameter using the Parameter Manager SDK for
29+
* GCP.
30+
*/
31+
public class CreateRegionalParam {
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+
39+
createRegionalParam(projectId, locationId, parameterId);
40+
}
41+
42+
// This is an example snippet for creating a new regional parameter.
43+
public static Parameter createRegionalParam(
44+
String projectId, String locationId, String parameterId) throws IOException {
45+
46+
// Endpoint to call the regional parameter manager server
47+
String apiEndpoint = String.format("parametermanager.%s.rep.googleapis.com:443", locationId);
48+
ParameterManagerSettings parameterManagerSettings =
49+
ParameterManagerSettings.newBuilder().setEndpoint(apiEndpoint).build();
50+
51+
// Initialize the client that will be used to send requests. This client only needs to be
52+
// created once, and can be reused for multiple requests.
53+
try (ParameterManagerClient client = ParameterManagerClient.create(parameterManagerSettings)) {
54+
// Build the parent name from the project.
55+
LocationName location = LocationName.of(projectId, locationId);
56+
57+
// Build the regional parameter to create.
58+
Parameter parameter = Parameter.newBuilder().build();
59+
60+
// Create the regional parameter.
61+
Parameter createdParameter =
62+
client.createParameter(location.toString(), parameter, parameterId);
63+
System.out.printf("Created regional parameter: %s\n", createdParameter.getName());
64+
65+
return createdParameter;
66+
}
67+
}
68+
}
69+
// [END parametermanager_create_regional_param]
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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_create_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.ParameterName;
24+
import com.google.cloud.parametermanager.v1.ParameterVersion;
25+
import com.google.cloud.parametermanager.v1.ParameterVersionPayload;
26+
import com.google.protobuf.ByteString;
27+
import java.io.IOException;
28+
29+
/**
30+
* This class demonstrates how to create a regional parameter version with an unformatted payload
31+
* using the Parameter Manager SDK for GCP.
32+
*/
33+
public class CreateRegionalParamVersion {
34+
35+
public static void main(String[] args) throws IOException {
36+
// TODO(developer): Replace these variables before running the sample.
37+
String projectId = "your-project-id";
38+
String locationId = "your-location-id";
39+
String parameterId = "your-parameter-id";
40+
String versionId = "your-version-id";
41+
String payload = "test123";
42+
43+
// Call the method to create a regional parameter version with unformatted payload.
44+
createRegionalParamVersion(projectId, locationId, parameterId, versionId, payload);
45+
}
46+
47+
// This is an example snippet that creates a regional parameter version with an unformatted
48+
// payload.
49+
public static ParameterVersion createRegionalParamVersion(
50+
String projectId, String locationId, String parameterId, String versionId, String payload)
51+
throws IOException {
52+
// Endpoint to call the regional parameter manager server
53+
String apiEndpoint = String.format("parametermanager.%s.rep.googleapis.com:443", locationId);
54+
ParameterManagerSettings parameterManagerSettings =
55+
ParameterManagerSettings.newBuilder().setEndpoint(apiEndpoint).build();
56+
57+
// Initialize the client that will be used to send requests. This client only
58+
// needs to be created once, and can be reused for multiple requests.
59+
try (ParameterManagerClient client = ParameterManagerClient.create(parameterManagerSettings)) {
60+
// Build the parameter name.
61+
ParameterName parameterName = ParameterName.of(projectId, locationId, parameterId);
62+
63+
// Convert the payload string to ByteString.
64+
ByteString byteStringPayload = ByteString.copyFromUtf8(payload);
65+
66+
// Create the parameter version payload.
67+
ParameterVersionPayload parameterVersionPayload =
68+
ParameterVersionPayload.newBuilder().setData(byteStringPayload).build();
69+
70+
// Create the parameter version with the unformatted payload.
71+
ParameterVersion parameterVersion =
72+
ParameterVersion.newBuilder().setPayload(parameterVersionPayload).build();
73+
74+
// Create the parameter version in the Parameter Manager.
75+
ParameterVersion createdParameterVersion =
76+
client.createParameterVersion(parameterName.toString(), parameterVersion, versionId);
77+
System.out.printf(
78+
"Created regional parameter version: %s\n", createdParameterVersion.getName());
79+
80+
return createdParameterVersion;
81+
}
82+
}
83+
}
84+
// [END parametermanager_create_regional_param_version]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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_create_regional_param_version_with_secret]
20+
21+
import com.google.cloud.parametermanager.v1.ParameterManagerClient;
22+
import com.google.cloud.parametermanager.v1.ParameterManagerSettings;
23+
import com.google.cloud.parametermanager.v1.ParameterName;
24+
import com.google.cloud.parametermanager.v1.ParameterVersion;
25+
import com.google.cloud.parametermanager.v1.ParameterVersionPayload;
26+
import com.google.protobuf.ByteString;
27+
import java.io.IOException;
28+
29+
/**
30+
* This class demonstrates how to create a regional parameter version with a JSON payload that
31+
* includes a secret reference using the Parameter Manager SDK for GCP.
32+
*/
33+
public class CreateRegionalParamVersionWithSecret {
34+
35+
public static void main(String[] args) throws IOException {
36+
// TODO(developer): Replace these variables before running the sample.
37+
String projectId = "your-project-id";
38+
String locationId = "your-location-id";
39+
String parameterId = "your-parameter-id";
40+
String versionId = "your-version-id";
41+
String secretId =
42+
"projects/your-project-id/locations/your-location-id"
43+
+ "/secrets/your-secret-id/versions/latest";
44+
45+
// Call the method to create a regional parameter version with JSON payload that includes a
46+
// secret reference.
47+
createRegionalParamVersionWithSecret(projectId, locationId, parameterId, versionId, secretId);
48+
}
49+
50+
// This is an example snippet that creates a regional parameter version with a JSON payload that
51+
// includes a secret reference.
52+
public static ParameterVersion createRegionalParamVersionWithSecret(
53+
String projectId, String locationId, String parameterId, String versionId, String secretId)
54+
throws IOException {
55+
// Endpoint to call the regional parameter manager server
56+
String apiEndpoint = String.format("parametermanager.%s.rep.googleapis.com:443", locationId);
57+
ParameterManagerSettings parameterManagerSettings =
58+
ParameterManagerSettings.newBuilder().setEndpoint(apiEndpoint).build();
59+
60+
// Initialize the client that will be used to send requests. This client only
61+
// needs to be created once, and can be reused for multiple requests.
62+
try (ParameterManagerClient client = ParameterManagerClient.create(parameterManagerSettings)) {
63+
// Build the parameter name.
64+
ParameterName parameterName = ParameterName.of(projectId, locationId, parameterId);
65+
66+
// Convert the JSON payload string to ByteString.
67+
String payload =
68+
String.format(
69+
"{\"username\": \"test-user\","
70+
+ "\"password\": \"__REF__(//secretmanager.googleapis.com/%s)\"}",
71+
secretId);
72+
ByteString byteStringPayload = ByteString.copyFromUtf8(payload);
73+
74+
// Create the parameter version payload with the secret reference.
75+
ParameterVersionPayload parameterVersionPayload =
76+
ParameterVersionPayload.newBuilder().setData(byteStringPayload).build();
77+
78+
// Create the parameter version with the JSON payload.
79+
ParameterVersion parameterVersion =
80+
ParameterVersion.newBuilder().setPayload(parameterVersionPayload).build();
81+
82+
// Create the parameter version in the Parameter Manager.
83+
ParameterVersion createdParameterVersion =
84+
client.createParameterVersion(parameterName.toString(), parameterVersion, versionId);
85+
System.out.printf(
86+
"Created regional parameter version: %s\n", createdParameterVersion.getName());
87+
88+
return createdParameterVersion;
89+
}
90+
}
91+
}
92+
// [END parametermanager_create_regional_param_version_with_secret]
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_create_structured_regional_param]
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.ParameterManagerSettings;
26+
import java.io.IOException;
27+
28+
/**
29+
* Example class to create a new regional parameter with a specific format using the Parameter
30+
* Manager SDK for GCP.
31+
*/
32+
public class CreateStructuredRegionalParam {
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+
ParameterFormat format = ParameterFormat.JSON;
40+
41+
// Call the method to create a regional parameter with the specified format.
42+
createStructuredRegionalParam(projectId, locationId, parameterId, format);
43+
}
44+
45+
// This is an example snippet that creates a regional parameter with a specific format.
46+
public static Parameter createStructuredRegionalParam(
47+
String projectId, String locationId, String parameterId, ParameterFormat format)
48+
throws IOException {
49+
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+
// Build the parent name from the project.
59+
LocationName location = LocationName.of(projectId, locationId);
60+
61+
// Build the regional parameter to create with the provided format.
62+
Parameter parameter = Parameter.newBuilder().setFormat(format).build();
63+
64+
// Create the regional parameter.
65+
Parameter createdParameter =
66+
client.createParameter(location.toString(), parameter, parameterId);
67+
System.out.printf(
68+
"Created regional parameter %s with format %s\n",
69+
createdParameter.getName(), createdParameter.getFormat());
70+
71+
return createdParameter;
72+
}
73+
}
74+
}
75+
// [END parametermanager_create_structured_regional_param]

0 commit comments

Comments
 (0)