8000 feat(parametermanager): Added delete, enable, disable samples for par… · rebeccaellis/java-docs-samples@99907fe · GitHub
[go: up one dir, main page]

Skip to content

Commit 99907fe

Browse files
feat(parametermanager): Added delete, enable, disable samples for parametermanager (GoogleCloudPlatform#10049)
1 parent 74bde7a commit 99907fe

File tree

5 files changed

+601
-0
lines changed

5 files changed

+601
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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_delete_regional_param]
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 java.io.IOException;
25+
26+
/**
27+
* This class demonstrates how to delete a regional parameter using the Parameter Manager SDK for
28+
* GCP.
29+
*/
30+
public class DeleteRegionalParam {
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 delete a regional parameter.
39+
deleteRegionalParam(projectId, locationId, parameterId);
40+
}
41+
42+
// This is an example snippet that deletes a regional parameter.
43+
public static void deleteRegionalParam(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+
// Delete the parameter.
57+
client.deleteParameter(parameterName.toString());
58+
System.out.printf("Deleted regional parameter: %s\n", parameterName.toString());
59+
}
60+
}
61+
}
62+
// [END parametermanager_delete_regional_param]
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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_delete_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 java.io.IOException;
25+
26+
/**
27+
* This class demonstrates how to delete a regional parameter version using the Parameter Manager
28+
* SDK for GCP.
29+
*/
30+
public class DeleteRegionalParamVersion {
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 locationId = "your-location-id";
35+
String parameterId = "your-parameter-id";
36+
String versionId = "your-version-id";
37+
38+
// Call the method to delete a regional parameter version.
39+
deleteRegionalParamVersion(projectId, locationId, parameterId, versionId);
40+
}
41+
42+
// This is an example snippet that deletes a regional parameter version.
43+
public static void deleteRegionalParamVersion(
44+
String projectId, String locationId, String parameterId, String versionId)
45+
throws IOException {
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
52+
// needs to be created once, and can be reused for multiple requests.
53+
try (ParameterManagerClient client = ParameterManagerClient.create(parameterManagerSettings)) {
54+
// Build the parameter version name.
55+
ParameterVersionName parameterVersionName =
56+
ParameterVersionName.of(projectId, locationId, parameterId, versionId);
57+
58+
// Delete the parameter version.
59+
client.deleteParameterVersion(parameterVersionName.toString());
60+
System.out.printf(
61+
"Deleted regional parameter version: %s\n", parameterVersionName.toString());
62+
}
63+
}
64+
}
65+
// [END parametermanager_delete_regional_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.regionalsamples;
18+
19+
// [START parametermanager_disable_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 com.google.protobuf.FieldMask;
26+
import com.google.protobuf.util.FieldMaskUtil;
27+
import java.io.IOException;
28+
29+
/**
30+
* This class demonstrates how to disable a regional parameter version using the Parameter Manager
31+
* SDK for GCP.
32+
*/
33+
public class DisableRegionalParamVersion {
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+
42+
// Call the method to disable a regional parameter version.
43+
disableRegionalParamVersion(projectId, locationId, parameterId, versionId);
44+
}
45+
46+
// This is an example snippet that disables a regional parameter version.
47+
public static ParameterVersion disableRegionalParamVersion(
48+
String projectId, String locationId, String parameterId, String versionId)
49+
throws IOException {
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
56+
// needs to be created once, and can be reused for multiple requests.
57+
try (ParameterManagerClient client = ParameterManagerClient.create(parameterManagerSettings)) {
58+
// Build the parameter version name.
59+
ParameterVersionName parameterVersionName =
60+
ParameterVersionName.of(projectId, locationId, parameterId, versionId);
61+
62+
// Set the parameter version to disable.
63+
ParameterVersion parameterVersion =
64+
ParameterVersion.newBuilder()
65+
.setName(parameterVersionName.toString())
66+
.setDisabled(true)
67+
.build();
68+
69+
// Build the field mask for the disabled field.
70+
FieldMask fieldMask = FieldMaskUtil.fromString("disabled");
71+
72+
// Update the parameter version to disable it.
73+
ParameterVersion disabledParameterVersion =
74+
client.updateParameterVersion(parameterVersion, fieldMask);
75+
System.out.printf(
76+
"Disabled regional parameter version %s for regional parameter %s\n",
77+
disabledParameterVersion.getName(), parameterId);
78+
79+
return disabledParameterVersion;
80+
}
81+
}
82+
}
83+
// [END parametermanager_disable_regional_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.regionalsamples;
18+
19+
// [START parametermanager_enable_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 com.google.protobuf.FieldMask;
26+
import com.google.protobuf.util.FieldMaskUtil;
27+
import java.io.IOException;
28+
29+
/**
30+
* This class demonstrates how to enable a regional parameter version using the Parameter Manager
31+
* SDK for GCP.
32+
*/
33+
public class EnableRegionalParamVersion {
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+
42+
// Call the method to enable a regional parameter version.
43+
enableRegionalParamVersion(projectId, locationId, parameterId, versionId);
44+
}
45+
46+
// This is an example snippet that enables a regional parameter version.
47+
public static ParameterVersion enableRegionalParamVersion(
48+
String projectId, String locationId, String parameterId, String versionId)
49+
throws IOException {
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
56+
// needs to be created once, and can be reused for multiple requests.
57+
try (ParameterManagerClient client = ParameterManagerClient.create(parameterManagerSettings)) {
58+
// Build the parameter version name.
59+
ParameterVersionName parameterVersionName =
60+
ParameterVersionName.of(projectId, locationId, parameterId, versionId);
61+
62+
// Set the parameter version to enable.
63+
ParameterVersion parameterVersion =
64+
ParameterVersion.newBuilder()
65+
.setName(parameterVersionName.toString())
66+
.setDisabled(false)
67+
.build();
68+
69+
// Build the field mask for the disabled field.
70+
FieldMask fieldMask = FieldMaskUtil.fromString("disabled");
71+
72+
// Update the parameter version to enable it.
73+
ParameterVersion enabledParameterVersion =
74+
client.updateParameterVersion(parameterVersion, fieldMask);
75+
System.out.printf(
76+
"Enabled regional parameter version %s for regional parameter %s\n",
77+
enabledParameterVersion.getName(), parameterId);
78+
79+
return enabledParameterVersion;
80+
}
81+
}
82+
}
83+
// [END parametermanager_enable_regional_param_version]

0 commit comments

Comments
 (0)
0