Skip to content

Commit 0e52e74

Browse files
feat(compute): add compute disk regional replicated sample (#9697)
* Implemented compute_disk_regional_replicated sample, created test * Fixed zone * Fixed test * Fixed test * Fixed disk size * Fixed code as requested in the comment
1 parent 8d282b8 commit 0e52e74

File tree

3 files changed

+120
-7
lines changed

3 files changed

+120
-7
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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_regional_replicated]
20+
import com.google.cloud.compute.v1.Disk;
21+
import com.google.cloud.compute.v1.InsertRegionDiskRequest;
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.ArrayList;
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 CreateReplicatedDisk {
32+
33+
public static void main(String[] args)
34+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
35+
// TODO(developer): Replace these variables before running the sample.
36+
// Project ID or project number of the Cloud project you want to use.
37+
String projectId = "YOUR_PROJECT_ID";
38+
// The region for the replicated disk to reside in.
39+
// The disk must be in the same region as the VM that you plan to attach it to.
40+
String region = "us-central1";
41+
// The zones within the region where the two disk replicas are located
42+
List<String> replicaZones = new ArrayList<>();
43+
replicaZones.add(String.format("projects/%s/zones/%s", projectId, "us-central1-a"));
44+
replicaZones.add(String.format("projects/%s/zones/%s", projectId, "us-central1-b"));
45+
// Name of the disk you want to create.
46+
String diskName = "YOUR_DISK_NAME";
47+
// Size of the new disk in gigabytes.
48+
int diskSizeGb = 100;
49+
// The type of replicated disk. This value uses the following format:
50+
// "regions/{region}/diskTypes/(pd-standard|pd-ssd|pd-balanced|pd-extreme)".
51+
// For example: "regions/us-west3/diskTypes/pd-ssd"
52+
String diskType = String.format("regions/%s/diskTypes/%s", region, "pd-standard");
53+
54+
createReplicatedDisk(projectId, region, replicaZones, diskName, diskSizeGb, diskType);
55+
}
56+
57+
// 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+
List<String> replicaZones, String diskName, int diskSizeGb, String diskType)
60+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
61+
// Initialize client that will be used to send requests. This client only needs to be created
62+
// once, and can be reused for multiple requests.
63+
try (RegionDisksClient regionDisksClient = RegionDisksClient.create()) {
64+
Disk disk = Disk.newBuilder()
65+
.setSizeGb(diskSizeGb)
66+
.setName(diskName)
67+
.setType(diskType)
68+
.addAllReplicaZones(replicaZones)
69+
.build();
70+
71+
InsertRegionDiskRequest insertRegionDiskRequest = InsertRegionDiskRequest.newBuilder()
72+
.setProject(projectId)
73+
.setRegion(region)
74+
.setDiskResource(disk)
75+
.build();
76+
77+
Operation response = regionDisksClient.insertAsync(insertRegionDiskRequest)
78+
.get(3, TimeUnit.MINUTES);
79+
80+
if (response.hasError()) {
81+
throw new Error("Error creating disk! " + response.getError());
82+
}
83+
return response.getStatus();
84+
}
85+
}
86+
}
87+
// [END compute_disk_regional_replicated]

compute/cloud-client/src/test/java/compute/Util.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import compute.deleteprotection.SetDeleteProtection;
3838
import compute.disks.DeleteDisk;
3939
import compute.disks.DeleteSnapshot;
40+
import compute.disks.RegionalDelete;
4041
import compute.reservation.DeleteReservation;
4142
import java.io.IOException;
4243
import java.nio.charset.StandardCharsets;
@@ -217,6 +218,22 @@ && isCreatedBeforeThresholdTime(disk.getCreationTimestamp())) {
217218
}
218219
}
219220

