|
| 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_custom] |
| 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.GuestOsFeature; |
| 24 | +import com.google.cloud.compute.v1.Operation; |
| 25 | +import com.google.cloud.compute.v1.Operation.Status; |
| 26 | +import java.io.IOException; |
| 27 | +import java.util.Arrays; |
| 28 | +import java.util.HashMap; |
| 29 | +import java.util.List; |
| 30 | +import java.util.Map; |
| 31 | +import java.util.concurrent.ExecutionException; |
| 32 | +import java.util.concurrent.TimeUnit; |
| 33 | +import java.util.concurrent.TimeoutException; |
| 34 | + |
| 35 | +public class CreateSecondaryCustomDisk { |
| 36 | + public static void main(String[] args) |
| 37 | + throws IOException, ExecutionException, InterruptedException, TimeoutException { |
| 38 | + // TODO(developer): Replace these variables before running the sample. |
| 39 | + // The project that contains the primary disk. |
| 40 | + String primaryProjectId = "PRIMARY_PROJECT_ID"; |
| 41 | + // The project that contains the secondary disk. |
| 42 | + String secondaryProjectId = "SECONDARY_PROJECT_ID"; |
| 43 | + // Name of the primary disk you want to use. |
| 44 | + String primaryDiskName = "PRIMARY_DISK_NAME"; |
| 45 | + // Name of the zone in which your primary disk is located. |
| 46 | + // Learn more about zones and regions: |
| 47 | + // https://cloud.google.com/compute/docs/disks/async-pd/about#supported_region_pairs |
| 48 | + String primaryDiskZone = "us-central1-a"; |
| 49 | + // Name of the disk you want to create. |
| 50 | + String secondaryDiskName = "SECONDARY_DISK_NAME"; |
| 51 | + // Name of the zone in which you want to create the secondary disk. |
| 52 | + String secondaryDiskZone = "us-east1-c"; |
| 53 | + // Size of the new disk in gigabytes. |
| 54 | + long diskSizeGb = 30L; |
| 55 | + // The type of the disk you want to create. This value uses the following format: |
| 56 | + // "projects/{projectId}/zones/{zone}/diskTypes/ |
| 57 | + // (pd-standard|pd-ssd|pd-balanced|pd-extreme)". |
| 58 | + String diskType = String.format( |
| 59 | + "projects/%s/zones/%s/diskTypes/pd-balanced", secondaryProjectId, secondaryDiskZone); |
| 60 | + |
| 61 | + createSecondaryCustomDisk(primaryProjectId, secondaryProjectId, primaryDiskName, |
| 62 | + secondaryDiskName, primaryDiskZone, secondaryDiskZone, diskSizeGb, diskType); |
| 63 | + } |
| 64 | + |
| 65 | + // Creates a secondary disk with specified custom parameters. |
| 66 | + public static Status createSecondaryCustomDisk(String primaryProjectId, String secondaryProjectId, |
| 67 | + String primaryDiskName, String secondaryDiskName, String primaryDiskZone, |
| 68 | + String secondaryDiskZone, long diskSizeGb
8000
span>, String diskType) |
| 69 | + throws IOException, ExecutionException, InterruptedException, TimeoutException { |
| 70 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 71 | + // once, and can be reused for multiple requests. |
| 72 | + try (DisksClient disksClient = DisksClient.create()) { |
| 73 | + String primaryDiskSource = String.format("projects/%s/zones/%s/disks/%s", |
| 74 | + primaryProjectId, primaryDiskZone, primaryDiskName); |
| 75 | + |
| 76 | + DiskAsyncReplication asyncReplication = DiskAsyncReplication.newBuilder() |
| 77 | + .setDisk(primaryDiskSource) |
| 78 | + .build(); |
| 79 | + |
| 80 | + // Define the guest OS features. |
| 81 | + List<GuestOsFeature> guestOsFeatures = Arrays.asList( |
| 82 | + GuestOsFeature.newBuilder().setType("UEFI_COMPATIBLE").build(), |
| 83 | + GuestOsFeature.newBuilder().setType("GVNIC").build(), |
| 84 | + GuestOsFeature.newBuilder().setType("MULTI_IP_SUBNET").build()); |
| 85 | + |
| 86 | + // Define the labels. |
| 87 | + Map<String, String> labels = new HashMap<>(); |
| 88 | + labels.put("secondary-disk-for-replication", "yes"); |
| 89 | + |
| 90 | + Disk disk = Disk.newBuilder() |
| 91 | + .setName(secondaryDiskName) |
| 92 | + .setSizeGb(diskSizeGb) |
| 93 | + .setType(diskType) |
| 94 | + .setZone(secondaryDiskZone) |
| 95 | + .addAllGuestOsFeatures(guestOsFeatures) |
| 96 | + .putAllLabels(labels) |
| 97 | + .setAsyncPrimaryDisk(asyncReplication) |
| 98 | + .build(); |
| 99 | + |
| 100 | + // Wait for the create disk operation to complete. |
| 101 | + Operation response = disksClient.insertAsync(secondaryProjectId, secondaryDiskZone, disk) |
| 102 | + .get(3, TimeUnit.MINUTES); |
| 103 | + |
| 104 | + if (response.hasError()) { |
| 105 | + throw new Error("Error creating secondary custom disks! " + response.getError()); |
| 106 | + } |
| 107 | + return response.getStatus(); |
| 108 | + } |
| 109 | + } |
| 110 | +} |
| 111 | +// [END compute_disk_create_secondary_custom] |
0 commit comments