Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
594de00
Implemented compute_consistency_group_create and compute_consistency_…
TetyanaYahodska Nov 5, 2024
556a8f8
Merge branch 'main' into compute_consistency_group_create
TetyanaYahodska Nov 6, 2024
411cc01
Implemented compute_consistency_group_clone sample
TetyanaYahodska Nov 15, 2024
d71d16e
Merge branch 'main' into compute_consistency_group_clone
TetyanaYahodska Nov 21, 2024
35b9bf4
Fixed code
TetyanaYahodska Nov 21, 2024
57f57d0
Created test
TetyanaYahodska Nov 21, 2024
ba07c6f
Created test and added needed classes
TetyanaYahodska Nov 22, 2024
5ec0411
Added clean up method for regional disks, added comments
TetyanaYahodska Nov 22, 2024
71ec736
Added clean up method for HyperdisksIT
TetyanaYahodska Nov 22, 2024
825b042
Fixed code following the comments
TetyanaYahodska Nov 26, 2024
5463364
Fixed code following the comments
TetyanaYahodska Nov 26, 2024
5993a3a
Fixed code following the comments
TetyanaYahodska Nov 26, 2024
87c3262
Merge branch 'main' into compute_consistency_group_clone
TetyanaYahodska Nov 26, 2024
aba231e
Fixed code
TetyanaYahodska Nov 27, 2024
8102127
Merge branch 'main' into compute_consistency_group_clone
TetyanaYahodska Nov 28, 2024
35b479c
Created test with mocked client
TetyanaYahodska Nov 28, 2024
00d0b60
Increased timeout
TetyanaYahodska Nov 28, 2024
2c9b306
Added check for zonal location and code
TetyanaYahodska Nov 29, 2024
92670ce
Fixed compute_disk_stop_replication sample
TetyanaYahodska Nov 29, 2024
f04073e
Fixed comment
TetyanaYahodska Nov 29, 2024
eddb541
added clea up method for Consistency Group
TetyanaYahodska Nov 29, 2024
011ef64
Changed zone
TetyanaYahodska Nov 29, 2024
88d83b3
Resolved merge conflict
TetyanaYahodska Nov 30, 2024
11ea034
Deleted not related classes
TetyanaYahodska Nov 30, 2024
636251a
Merge branch 'main' into compute_consistency_group_clone
TetyanaYahodska Dec 10, 2024
99cb528
Created compute_consistency_group_clone_zonal_disk sample and test
TetyanaYahodska Dec 10, 2024
0b8767d
Resolved merge conflict
TetyanaYahodska Dec 13, 2024
18abd6d
Fixed code for samples with zonal/regional disk location
TetyanaYahodska Dec 13, 2024
8b4e3b4
Merge branch 'main' into compute_consistency_group_clone
TetyanaYahodska Dec 18, 2024
6109822
Fixed Util class
TetyanaYahodska Dec 18, 2024
ed0be28
Fixed code
TetyanaYahodska Dec 18, 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
Expand Up @@ -20,6 +20,7 @@
import com.google.cloud.compute.v1.Disk;
import com.google.cloud.compute.v1.InsertRegionDiskRequest;
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.ArrayList;
Expand Down Expand Up @@ -55,7 +56,7 @@ public static void main(String[] args)
}

