Skip to content

Commit 1e9647e

Browse files
feat(compute): add compute consistency group clone sample (GoogleCloudPlatform#9885)
* Implemented compute_consistency_group_clone and compute_consistency_group_clone_regional_disk samples, created tests * Fixed naming
1 parent db76fdc commit 1e9647e

File tree

3 files changed

+199
-0
lines changed

3 files changed

+199
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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_clone_regional_disk]
20+
import com.google.cloud.compute.v1.BulkInsertDiskResource;
21+
import com.google.cloud.compute.v1.BulkInsertRegionDiskRequest;
22+
import com.google.cloud.compute.v1.Operation;
23+
import com.google.cloud.compute.v1.Operation.Status;
24+
import com.google.cloud.compute.v1.RegionDisksClient;
25+
import java.io.IOException;
26+
import java.util.concurrent.ExecutionException;
27+
import java.util.concurrent.TimeUnit;
28+
import java.util.concurrent.TimeoutException;
29+
30+
public class CloneRegionalDisksFromConsistencyGroup {
31+
32+
public static void main(String[] args)
33+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
34+
// TODO(developer): Replace these variables before running the sample.
35+
// Project ID or project number of the Cloud project you want to use.
36+
String project = "YOUR_PROJECT_ID";
37+
// Region in which your disks and consistency group are located.
38+
String region = "us-central1";
39+
// Name of the consistency group you want to clone disks from.
40+
String consistencyGroupName = "YOUR_CONSISTENCY_GROUP_NAME";
41+
42+
cloneRegionalDisksFromConsistencyGroup(project, region, consistencyGroupName);
43+
}
44+
45+
// Clones regional disks from a consistency group.
46+
public static Status cloneRegionalDisksFromConsistencyGroup(
47+
String project, String region, String consistencyGroupName)
48+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
49+
String sourceConsistencyGroupPolicy = String.format(
50+
"projects/%s/regions/%s/resourcePolicies/%s", project, region, consistencyGroupName);
51+
52+
// Initialize client that will be used to send requests. This client only needs to be created
53+
// once, and can be reused for multiple requests.
54+
try (RegionDisksClient disksClient = RegionDisksClient.create()) {
55+
BulkInsertRegionDiskRequest request = BulkInsertRegionDiskRequest.newBuilder()
56+
.setProject(project)
57+
.setRegion(region)
58+
.setBulkInsertDiskResourceResource(
59+
BulkInsertDiskResource.newBuilder()
60+
.setSourceConsistencyGroupPolicy(sourceConsistencyGroupPolicy)
61+
.build())
62+
.build();
63+
64+
Operation response = disksClient.bulkInsertAsync(request).get(3, TimeUnit.MINUTES);
65+
66+
if (response.hasError()) {
67+
throw new Error("Error cloning regional disks! " + response.getError());
68+
}
69+
return response.getStatus();
70+
}
71+
}
72+
}
73+
// [END compute_consistency_group_clone_regional_disk]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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_clone]
20+
import com.google.cloud.compute.v1.BulkInsertDiskRequest;
21+
import com.google.cloud.compute.v1.BulkInsertDiskResource;
22+
import com.google.cloud.compute.v1.DisksClient;
23+
import com.google.cloud.compute.v1.Operation;
24+
import com.google.cloud.compute.v1.Operation.Status;
25+
import java.io.IOException;
26+
import java.util.concurrent.ExecutionException;
27+
import java.util.concurrent.TimeUnit;
28+
import java.util.concurrent.TimeoutException;
29+
30+
public class CloneZonalDisksFromConsistencyGroup {
31+
public static void main(String[] args)
32+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
33+
// TODO(developer): Replace these variables before running the sample.
34+
// Project ID or project number of the Cloud project you want to use.
35+
String project = "YOUR_PROJECT_ID";
36+
// Zone in which your disks are located.
37+
String zone = "us-central1-a";
38+
// Name of the consistency group you want to clone disks from.
39+
String consistencyGroupName = "YOUR_CONSISTENCY_GROUP_NAME";
40+
41+
cloneZonalDisksFromConsistencyGroup(project, zone, consistencyGroupName);
42+
}
43+
44+
// Clones zonal disks from a consistency group.
45+
public static Status cloneZonalDisksFromConsistencyGroup(
46+
String project, String zone, String consistencyGroupName)
47+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
48+
String region = zone.substring(0, zone.lastIndexOf('-'));
49+
String sourceConsistencyGroupPolicy = String.format(
50+
"projects/%s/regions/%s/resourcePolicies/%s", project, region, consistencyGroupName);
51+
52+
// Initialize client that will be used to send requests. This client only needs to be created
53+
// once, and can be reused for multiple requests.
54+
try (DisksClient disksClient = DisksClient.create()) {
55+
BulkInsertDiskRequest request = BulkInsertDiskRequest.newBuilder()
56+
.setProject(project)
57+
.setZone(zone)
58+
.setBulkInsertDiskResourceResource(
59+
BulkInsertDiskResource.newBuilder()
60+
.setSourceConsistencyGroupPolicy(sourceConsistencyGroupPolicy)
61+
.build())
62+
.build();
63+
64+
Operation response = disksClient.bulkInsertAsync(request).get(3, TimeUnit.MINUTES);
65+
66+
if (response.hasError()) {
67+
throw new Error("Error cloning zonal disks! " + response.getError());
68+
}
69+
return response.getStatus();
70+
}
71+
}
72+
}
73+
// [END compute_consistency_group_clone]

