Skip to content

Commit

Permalink
Fixed lint issue
Browse files Browse the repository at this point in the history
  • Loading branch information
TetyanaYahodska committed Dec 27, 2024
2 parents 7dca8a1 + 8e31c56 commit 92cd526
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 30 deletions.
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]

63 changes: 33 additions & 30 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 All @@ -32,7 +33,6 @@
import com.google.cloud.compute.v1.NetworkInterface;
import com.google.cloud.compute.v1.Operation;
import com.google.cloud.compute.v1.Operation.Status;
import com.google.cloud.compute.v1.RegionDisksClient;
import com.google.cloud.compute.v1.Snapshot;
import com.google.cloud.compute.v1.SnapshotsClient;
import compute.DeleteInstance;
Expand All @@ -48,7 +48,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 @@ -79,6 +78,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 static String SECONDARY_CUSTOM_DISK;

Expand Down Expand Up @@ -111,15 +111,18 @@ 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;
SECONDARY_CUSTOM_DISK = "gcloud-test-disk-custom-" + 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 @@ -186,6 +189,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);
DeleteDisk.deleteDisk(PROJECT_ID, "us-central1-c", SECONDARY_CUSTOM_DISK);

stdOut.close();
Expand Down Expand Up @@ -267,19 +271,6 @@ public static void createRegionalDisk()
REGIONAL_BLANK_DISK, diskType, 10, Optional.empty(), Optional.empty());
}

public static Disk getDisk(String projectId, String zone, String diskName) throws IOException {
try (DisksClient disksClient = DisksClient.create()) {
return disksClient.get(projectId, zone, diskName);
}
}

public static Disk getRegionalDisk(String projectId, String region, String diskName)
throws IOException {
try (RegionDisksClient disksClient = RegionDisksClient.create()) {
return disksClient.get(projectId, region, diskName);
}
}

@BeforeEach
public void beforeEach() {
stdOut = new ByteArrayOutputStream();
Expand All @@ -304,7 +295,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 @@ -324,8 +315,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 @@ -334,11 +325,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 = getRegionalDisk(PROJECT_ID, REGION, REGIONAL_REPLICATED_DISK);
Disk disk = Util.getRegionalDisk(PROJECT_ID, REGION, REGIONAL_REPLICATED_DISK);

assertThat(disk.getName()).isEqualTo(REGIONAL_REPLICATED_DISK);
assertThat(disk.getReplicaZonesCount()).isEqualTo(2);
assertThat(status).isEqualTo(Status.DONE);
assertEquals(REGIONAL_REPLICATED_DISK, disk.getName());
}

@Test
Expand All @@ -349,10 +339,24 @@ 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 = getRegionalDisk(PROJECT_ID, "us-central1", SECONDARY_REGIONAL_DISK);
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(disk.getName()).isEqualTo(SECONDARY_REGIONAL_DISK);
assertThat(status).isEqualTo(Status.DONE);
assertEquals(SECONDARY_DISK, disk.getName());
}

@Test
Expand All @@ -361,12 +365,11 @@ public void testCreateSecondaryCustomDisk()
String diskType = String.format(
"projects/%s/zones/%s/diskTypes/pd-ssd", PROJECT_ID, ZONE);
Status status = CreateSecondaryCustomDisk.createSecondaryCustomDisk(
PROJECT_ID, PROJECT_ID, EMPTY_DISK_NAME,
SECONDARY_CUSTOM_DISK, ZONE,
PROJECT_ID, PROJECT_ID, EMPTY_DISK_NAME, SECONDARY_CUSTOM_DISK, ZONE,
"us-central1-c", DISK_SIZE, diskType);
Disk disk = getDisk(PROJECT_ID, "us-central1-c", SECONDARY_CUSTOM_DISK);
Disk disk = Util.getDisk(PROJECT_ID, "us-central1-c", SECONDARY_CUSTOM_DISK);

assertThat(disk.getName()).isEqualTo(SECONDARY_CUSTOM_DISK);
assertThat(status).isEqualTo(Status.DONE);
assertEquals(SECONDARY_CUSTOM_DISK, disk.getName());
}
}

0 comments on commit 92cd526

Please sign in to comment.