Skip to content

feat(compute): add compute disk create secondary sample. #9643

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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]

37 changes: 30 additions & 7 deletions compute/cloud-client/src/test/java/compute/disks/DisksIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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());
}

Expand All @@ -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
Expand All @@ -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());
}
}
Loading