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 start/stop replication samples #9648

Draft
wants to merge 22 commits into
base: main
Choose a base branch
from
Draft
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,104 @@
/*
* 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.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 projectId = "YOUR_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", projectId, secondaryDiskRegion);

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

// Creates a secondary disk in a specified region.
public static Disk createDiskSecondaryRegional(String projectId, String primaryDiskName,
String primaryDiskRegion, String secondaryDiskName, String secondaryDiskRegion,
long diskSizeGb, String diskType)
throws IOException, ExecutionException, InterruptedException, TimeoutException {
// An iterable collection of zone names in which you want to keep
// the new disks' replicas. One of the replica zones of the clone must match
// the zone of the source disk.
List<String> replicaZones = Arrays.asList(
String.format("projects/%s/zones/%s-c", projectId, secondaryDiskRegion),
String.format("projects/%s/zones/%s-b", projectId, secondaryDiskRegion));

String primaryDiskSource = String.format("projects/%s/regions/%s/disks/%s",
projectId, primaryDiskRegion, 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 (RegionDisksClient disksClient = RegionDisksClient.create()) {

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(projectId, secondaryDiskRegion, disk)
.get(3, TimeUnit.MINUTES);

if (response.hasError()) {
return null;
}
return disksClient.get(projectId, secondaryDiskRegion, secondaryDiskName);
}
}
}
// [END compute_disk_create_secondary_regional]
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* 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_start_replication]
import com.google.cloud.compute.v1.Operation;
import com.google.cloud.compute.v1.RegionDisksClient;
import com.google.cloud.compute.v1.RegionDisksStartAsyncReplicationRequest;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

public class StartDiskReplication {

public static void main(String[] args)
throws IOException, ExecutionException, InterruptedException {
// TODO(developer): Replace these variables before running the sample.
// The project that contains the primary disk.
String projectId = "YOUR_PROJECT_ID";
// Name of the primary disk.
String primaryDiskName = "PRIMARY_DISK_NAME";
// Name of the secondary disk.
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 your secondary disk is located.
String secondaryDiskRegion = "us-east1";

startDiskAsyncReplication(projectId, primaryDiskName, primaryDiskRegion,
secondaryDiskName, secondaryDiskRegion);
}

// Starts asynchronous replication for the specified disk.
public static void startDiskAsyncReplication(String projectId, String primaryDiskName,
String primaryDiskRegion, String secondaryDiskName, String secondaryDiskRegion)
throws IOException, ExecutionException, InterruptedException {
// 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()) {

String asyncSecondaryDiskPath = String.format("projects/%s/regions/%s/disks/%s",
projectId, secondaryDiskRegion, secondaryDiskName);

RegionDisksStartAsyncReplicationRequest startAsyncReplicationRequest =
RegionDisksStartAsyncReplicationRequest.newBuilder()
.setAsyncSecondaryDisk(asyncSecondaryDiskPath)
.build();

Operation response = disksClient.startAsyncReplicationAsync(
projectId, primaryDiskRegion, primaryDiskName, startAsyncReplicationRequest).get();

if (response.hasError()) {
return;
}
System.out.println("Async replication started successfully.");
}
}
}
// [END compute_disk_start_replication]
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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_stop_replication]
import com.google.cloud.compute.v1.Operation;
import com.google.cloud.compute.v1.RegionDisksClient;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

public class StopDiskReplication {

public static void main(String[] args)
throws IOException, ExecutionException, InterruptedException {
// TODO(developer): Replace these variables before running the sample.
// The project that contains the primary disk.
String projectId = "YOUR_PROJECT_ID";
// Name of the region in which your secondary disk is located.
String secondaryDiskRegion = "us-east1";
// Name of the secondary disk.
String secondaryDiskName = "SECONDARY_DISK_NAME";
stopDiskAsyncReplication(projectId, secondaryDiskRegion, secondaryDiskName);
}

// Stops asynchronous replication for the specified disk.
public static void stopDiskAsyncReplication(
String project, String secondaryDiskRegion, String secondaryDiskName)
throws IOException, ExecutionException, InterruptedException {
// 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()) {
Operation response = disksClient.stopAsyncReplicationAsync(
project, secondaryDiskRegion, secondaryDiskName).get();

if (response.hasError()) {
return;
}
System.out.println("Async replication stopped successfully.");
}
}
}
// [END compute_disk_stop_replication]
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* 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;

import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.Timeout;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
@Timeout(value = 3, unit = TimeUnit.MINUTES)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class DiskReplicationIT {

private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
private static final String PRIMARY_REGION = "us-central1";
private static final String SECONDARY_REGION = "us-east1";
private static final String DISK_TYPE = String.format(
"projects/%s/regions/%s/diskTypes/pd-balanced", PROJECT_ID, SECONDARY_REGION);
private static final List<String> replicaZones = Arrays.asList(
String.format("projects/%s/zones/%s-c", PROJECT_ID, PRIMARY_REGION),
String.format("projects/%s/zones/%s-b", PROJECT_ID, PRIMARY_REGION));
static String templateUUID = UUID.randomUUID().toString().substring(0, 8);
private static final String PRIMARY_DISK_NAME = "test-disk-primary-" + templateUUID;
private static final String SECONDARY_DISK_NAME = "test-disk-secondary-" + templateUUID;
private static ByteArrayOutputStream stdOut;

// Check if the required environment variables are set.
public static void requireEnvVar(String envVarName) {
assertWithMessage(String.format("Missing environment variable '%s' ", envVarName))
.that(System.getenv(envVarName)).isNotEmpty();
}

@BeforeAll
public static void setUp()
throws IOException, ExecutionException, InterruptedException, TimeoutException {
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
requireEnvVar("GOOGLE_CLOUD_PROJECT");

// Create a primary disk to replicate from.
RegionalCreateFromSource.createRegionalDisk(PROJECT_ID, PRIMARY_REGION, replicaZones,
PRIMARY_DISK_NAME, DISK_TYPE, 10,
Optional.empty(), Optional.empty());
TimeUnit.SECONDS.sleep(10);
CreateDiskSecondaryRegional.createDiskSecondaryRegional(PROJECT_ID, PRIMARY_DISK_NAME,
PRIMARY_REGION, SECONDARY_DISK_NAME, SECONDARY_REGION, 10L, DISK_TYPE);
TimeUnit.SECONDS.sleep(10);
}

@BeforeEach
public void beforeEach() {
stdOut = new ByteArrayOutputStream();
System.setOut(new PrintStream(stdOut));
}

@AfterEach
public void afterEach() {
stdOut = null;
System.setOut(null);
}

@Test
@Order(1)
public void testStartDiskAsyncReplication()
throws IOException, ExecutionException, InterruptedException {
StartDiskReplication.startDiskAsyncReplication(PROJECT_ID, PRIMARY_DISK_NAME,
PRIMARY_REGION, SECONDARY_DISK_NAME, SECONDARY_REGION);

assertThat(stdOut.toString().contains("Async replication started successfully."));
}

@Test
@Order(2)
public void testStopPrimaryDiskAsyncReplication()
throws IOException, ExecutionException, InterruptedException, TimeoutException {
StopDiskReplication.stopDiskAsyncReplication(PROJECT_ID, PRIMARY_REGION, PRIMARY_DISK_NAME);

assertThat(stdOut.toString().contains("Async replication stopped successfully."));

RegionalDelete.deleteRegionalDisk(PROJECT_ID, PRIMARY_REGION, PRIMARY_DISK_NAME);
}

@Test
@Order(2)
public void testStopSecondaryDiskAsyncReplication()
throws IOException, ExecutionException, InterruptedException, TimeoutException {
StopDiskReplication.stopDiskAsyncReplication(PROJECT_ID, SECONDARY_REGION, SECONDARY_DISK_NAME);

assertThat(stdOut.toString().contains("Async replication stopped successfully."));
RegionalDelete.deleteRegionalDisk(PROJECT_ID, SECONDARY_REGION, SECONDARY_DISK_NAME);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@
@Timeout(value = 6, unit = TimeUnit.MINUTES)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class HyperdisksIT {

private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
private static final String ZONE = "us-central1-a";
private static final String ZONE = "us-central1-b";
private static final String HYPERDISK_NAME = "test-hyperdisk-enc-" + UUID.randomUUID();
private static final String HYPERDISK_IN_POOL_NAME = "test-hyperdisk-enc-" + UUID.randomUUID();
private static final String STORAGE_POOL_NAME = "test-storage-pool-enc-" + UUID.randomUUID();
Expand All @@ -62,6 +63,8 @@ public static void setUp()
throws IOException, ExecutionException, InterruptedException, TimeoutException {
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
requireEnvVar("GOOGLE_CLOUD_PROJECT");
Util.cleanUpExistingStoragePool("test-storage-pool-enc-", PROJECT_ID, ZONE);
Util.cleanUpExistingDisks("test-hyperdisk-enc-", PROJECT_ID, ZONE);
}

@AfterAll
Expand Down