Skip to content

Commit 2771ec8

Browse files
feat(parametermanager): Added samples for delete, enable and disable parameter & parameter version (#10060)
1 parent 47fd761 commit 2771ec8

File tree

5 files changed

+307
-0
lines changed

5 files changed

+307
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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_delete_param]
20+
21+
import com.google.cloud.parametermanager.v1.ParameterManagerClient;
22+
import com.google.cloud.parametermanager.v1.ParameterName;
23+
import java.io.IOException;
24+
25+
/** This class demonstrates how to delete a parameter using the Parameter Manager SDK for GCP. */
26+
public class DeleteParam {
27+
28+
public static void main(String[] args) throws IOException {
29+
// TODO(developer): Replace these variables before running the sample.
30+
String projectId = "your-project-id";
31+
String parameterId = "your-parameter-id";
32+
33+
// Call the method to delete a parameter.
34+
deleteParam(projectId, parameterId);
35+
}
36+
37+
// This is an example snippet for deleting a parameter.
38+
public static void deleteParam(String projectId, String parameterId) 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 parameter name.
45+
ParameterName parameterName = ParameterName.of(projectId, locationId, parameterId);
46+
47+
// Delete the parameter.
48+
client.deleteParameter(parameterName);
49+
System.out.printf("Deleted parameter: %s\n", parameterName.toString());
50+
}
51+
}
52+
}
53+
// [END parametermanager_delete_param]
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_delete_param_version]
20+
21+
import com.google.cloud.parametermanager.v1.ParameterManagerClient;
22+
import com.google.cloud.parametermanager.v1.ParameterVersionName;
23+
import java.io.IOException;
24+
25+
/**
26+
* This class demonstrates how to delete a parameter version using the Parameter Manager SDK for
27+
* GCP.
28+
*/
29+
public class DeleteParamVersion {
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 delete a parameter version.
38+
deleteParamVersion(projectId, parameterId, versionId);
39+
}
40+
41+
// This is an example snippet for deleting a parameter version.
42+
public static void deleteParamVersion(String projectId, String parameterId, String versionId)
43+
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+
// Delete the parameter version.
54+
client.deleteParameterVersion(parameterVersionName);
55+
System.out.printf("Deleted parameter version: %s\n", parameterVersionName.toString());
56+
}
57+
}
58+
}
59+
// [END parametermanager_delete_param_version]
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_disable_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 com.google.protobuf.FieldMask;
25+
import com.google.protobuf.util.FieldMaskUtil;
26+
import java.io.IOException;
27+
28+
/**
29+
* This class demonstrates how to disable a parameter version using the Parameter Manager SDK for
30+
* GCP.
31+
*/
32+
public class DisableParamVersion {
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+
40+
// Call the method to disable a parameter version.
41+
disableParamVersion(projectId, parameterId, versionId);
42+
}
43+
44+
// This is an example snippet for disabling a parameter version.
45+
public static ParameterVersion disableParamVersion(
46+
String projectId, String parameterId, String versionId) throws IOException {
47+
// Initialize the client that will be used to send requests. This client only
48+
// needs to be created once, and can be reused for multiple requests.
49+
try (ParameterManagerClient client = ParameterManagerClient.create()) {
50+
String locationId = "global";
51+
52+
// Build the parameter version name.
53+
ParameterVersionName parameterVersionName =
54+
ParameterVersionName.of(projectId, locationId, parameterId, versionId);
55+
56+
// Set the parameter version to disable.
57+
ParameterVersion parameterVersion =
58+
ParameterVersion.newBuilder()
59+
.setName(parameterVersionName.toString())
60+
.setDisabled(true)
61+
.build();
62+
63+
// Build the field mask for the disabled field.
64+
FieldMask fieldMask = FieldMaskUtil.fromString("disabled");
65+
66+
// Update the parameter version to disable it.
67+
ParameterVersion disabledParameterVersion =
68+
client.updateParameterVersion(parameterVersion, fieldMask);
69+
System.out.printf(
70+
"Disabled parameter version %s for parameter %s\n",
71+
disabledParameterVersion.getName(), parameterId);
72+
73+
return disabledParameterVersion;
74+
}
75+
}
76+
}
77+
// [END parametermanager_disable_param_version]
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_enable_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 com.google.protobuf.FieldMask;
25+
import com.google.protobuf.util.FieldMaskUtil;
26+
import java.io.IOException;
27+
28+
/**
29+
* This class demonstrates how to enable a parameter version using the Parameter Manager SDK for
30+
* GCP.
31+
*/
32+
public class EnableParamVersion {
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+
40+
// Call the method to enable a parameter version.
41+
enableParamVersion(projectId, parameterId, versionId);
42+
}
43+
44+
// This is an example snippet for enabling a parameter version.
45+
public static ParameterVersion enableParamVersion(
46+
String projectId, String parameterId, String versionId) throws IOException {
47+
// Initialize the client that will be used to send requests. This client only
48+
// needs to be created once, and can be reused for multiple requests.
49+
try (ParameterManagerClient client = ParameterManagerClient.create()) {
50+
String locationId = "global";
51+
52+
// Build the parameter version name.
53+
ParameterVersionName parameterVersionName =
54+
ParameterVersionName.of(projectId, locationId, parameterId, versionId);
55+
56+
// Set the parameter version to enable.
57+
ParameterVersion parameterVersion =
58+
ParameterVersion.newBuilder()
59+
.setName(parameterVersionName.toString())
60+
.setDisabled(false)
61+
.build();
62+
63+
// Build the field mask for the disabled field.
64+
FieldMask fieldMask = FieldMaskUtil.fromString("disabled");
65+
66+
// Update the parameter version to enable it.
67+
ParameterVersion enabledParameterVersion =
68+
client.updateParameterVersion(parameterVersion, fieldMask);
69+
System.out.printf(
70+
"Enabled parameter version %s for parameter %s\n",
71+
enabledParameterVersion.getName(), parameterId);
72+
73+
return enabledParameterVersion;
74+
}
75+
}
76+
}
77+
// [END parametermanager_enable_param_version]

