10000 feat(parametermanager): Added samples for creating parameter and vers… · rebeccaellis/java-docs-samples@74bde7a · GitHub
[go: up one dir, main page]

Skip to content

Commit 74bde7a

Browse files
feat(parametermanager): Added samples for creating parameter and versions (GoogleCloudPlatform#10052)
* feat(parametermanager): Added create samples for parametermanager * chor(parametermanager): Fixed lint * fix(parametermanager): Removed unused imports
1 parent a1cbfc0 commit 74bde7a

File tree

6 files changed

+646
-0
lines changed

6 files changed

+646
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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_create_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 java.io.IOException;
25+
26+
/** This class demonstrates how to create a parameter using the Parameter Manager SDK for GCP. */
27+
public class CreateParam {
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 create parameter.
35+
createParam(projectId, parameterId);
36+
}
37+
38+
// This is an example snippet for creating a new parameter.
39+
public static Parameter createParam(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 parent name from the project.
46+
LocationName location = LocationName.of(projectId, locationId);
47+
48+
// Build the parameter to create.
49+
Parameter parameter = Parameter.newBuilder().build();
50+
51+
// Create the parameter.
52+
Parameter createdParameter =
53+
client.createParameter(location.toString(), parameter, parameterId);
54+
System.out.printf("Created parameter: %s\n", createdParameter.getName());
55+
56+
return createdParameter;
57+
}
58+
}
59+
}
60+
// [END parametermanager_create_param]
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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_create_param_version]
20+
21+
import com.google.cloud.parametermanager.v1.ParameterManagerClient;
22+
import com.google.cloud.parametermanager.v1.ParameterName;
23+
import com.google.cloud.parametermanager.v1.ParameterVersion;
24+
import com.google.cloud.parametermanager.v1.ParameterVersionPayload;
25+
import com.google.protobuf.ByteString;
26+
import java.io.IOException;
27+
28+
/**
29+
* This class demonstrates how to create a parameter version with an unformatted payload using the
30+
* Parameter Manager SDK for GCP.
31+
*/
32+
public class CreateParamVersion {
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 parameterId = "your-parameter-id";
38+
String versionId = "your-version-id";
39+
String payload = "test123";
40+
41+
// Call the method to create a parameter version with unformatted payload.
42+
createParamVersion(projectId, parameterId, versionId, payload);
43+
}
44+
45+
// This is an example snippet that creates a parameter version with an unformatted payload.
46+
public static ParameterVersion createParamVersion(
47+
String projectId, String parameterId, String versionId, String payload) throws IOException {
48+
// Initialize the client that will be used to send requests. This client only
49+
// needs to be created once, and can be reused for multiple requests.
50+
try (ParameterManagerClient client = ParameterManagerClient.create()) {
51+
String locationId = "global";
52+
53+
// Build the parameter name.
54+
ParameterName parameterName = ParameterName.of(projectId, locationId, parameterId);
55+
56+
// Convert the payload string to ByteString.
57+
ByteString byteStringPayload = ByteString.copyFromUtf8(payload);
58+
59+
// Create the parameter version payload.
60+
ParameterVersionPayload parameterVersionPayload =
61+
ParameterVersionPayload.newBuilder().setData(byteStringPayload).build();
62+
63+
// Create the parameter version with the unformatted payload.
64+
ParameterVersion parameterVersion =
65+
ParameterVersion.newBuilder().setPayload(parameterVersionPayload).build();
66+
67+
// Create the parameter version in the Parameter Manager.
68+
ParameterVersion createdParameterVersion =
69+
client.createParameterVersion(parameterName.toString(), parameterVersion, versionId);
70+
System.out.printf("Created parameter version: %s\n", createdParameterVersion.getName());
71+
72+
return createdParameterVersion;
73+
}
74+
}
75+
}
76+
// [END parametermanager_create_param_version]
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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_create_param_version_with_secret]
20+
21+
import com.google.cloud.parametermanager.v1.ParameterManagerClient;
22+
import com.google.cloud.parametermanager.v1.ParameterName;
23+
import com.google.cloud.parametermanager.v1.ParameterVersion;
24+
import com.google.cloud.parametermanager.v1.ParameterVersionPayload;
25+
import com.google.protobuf.ByteString;
26+
import java.io.IOException;
27+
28+
/**
29+
* This class demonstrates how to create a parameter version with a JSON payload that includes a
30+
* secret reference using the Parameter Manager SDK for GCP.
31+
*/
32+
public class CreateParamVersionWithSecret {
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 parameterId = "your-parameter-id";
38+
String versionId = "your-version-id";
39+
String secretId = "projects/your-project-id/secrets/your-secret-id/versions/latest";
40+
41+
// Call the method to create parameter version with JSON payload that includes a secret
42+
// reference.
43+
createParamVersionWithSecret(projectId, parameterId, versionId, secretId);
44+
}
45+
46+
// This is an example snippet that creates a parameter version with a JSON payload that includes a
47+
// secret reference.
48+
public static ParameterVersion createParamVersionWithSecret(
49+
String projectId, String parameterId, String versionId, String secretId) throws IOException {
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()) {
53+
String locationId = "global";
54+
55+
// Build the parameter name.
56+
ParameterName parameterName = ParameterName.of(projectId, locationId, parameterId);
57+
58+
// Convert the JSON payload string to ByteString.
59+
String payload =
60+
String.format(
61+
"{\"username\": \"test-user\", "
62+
+ "\"password\": \"__REF__(//secretmanager.googleapis.com/%s)\"}",
63+
secretId);
64+
ByteString byteStringPayload = ByteString.copyFromUtf8(payload);
65+
66+
// Create the parameter version payload with the secret reference.
67+
ParameterVersionPayload parameterVersionPayload =
68+
ParameterVersionPayload.newBuilder().setData(byteStringPayload).build();
69+
70+
// Create the parameter version with the JSON 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("Created parameter version: %s\n", createdParameterVersion.getName());
78+
79+
return createdParameterVersion;
80+
}
81+
}
82+
}
83+
// [END parametermanager_create_param_version_with_secret]
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_create_structured_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 java.io.IOException;
26+
27+
/**
28+
* Example class to create a new parameter with a specific format using the Parameter Manager SDK
29+
* for GCP.
30+
*/
31+
public class CreateStructuredParam {
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 parameterId = "your-parameter-id";
37+
ParameterFormat format = ParameterFormat.YAML;
38+
39+
// Call the method to create a parameter with the specified format.
40+
createStructuredParameter(projectId, parameterId, format);
41+
}
42+
43+
// This is an example snippet for creating a new parameter with a specific format.
44+
public static Parameter createStructuredParameter(
45+
String projectId, String parameterId, ParameterFormat format) throws IOException {
46+
// Initialize the client that will be used to send requests.
47+
try (ParameterManagerClient client = ParameterManagerClient.create()) {
48+
String locationId = "global";
49+
50+
// Build the parent name from the project.
51+
LocationName location = LocationName.of(projectId, locationId);
52+
53+
// Build the parameter to create with the provided format.
54+
Parameter parameter = Parameter.newBuilder().setFormat(format).build();
55+
56+
// Create the parameter.
57+
Parameter createdParameter =
58+
client.createParameter(location.toString(), parameter, parameterId);
59+
System.out.printf(
60+
"Created parameter %s with format %s\n",
61+
createdParameter.getName(), createdParameter.getFormat());
62+
63+
return createdParameter;
64+
}
65+
}
66+
}
67+
// [END parametermanager_create_structured_param]
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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_create_structured_param_version]
20+
21+
import com.google.cloud.parametermanager.v1.ParameterManagerClient;
22+
import com.google.cloud.parametermanager.v1.ParameterName;
23+
import com.google.cloud.parametermanager.v1.ParameterVersion;
24+
import com.google.cloud.parametermanager.v1.ParameterVersionPayload;
25+
import com.google.protobuf.ByteString;
26+
import java.io.IOException;
27+
28+
/**
29+
* This class demonstrates how to create a parameter version with a JSON payload using the Parameter
30+
* Manager SDK for GCP.
31+
*/
32+
public class CreateStructuredParamVersion {
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 parameterId = "your-parameter-id";
38+
String versionId = "your-version-id";
39+
String jsonPayload = "{\"username\": \"test-user\", \"host\": \"localhost\"}";
40+
41+
// Call the method to create a parameter version with JSON payload.
42+
createStructuredParamVersion(projectId, parameterId, versionId, jsonPayload);
43+
}
44+
45+
// This is an example snippet for creating a new parameter version with the given JSON payload.
46+
public static ParameterVersion createStructuredParamVersion(
47+
String projectId, String parameterId, String versionId, String jsonPayload)
48+
throws IOException {
49+
// Initialize the client that will be used to send requests. This client only
50+
// needs to be created once, and can be reused for multiple requests.
51+
try (ParameterManagerClient client = ParameterManagerClient.create()) {
52+
String locationId = "global";
53+
54+
// Build the parameter name.
55+
ParameterName parameterName = ParameterName.of(projectId, locationId, parameterId);
56+
57+
// Convert the JSON payload string to ByteString.
58+
ByteString byteStringPayload = ByteString.copyFromUtf8(jsonPayload);
59+
60+
// Create the parameter version payload.
61+
ParameterVersionPayload parameterVersionPayload =
62+
ParameterVersionPayload.newBuilder().setData(byteStringPayload).build();
63+
64+
// Create the parameter version with the JSON payload.
65+
ParameterVersion parameterVersion =
66+
ParameterVersion.newBuilder().setPayload(parameterVersionPayload).build();
67+
68+
// Create the parameter version in the Parameter Manager.
69+
ParameterVersion createdParameterVersion =
70+
client.createParameterVersion(parameterName.toString(), parameterVersion, versionId);
71+
System.out.printf("Created parameter version: %s\n", createdParameterVersion.getName());
72+
73+
return createdParameterVersion;
74+
}
75+
}
76+
}
77+
// [END parametermanager_create_structured_param_version]

0 commit comments

Comments
 (0)
0