Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(compute): add compute consistency group clone sample #9885

Merged
merged 5 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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);
Sita04 marked this conversation as resolved.
Show resolved Hide resolved

// 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]
Original file line number Diff line number Diff line change
@@ -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 zone = "us-central1-a";
// Name of the consistency group you want to clone disks from.
String consistencyGroupName = "YOUR_CONSISTENCY_GROUP_NAME";

cloneZonalDisksFromConsistencyGroup(project, zone, 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]
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -39,6 +41,8 @@
import com.google.cloud.compute.v1.StopGroupAsyncReplicationDiskRequest;
import com.google.cloud.compute.v1.StopGroupAsyncReplicationRegionDiskRequest;
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;
Expand Down Expand Up @@ -248,4 +252,53 @@ public void testStopZonalDiskReplicationConsistencyGroup() throws Exception {
assertEquals(Status.DONE, status);
}
}

@Test
public void testCloneRegionalDisksFromConsistencyGroup() throws Exception {
try (MockedStatic<RegionDisksClient> 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<DisksClient> 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);
}
}
}
Loading