Skip to content

feat(compute): add compute consistency group stop replication #9694

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

Merged
merged 26 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
594de00
Implemented compute_consistency_group_create and compute_consistency_…
TetyanaYahodska Nov 5, 2024
556a8f8
Merge branch 'main' into compute_consistency_group_create
TetyanaYahodska Nov 6, 2024
b9d3569
Implemented compute_consistency_group_stop_replication sample
TetyanaYahodska Nov 21, 2024
3bf8236
Implemented compute_consistency_group_stop_replication sample
TetyanaYahodska Nov 22, 2024
85f1446
Merge branch 'main' into compute_consistency_group_stop_replication
TetyanaYahodska Nov 22, 2024
9a12b30
Created test and added needed classes for testing
TetyanaYahodska Nov 22, 2024
e2db968
Fixed test
TetyanaYahodska Nov 22, 2024
1d57bb3
Merge branch 'main' into compute_consistency_group_stop_replication
TetyanaYahodska Nov 24, 2024
d116264
Moved clean up methods
TetyanaYahodska Nov 24, 2024
df97636
Added clean up methods for reservations
TetyanaYahodska Nov 24, 2024
c7306ce
Fixed clean up method
TetyanaYahodska Nov 24, 2024
834d52a
Fixed clean up method
TetyanaYahodska Nov 24, 2024
9c6dc48
Added timeout
TetyanaYahodska Nov 25, 2024
1900ef2
Resolved merge conflict
TetyanaYahodska Nov 30, 2024
c30e81b
Reverted not related changes
TetyanaYahodska Nov 30, 2024
1f08297
Reverted not related changes
TetyanaYahodska Nov 30, 2024
9d1635b
Reverted not related changes
TetyanaYahodska Nov 30, 2024
f2ca349
Reverted not related changes
TetyanaYahodska Nov 30, 2024
a601064
Resolved merge conflict
TetyanaYahodska Dec 13, 2024
a03149a
Merge branch 'main' into compute_consistency_group_stop_replication
TetyanaYahodska Dec 18, 2024
63dd42f
Fixed code
TetyanaYahodska Dec 18, 2024
12e6d11
Split samples for zonal location
TetyanaYahodska Dec 18, 2024
f0a547f
Merge branch 'main' into compute_consistency_group_stop_replication
TetyanaYahodska Dec 19, 2024
4b99978
Added comments for methods
TetyanaYahodska Dec 19, 2024
107e4a4
Merge branch 'main' into compute_consistency_group_stop_replication
TetyanaYahodska Dec 23, 2024
78a3f24
Fixed comments
TetyanaYahodska Dec 23, 2024
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,72 @@
/*
* 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_regional_stop_replication]
import com.google.cloud.compute.v1.DisksStopGroupAsyncReplicationResource;
import com.google.cloud.compute.v1.Operation;
import com.google.cloud.compute.v1.Operation.Status;
import com.google.cloud.compute.v1.RegionDisksClient;
import com.google.cloud.compute.v1.StopGroupAsyncReplicationRegionDiskRequest;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class StopRegionalDiskReplicationConsistencyGroup {

public static void main(String[] args)
throws IOException, InterruptedException, ExecutionException, TimeoutException {
// TODO(developer): Replace these variables before running the sample.
// Project ID or project number of the Cloud project that contains the disk.
String project = "YOUR_PROJECT_ID";
// Region of the disk.
String region = "us-central1";
// Name of the consistency group.
String consistencyGroupName = "CONSISTENCY_GROUP";

stopRegionalDiskReplicationConsistencyGroup(project, region, consistencyGroupName);
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a method comment here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Added

// Stops replication of a consistency group for a project in a given region.
public static Status stopRegionalDiskReplicationConsistencyGroup(
String project, String region, String consistencyGroupName)
throws IOException, InterruptedException, ExecutionException, TimeoutException {
String resourcePolicy = 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()) {
StopGroupAsyncReplicationRegionDiskRequest request =
StopGroupAsyncReplicationRegionDiskRequest.newBuilder()
.setProject(project)
.setRegion(region)
.setDisksStopGroupAsyncReplicationResourceResource(
DisksStopGroupAsyncReplicationResource.newBuilder()
.setResourcePolicy(resourcePolicy).build())
.build();
Operation response = disksClient.stopGroupAsyncReplicationAsync(request)
.get(3, TimeUnit.MINUTES);

if (response.hasError()) {
throw new Error("Error stopping disk replication! " + response.getError());
}
return response.getStatus();
}
}
}
// [END compute_consistency_group_regional_stop_replication]
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_stop_replication]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be compute_consistency_group_zonal_stop_replication?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For other samples, we changed the tag only for regional location. I followed this approach.

import com.google.cloud.compute.v1.DisksClient;
import com.google.cloud.compute.v1.DisksStopGroupAsyncReplicationResource;
import com.google.cloud.compute.v1.Operation;
import com.google.cloud.compute.v1.Operation.Status;
import com.google.cloud.compute.v1.StopGroupAsyncReplicationDiskRequest;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class StopZonalDiskReplicationConsistencyGroup {
public static void main(String[] args)
throws IOException, InterruptedException, ExecutionException, TimeoutException {
// TODO(developer): Replace these variables before running the sample.
// Project ID or project number of the Cloud project that contains the disk.
String project = "YOUR_PROJECT_ID";
// Zone of the disk.
String zone = "us-central1-a";
// Name of the consistency group.
String consistencyGroupName = "CONSISTENCY_GROUP";

stopZonalDiskReplicationConsistencyGroup(project, zone, consistencyGroupName);
}

// Stops replication of a consistency group for a project in a given zone.
public static Status stopZonalDiskReplicationConsistencyGroup(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a method comment here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Added

String project, String zone, String consistencyGroupName)
throws IOException, InterruptedException, ExecutionException, TimeoutException {
String region = zone.substring(0, zone.lastIndexOf('-'));

String resourcePolicy = String.format("projects/%s/regions/%s/resourcePolicies/%s",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, shouldn't this be projects/%s/zones/%s/resourcePolicies/%s?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this is the URL of consistency group and has the correct format - projects/PROJECT/regions/REGION/resourcePolicies/CONSISTENCY_GROUP_NAME

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()) {
StopGroupAsyncReplicationDiskRequest request =
StopGroupAsyncReplicationDiskRequest.newBuilder()
.setProject(project)
.setZone(zone)
.setDisksStopGroupAsyncReplicationResourceResource(
DisksStopGroupAsyncReplicationResource.newBuilder()
.setResourcePolicy(resourcePolicy).build())
.build();
Operation response = disksClient.stopGroupAsyncReplicationAsync(request)
.get(3, TimeUnit.MINUTES);

if (response.hasError()) {
throw new Error("Error stopping disk replication! " + response.getError());
}
return response.getStatus();
}
}
}
// [END compute_consistency_group_stop_replication]
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,16 @@
import com.google.cloud.compute.v1.RegionDisksClient;
import com.google.cloud.compute.v1.RemoveResourcePoliciesRegionDiskRequest;
import com.google.cloud.compute.v1.ResourcePoliciesClient;
import com.google.cloud.compute.v1.StopGroupAsyncReplicationDiskRequest;
import com.google.cloud.compute.v1.StopGroupAsyncReplicationRegionDiskRequest;
import compute.disks.consistencygroup.AddDiskToConsistencyGroup;
import compute.disks.consistencygroup.CreateConsistencyGroup;
import compute.disks.consistencygroup.DeleteConsistencyGroup;
import compute.disks.consistencygroup.ListRegionalDisksInConsistencyGroup;
import compute.disks.consistencygroup.ListZonalDisksInConsistencyGroup;
import compute.disks.consistencygroup.RemoveDiskFromConsistencyGroup;
import compute.disks.consistencygroup.StopRegionalDiskReplicationConsistencyGroup;
import compute.disks.consistencygroup.StopZonalDiskReplicationConsistencyGroup;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
Expand All @@ -54,6 +58,7 @@
public class ConsistencyGroupIT {
private static final String PROJECT_ID = "project-id";
private static final String REGION = "asia-east1";
private static final String ZONE = "asia-east1-c";
private static final String CONSISTENCY_GROUP_NAME = "consistency-group";
private static final String DISK_NAME = "disk-for-consistency";

Expand Down Expand Up @@ -193,4 +198,54 @@ public void testListZonalDisksInConsistencyGroup() throws Exception {
verify(mockResponse, times(1)).iterateAll();
}
}

@Test
public void testStopRegionalDiskReplicationConsistencyGroup() 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.stopGroupAsyncReplicationAsync(
any(StopGroupAsyncReplicationRegionDiskRequest.class))).thenReturn(mockFuture);
when(mockFuture.get(anyLong(), any(TimeUnit.class))).thenReturn(operation);
when(operation.getStatus()).thenReturn(Status.DONE);

Status status = StopRegionalDiskReplicationConsistencyGroup
.stopRegionalDiskReplicationConsistencyGroup(
PROJECT_ID, REGION, CONSISTENCY_GROUP_NAME);

verify(mockClient, times(1)).stopGroupAsyncReplicationAsync(
any(StopGroupAsyncReplicationRegionDiskRequest.class));
verify(mockFuture, times(1)).get(anyLong(), any(TimeUnit.class));
assertEquals(Status.DONE, status);
}
}

@Test
public void testStopZonalDiskReplicationConsistencyGroup() throws Exception {
try (MockedStatic<DisksClient> mockedDisksClient =
mockStatic(DisksClient.class)) {
Operation operation = mock(Operation.class);
DisksClient mockClient = mock(DisksClient.class);
OperationFuture mockFuture = mock(OperationFuture.class);

mockedDisksClient.when(DisksClient::create).thenReturn(mockClient);
when(mockClient.stopGroupAsyncReplicationAsync(
any(StopGroupAsyncReplicationDiskRequest.class))).thenReturn(mockFuture);
when(mockFuture.get(anyLong(), any(TimeUnit.class))).thenReturn(operation);
when(operation.getStatus()).thenReturn(Status.DONE);

Status status = StopZonalDiskReplicationConsistencyGroup
.stopZonalDiskReplicationConsistencyGroup(
PROJECT_ID, ZONE, CONSISTENCY_GROUP_NAME);

verify(mockClient, times(1)).stopGroupAsyncReplicationAsync(
any(StopGroupAsyncReplicationDiskRequest.class));
verify(mockFuture, times(1)).get(anyLong(), any(TimeUnit.class));
assertEquals(Status.DONE, status);
}
}
}
Loading