|
| 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 = 200; |
| 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 Disk 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 | + System.out.println("Failed to create regional replicated disk."); |
| 82 | + return null; |
| 83 | + } |
| 84 | + return regionDisksClient.get(projectId, region, diskName); |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | +// [END compute_disk_regional_replicated] |
0 commit comments