Skip to content

Commit 1ce1ddf

Browse files
Implemented compute_disk_start_replication and compute_disk_stop_replication samples, created tests
1 parent 2df21d1 commit 1ce1ddf

File tree

5 files changed

+471
-0
lines changed

5 files changed

+471
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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_create_secondary]
20+
import com.google.cloud.compute.v1.Disk;
21+
import com.google.cloud.compute.v1.DiskAsyncReplication;
22+
import com.google.cloud.compute.v1.DisksClient;
23+
import com.google.cloud.compute.v1.Operation;
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 CreateDiskSecondary {
30+
public static void main(String[] args)
31+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
32+
// TODO(developer): Replace these variables before running the sample.
33+
// The project that contains the primary disk.
34+
String primaryProjectId = "YOUR_PROJECT_ID";
35+
// The project that contains the secondary disk.
36+
String secondaryProjectId = "YOUR_PROJECT_ID";
37+
// Name of the primary disk you want to use.
38+
String primaryDiskName = "PRIMARY_DISK_NAME";
39+
// Name of the zone in which your primary disk is located.
40+
// Learn more about zones and regions:
41+
// https://cloud.google.com/compute/docs/disks/async-pd/about#supported_region_pairs
42+
String primaryDiskZone = "us-central1-a";
43+
// Name of the disk you want to create.
44+
String secondaryDiskName = "SECONDARY_DISK_NAME";
45+
// Name of the zone in which you want to create the secondary disk.
46+
String secondaryDiskZone = "us-east1-c";
47+
// Size of the new disk in gigabytes.
48+
long diskSizeGb = 30L;
49+
// The type of the disk you want to create. This value uses the following format:
50+
// "projects/{projectId}/zones/{zone}/diskTypes/
51+
// (pd-standard|pd-ssd|pd-balanced|pd-extreme)".
52+
String diskType = String.format(
53+
"projects/%s/zones/%s/diskTypes/pd-balanced", secondaryProjectId, secondaryDiskZone);
54+
55+
createDiskSecondary(primaryProjectId, secondaryProjectId, primaryDiskName, secondaryDiskName,
56+
primaryDiskZone, secondaryDiskZone, diskSizeGb, diskType);
57+
}
58+
59+
// Creates a secondary disk in a specified zone.
60+
public static Disk createDiskSecondary(String primaryProjectId, String secondaryProjectId,
61+
String primaryDiskName, String secondaryDiskName, String primaryDiskZone,
62+
String secondaryDiskZone, long diskSizeGb, String diskType)
63+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
64+
String primaryDiskSource = String.format("projects/%s/zones/%s/disks/%s",
65+
primaryProjectId, primaryDiskZone, primaryDiskName);
66+
67+
DiskAsyncReplication asyncReplication = DiskAsyncReplication.newBuilder()
68+
.setDisk(primaryDiskSource)
69+
.build();
70+
71+
// Initialize client that will be used to send requests. This client only needs to be created
72+
// once, and can be reused for multiple requests.
73+
try (DisksClient disksClient = DisksClient.create()) {
74+
Disk disk = Disk.newBuilder()
75+
.setName(secondaryDiskName)
76+
.setSizeGb(diskSizeGb)
77+
.setType(diskType)
78+
.setZone(secondaryDiskZone)
79+
.setAsyncPrimaryDisk(asyncReplication)
80+
.build();
81+
82+
// Wait for the create disk operation to complete.
83+
Operation response = disksClient.insertAsync(secondaryProjectId, secondaryDiskZone, disk)
84+
.get(3, TimeUnit.MINUTES);
85+
86+
if (response.hasError()) {
87+
return null;
88+
}
89+
return disksClient.get(secondaryProjectId, secondaryDiskZone, secondaryDiskName);
90+
}
91+
}
92+
}
93+
// [END compute_disk_create_secondary]
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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_create_secondary_regional]
20+
import com.google.cloud.compute.v1.Disk;
21+
import com.google.cloud.compute.v1.DiskAsyncReplication;
22+
import com.google.cloud.compute.v1.Operation;
23+
import com.google.cloud.compute.v1.RegionDisksClient;
24+
import java.io.IOException;
25+
import java.util.Arrays;
26+
import java.util.List;
27+
import java.util.concurrent.ExecutionException;
28+
import java.util.concurrent.TimeUnit;
29+
import java.util.concurrent.TimeoutException;
30+
31+
public class CreateDiskSecondaryRegional {
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 primaryProjectId = "YOUR_PROJECT_ID";
37+
// The project that contains the secondary disk.
38+
String secondaryProjectId = "YOUR_PROJECT_ID";
39+
// Name of the primary disk you want to use.
40+
String primaryDiskName = "PRIMARY_DISK_NAME";
41+
// Name of the disk you want to create.
42+
String secondaryDiskName = "SECONDARY_DISK_NAME";
43+
// Name of the region in which your primary disk is located.
44+
// Learn more about zones and regions:
45+
// https://cloud.google.com/compute/docs/disks/async-pd/about#supported_region_pairs
46+
String primaryDiskRegion = "us-central1";
47+
// Name of the region in which you want to create the secondary disk.
48+
String secondaryDiskRegion = "us-east1";
49+
// Size of the new disk in gigabytes.
50+
// Learn more about disk requirements:
51+
// https://cloud.google.com/compute/docs/disks/async-pd/configure?authuser=0#disk_requirements
52+
long diskSizeGb = 30L;
53+
// The type of the disk you want to create. This value uses the following format:
54+
// "projects/{projectId}/zones/{zone}/diskTypes/
55+
// (pd-standard|pd-ssd|pd-balanced|pd-extreme)".
56+
String diskType = String.format(
57+
"projects/%s/regions/%s/diskTypes/pd-balanced", secondaryProjectId, secondaryDiskRegion);
58+
59+
createDiskSecondaryRegional(primaryProjectId, secondaryProjectId, primaryDiskName,
60+
secondaryDiskName, primaryDiskRegion, secondaryDiskRegion, diskSizeGb, diskType);
61+
}
62+
63+
// Creates a secondary disk in a specified region.
64+
public static Disk createDiskSecondaryRegional(String projectId, String secondaryProjectId,
65+
String primaryDiskName, String secondaryDiskName, String primaryDiskRegion,
66+
String secondaryDiskRegion, long diskSizeGb, String diskType)
67+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
68+
// An iterable collection of zone names in which you want to keep
69+
// the new disks' replicas. One of the replica zones of the clone must match
70+
// the zone of the source disk.
71+
List<String> replicaZones = Arrays.asList(
72+
String.format("projects/%s/zones/%s-c", secondaryProjectId, secondaryDiskRegion),
73+
String.format("projects/%s/zones/%s-b", secondaryProjectId, secondaryDiskRegion));
74+
75+
String primaryDiskSource = String.format("projects/%s/regions/%s/disks/%s",
76+
projectId, primaryDiskRegion, primaryDiskName);
77+
78+
DiskAsyncReplication asyncReplication = DiskAsyncReplication.newBuilder()
79+
.setDisk(primaryDiskSource)
80+
.build();
81+
82+
// Initialize client that will be used to send requests. This client only needs to be created
83+
// once, and can be reused for multiple requests.
84+
try (RegionDisksClient disksClient = RegionDisksClient.create()) {
85+
86+
Disk disk = Disk.newBuilder()
87+
.addAllReplicaZones(replicaZones)
88+
.setName(secondaryDiskName)
89+
.setSizeGb(diskSizeGb)
90+
.setType(diskType)
91+
.setRegion(secondaryDiskRegion)
92+
.setAsyncPrimaryDisk(asyncReplication)
93+
.build();
94+
95+
// Wait for the create disk operation to complete.
96+
Operation response = disksClient.insertAsync(secondaryProjectId, secondaryDiskRegion, disk)
97+
.get(3, TimeUnit.MINUTES);
98+
99+
if (response.hasError()) {
100+
return null;
101+
}
102+
return disksClient.get(secondaryProjectId, secondaryDiskRegion, secondaryDiskName);
103+
}
104+
}
105+
}
106+
// [END compute_disk_create_secondary_regional]
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
// Uncomment this line if your disk has zonal location.
21+
// import com.google.cloud.compute.v1.DisksClient;
22+
// import com.google.cloud.compute.v1.DisksStartAsyncReplicationRequest;
23+
import com.google.cloud.compute.v1.Operation;
24+
import com.google.cloud.compute.v1.RegionDisksClient;
25+
import com.google.cloud.compute.v1.RegionDisksStartAsyncReplicationRequest;
26+
import java.io.IOException;
27+
import java.util.concurrent.ExecutionException;
28+
29+
public class StartDiskReplication {
30+
31+
public static void main(String[] args)
32+
throws IOException, ExecutionException, InterruptedException {
33+
// TODO(developer): Replace these variables before running the sample.
34+
// The project that contains the primary disk.
35+
String primaryProjectId = "PRIMARY_PROJECT_ID";
36+
// Name of the primary disk.
37+
String primaryDiskName = "PRIMARY_DISK_NAME";
38+
// Name of the region in which your primary disk is located.
39+
// Learn more about zones and regions:
40+
// https://cloud.google.com/compute/docs/disks/async-pd/about#supported_region_pairs
41+
String primaryDiskLocation = "YOUR_PRIMARY_DISK_LOCATION";
42+
// Path to your secondary disk.
43+
// This value uses the following format for zonal location:
44+
// "projects/%sYOUR_PROJECT_ID/zones/YOUR_PRIMARY_DISK_LOCATION/disks/PRIMARY_DISK_NAME"
45+
String secondaryDiskPath =
46+
"projects/%s/regions/YOUR_SECONDARY_DISK_LOCATION/disks/SECONDARY_DISK_NAME";
47+
48+
startDiskAsyncReplication(
49+
primaryProjectId, primaryDiskName, primaryDiskLocation, secondaryDiskPath);
50+
}
51+
52+
// Starts asynchronous replication for the specified disk.
53+
public static void startDiskAsyncReplication(String primaryProjectId, String primaryDiskName,
54+
String primaryDiskLocation, String secondaryDiskPath)
55+
throws IOException, ExecutionException, InterruptedException {
56+
// Initialize client that will be used to send requests. This client only needs to be created
57+
// once, and can be reused for multiple requests.
58+
59+
// Uncomment these lines if your disk has zonal location.
60+
// try (DisksClient disksClient = DisksClient.create()) {
61+
// DisksStartAsyncReplicationRequest startAsyncReplicationRequest =
62+
// DisksStartAsyncReplicationRequest.newBuilder()
63+
// .setAsyncSecondaryDisk(secondaryDiskPath)
64+
// .build();
65+
66+
try (RegionDisksClient disksClient = RegionDisksClient.create()) {
67+
68+
RegionDisksStartAsyncReplicationRequest replicationRequest =
69+
RegionDisksStartAsyncReplicationRequest.newBuilder()
70+
.setAsyncSecondaryDisk(secondaryDiskPath)
71+
.build();
72+
73+
Operation response = disksClient.startAsyncReplicationAsync(
74+
primaryProjectId, primaryDiskLocation, primaryDiskName, replicationRequest).get();
75+
76+
if (response.hasError()) {
77+
return;
78+
}
79+
System.out.println("Async replication started successfully.");
80+
}
81+
}
82+
}
83+
// [END compute_disk_start_replication]
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
// Uncomment this line if your disk has zonal location.
21+
// import com.google.cloud.compute.v1.DisksClient;
22+
import com.google.cloud.compute.v1.Operation;
23+
import com.google.cloud.compute.v1.RegionDisksClient;
24+
import java.io.IOException;
25+
import java.util.concurrent.ExecutionException;
26+
27+
public class StopDiskReplication {
28+
29+
public static void main(String[] args)
30+
throws IOException, ExecutionException, InterruptedException {
31+
// TODO(developer): Replace these variables before running the sample.
32+
// Project ID or project number of the Cloud project you want to use.
33+
String projectId = "YOUR_PROJECT_ID";
34+
// Location of your disk.
35+
String diskLocation = "us-central1-a";
36+
// Name of your disk.
37+
String diskName = "YOUR_DISK_NAME";
38+
39+
stopDiskAsyncReplication(projectId, diskLocation, diskName);
40+
}
41+
42+
// Stops asynchronous replication for the specified disk.
43+
public static void stopDiskAsyncReplication(
44+
String project, String diskLocation, String diskName)
45+
throws IOException, ExecutionException, InterruptedException {
46+
// Initialize client that will be used to send requests. This client only needs to be created
47+
// once, and can be reused for multiple requests.
48+
49+
// Uncomment this line if your disk has zonal location.
50+
//try (DisksClient disksClient = DisksClient.create()) {
51+
52+
try (RegionDisksClient disksClient = RegionDisksClient.create()) {
53+
Operation response = disksClient.stopAsyncReplicationAsync(
54+
project, diskLocation, diskName).get();
55+
56+
if (response.hasError()) {
57+
return;
58+
}
59+
System.out.println("Async replication stopped successfully.");
60+
}
61+
}
62+
}
63+
// [END compute_disk_stop_replication]

0 commit comments

Comments
 (0)