Skip to content
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

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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,94 @@
/*
* 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 CreateDiskSecondary {
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);

createDiskSecondary(primaryProjectId, secondaryProjectId, primaryDiskName, secondaryDiskName,
primaryDiskZone, secondaryDiskZone, diskSizeGb, diskType);
}

// Creates a secondary disk in a specified zone.
public static Disk createDiskSecondary(String primaryProjectId, String secondaryProjectId,
String primaryDiskName, String secondaryDiskName, String primaryDiskZone,
String secondaryDiskZone, long diskSizeGb, String diskType)
throws IOException, ExecutionException, InterruptedException, TimeoutException {
String primaryDiskSource = String.format("projects/%s/zones/%s/disks/%s",
primaryProjectId, primaryDiskZone, primaryDiskName);

DiskAsyncReplication asyncReplication = DiskAsyncReplication.newBuilder()
.setDisk(primaryDiskSource)
.build();

// 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()) {
Disk disk = Disk.newBuilder()
.setName(secondaryDiskName)
.setSizeGb(diskSizeGb)
.setType(diskType)
.setZone(secondaryDiskZone)
.setAsyncPrimaryDisk(asyncReplication)
.build();

// Wait for the create disk operation to complete.
Operation response = disksClient.insertAsync(secondaryProjectId, secondaryDiskZone, disk)
.get(3, TimeUnit.MINUTES);

if (response.hasError()) {
return null;
}
return disksClient.get(secondaryProjectId, secondaryDiskZone, secondaryDiskName);
}
}
}
// [END compute_disk_create_secondary]

36 changes: 26 additions & 10 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.assertNotNull;

import com.google.cloud.compute.v1.AttachedDisk;
import com.google.cloud.compute.v1.AttachedDiskInitializeParams;
Expand Down Expand Up @@ -69,11 +70,10 @@ public class DisksIT {
private static String EMPTY_DISK_NAME;
private static String SNAPSHOT_NAME;
private static String DISK_TYPE;

private static String ZONAL_BLANK_DISK;

private static String REGIONAL_BLANK_DISK;

private static String SECONDARY_DISK;
private static final long DISK_SIZE = 10L;
private ByteArrayOutputStream stdOut;

// Check if the required environment variables are set.
Expand Down Expand Up @@ -101,6 +101,7 @@ public static void setup()
DISK_TYPE = String.format("zones/%s/diskTypes/pd-ssd", ZONE);
ZONAL_BLANK_DISK = "gcloud-test-disk-zattach-" + uuid;
REGIONAL_BLANK_DISK = "gcloud-test-disk-rattach-" + uuid;
SECONDARY_DISK = "gcloud-test-disk-secondary-" + uuid;

// Cleanup existing stale instances.
Util.cleanUpExistingInstances("test-disks", PROJECT_ID, ZONE);
Expand All @@ -112,24 +113,24 @@ public static void setup()
try (ImagesClient imagesClient = ImagesClient.create()) {
debianImage = imagesClient.getFromFamily("debian-cloud", "debian-11");
}
CreateDiskFromImage.createDiskFromImage(PROJECT_ID, ZONE, DISK_NAME, DISK_TYPE, 10,
CreateDiskFromImage.createDiskFromImage(PROJECT_ID, ZONE, DISK_NAME, DISK_TYPE, DISK_SIZE,
debianImage.getSelfLink());
assertThat(stdOut.toString()).contains("Disk created from image.");

// Create disk from snapshot.
CreateDiskFromImage.createDiskFromImage(PROJECT_ID, ZONE, DISK_NAME_DUMMY, DISK_TYPE, 10,
CreateDiskFromImage.createDiskFromImage(PROJECT_ID, ZONE, DISK_NAME_DUMMY, DISK_TYPE, DISK_SIZE,
debianImage.getSelfLink());
TimeUnit.SECONDS.sleep(10);
createDiskSnapshot(PROJECT_ID, ZONE, DISK_NAME_DUMMY, SNAPSHOT_NAME);
String diskSnapshotLink = String.format("projects/%s/global/snapshots/%s", PROJECT_ID,
SNAPSHOT_NAME);
TimeUnit.SECONDS.sleep(5);
CreateDiskFromSnapshot.createDiskFromSnapshot(PROJECT_ID, ZONE, DISK_NAME_2, DISK_TYPE, 10,
diskSnapshotLink);
CreateDiskFromSnapshot.createDiskFromSnapshot(PROJECT_ID, ZONE, DISK_NAME_2, DISK_TYPE,
DISK_SIZE, diskSnapshotLink);
assertThat(stdOut.toString()).contains("Disk created.");

// Create empty disk.
CreateEmptyDisk.createEmptyDisk(PROJECT_ID, ZONE, EMPTY_DISK_NAME, DISK_TYPE, 10);
CreateEmptyDisk.createEmptyDisk(PROJECT_ID, ZONE, EMPTY_DISK_NAME, DISK_TYPE, DISK_SIZE);
assertThat(stdOut.toString()).contains("Empty disk created.");

// Set Disk autodelete.
Expand Down Expand Up @@ -210,7 +211,7 @@ public static void createInstance(String projectId, String zone, String instance
.setAutoDelete(false)
.setBoot(true)
.setInitializeParams(AttachedDiskInitializeParams.newBuilder()
.setDiskSizeGb(10)
.setDiskSizeGb(DISK_SIZE)
.setSourceImage(sourceImage)
.setDiskName(diskName)
.build())
Expand Down Expand Up @@ -239,7 +240,7 @@ public static void createInstance(String projectId, String zone, String instance
public static void createZonalDisk()
throws IOException, ExecutionException, InterruptedException, TimeoutException {
String diskType = String.format("zones/%s/diskTypes/pd-standard", ZONE);
CreateEmptyDisk.createEmptyDisk(PROJECT_ID, ZONE, ZONAL_BLANK_DISK, diskType, 12);
CreateEmptyDisk.createEmptyDisk(PROJECT_ID, ZONE, ZONAL_BLANK_DISK, diskType, DISK_SIZE);
}

public static void createRegionalDisk()
Expand Down Expand Up @@ -301,4 +302,19 @@ public void testDiskAttachResize()
Util.getRegionalDisk(PROJECT_ID, REGION, REGIONAL_BLANK_DISK).getSizeGb());
}

@Test
public void testCreateDiskSecondary()
throws IOException, ExecutionException, InterruptedException, TimeoutException {
String diskType = String.format(
"projects/%s/zones/%s/diskTypes/pd-ssd", PROJECT_ID, ZONE);
Disk disk = CreateDiskSecondary.createDiskSecondary(
PROJECT_ID, PROJECT_ID, EMPTY_DISK_NAME, SECONDARY_DISK, ZONE,
"us-central1-c", DISK_SIZE, diskType);

// Verify that the secondary disk was created.
assertNotNull(disk);
assertThat(disk.getAsyncPrimaryDisk().getDisk().contains(EMPTY_DISK_NAME));

DeleteDisk.deleteDisk(PROJECT_ID, "us-central1-c", SECONDARY_DISK);
}
}