Skip to content

Commit d8d6253

Browse files
feat(compute): add compute disk start/stop replication samples (#9650)
* Implemented compute_disk_start_replication and compute_disk_stop_replication samples, created tests * Fixed test * Deleted not related classes * Fixed lint issue * Increased timeout * Split samples for zonal location * Fixed code * Fixed code * Increased timeout * Increased timeout
1 parent 0e52e74 commit d8d6253

15 files changed

+486
-22
lines changed

compute/cloud-client/src/main/java/compute/disks/CreateReplicatedDisk.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.google.cloud.compute.v1.Disk;
2121
import com.google.cloud.compute.v1.InsertRegionDiskRequest;
2222
import com.google.cloud.compute.v1.Operation;
23+
import com.google.cloud.compute.v1.Operation.Status;
2324
import com.google.cloud.compute.v1.RegionDisksClient;
2425
import java.io.IOException;
2526
import java.util.ArrayList;
@@ -55,7 +56,7 @@ public static void main(String[] args)
5556
}
5657

5758
// Create a disk for synchronous data replication between two zones in the same region
58-
public static Operation.Status createReplicatedDisk(String projectId, String region,
59+
public static Status createReplicatedDisk(String projectId, String region,
5960
List<String> replicaZones, String diskName, int diskSizeGb, String diskType)
6061
throws IOException, InterruptedException, ExecutionException, TimeoutException {
6162
// Initialize client that will be used to send requests. This client only needs to be created
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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;
18+
19+
// [START compute_regional_disk_start_replication]
20+
import com.google.cloud.compute.v1.Operation;
21+
import com.google.cloud.compute.v1.Operation.Status;
22+
import com.google.cloud.compute.v1.RegionDisksClient;
23+
import com.google.cloud.compute.v1.RegionDisksStartAsyncReplicationRequest;
24+
import com.google.cloud.compute.v1.StartAsyncReplicationRegionDiskRequest;
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 StartRegionalDiskReplication {
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+
// The project that contains the primary disk.
36+
String projectId = "YOUR_PROJECT_ID";
37+
// Name of the primary disk.
38+
String primaryDiskName = "PRIMARY_DISK_NAME";
39+
// Name of the secondary disk.
40+
String secondaryDiskName = "SECONDARY_DISK_NAME";
41+
// Name of the region in which your primary disk is located.
42+
// Learn more about zones and regions:
43+
// https://cloud.google.com/compute/docs/disks/async-pd/about#supported_region_pairs
44+
String primaryDiskLocation = "us-central1-a";
45+
// Name of the region in which your secondary disk is located.
46+
String secondaryDiskLocation = "us-east1-b";
47+
48+
startRegionalDiskAsyncReplication(projectId, primaryDiskName, primaryDiskLocation,
49+
secondaryDiskName, secondaryDiskLocation);
50+
}
51+
52+
// Starts asynchronous replication for the specified regional disk.
53+
public static Status startRegionalDiskAsyncReplication(String projectId, String primaryDiskName,
54+
String primaryDiskLocation, String secondaryDiskName, String secondaryDiskLocation)
55+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
56+
String secondaryDiskPath = String.format("projects/%s/regions/%s/disks/%s",
57+
projectId, secondaryDiskLocation, secondaryDiskName);
58+
59+
// Initialize client that will be used to send requests. This client only needs to be created
60+
// once, and can be reused for multiple requests.
61+
try (RegionDisksClient disksClient = RegionDisksClient.create()) {
62+
RegionDisksStartAsyncReplicationRequest diskRequest =
63+
RegionDisksStartAsyncReplicationRequest.newBuilder()
64+
.setAsyncSecondaryDisk(secondaryDiskPath)
65+
.build();
66+
StartAsyncReplicationRegionDiskRequest request =
67+
StartAsyncReplicationRegionDiskRequest.newBuilder()
68+
.setDisk(primaryDiskName)
69+
.setRegionDisksStartAsyncReplicationRequestResource(diskRequest)
70+
.setProject(projectId)
71+
.setRegion(primaryDiskLocation)
72+
.build();
73+
Operation response = disksClient.startAsyncReplicationAsync(request).get(1, TimeUnit.MINUTES);
74+
75+
if (response.hasError()) {
76+
throw new Error("Error starting replication! " + response.getError());
77+
}
78+
return response.getStatus();
79+
}
80+
}
81+
}
82+
// [END compute_regional_disk_start_replication]
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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;
18+
19+
// [START compute_disk_start_replication]
20+
import com.google.cloud.compute.v1.DisksClient;
21+
import com.google.cloud.compute.v1.DisksStartAsyncReplicationRequest;
22+
import com.google.cloud.compute.v1.Operation;
23+
import com.google.cloud.compute.v1.Operation.Status;
24+
import com.google.cloud.compute.v1.StartAsyncReplicationDiskRequest;
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 StartZonalDiskReplication {
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+
// The project that contains the primary disk.
36+
String projectId = "YOUR_PROJECT_ID";
37+
// Name of the primary disk.
38+
String primaryDiskName = "PRIMARY_DISK_NAME";
39+
// Name of the secondary disk.
40+
String secondaryDiskName = "SECONDARY_DISK_NAME";
41+
// Name of the zone in which your primary disk is located.
42+
// Learn more about zones and regions:
43+
// https://cloud.google.com/compute/docs/disks/async-pd/about#supported_region_pairs
44+
String primaryDiskLocation = "us-central1-a";
45+
// Name of the zone in which your secondary disk is located.
46+
String secondaryDiskLocation = "us-east1-b";
47+
48+
startZonalDiskAsyncReplication(projectId, primaryDiskName, primaryDiskLocation,
49+
secondaryDiskName, secondaryDiskLocation);
50+
}
51+
52+
// Starts asynchronous replication for the specified zonal disk.
53+
public static Status startZonalDiskAsyncReplication(String projectId, String primaryDiskName,
54+
String primaryDiskLocation, String secondaryDiskName, String secondaryDiskLocation)
55+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
56+
String secondaryDiskPath = String.format("projects/%s/zones/%s/disks/%s",
57+
projectId, secondaryDiskLocation, secondaryDiskName);
58+
// Initialize client that will be used to send requests. This client only needs to be created
59+
// once, and can be reused for multiple requests.
60+
try (DisksClient disksClient = DisksClient.create()) {
61+
DisksStartAsyncReplicationRequest diskRequest =
62+
DisksStartAsyncReplicationRequest.newBuilder()
63+
.setAsyncSecondaryDisk(secondaryDiskPath)
64+
.build();
65+
66+
StartAsyncReplicationDiskRequest request =
67+
StartAsyncReplicationDiskRequest.newBuilder()
68+
.setDisk(primaryDiskName)
69+
.setDisksStartAsyncReplicationRequestResource(diskRequest)
70+
.setProject(projectId)
71+
.setZone(primaryDiskLocation)
72+
.build();
73+
Operation response = disksClient.startAsyncReplicationAsync(request).get(1, TimeUnit.MINUTES);
74+
75+
if (response.hasError()) {
76+
throw new Error("Error starting replication! " + response.getError());
77+
}
78+
return response.getStatus();
79+
}
80+
}
81+
}
82+
// [END compute_disk_start_replication]
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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;
18+
19+
// [START compute_regional_disk_stop_replication]
20+
import com.google.cloud.compute.v1.Operation;
21+
import com.google.cloud.compute.v1.Operation.Status;
22+
import com.google.cloud.compute.v1.RegionDisksClient;
23+
import com.google.cloud.compute.v1.StopAsyncReplicationRegionDiskRequest;
24+
import java.io.IOException;
25+
import java.util.concurrent.ExecutionException;
26+
import java.util.concurrent.TimeUnit;
27+
import java.util.concurrent.TimeoutException;
28+
29+
public class StopRegionalDiskReplication {
30+
31+
public static void main(String[] args)
32+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
33+
// TODO(developer): Replace these variables before running the sample.
34+
// The project that contains the primary disk.
35+
String projectId = "YOUR_PROJECT_ID";
36+
// Name of the region or zone in which your secondary disk is located.
37+
String secondaryDiskLocation = "us-east1-b";
38+
// Name of the secondary disk.
39+
String secondaryDiskName = "SECONDARY_DISK_NAME";
40+
41+
stopRegionalDiskAsyncReplication(projectId, secondaryDiskLocation, secondaryDiskName);
42+
}
43+
44+
// Stops asynchronous replication for the specified disk.
45+
public static Status stopRegionalDiskAsyncReplication(
46+
String project, String secondaryDiskLocation, String secondaryDiskName)
47+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
48+
// Initialize client that will be used to send requests. This client only needs to be created
49+
// once, and can be reused for multiple requests.
50+
try (RegionDisksClient disksClient = RegionDisksClient.create()) {
51+
StopAsyncReplicationRegionDiskRequest stopReplicationDiskRequest =
52+
StopAsyncReplicationRegionDiskRequest.newBuilder()
53+
.setDisk(secondaryDiskName)
54+
.setProject(project)
55+
.setRegion(secondaryDiskLocation)
56+
.build();
57+
Operation response = disksClient.stopAsyncReplicationAsync(stopReplicationDiskRequest)
58+
.get(1, TimeUnit.MINUTES);
59+
60+
if (response.hasError()) {
61+
throw new Error("Error stopping replication! " + response.getError());
62+
}
63+
return response.getStatus();
64+
}
65+
}
66+
}
67+
// [END compute_regional_disk_stop_replication]
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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;
18+
19+
// [START compute_disk_stop_replication]
20+
import com.google.cloud.compute.v1.DisksClient;
21+
import com.google.cloud.compute.v1.Operation;
22+
import com.google.cloud.compute.v1.Operation.Status;
23+
import com.google.cloud.compute.v1.StopAsyncReplicationDiskRequest;
24+
import java.io.IOException;
25+
import java.util.concurrent.ExecutionException;
26+
import java.util.concurrent.TimeUnit;
27+
import java.util.concurrent.TimeoutException;
28+
29+
public class StopZonalDiskReplication {
30+
31+
public static void main(String[] args)
32+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
33+
// TODO(developer): Replace these variables before running the sample.
34+
// The project that contains the primary disk.
35+
String projectId = "YOUR_PROJECT_ID";
36+
// Name of the region or zone in which your secondary disk is located.
37+
String secondaryDiskLocation = "us-east1-b";
38+
// Name of the secondary disk.
39+
String secondaryDiskName = "SECONDARY_DISK_NAME";
40+
41+
stopZonalDiskAsyncReplication(projectId, secondaryDiskLocation, secondaryDiskName);
42+
}
43+
44+
// Stops asynchronous replication for the specified disk.
45+
public static Status stopZonalDiskAsyncReplication(
46+
String project, String secondaryDiskLocation, String secondaryDiskName)
47+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
48+
// Initialize client that will be used to send requests. This client only needs to be created
49+
// once, and can be reused for multiple requests.
50+
try (DisksClient disksClient = DisksClient.create()) {
51+
StopAsyncReplicationDiskRequest stopReplicationDiskRequest =
52+
StopAsyncReplicationDiskRequest.newBuilder()
53+
.setProject(project)
54+
.setDisk(secondaryDiskName)
55+
.setZone(secondaryDiskLocation)
56+
.build();
57+
Operation response = disksClient.stopAsyncReplicationAsync(stopReplicationDiskRequest)
58+
.get(1, TimeUnit.MINUTES);
59+
60+
if (response.hasError()) {
61+
throw new Error("Error stopping replication! " + response.getError());
62+
}
63+
return response.getStatus();
64+
}
65+
}
66+
}
67+
// [END compute_disk_stop_replication]

