From 9b3b124b5c09748a8dc657557b76ee9057dbef5b Mon Sep 17 00:00:00 2001 From: Tetiana Yahodska Date: Thu, 19 Dec 2024 22:41:45 +0100 Subject: [PATCH 1/2] Implemented compute_consistency_group_clone and compute_consistency_group_clone_regional_disk samples, created tests --- ...loneRegionalDisksFromConsistencyGroup.java | 73 +++++++++++++++++++ .../CloneZonalDisksFromConsistencyGroup.java | 73 +++++++++++++++++++ .../compute/disks/ConsistencyGroupIT.java | 53 ++++++++++++++ 3 files changed, 199 insertions(+) create mode 100644 compute/cloud-client/src/main/java/compute/disks/consistencygroup/CloneRegionalDisksFromConsistencyGroup.java create mode 100644 compute/cloud-client/src/main/java/compute/disks/consistencygroup/CloneZonalDisksFromConsistencyGroup.java diff --git a/compute/cloud-client/src/main/java/compute/disks/consistencygroup/CloneRegionalDisksFromConsistencyGroup.java b/compute/cloud-client/src/main/java/compute/disks/consistencygroup/CloneRegionalDisksFromConsistencyGroup.java new file mode 100644 index 00000000000..b7b0585902d --- /dev/null +++ b/compute/cloud-client/src/main/java/compute/disks/consistencygroup/CloneRegionalDisksFromConsistencyGroup.java @@ -0,0 +1,73 @@ +/* + * Copyright 2024 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package compute.disks.consistencygroup; + +// [START compute_consistency_group_clone_regional_disk] +import com.google.cloud.compute.v1.BulkInsertDiskResource; +import com.google.cloud.compute.v1.BulkInsertRegionDiskRequest; +import com.google.cloud.compute.v1.Operation; +import com.google.cloud.compute.v1.Operation.Status; +import com.google.cloud.compute.v1.RegionDisksClient; +import java.io.IOException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +public class CloneRegionalDisksFromConsistencyGroup { + + public static void main(String[] args) + throws IOException, ExecutionException, InterruptedException, TimeoutException { + // TODO(developer): Replace these variables before running the sample. + // Project ID or project number of the Cloud project you want to use. + String project = "YOUR_PROJECT_ID"; + // Region in which your disks and consistency group are located. + String region = "us-central1"; + // Name of the consistency group you want to clone disks from. + String consistencyGroupName = "YOUR_CONSISTENCY_GROUP_NAME"; + + cloneRegionalDisksFromConsistencyGroup(project, region, consistencyGroupName); + } + + // Clones regional disks from a consistency group. + public static Status cloneRegionalDisksFromConsistencyGroup( + String project, String region, String consistencyGroupName) + throws IOException, InterruptedException, ExecutionException, TimeoutException { + String sourceConsistencyGroupPolicy = String.format( + "projects/%s/regions/%s/resourcePolicies/%s", project, region, consistencyGroupName); + + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. + try (RegionDisksClient disksClient = RegionDisksClient.create()) { + BulkInsertRegionDiskRequest request = BulkInsertRegionDiskRequest.newBuilder() + .setProject(project) + .setRegion(region) + .setBulkInsertDiskResourceResource( + BulkInsertDiskResource.newBuilder() + .setSourceConsistencyGroupPolicy(sourceConsistencyGroupPolicy) + .build()) + .build(); + + Operation response = disksClient.bulkInsertAsync(request).get(3, TimeUnit.MINUTES); + + if (response.hasError()) { + throw new Error("Error cloning regional disks! " + response.getError()); + } + return response.getStatus(); + } + } +} +// [END compute_consistency_group_clone_regional_disk] diff --git a/compute/cloud-client/src/main/java/compute/disks/consistencygroup/CloneZonalDisksFromConsistencyGroup.java b/compute/cloud-client/src/main/java/compute/disks/consistencygroup/CloneZonalDisksFromConsistencyGroup.java new file mode 100644 index 00000000000..63db688d465 --- /dev/null +++ b/compute/cloud-client/src/main/java/compute/disks/consistencygroup/CloneZonalDisksFromConsistencyGroup.java @@ -0,0 +1,73 @@ +/* + * Copyright 2024 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package compute.disks.consistencygroup; + +// [START compute_consistency_group_clone] +import com.google.cloud.compute.v1.BulkInsertDiskRequest; +import com.google.cloud.compute.v1.BulkInsertDiskResource; +import com.google.cloud.compute.v1.DisksClient; +import com.google.cloud.compute.v1.Operation; +import com.google.cloud.compute.v1.Operation.Status; +import java.io.IOException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +public class CloneZonalDisksFromConsistencyGroup { + public static void main(String[] args) + throws IOException, ExecutionException, InterruptedException, TimeoutException { + // TODO(developer): Replace these variables before running the sample. + // Project ID or project number of the Cloud project you want to use. + String project = "YOUR_PROJECT_ID"; + // Zone in which your disks are located. + String disksLocation = "us-central1-a"; + // Name of the consistency group you want to clone disks from. + String consistencyGroupName = "YOUR_CONSISTENCY_GROUP_NAME"; + + cloneZonalDisksFromConsistencyGroup(project, disksLocation, consistencyGroupName); + } + + // Clones zonal disks from a consistency group. + public static Status cloneZonalDisksFromConsistencyGroup( + String project, String zone, String consistencyGroupName) + throws IOException, InterruptedException, ExecutionException, TimeoutException { + String region = zone.substring(0, zone.lastIndexOf('-')); + String sourceConsistencyGroupPolicy = String.format( + "projects/%s/regions/%s/resourcePolicies/%s", project, region, consistencyGroupName); + + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. + try (DisksClient disksClient = DisksClient.create()) { + BulkInsertDiskRequest request = BulkInsertDiskRequest.newBuilder() + .setProject(project) + .setZone(zone) + .setBulkInsertDiskResourceResource( + BulkInsertDiskResource.newBuilder() + .setSourceConsistencyGroupPolicy(sourceConsistencyGroupPolicy) + .build()) + .build(); + + Operation response = disksClient.bulkInsertAsync(request).get(3, TimeUnit.MINUTES); + + if (response.hasError()) { + throw new Error("Error cloning zonal disks! " + response.getError()); + } + return response.getStatus(); + } + } +} +// [END compute_consistency_group_clone] diff --git a/compute/cloud-client/src/test/java/compute/disks/ConsistencyGroupIT.java b/compute/cloud-client/src/test/java/compute/disks/ConsistencyGroupIT.java index 75716d474df..92c53e6a741 100644 --- a/compute/cloud-client/src/test/java/compute/disks/ConsistencyGroupIT.java +++ b/compute/cloud-client/src/test/java/compute/disks/ConsistencyGroupIT.java @@ -27,6 +27,8 @@ import com.google.api.gax.longrunning.OperationFuture; import com.google.cloud.compute.v1.AddResourcePoliciesRegionDiskRequest; +import com.google.cloud.compute.v1.BulkInsertDiskRequest; +import com.google.cloud.compute.v1.BulkInsertRegionDiskRequest; import com.google.cloud.compute.v1.DisksClient; import com.google.cloud.compute.v1.InsertResourcePolicyRequest; import com.google.cloud.compute.v1.ListDisksRequest; @@ -37,6 +39,8 @@ import com.google.cloud.compute.v1.RemoveResourcePoliciesRegionDiskRequest; import com.google.cloud.compute.v1.ResourcePoliciesClient; import compute.disks.consistencygroup.AddDiskToConsistencyGroup; +import compute.disks.consistencygroup.CloneRegionalDisksFromConsistencyGroup; +import compute.disks.consistencygroup.CloneZonalDisksFromConsistencyGroup; import compute.disks.consistencygroup.CreateConsistencyGroup; import compute.disks.consistencygroup.DeleteConsistencyGroup; import compute.disks.consistencygroup.ListRegionalDisksInConsistencyGroup; @@ -193,4 +197,53 @@ public void testListZonalDisksInConsistencyGroup() throws Exception { verify(mockResponse, times(1)).iterateAll(); } } + + @Test + public void testCloneRegionalDisksFromConsistencyGroup() throws Exception { + try (MockedStatic mockedRegionDisksClient = + mockStatic(RegionDisksClient.class)) { + Operation operation = mock(Operation.class); + RegionDisksClient mockClient = mock(RegionDisksClient.class); + OperationFuture mockFuture = mock(OperationFuture.class); + + mockedRegionDisksClient.when(RegionDisksClient::create).thenReturn(mockClient); + when(mockClient.bulkInsertAsync(any(BulkInsertRegionDiskRequest.class))) + .thenReturn(mockFuture); + when(mockFuture.get(anyLong(), any(TimeUnit.class))).thenReturn(operation); + when(operation.getStatus()).thenReturn(Status.DONE); + + Status status = CloneRegionalDisksFromConsistencyGroup + .cloneRegionalDisksFromConsistencyGroup( + PROJECT_ID, REGION, CONSISTENCY_GROUP_NAME); + + verify(mockClient, times(1)) + .bulkInsertAsync(any(BulkInsertRegionDiskRequest.class)); + verify(mockFuture, times(1)).get(anyLong(), any(TimeUnit.class)); + assertEquals(Status.DONE, status); + } + } + + @Test + public void testCloneZonalDisksFromConsistencyGroup() throws Exception { + try (MockedStatic mockedRegionDisksClient = + mockStatic(DisksClient.class)) { + Operation operation = mock(Operation.class); + DisksClient mockClient = mock(DisksClient.class); + OperationFuture mockFuture = mock(OperationFuture.class); + + mockedRegionDisksClient.when(DisksClient::create).thenReturn(mockClient); + when(mockClient.bulkInsertAsync(any(BulkInsertDiskRequest.class))) + .thenReturn(mockFuture); + when(mockFuture.get(anyLong(), any(TimeUnit.class))).thenReturn(operation); + when(operation.getStatus()).thenReturn(Status.DONE); + + Status status = CloneZonalDisksFromConsistencyGroup + .cloneZonalDisksFromConsistencyGroup(PROJECT_ID, REGION, CONSISTENCY_GROUP_NAME); + + verify(mockClient, times(1)) + .bulkInsertAsync(any(BulkInsertDiskRequest.class)); + verify(mockFuture, times(1)).get(anyLong(), any(TimeUnit.class)); + assertEquals(Status.DONE, status); + } + } } \ No newline at end of file From 99a06ea7e0aaf1df1ce3850b759086d844fb9166 Mon Sep 17 00:00:00 2001 From: Tetiana Yahodska Date: Mon, 30 Dec 2024 10:39:32 +0100 Subject: [PATCH 2/2] Fixed naming --- .../consistencygroup/CloneZonalDisksFromConsistencyGroup.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/compute/cloud-client/src/main/java/compute/disks/consistencygroup/CloneZonalDisksFromConsistencyGroup.java b/compute/cloud-client/src/main/java/compute/disks/consistencygroup/CloneZonalDisksFromConsistencyGroup.java index 63db688d465..b819829a100 100644 --- a/compute/cloud-client/src/main/java/compute/disks/consistencygroup/CloneZonalDisksFromConsistencyGroup.java +++ b/compute/cloud-client/src/main/java/compute/disks/consistencygroup/CloneZonalDisksFromConsistencyGroup.java @@ -34,11 +34,11 @@ public static void main(String[] args) // Project ID or project number of the Cloud project you want to use. String project = "YOUR_PROJECT_ID"; // Zone in which your disks are located. - String disksLocation = "us-central1-a"; + String zone = "us-central1-a"; // Name of the consistency group you want to clone disks from. String consistencyGroupName = "YOUR_CONSISTENCY_GROUP_NAME"; - cloneZonalDisksFromConsistencyGroup(project, disksLocation, consistencyGroupName); + cloneZonalDisksFromConsistencyGroup(project, zone, consistencyGroupName); } // Clones zonal disks from a consistency group.