|
| 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.Disk; |
| 21 | +import com.google.cloud.compute.v1.Operation; |
| 22 | +import com.google.cloud.compute.v1.RegionDisksClient; |
| 23 | +import com.google.cloud.compute.v1.RegionDisksRemoveResourcePoliciesRequest; |
| 24 | +import com.google.cloud.compute.v1.RemoveResourcePoliciesRegionDiskRequest; |
| 25 | +// If your disk has zonal location uncomment these lines |
| 26 | +//import com.google.cloud.compute.v1.DisksClient; |
| 27 | +//import com.google.cloud.compute.v1.DisksRemoveResourcePoliciesRequest; |
| 28 | +//import com.google.cloud.compute.v1.RemoveResourcePoliciesDiskRequest; |
| 29 | +import java.io.IOException; |
| 30 | +import java.util.Arrays; |
| 31 | +import java.util.concurrent.ExecutionException; |
| 32 | + |
| 33 | +public class RemoveDiskFromConsistencyGroup { |
| 34 | + |
| 35 | + public static void main(String[] args) |
| 36 | + throws IOException, ExecutionException, InterruptedException { |
| 37 | + // TODO(developer): Replace these variables before running the sample. |
| 38 | + // The project that contains the disk. |
| 39 | + String project = "YOUR_PROJECT_ID"; |
| 40 | + // The zone or region of the disk. |
| 41 | + String location = "us-central1"; |
| 42 | + // The name of the disk. |
| 43 | + String diskName = "DISK_NAME"; |
| 44 | + // The name of the consistency group. |
| 45 | + String consistencyGroupName = "CONSISTENCY_GROUP"; |
| 46 | + // The region of the consistency group. |
| 47 | + String consistencyGroupLocation = "us-central1"; |
| 48 | + removeDiskFromConsistencyGroup( |
| 49 | + project, location, diskName, consistencyGroupName, consistencyGroupLocation); |
| 50 | + } |
| 51 | + |
| 52 | + // Removes a disk from a Consistency Group. |
| 53 | + public static Disk removeDiskFromConsistencyGroup( |
| 54 | + String project, String location, String diskName, |
| 55 | + String consistencyGroupName, String consistencyGroupLocation) |
| 56 | + throws IOException, ExecutionException, InterruptedException { |
| 57 | + String consistencyGroupUrl = String.format( |
| 58 | + "https://www.googleapis.com/compute/v1/projects/%s/regions/%s/resourcePolicies/%s", |
| 59 | + project, consistencyGroupLocation, consistencyGroupName); |
| 60 | + // If your disk has zonal location uncomment these lines |
| 61 | + // try (DisksClient disksClient = DisksClient.create()) { |
| 62 | + // RemoveResourcePoliciesDiskRequest request = |
| 63 | + // RemoveResourcePoliciesDiskRequest.newBuilder() |
| 64 | + // .setDisk(diskName) |
| 65 | + // .setDisksRemoveResourcePoliciesRequestResource( |
| 66 | + // DisksRemoveResourcePoliciesRequest.newBuilder() |
| 67 | + // .addAllResourcePolicies(Arrays.asList(consistencyGroupUrl)) |
| 68 | + // .build()) |
| 69 | + // .setProject(project) |
| 70 | + // .setZone(location) |
| 71 | + // .build(); |
| 72 | + |
| 73 | + try (RegionDisksClient disksClient = RegionDisksClient.create()) { |
| 74 | + RemoveResourcePoliciesRegionDiskRequest disksRequest = |
| 75 | + RemoveResourcePoliciesRegionDiskRequest.newBuilder() |
| 76 | + .setDisk(diskName) |
| 77 | + .setRegion(location) |
| 78 | + .setProject(project) |
| 79 | + .setRegionDisksRemoveResourcePoliciesRequestResource( |
| 80 | + RegionDisksRemoveResourcePoliciesRequest.newBuilder() |
| 81 | + .addAllResourcePolicies(Arrays.asList(consistencyGroupUrl)) |
| 82 | + .build()) |
| 83 | + .build(); |
| 84 | + |
| 85 | + Operation response = disksClient.removeResourcePoliciesAsync(disksRequest).get(); |
| 86 | + if (response.hasError()) { |
| 87 | + return null; |
| 88 | + } |
| 89 | + return disksClient.get(project, location, diskName); |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | +// [END compute_consistency_group_remove_disk] |
0 commit comments