From 8e31c56ea1c319b14562ef82fe69e46e4fe9570b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A2=D0=B5=D1=82=D1=8F=D0=BD=D0=B0=20=D0=AF=D0=B3=D0=BE?= =?UTF-8?q?=D0=B4=D1=81=D1=8C=D0=BA=D0=B0?= <49729677+TetyanaYahodska@users.noreply.github.com> Date: Fri, 27 Dec 2024 18:53:31 +0100 Subject: [PATCH] feat(compute): add compute disk create secondary sample. (#9643) * Implemented compute_disk_create_secondary sample, created test * Fixed code * Fixed variable * Fixed code * Merged changes from main * Fixed lint issue --- .../disks/CreateDiskSecondaryZonal.java | 92 +++++++++++++++++++ .../src/test/java/compute/disks/DisksIT.java | 37 ++++++-- 2 files changed, 122 insertions(+), 7 deletions(-) create mode 100644 compute/cloud-client/src/main/java/compute/disks/CreateDiskSecondaryZonal.java diff --git a/compute/cloud-client/src/main/java/compute/disks/CreateDiskSecondaryZonal.java b/compute/cloud-client/src/main/java/compute/disks/CreateDiskSecondaryZonal.java new file mode 100644 index 00000000000..58135d4a3a3 --- /dev/null +++ b/compute/cloud-client/src/main/java/compute/disks/CreateDiskSecondaryZonal.java @@ -0,0 +1,92 @@ +/* + * Copyright 2024 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package compute.disks; + +// [START compute_disk_create_secondary] +import com.google.cloud.compute.v1.Disk; +import com.google.cloud.compute.v1.DiskAsyncReplication; +import com.google.cloud.compute.v1.DisksClient; +import com.google.cloud.compute.v1.Operation; +import java.io.IOException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +public class CreateDiskSecondaryZonal { + public static void main(String[] args) + throws IOException, ExecutionException, InterruptedException, TimeoutException { + // TODO(developer): Replace these variables before running the sample. + // The project that contains the primary disk. + String primaryProjectId = "PRIMARY_PROJECT_ID"; + // The project that contains the secondary disk. + String secondaryProjectId = "SECONDARY_PROJECT_ID"; + // Name of the primary disk you want to use. + String primaryDiskName = "PRIMARY_DISK_NAME"; + // Name of the zone in which your primary disk is located. + // Learn more about zones and regions: + // https://cloud.google.com/compute/docs/disks/async-pd/about#supported_region_pairs + String primaryDiskZone = "us-central1-a"; + // Name of the disk you want to create. + String secondaryDiskName = "SECONDARY_DISK_NAME"; + // Name of the zone in which you want to create the secondary disk. + String secondaryDiskZone = "us-east1-c"; + // Size of the new disk in gigabytes. + long diskSizeGb = 30L; + // The type of the disk you want to create. This value uses the following format: + // "projects/{projectId}/zones/{zone}/diskTypes/ + // (pd-standard|pd-ssd|pd-balanced|pd-extreme)". + String diskType = String.format( + "projects/%s/zones/%s/diskTypes/pd-balanced", secondaryProjectId, secondaryDiskZone); + + createDiskSecondaryZonal(primaryProjectId, secondaryProjectId, primaryDiskName, + secondaryDiskName, primaryDiskZone, secondaryDiskZone, diskSizeGb, diskType); + } + + // Creates a secondary disk in a specified zone. + public static Operation.Status createDiskSecondaryZonal(String primaryProjectId, + String secondaryProjectId, String primaryDiskName, String secondaryDiskName, + String primaryDiskZone, String secondaryDiskZone, long diskSizeGb, String diskType) + throws IOException, ExecutionException, InterruptedException, TimeoutException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. + try (DisksClient disksClient = DisksClient.create()) { + String primaryDiskSource = String.format("projects/%s/zones/%s/disks/%s", + primaryProjectId, primaryDiskZone, primaryDiskName); + + DiskAsyncReplication asyncReplication = DiskAsyncReplication.newBuilder() + .setDisk(primaryDiskSource) + .build(); + Disk disk = Disk.newBuilder() + .setName(secondaryDiskName) + .setZone(secondaryDiskZone) + .setSizeGb(diskSizeGb) + .setType(diskType) + .setAsyncPrimaryDisk(asyncReplication) + .build(); + + Operation response = disksClient.insertAsync(secondaryProjectId, secondaryDiskZone, disk) + .get(3, TimeUnit.MINUTES); + + if (response.hasError()) { + throw new Error("Error creating secondary disks! " + response.getError()); + } + return response.getStatus(); + } + } +} +// [END compute_disk_create_secondary] + diff --git a/compute/cloud-client/src/test/java/compute/disks/DisksIT.java b/compute/cloud-client/src/test/java/compute/disks/DisksIT.java index 20d888096cc..2e075d663e5 100644 --- a/compute/cloud-client/src/test/java/compute/disks/DisksIT.java +++ b/compute/cloud-client/src/test/java/compute/disks/DisksIT.java @@ -18,6 +18,7 @@ import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertWithMessage; +import static org.junit.Assert.assertEquals; import com.google.cloud.compute.v1.AttachedDisk; import com.google.cloud.compute.v1.AttachedDiskInitializeParams; @@ -46,7 +47,6 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; -import org.junit.Assert; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; @@ -77,6 +77,7 @@ public class DisksIT { String.format("projects/%s/zones/%s-a", PROJECT_ID, REGION), String.format("projects/%s/zones/%s-b", PROJECT_ID, REGION)); private static String SECONDARY_REGIONAL_DISK; + private static String SECONDARY_DISK; private static final long DISK_SIZE = 10L; private ByteArrayOutputStream stdOut; @@ -107,14 +108,17 @@ public static void setup() REGIONAL_BLANK_DISK = "gcloud-test-disk-rattach-" + uuid; REGIONAL_REPLICATED_DISK = "gcloud-test-disk-replicated-" + uuid; SECONDARY_REGIONAL_DISK = "gcloud-test-disk-secondary-regional-" + uuid; + SECONDARY_DISK = "gcloud-test-disk-secondary-" + uuid; // Cleanup existing stale resources. Util.cleanUpExistingInstances("test-disks", PROJECT_ID, ZONE); Util.cleanUpExistingDisks("gcloud-test-", PROJECT_ID, ZONE); - Util.cleanUpExistingRegionalDisks("gcloud-test-disk-secondary-regional-", PROJECT_ID, REGION); - Util.cleanUpExistingRegionalDisks("gcloud-test-disk-rattach-", PROJECT_ID, REGION); - Util.cleanUpExistingSnapshots("gcloud-test-snapshot-", PROJECT_ID); + Util.cleanUpExistingDisks("gcloud-test-", PROJECT_ID, "us-central1-c"); + Util.cleanUpExistingRegionalDisks( + "gcloud-test-disk-secondary-regional-", PROJECT_ID, "us-central1"); Util.cleanUpExistingRegionalDisks("gcloud-test-disk-", PROJECT_ID, REGION); + Util.cleanUpExistingSnapshots("gcloud-test-snapshot-", PROJECT_ID); + // Create disk from image. Image debianImage = null; try (ImagesClient imagesClient = ImagesClient.create()) { @@ -181,6 +185,7 @@ public static void cleanUp() RegionalDelete.deleteRegionalDisk(PROJECT_ID, REGION, REGIONAL_BLANK_DISK); RegionalDelete.deleteRegionalDisk(PROJECT_ID, REGION, REGIONAL_REPLICATED_DISK); RegionalDelete.deleteRegionalDisk(PROJECT_ID, "us-central1", SECONDARY_REGIONAL_DISK); + DeleteDisk.deleteDisk(PROJECT_ID, "us-central1-c", SECONDARY_DISK); stdOut.close(); System.setOut(out); @@ -285,7 +290,7 @@ public void testDiskAttachResize() throws IOException, ExecutionException, InterruptedException, TimeoutException { // Test disk attach. Instance instance = Util.getInstance(PROJECT_ID, ZONE, INSTANCE_NAME); - Assert.assertEquals(1, instance.getDisksCount()); + assertEquals(1, instance.getDisksCount()); Disk zonalDisk = Util.getDisk(PROJECT_ID, ZONE, ZONAL_BLANK_DISK); Disk regionalDisk = Util.getRegionalDisk(PROJECT_ID, REGION, REGIONAL_BLANK_DISK); @@ -305,8 +310,8 @@ public void testDiskAttachResize() ResizeRegionalDisk.resizeRegionalDisk(PROJECT_ID, regionalDisk.getRegion().split("regions/")[1], regionalDisk.getName(), 23); - Assert.assertEquals(22, Util.getDisk(PROJECT_ID, ZONE, ZONAL_BLANK_DISK).getSizeGb()); - Assert.assertEquals(23, + assertEquals(22, Util.getDisk(PROJECT_ID, ZONE, ZONAL_BLANK_DISK).getSizeGb()); + assertEquals(23, Util.getRegionalDisk(PROJECT_ID, REGION, REGIONAL_BLANK_DISK).getSizeGb()); } @@ -315,8 +320,10 @@ public void testCreateReplicatedDisk() throws IOException, ExecutionException, InterruptedException, TimeoutException { Status status = CreateReplicatedDisk.createReplicatedDisk(PROJECT_ID, REGION, replicaZones, REGIONAL_REPLICATED_DISK, 100, DISK_TYPE); + Disk disk = Util.getRegionalDisk(PROJECT_ID, REGION, REGIONAL_REPLICATED_DISK); assertThat(status).isEqualTo(Status.DONE); + assertEquals(REGIONAL_REPLICATED_DISK, disk.getName()); } @Test @@ -327,7 +334,23 @@ public void testCreateDiskSecondaryRegional() Status status = CreateDiskSecondaryRegional.createDiskSecondaryRegional( PROJECT_ID, PROJECT_ID, REGIONAL_BLANK_DISK, SECONDARY_REGIONAL_DISK, REGION, "us-central1", DISK_SIZE, diskType); + Disk disk = Util.getRegionalDisk(PROJECT_ID, "us-central1", SECONDARY_REGIONAL_DISK); + + assertThat(status).isEqualTo(Status.DONE); + assertEquals(SECONDARY_REGIONAL_DISK, disk.getName()); + } + + @Test + public void testCreateDiskSecondaryZonal() + throws IOException, ExecutionException, InterruptedException, TimeoutException { + String diskType = String.format( + "projects/%s/zones/%s/diskTypes/pd-ssd", PROJECT_ID, ZONE); + Status status = CreateDiskSecondaryZonal.createDiskSecondaryZonal( + PROJECT_ID, PROJECT_ID, EMPTY_DISK_NAME, SECONDARY_DISK, ZONE, + "us-central1-c", DISK_SIZE, diskType); + Disk disk = Util.getDisk(PROJECT_ID, "us-central1-c", SECONDARY_DISK); assertThat(status).isEqualTo(Status.DONE); + assertEquals(SECONDARY_DISK, disk.getName()); } }