221+
// Delete regional disks which starts with the given prefixToDelete and
222+
// has creation timestamp >24 hours.
223+
public static void cleanUpExistingRegionalDisks(
224+
String prefixToDelete, String projectId, String region)
225+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
226+
try (RegionDisksClient disksClient = RegionDisksClient.create()) {
227+
for (Disk disk : disksClient.list(projectId, region).iterateAll()) {
228+
if (disk.getName().contains(prefixToDelete)
229+
&& disk.getRegion().equals(region)
230+
&& isCreatedBeforeThresholdTime(disk.getCreationTimestamp())) {
231+
RegionalDelete.deleteRegionalDisk(projectId, region, disk.getName());
232+
}
233+
}
234+
}
235+
}
236+
220237
// Delete snapshots which starts with the given prefixToDelete and
221238
// has creation timestamp >24 hours.
222239
public static void cleanUpExistingSnapshots(String prefixToDelete, String projectId)

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,12 @@ public class DisksIT {
6969
private static String EMPTY_DISK_NAME;
7070
private static String SNAPSHOT_NAME;
7171
private static String DISK_TYPE;
72-
7372
private static String ZONAL_BLANK_DISK;
74-
7573
private static String REGIONAL_BLANK_DISK;
76-
74+
private static String REGIONAL_REPLICATED_DISK;
75+
private static final List<String> replicaZones = Arrays.asList(
76+
String.format("projects/%s/zones/%s-a", PROJECT_ID, REGION),
77+
String.format("projects/%s/zones/%s-b", PROJECT_ID, REGION));
7778
private ByteArrayOutputStream stdOut;
7879

7980
// Check if the required environment variables are set.
@@ -101,12 +102,13 @@ public static void setup()
101102
DISK_TYPE = String.format("zones/%s/diskTypes/pd-ssd", ZONE);
102103
ZONAL_BLANK_DISK = "gcloud-test-disk-zattach-" + uuid;
103104
REGIONAL_BLANK_DISK = "gcloud-test-disk-rattach-" + uuid;
105+
REGIONAL_REPLICATED_DISK = "gcloud-test-disk-replicated-" + uuid;
104106

105107
// Cleanup existing stale instances.
106108
Util.cleanUpExistingInstances("test-disks", PROJECT_ID, ZONE);
107109
Util.cleanUpExistingDisks("gcloud-test-", PROJECT_ID, ZONE);
108110
Util.cleanUpExistingSnapshots("gcloud-test-snapshot-", PROJECT_ID);
109-
111+
Util.cleanUpExistingRegionalDisks("gcloud-test-disk-", PROJECT_ID, REGION);
110112
// Create disk from image.
111113
Image debianImage = null;
112114
try (ImagesClient imagesClient = ImagesClient.create()) {
@@ -170,6 +172,7 @@ public static void cleanUp()
170172
DeleteDisk.deleteDisk(PROJECT_ID, ZONE, EMPTY_DISK_NAME);
171173
DeleteDisk.deleteDisk(PROJECT_ID, ZONE, ZONAL_BLANK_DISK);
172174
RegionalDelete.deleteRegionalDisk(PROJECT_ID, REGION, REGIONAL_BLANK_DISK);
175+
RegionalDelete.deleteRegionalDisk(PROJECT_ID, REGION, REGIONAL_REPLICATED_DISK);
173176

174177
stdOut.close();
175178
System.setOut(out);
@@ -245,9 +248,7 @@ public static void createZonalDisk()
245248
public static void createRegionalDisk()
246249
throws IOException, ExecutionException, InterruptedException, TimeoutException {
247250
String diskType = String.format("regions/%s/diskTypes/pd-balanced", REGION);
248-
List<String> replicaZones = Arrays.asList(
249-
String.format("projects/%s/zones/%s-a", PROJECT_ID, REGION),
250-
String.format("projects/%s/zones/%s-b", PROJECT_ID, REGION));
251+
251252
RegionalCreateFromSource.createRegionalDisk(PROJECT_ID, REGION, replicaZones,
252253
REGIONAL_BLANK_DISK, diskType, 11, Optional.empty(), Optional.empty());
253254
}
@@ -301,4 +302,12 @@ public void testDiskAttachResize()
301302
Util.getRegionalDisk(PROJECT_ID, REGION, REGIONAL_BLANK_DISK).getSizeGb());
302303
}
303304

305+
@Test
306+
public void testCreateReplicatedDisk()
307+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
308+
Operation.Status status = CreateReplicatedDisk.createReplicatedDisk(PROJECT_ID, REGION,
309+
replicaZones, REGIONAL_REPLICATED_DISK, 100, DISK_TYPE);
310+
311+
assertThat(status).isEqualTo(Operation.Status.DONE);
312+
}
304313
}

0 commit comments

Comments
 (0)