Skip to content

Commit 7afa0d2

Browse files
feat(compute): add compute consistency group stop replication (GoogleCloudPlatform#9694)
* Implemented compute_consistency_group_create and compute_consistency_group_delete samples, created test * Implemented compute_consistency_group_stop_replication sample * Implemented compute_consistency_group_stop_replication sample * Created test and added needed classes for testing * Fixed test * Moved clean up methods * Added clean up methods for reservations * Fixed clean up method * Fixed clean up method * Added timeout * Reverted not related changes * Reverted not related changes * Reverted not related changes * Reverted not related changes * Fixed code * Split samples for zonal location * Added comments for methods * Fixed comments
1 parent 07416aa commit 7afa0d2

File tree

3 files changed

+200
-0
lines changed

3 files changed

+200
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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_regional_stop_replication]
20+
import com.google.cloud.compute.v1.DisksStopGroupAsyncReplicationResource;
21+
import com.google.cloud.compute.v1.Operation;
22+
import com.google.cloud.compute.v1.Operation.Status;
23+
import com.google.cloud.compute.v1.RegionDisksClient;
24+
import com.google.cloud.compute.v1.StopGroupAsyncReplicationRegionDiskRequest;
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 StopRegionalDiskReplicationConsistencyGroup {
31+
32+
public static void main(String[] args)
33+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
34+
// TODO(developer): Replace these variables before running the sample.
35+
// Project ID or project number of the Cloud project that contains the disk.
36+
String project = "YOUR_PROJECT_ID";
37+
// Region of the disk.
38+
String region = "us-central1";
39+
// Name of the consistency group.
40+
String consistencyGroupName = "CONSISTENCY_GROUP";
41+
42+
stopRegionalDiskReplicationConsistencyGroup(project, region, consistencyGroupName);
43+
}
44+
45+
// Stops replication of a consistency group for a project in a given region.
46+
public static Status stopRegionalDiskReplicationConsistencyGroup(
47+
String project, String region, String consistencyGroupName)
48+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
49+
String resourcePolicy = String.format("projects/%s/regions/%s/resourcePolicies/%s",
50+
project, region, consistencyGroupName);
51+
// Initialize client that will be used to send requests. This client only needs to be created
52+
// once, and can be reused for multiple requests.
53+
try (RegionDisksClient disksClient = RegionDisksClient.create()) {
54+
StopGroupAsyncReplicationRegionDiskRequest request =
55+
StopGroupAsyncReplicationRegionDiskRequest.newBuilder()
56+
.setProject(project)
57+
.setRegion(region)
58+
.setDisksStopGroupAsyncReplicationResourceResource(
59+
DisksStopGroupAsyncReplicationResource.newBuilder()
60+
.setResourcePolicy(resourcePolicy).build())
61+
.build();
62+
Operation response = disksClient.stopGroupAsyncReplicationAsync(request)
63+
.get(3, TimeUnit.MINUTES);
64+
65+
if (response.hasError()) {
66+
throw new Error("Error stopping disk replication! " + response.getError());
67+
}
68+
return response.getStatus();
69+
}
70+
}
71+
}
72+
// [END compute_consistency_group_regional_stop_replication]
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_stop_replication]
20+
import com.google.cloud.compute.v1.DisksClient;
21+
import com.google.cloud.compute.v1.DisksStopGroupAsyncReplicationResource;
22+
import com.google.cloud.compute.v1.Operation;
23+
import com.google.cloud.compute.v1.Operation.Status;
24+
import com.google.cloud.compute.v1.StopGroupAsyncReplicationDiskRequest;
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 StopZonalDiskReplicationConsistencyGroup {
31+
public static void main(String[] args)
32+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
33+
// TODO(developer): Replace these variables before running the sample.
34+
// Project ID or project number of the Cloud project that contains the disk.
35+
String project = "YOUR_PROJECT_ID";
36+
// Zone of the disk.
37+
String zone = "us-central1-a";
38+
// Name of the consistency group.
39+
String consistencyGroupName = "CONSISTENCY_GROUP";
40+
41+
stopZonalDiskReplicationConsistencyGroup(project, zone, consistencyGroupName);
42+
}
43+
44+
// Stops replication of a consistency group for a project in a given zone.
45+
public static Status stopZonalDiskReplicationConsistencyGroup(
46+
String project, String zone, String consistencyGroupName)
47+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
48+
String region = zone.substring(0, zone.lastIndexOf('-'));
49+
50+
String resourcePolicy = String.format("projects/%s/regions/%s/resourcePolicies/%s",
51+
project, region, consistencyGroupName);
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+
StopGroupAsyncReplicationDiskRequest request =
56+
StopGroupAsyncReplicationDiskRequest.newBuilder()
57+
.setProject(project)
58+
.setZone(zone)
59+
.setDisksStopGroupAsyncReplicationResourceResource(
60+
DisksStopGroupAsyncReplicationResource.newBuilder()
61+
.setResourcePolicy(resourcePolicy).build())
62+
.build();
63+
Operation response = disksClient.stopGroupAsyncReplicationAsync(request)
64+
.get(3, TimeUnit.MINUTES);
65+
66+
if (response.hasError()) {
67+
throw new Error("Error stopping disk replication! " + response.getError());
68+
}
69+
return response.getStatus();
70+
}
71+
}
72+
}
73+
// [END compute_consistency_group_stop_replication]

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,16 @@
3636
import com.google.cloud.compute.v1.RegionDisksClient;
3737
import com.google.cloud.compute.v1.RemoveResourcePoliciesRegionDiskRequest;
3838
import com.google.cloud.compute.v1.ResourcePoliciesClient;
39+
import com.google.cloud.compute.v1.StopGroupAsyncReplicationDiskRequest;
40+
import com.google.cloud.compute.v1.StopGroupAsyncReplicationRegionDiskRequest;
3941
import compute.disks.consistencygroup.AddDiskToConsistencyGroup;
4042
import compute.disks.consistencygroup.CreateConsistencyGroup;
4143
import compute.disks.consistencygroup.DeleteConsistencyGroup;
4244
import compute.disks.consistencygroup.ListRegionalDisksInConsistencyGroup;
4345
import compute.disks.consistencygroup.ListZonalDisksInConsistencyGroup;
4446
import compute.disks.consistencygroup.RemoveDiskFromConsistencyGroup;
47+
import compute.disks.consistencygroup.StopRegionalDiskReplicationConsistencyGroup;
48+
import compute.disks.consistencygroup.StopZonalDiskReplicationConsistencyGroup;
4549
import java.util.concurrent.TimeUnit;
4650
import org.junit.jupiter.api.Test;
4751
import org.junit.jupiter.api.Timeout;
@@ -54,6 +58,7 @@
5458
public class ConsistencyGroupIT {
5559
private static final String PROJECT_ID = "project-id";
5660
private static final String REGION = "asia-east1";
61+
private static final String ZONE = "asia-east1-c";
5762
private static final String CONSISTENCY_GROUP_NAME = "consistency-group";
5863
private static final String DISK_NAME = "disk-for-consistency";
5964

@@ -193,4 +198,54 @@ public void testListZonalDisksInConsistencyGroup() throws Exception {
193198
verify(mockResponse, times(1)).iterateAll();
194199
}
195200
}
201+
202+
@Test
203+
public void testStopRegionalDiskReplicationConsistencyGroup() throws Exception {
204+
try (MockedStatic<RegionDisksClient> mockedRegionDisksClient =
205+
mockStatic(RegionDisksClient.class)) {
206+
Operation operation = mock(Operation.class);
207+
RegionDisksClient mockClient = mock(RegionDisksClient.class);
208+
OperationFuture mockFuture = mock(OperationFuture.class);
209+
210+
mockedRegionDisksClient.when(RegionDisksClient::create).thenReturn(mockClient);
211+
when(mockClient.stopGroupAsyncReplicationAsync(
212+
any(StopGroupAsyncReplicationRegionDiskRequest.class))).thenReturn(mockFuture);
213+
when(mockFuture.get(anyLong(), any(TimeUnit.class))).thenReturn(operation);
214+
when(operation.getStatus()).thenReturn(Status.DONE);
215+
216+
Status status = StopRegionalDiskReplicationConsistencyGroup
217+
.stopRegionalDiskReplicationConsistencyGroup(
218+
PROJECT_ID, REGION, CONSISTENCY_GROUP_NAME);
219+
220+
verify(mockClient, times(1)).stopGroupAsyncReplicationAsync(
221+
any(StopGroupAsyncReplicationRegionDiskRequest.class));
222+
verify(mockFuture, times(1)).get(anyLong(), any(TimeUnit.class));
223+
assertEquals(Status.DONE, status);
224+
}
225+
}
226+
227+
@Test
228+
public void testStopZonalDiskReplicationConsistencyGroup() throws Exception {
229+
try (MockedStatic<DisksClient> mockedDisksClient =
230+
mockStatic(DisksClient.class)) {
231+
Operation operation = mock(Operation.class);
232+
DisksClient mockClient = mock(DisksClient.class);
233+
OperationFuture mockFuture = mock(OperationFuture.class);
234+
235+
mockedDisksClient.when(DisksClient::create).thenReturn(mockClient);
236+
when(mockClient.stopGroupAsyncReplicationAsync(
237+
any(StopGroupAsyncReplicationDiskRequest.class))).thenReturn(mockFuture);
238+
when(mockFuture.get(anyLong(), any(TimeUnit.class))).thenReturn(operation);
239+
when(operation.getStatus()).thenReturn(Status.DONE);
240+
241+
Status status = StopZonalDiskReplicationConsistencyGroup
242+
.stopZonalDiskReplicationConsistencyGroup(
243+
PROJECT_ID, ZONE, CONSISTENCY_GROUP_NAME);
244+
245+
verify(mockClient, times(1)).stopGroupAsyncReplicationAsync(
246+
any(StopGroupAsyncReplicationDiskRequest.class));
247+
verify(mockFuture, times(1)).get(anyLong(), any(TimeUnit.class));
248+
assertEquals(Status.DONE, status);
249+
}
250+
}
196251
}

0 commit comments

Comments
 (0)