// Create a disk for synchronous data replication between two zones in the same region
public static Operation.Status createReplicatedDisk(String projectId, String region,
public static Status createReplicatedDisk(String projectId, String region,
List<String> replicaZones, String diskName, int diskSizeGb, String diskType)
throws IOException, InterruptedException, ExecutionException, TimeoutException {
// Initialize client that will be used to send requests. This client only needs to be created
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* 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.consistencygroup;

// [START compute_consistency_group_add_regional_disk]
import com.google.cloud.compute.v1.AddResourcePoliciesRegionDiskRequest;
import com.google.cloud.compute.v1.Operation;
import com.google.cloud.compute.v1.Operation.Status;
import com.google.cloud.compute.v1.RegionDisksAddResourcePoliciesRequest;
import com.google.cloud.compute.v1.RegionDisksClient;
import java.io.IOException;
import java.util.Arrays;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class AddRegionalDiskToConsistencyGroup {
public static void main(String[] args)
throws IOException, ExecutionException, InterruptedException, TimeoutException {
// TODO(developer): Replace these variables before running the sample.
// Project ID or project number of the Cloud project that contains the disk.
String project = "YOUR_PROJECT_ID";
// Region in which your disk and consistency group are located.
String region = "us-central1";
// Name of the disk.
String diskName = "DISK_NAME";
// Name of the consistency group.
String consistencyGroupName = "CONSISTENCY_GROUP";

addRegionalDiskToConsistencyGroup(project, region, diskName, consistencyGroupName);
}

// Adds regional disk to a consistency group.
public static Status addRegionalDiskToConsistencyGroup(
String project, String region, String diskName, String consistencyGroupName)
throws IOException, ExecutionException, InterruptedException, TimeoutException {
String consistencyGroupUrl = String.format(
"https://www.googleapis.com/compute/v1/projects/%s/regions/%s/resourcePolicies/%s",
project, region, consistencyGroupName);

// 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()) {
AddResourcePoliciesRegionDiskRequest request =
AddResourcePoliciesRegionDiskRequest.newBuilder()
.setDisk(diskName)
.setRegion(region)
.setProject(project)
.setRegionDisksAddResourcePoliciesRequestResource(
RegionDisksAddResourcePoliciesRequest.newBuilder()
.addAllResourcePolicies(Arrays.asList(consistencyGroupUrl))
.build())
.build();
Operation response = disksClient.addResourcePoliciesAsync(request).get(1, TimeUnit.MINUTES);

if (response.hasError()) {
throw new Error("Error adding regional disk to consistency group! " + response.getError());
}
return response.getStatus();
}
}
}
// [END compute_consistency_group_add_regional_disk]
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* 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.consistencygroup;

// [START compute_consistency_group_add_zonal_disk]
import com.google.cloud.compute.v1.AddResourcePoliciesDiskRequest;
import com.google.cloud.compute.v1.DisksAddResourcePoliciesRequest;
import com.google.cloud.compute.v1.DisksClient;
import com.google.cloud.compute.v1.Operation;
import com.google.cloud.compute.v1.Operation.Status;
import java.io.IOException;
import java.util.Arrays;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class AddZonalDiskToConsistencyGroup {
public static void main(String[] args)
throws IOException, ExecutionException, InterruptedException, TimeoutException {
// TODO(developer): Replace these variables before running the sample.
// Project ID or project number of the Cloud project that contains the disk.
String project = "YOUR_PROJECT_ID";
// Zone of the disk.
String diskLocation = "us-central1-a";
// Name of the disk.
String diskName = "DISK_NAME";
// Name of the consistency group.
String consistencyGroupName = "CONSISTENCY_GROUP";

addZonalDiskToConsistencyGroup(
project, diskLocation, diskName, consistencyGroupName);
}

// Adds zonal disk to a consistency group.
public static Status addZonalDiskToConsistencyGroup(
String project, String diskLocation, String diskName,
String consistencyGroupName)
throws IOException, ExecutionException, InterruptedException, TimeoutException {
String region = diskLocation.substring(0, diskLocation.lastIndexOf('-'));
String consistencyGroupUrl = String.format(
"https://www.googleapis.com/compute/v1/projects/%s/regions/%s/resourcePolicies/%s",
project, region, consistencyGroupName);

// 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()) {
AddResourcePoliciesDiskRequest request =
AddResourcePoliciesDiskRequest.newBuilder()
.setDisk(diskName)
.setZone(diskLocation)
.setProject(project)
.setDisksAddResourcePoliciesRequestResource(
DisksAddResourcePoliciesRequest.newBuilder()
.addAllResourcePolicies(Arrays.asList(consistencyGroupUrl))
.build())
.build();
Operation response = disksClient.addResourcePoliciesAsync(request).get(1, TimeUnit.MINUTES);

if (response.hasError()) {
throw new Error("Error adding disk to consistency group! " + response.getError());
}
return response.getStatus();
}
}
}
// [END compute_consistency_group_add_zonal_disk]
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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.consistencygroup;

// [START compute_consistency_group_clone_regional_disk]
import com.google.cloud.compute.v1.BulkInsertDiskResource;
import com.google.cloud.compute.v1.BulkInsertRegionDiskRequest;
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.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class CloneRegionalDisksFromConsistencyGroup {

public static void main(String[] args)
throws IOException, ExecutionException, InterruptedException, TimeoutException {
// TODO(developer): Replace these variables before running the sample.
// Project ID or project number of the Cloud project you want to use.
String project = "YOUR_PROJECT_ID";
// Region in which your disks and consistency group are located.
String region = "us-central1";
// Name of the consistency group you want to clone disks from.
String consistencyGroupName = "YOUR_CONSISTENCY_GROUP_NAME";

cloneRegionalDisksFromConsistencyGroup(project, region, consistencyGroupName);
}

// Clones regional disks from a consistency group.
public static Status cloneRegionalDisksFromConsistencyGroup(
String project, String region, String consistencyGroupName)
throws IOException, InterruptedException, ExecutionException, TimeoutException {
String sourceConsistencyGroupPolicy = String.format(
"projects/%s/regions/%s/resourcePolicies/%s", project, region, consistencyGroupName);

// 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()) {
BulkInsertRegionDiskRequest request = BulkInsertRegionDiskRequest.newBuilder()
.setProject(project)
.setRegion(region)
.setBulkInsertDiskResourceResource(
BulkInsertDiskResource.newBuilder()
.setSourceConsistencyGroupPolicy(sourceConsistencyGroupPolicy)
.build())
.build();

Operation response = disksClient.bulkInsertAsync(request).get(3, TimeUnit.MINUTES);

if (response.hasError()) {
throw new Error("Error cloning regional disks! " + response.getError());
}
return response.getStatus();
}
}
}
// [END compute_consistency_group_clone_regional_disk]
Loading
Loading