-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(compute): add compute consistency group stop replication (#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
- Loading branch information
1 parent
07416aa
commit 7afa0d2
Showing
3 changed files
with
200 additions
and
0 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
...main/java/compute/disks/consistencygroup/StopRegionalDiskReplicationConsistencyGroup.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
// 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] |
73 changes: 73 additions & 0 deletions
73
...rc/main/java/compute/disks/consistencygroup/StopZonalDiskReplicationConsistencyGroup.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
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( | ||
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", | ||
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters