1+ /*
2+ * Copyright 2024 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 compute .disks .consistencygroup ;
18+
19+ // [START compute_consistency_group_remove_disk]
20+ import com .google .cloud .compute .v1 .DisksClient ;
21+ import com .google .cloud .compute .v1 .DisksRemoveResourcePoliciesRequest ;
22+ import com .google .cloud .compute .v1 .Operation ;
23+ import com .google .cloud .compute .v1 .RegionDisksClient ;
24+ import com .google .cloud .compute .v1 .RegionDisksRemoveResourcePoliciesRequest ;
25+ import com .google .cloud .compute .v1 .RemoveResourcePoliciesDiskRequest ;
26+ import com .google .cloud .compute .v1 .RemoveResourcePoliciesRegionDiskRequest ;
27+ import java .io .IOException ;
28+ import java .util .Arrays ;
29+ import java .util .concurrent .ExecutionException ;
30+ import java .util .concurrent .TimeUnit ;
31+ import java .util .concurrent .TimeoutException ;
32+
33+ public class RemoveDiskFromConsistencyGroup {
34+
35+ public static void main (String [] args )
36+ throws IOException , ExecutionException , InterruptedException , TimeoutException {
37+ // TODO(developer): Replace these variables before running the sample.
38+ // Project ID or project number of the Cloud project that contains the disk.
39+ String project = "YOUR_PROJECT_ID" ;
40+ // Zone or region of the disk.
41+ String location = "us-central1" ;
42+ // Name of the disk.
43+ String diskName = "DISK_NAME" ;
44+ // Name of the consistency group.
45+ String consistencyGroupName = "CONSISTENCY_GROUP" ;
46+ // Region of the consistency group.
47+ String consistencyGroupLocation = "us-central1" ;
48+
49+ removeDiskFromConsistencyGroup (
50+ project , location , diskName , consistencyGroupName , consistencyGroupLocation );
51+ }
52+
53+ // Removes a disk from a consistency group.
54+ public static Operation .Status removeDiskFromConsistencyGroup (
55+ String project , String location , String diskName ,
56+ String consistencyGroupName , String consistencyGroupLocation )
57+ throws IOException , ExecutionException , InterruptedException , TimeoutException {
58+ String consistencyGroupUrl = String .format (
59+ "https://www.googleapis.com/compute/v1/projects/%s/regions/%s/resourcePolicies/%s" ,
60+ project , consistencyGroupLocation , consistencyGroupName );
61+ Operation response ;
62+ if (Character .isDigit (location .charAt (location .length () - 1 ))) {
63+ // Initialize client that will be used to send requests. This client only needs to be created
64+ // once, and can be reused for multiple requests.
65+ try (RegionDisksClient disksClient = RegionDisksClient .create ()) {
66+ RemoveResourcePoliciesRegionDiskRequest request =
67+ RemoveResourcePoliciesRegionDiskRequest .newBuilder ()
68+ .setDisk (diskName )
69+ .setRegion (location )
70+ .setProject (project )
71+ .setRegionDisksRemoveResourcePoliciesRequestResource (
72+ RegionDisksRemoveResourcePoliciesRequest .newBuilder ()
73+ .addAllResourcePolicies (Arrays .asList (consistencyGroupUrl ))
74+ .build ())
75+ .build ();
76+
77+ response = disksClient .removeResourcePoliciesAsync (request ).get (1 , TimeUnit .MINUTES );
78+ }
79+ } else {
80+ try (DisksClient disksClient = DisksClient .create ()) {
81+ RemoveResourcePoliciesDiskRequest request =
82+ RemoveResourcePoliciesDiskRequest .newBuilder ()
83+ .setDisk (diskName )
84+ .setZone (location )
85+ .setProject (project )
86+ .setDisksRemoveResourcePoliciesRequestResource (
87+ DisksRemoveResourcePoliciesRequest .newBuilder ()
88+ .addAllResourcePolicies (Arrays .asList (consistencyGroupUrl ))
89+ .build ())
90+ .build ();
91+ response = disksClient .removeResourcePoliciesAsync (request ).get (1 , TimeUnit .MINUTES );
92+ }
93+ }
94+ if (response .hasError ()) {
95+ throw new Error ("Error removing disk from consistency group! " + response .getError ());
96+ }
97+ return response .getStatus ();
98+ }
99+ }
100+ // [END compute_consistency_group_remove_disk]
0 commit comments