Skip to content

feat(compute): add compute disk create secondary regional sample #9641

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 27 commits into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
cfd7a6c
Implemented compute_disk_create_secondary_regional. created test
TetyanaYahodska Nov 2, 2024
c6a4006
Fixed test
TetyanaYahodska Nov 3, 2024
316add2
Fixed test
TetyanaYahodska Nov 3, 2024
3f7445c
Fixed test
TetyanaYahodska Nov 3, 2024
668752f
Fixed zone
TetyanaYahodska Nov 4, 2024
43ef8a1
Fixed naming
TetyanaYahodska Nov 4, 2024
b258e92
Fixed spaces
TetyanaYahodska Nov 4, 2024
87072f9
Fixed code
TetyanaYahodska Nov 5, 2024
983c32b
Fixed indentations
TetyanaYahodska Nov 5, 2024
91d0bbf
Fixed variable
TetyanaYahodska Nov 6, 2024
61ffe38
Merge branch 'main' into compute_create_secondary_disk
TetyanaYahodska Nov 8, 2024
ba184d0
Fixed code
TetyanaYahodska Nov 8, 2024
03c9dcb
Merge branch 'main' into compute_create_secondary_disk
TetyanaYahodska Nov 8, 2024
6b6f8ba
Merge branch 'main' into compute_create_secondary_disk
TetyanaYahodska Nov 19, 2024
bf851d2
Added cleanup methods
TetyanaYahodska Nov 19, 2024
d6cd5af
Resolved merge conflict
TetyanaYahodska Nov 30, 2024
0da2d72
Fixed lint issue
TetyanaYahodska Nov 30, 2024
52cb9b9
Fixed lint issue
TetyanaYahodska Nov 30, 2024
f5108c2
Fixed test
TetyanaYahodska Dec 1, 2024
64c15f3
Resolved merge conflict
TetyanaYahodska Dec 18, 2024
545b7ef
Fixed code
TetyanaYahodska Dec 18, 2024
d863a9a
Fixed code
TetyanaYahodska Dec 18, 2024
9262c78
Fixed code
TetyanaYahodska Dec 18, 2024
025ab5a
Resolved merge conflict
TetyanaYahodska Dec 19, 2024
1b75b5e
Deleted duplicated assertion
TetyanaYahodska Dec 23, 2024
64c2cc6
Merge branch 'main' into compute_create_secondary_disk
TetyanaYahodska Dec 23, 2024
cac1ffd
Merge branch 'main' into compute_create_secondary_disk
TetyanaYahodska Dec 23, 2024
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,103 @@
/*
* 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_regional]
import com.google.cloud.compute.v1.Disk;
import com.google.cloud.compute.v1.DiskAsyncReplication;
import com.google.cloud.compute.v1.Operation;
import com.google.cloud.compute.v1.Operation.Status;
import com.google.cloud.compute.v1.RegionDisksClient;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class CreateDiskSecondaryRegional {
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 disk you want to create.
String secondaryDiskName = "SECONDARY_DISK_NAME";
// Name of the region 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 primaryDiskRegion = "us-central1";
// Name of the region in which you want to create the secondary disk.
String secondaryDiskRegion = "us-east1";
// Size of the new disk in gigabytes.
// Learn more about disk requirements:
// https://cloud.google.com/compute/docs/disks/async-pd/configure?authuser=0#disk_requirements
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/regions/%s/diskTypes/pd-balanced", secondaryProjectId, secondaryDiskRegion);

createDiskSecondaryRegional(primaryProjectId, secondaryProjectId, primaryDiskName,
secondaryDiskName, primaryDiskRegion, secondaryDiskRegion, diskSizeGb, diskType);
}

// Creates a secondary disk in a specified region.
public static Status createDiskSecondaryRegional(String projectId,
String secondaryProjectId, String primaryDiskName, String secondaryDiskName,
String primaryDiskRegion, String secondaryDiskRegion, long diskSizeGb, String diskType)
throws IOException, ExecutionException, InterruptedException, TimeoutException {
List<String> replicaZones = Arrays.asList(
String.format("projects/%s/zones/%s-c", secondaryProjectId, secondaryDiskRegion),
String.format("projects/%s/zones/%s-b", secondaryProjectId, secondaryDiskRegion));

String primaryDiskSource = String.format("projects/%s/regions/%s/disks/%s",
projectId, primaryDiskRegion, primaryDiskName);

// 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 (RegionDisksClient disksClient = RegionDisksClient.create()) {
DiskAsyncReplication asyncReplication = DiskAsyncReplication.newBuilder()
.setDisk(primaryDiskSource)
.build();

Disk disk = Disk.newBuilder()
.addAllReplicaZones(replicaZones)
.setName(secondaryDiskName)
.setSizeGb(diskSizeGb)
.setType(diskType)
.setRegion(secondaryDiskRegion)
.setAsyncPrimaryDisk(asyncReplication)
.build();

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure this Operation only takes 3 minutes to run?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As for other operations, 3 minutes was enough.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it the same case with this operation as well? We don't want flaky tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also set it to 3 minutes for other disk creation operations. Why should it be different in this case?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just confirming if this is enough time. Not opposing your claim.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is enough time. The test passed without errors.


if (response.hasError()) {
throw new Error("Error creating secondary disks! " + response.getError());
}
return response.getStatus();
}
}
}
// [END compute_disk_create_secondary_regional]
35 changes: 27 additions & 8 deletions compute/cloud-client/src/test/java/compute/disks/DisksIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public class DisksIT {
private static final List<String> replicaZones = Arrays.asList(
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 final long DISK_SIZE = 10L;
private ByteArrayOutputStream stdOut;

// Check if the required environment variables are set.
Expand Down Expand Up @@ -104,35 +106,39 @@ public static void setup()
ZONAL_BLANK_DISK = "gcloud-test-disk-zattach-" + uuid;
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;

// Cleanup existing stale instances.
// 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.cleanUpExistingRegionalDisks("gcloud-test-disk-", PROJECT_ID, REGION);
// Create disk from image.
Image debianImage = null;
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,
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 @@ -174,6 +180,7 @@ public static void cleanUp()
DeleteDisk.deleteDisk(PROJECT_ID, ZONE, ZONAL_BLANK_DISK);
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);

stdOut.close();
System.setOut(out);
Expand Down Expand Up @@ -214,7 +221,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 @@ -243,15 +250,15 @@ 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()
throws IOException, ExecutionException, InterruptedException, TimeoutException {
String diskType = String.format("regions/%s/diskTypes/pd-balanced", REGION);

RegionalCreateFromSource.createRegionalDisk(PROJECT_ID, REGION, replicaZones,
REGIONAL_BLANK_DISK, diskType, 11, Optional.empty(), Optional.empty());
REGIONAL_BLANK_DISK, diskType, 10, Optional.empty(), Optional.empty());
}

@BeforeEach
Expand Down Expand Up @@ -311,4 +318,16 @@ public void testCreateReplicatedDisk()

assertThat(status).isEqualTo(Status.DONE);
}

@Test
public void testCreateDiskSecondaryRegional()
throws IOException, ExecutionException, InterruptedException, TimeoutException {
String diskType = String.format(
"projects/%s/regions/%s/diskTypes/pd-balanced", PROJECT_ID, REGION);
Status status = CreateDiskSecondaryRegional.createDiskSecondaryRegional(
PROJECT_ID, PROJECT_ID, REGIONAL_BLANK_DISK, SECONDARY_REGIONAL_DISK,
REGION, "us-central1", DISK_SIZE, diskType);

assertThat(status).isEqualTo(Status.DONE);
}
}
Loading