compute/cloud-client/src/main/java/compute/disks/consistencygroup/AddDiskToConsistencyGroup.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.google.cloud.compute.v1.DisksAddResourcePoliciesRequest;
2323
import com.google.cloud.compute.v1.DisksClient;
2424
import com.google.cloud.compute.v1.Operation;
25+
import com.google.cloud.compute.v1.Operation.Status;
2526
import com.google.cloud.compute.v1.RegionDisksAddResourcePoliciesRequest;
2627
import com.google.cloud.compute.v1.RegionDisksClient;
2728
import java.io.IOException;
@@ -50,7 +51,7 @@ public static void main(String[] args)
5051
}
5152

5253
// Adds a disk to a consistency group.
53-
public static Operation.Status addDiskToConsistencyGroup(
54+
public static Status addDiskToConsistencyGroup(
5455
String project, String location, String diskName,
5556
String consistencyGroupName, String consistencyGroupLocation)
5657
throws IOException, ExecutionException, InterruptedException, TimeoutException {

compute/cloud-client/src/main/java/compute/disks/consistencygroup/CreateConsistencyGroup.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
// [START compute_consistency_group_create]
2020
import com.google.cloud.compute.v1.InsertResourcePolicyRequest;
2121
import com.google.cloud.compute.v1.Operation;
22+
import com.google.cloud.compute.v1.Operation.Status;
2223
import com.google.cloud.compute.v1.ResourcePoliciesClient;
2324
import com.google.cloud.compute.v1.ResourcePolicy;
2425
import java.io.IOException;
@@ -42,7 +43,7 @@ public static void main(String[] args)
4243
}
4344

4445
// Creates a new consistency group resource policy in the specified project and region.
45-
public static Operation.Status createConsistencyGroup(
46+
public static Status createConsistencyGroup(
4647
String project, String region, String consistencyGroupName)
4748
throws IOException, ExecutionException, InterruptedException, TimeoutException {
4849
// Initialize client that will be used to send requests. This client only needs to be created

compute/cloud-client/src/main/java/compute/disks/consistencygroup/DeleteConsistencyGroup.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
// [START compute_consistency_group_delete]
2020
import com.google.cloud.compute.v1.Operation;
21+
import com.google.cloud.compute.v1.Operation.Status;
2122
import com.google.cloud.compute.v1.ResourcePoliciesClient;
2223
import java.io.IOException;
2324
import java.util.concurrent.ExecutionException;
@@ -40,7 +41,7 @@ public static void main(String[] args)
4041
}
4142

4243
// Deletes a consistency group resource policy in the specified project and region.
43-
public static Operation.Status deleteConsistencyGroup(
44+
public static Status deleteConsistencyGroup(
4445
String project, String region, String consistencyGroupName)
4546
throws IOException, ExecutionException, InterruptedException, TimeoutException {
4647
// Initialize client that will be used to send requests. This client only needs to be created

compute/cloud-client/src/main/java/compute/disks/consistencygroup/RemoveDiskFromConsistencyGroup.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.google.cloud.compute.v1.DisksClient;
2121
import com.google.cloud.compute.v1.DisksRemoveResourcePoliciesRequest;
2222
import com.google.cloud.compute.v1.Operation;
23+
import com.google.cloud.compute.v1.Operation.Status;
2324
import com.google.cloud.compute.v1.RegionDisksClient;
2425
import com.google.cloud.compute.v1.RegionDisksRemoveResourcePoliciesRequest;
2526
import com.google.cloud.compute.v1.RemoveResourcePoliciesDiskRequest;
@@ -51,7 +52,7 @@ public static void main(String[] args)
5152
}
5253

5354
// Removes a disk from a consistency group.
54-
public static Operation.Status removeDiskFromConsistencyGroup(
55+
public static Status removeDiskFromConsistencyGroup(
5556
String project, String location, String diskName,
5657
String consistencyGroupName, String consistencyGroupLocation)
5758
throws IOException, ExecutionException, InterruptedException, TimeoutException {

0 commit comments

Comments
 (0)