parametermanager/src/test/java/parametermanager/SnippetsIT.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,47 @@ public void afterEach() {
325325
System.setOut(null);
326326
}
327327

328+
@Test
329+
public void testDisableParamVersion() throws IOException {
330+
ParameterVersionName parameterVersionName = TEST_PARAMETER_VERSION_NAME_TO_GET_1;
331+
DisableParamVersion.disableParamVersion(
332+
parameterVersionName.getProject(),
333+
parameterVersionName.getParameter(),
334+
parameterVersionName.getParameterVersion());
335+
336+
assertThat(stdOut.toString()).contains("Disabled parameter version");
337+
}
338+
339+
@Test
340+
public void testEnableParamVersion() throws IOException {
341+
ParameterVersionName parameterVersionName = TEST_PARAMETER_VERSION_NAME_TO_GET_1;
342+
EnableParamVersion.enableParamVersion(
343+
parameterVersionName.getProject(),
344+
parameterVersionName.getParameter(),
345+
parameterVersionName.getParameterVersion());
346+
347+
assertThat(stdOut.toString()).contains("Enabled parameter version");
348+
}
349+
350+
@Test
351+
public void testDeleteParamVersion() throws IOException {
352+
ParameterVersionName parameterVersionName = TEST_PARAMETER_VERSION_NAME_TO_DELETE;
353+
DeleteParamVersion.deleteParamVersion(
354+
parameterVersionName.getProject(),
355+
parameterVersionName.getParameter(),
356+
parameterVersionName.getParameterVersion());
357+
358+
assertThat(stdOut.toString()).contains("Deleted parameter version:");
359+
}
360+
361+
@Test
362+
public void testDeleteParam() throws IOException {
363+
ParameterName parameterName = TEST_PARAMETER_NAME_TO_DELETE;
364+
DeleteParam.deleteParam(parameterName.getProject(), parameterName.getParameter());
365+
366+
assertThat(stdOut.toString()).contains("Deleted parameter:");
367+
}
368+
328369
@Test
329370
public void testGetParam() throws IOException {
330371
ParameterName parameterName = TEST_PARAMETER_NAME_TO_GET;

0 commit comments

Comments
 (0)