compute/cloud-client/src/test/java/compute/disks/ConsistencyGroupIT.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
import com.google.api.gax.longrunning.OperationFuture;
2929
import com.google.cloud.compute.v1.AddResourcePoliciesRegionDiskRequest;
30+
import com.google.cloud.compute.v1.BulkInsertDiskRequest;
31+
import com.google.cloud.compute.v1.BulkInsertRegionDiskRequest;
3032
import com.google.cloud.compute.v1.DisksClient;
3133
import com.google.cloud.compute.v1.InsertResourcePolicyRequest;
3234
import com.google.cloud.compute.v1.ListDisksRequest;
@@ -39,6 +41,8 @@
3941
import com.google.cloud.compute.v1.StopGroupAsyncReplicationDiskRequest;
4042
import com.google.cloud.compute.v1.StopGroupAsyncReplicationRegionDiskRequest;
4143
import compute.disks.consistencygroup.AddDiskToConsistencyGroup;
44+
import compute.disks.consistencygroup.CloneRegionalDisksFromConsistencyGroup;
45+
import compute.disks.consistencygroup.CloneZonalDisksFromConsistencyGroup;
4246
import compute.disks.consistencygroup.CreateConsistencyGroup;
4347
import compute.disks.consistencygroup.DeleteConsistencyGroup;
4448
import compute.disks.consistencygroup.ListRegionalDisksInConsistencyGroup;
@@ -248,4 +252,53 @@ public void testStopZonalDiskReplicationConsistencyGroup() throws Exception {
248252
assertEquals(Status.DONE, status);
249253
}
250254
}
255+
256+
@Test
257+
public void testCloneRegionalDisksFromConsistencyGroup() throws Exception {
258+
try (MockedStatic<RegionDisksClient> mockedRegionDisksClient =
259+
mockStatic(RegionDisksClient.class)) {
260+
Operation operation = mock(Operation.class);
261+
RegionDisksClient mockClient = mock(RegionDisksClient.class);
262+
OperationFuture mockFuture = mock(OperationFuture.class);
263+
264+
mockedRegionDisksClient.when(RegionDisksClient::create).thenReturn(mockClient);
265+
when(mockClient.bulkInsertAsync(any(BulkInsertRegionDiskRequest.class)))
266+
.thenReturn(mockFuture);
267+
when(mockFuture.get(anyLong(), any(TimeUnit.class))).thenReturn(operation);
268+
when(operation.getStatus()).thenReturn(Status.DONE);
269+
270+
Status status = CloneRegionalDisksFromConsistencyGroup
271+
.cloneRegionalDisksFromConsistencyGroup(
272+
PROJECT_ID, REGION, CONSISTENCY_GROUP_NAME);
273+
274+
verify(mockClient, times(1))
275+
.bulkInsertAsync(any(BulkInsertRegionDiskRequest.class));
276+
verify(mockFuture, times(1)).get(anyLong(), any(TimeUnit.class));
277+
assertEquals(Status.DONE, status);
278+
}
279+
}
280+
281+
@Test
282+
public void testCloneZonalDisksFromConsistencyGroup() throws Exception {
283+
try (MockedStatic<DisksClient> mockedRegionDisksClient =
284+
mockStatic(DisksClient.class)) {
285+
Operation operation = mock(Operation.class);
286+
DisksClient mockClient = mock(DisksClient.class);
287+
OperationFuture mockFuture = mock(OperationFuture.class);
288+
289+
mockedRegionDisksClient.when(DisksClient::create).thenReturn(mockClient);
290+
when(mockClient.bulkInsertAsync(any(BulkInsertDiskRequest.class)))
291+
.thenReturn(mockFuture);
292+
when(mockFuture.get(anyLong(), any(TimeUnit.class))).thenReturn(operation);
293+
when(operation.getStatus()).thenReturn(Status.DONE);
294+
295+
Status status = CloneZonalDisksFromConsistencyGroup
296+
.cloneZonalDisksFromConsistencyGroup(PROJECT_ID, REGION, CONSISTENCY_GROUP_NAME);
297+
298+
verify(mockClient, times(1))
299+
.bulkInsertAsync(any(BulkInsertDiskRequest.class));
300+
verify(mockFuture, times(1)).get(anyLong(), any(TimeUnit.class));
301+
assertEquals(Status.DONE, status);
302+
}
303+
}
251304
}

0 commit comments

Comments
 (0)