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 consistency group remove disk #9682

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* 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_disk]
import com.google.cloud.compute.v1.AddResourcePoliciesRegionDiskRequest;
import com.google.cloud.compute.v1.Disk;
// If your disk has zonal location uncomment these lines
//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.RegionDisksAddResourcePoliciesRequest;
import com.google.cloud.compute.v1.RegionDisksClient;
import java.io.IOException;
import java.util.Arrays;
import java.util.concurrent.ExecutionException;

public class AddDiskToConsistencyGroup {

public static void main(String[] args)
throws IOException, ExecutionException, InterruptedException {
// TODO(developer): Replace these variables before running the sample.
// The project that contains the disk.
String project = "YOUR_PROJECT_ID";
// The zone or region of the disk.
String location = "us-central1";
// The name of the disk.
String diskName = "DISK_NAME";
// The name of the consistency group.
String consistencyGroupName = "CONSISTENCY_GROUP";
// The region of the consistency group.
String consistencyGroupLocation = "us-central1";
addDiskToConsistencyGroup(
project, location, diskName, consistencyGroupName, consistencyGroupLocation);
}

// Adds a disk to a Consistency Group.
public static Disk addDiskToConsistencyGroup(
String project, String location, String diskName,
String consistencyGroupName, String consistencyGroupLocation)
throws IOException, ExecutionException, InterruptedException {
String consistencyGroupUrl = String.format(
"https://www.googleapis.com/compute/v1/projects/%s/regions/%s/resourcePolicies/%s",
project, consistencyGroupLocation, consistencyGroupName);
// If your disk has zonal location uncomment these lines
// try (DisksClient disksClient = DisksClient.create()) {
// AddResourcePoliciesDiskRequest request =
// AddResourcePoliciesDiskRequest.newBuilder()
// .setDisk(diskName)
// .setDisksAddResourcePoliciesRequestResource(
// DisksAddResourcePoliciesRequest.newBuilder()
// .addAllResourcePolicies(Arrays.asList(consistencyGroupUrl))
// .build())
// .setProject(project)
// .setZone(location)
// .build();

try (RegionDisksClient disksClient = RegionDisksClient.create()) {
AddResourcePoliciesRegionDiskRequest disksRequest =
AddResourcePoliciesRegionDiskRequest.newBuilder()
.setDisk(diskName)
.setRegion(location)
.setProject(project)
.setRegionDisksAddResourcePoliciesRequestResource(
RegionDisksAddResourcePoliciesRequest.newBuilder()
.addAllResourcePolicies(Arrays.asList(consistencyGroupUrl))
.build())
.build();

Operation response = disksClient.addResourcePoliciesAsync(disksRequest).get();
if (response.hasError()) {
return null;
}
return disksClient.get(project, location, diskName);
}
}
}
// [END compute_consistency_group_add_disk]
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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_create]
import com.google.cloud.compute.v1.Operation;
import com.google.cloud.compute.v1.ResourcePoliciesClient;
import com.google.cloud.compute.v1.ResourcePolicy;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

public class CreateDiskConsistencyGroup {

public static void main(String[] args)
throws IOException, ExecutionException, InterruptedException {
// 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";
// Name of the region in which you want to create the consistency group.
String region = "us-central1";
// Name of the consistency group you want to create.
String consistencyGroupName = "YOUR_CONSISTENCY_GROUP_NAME";

createDiskConsistencyGroup(project, region, consistencyGroupName);
}

// Creates a new disk consistency group resource policy in the specified project and region.
// Return a link to the consistency group.
public static String createDiskConsistencyGroup(
String project, String region, String consistencyGroupName)
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 (ResourcePoliciesClient regionResourcePoliciesClient = ResourcePoliciesClient.create()) {
ResourcePolicy resourcePolicy =
ResourcePolicy.newBuilder()
.setName(consistencyGroupName)
.setRegion(region)
.setDiskConsistencyGroupPolicy(
ResourcePolicy.newBuilder().getDiskConsistencyGroupPolicy())
.build();

Operation response =
regionResourcePoliciesClient.insertAsync(project, region, resourcePolicy).get();

if (response.hasError()) {
return null;
}
return regionResourcePoliciesClient.get(project, region, consistencyGroupName).getSelfLink();
}
}
}
// [END compute_consistency_group_create]
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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_delete]
import com.google.cloud.compute.v1.Operation;
import com.google.cloud.compute.v1.ResourcePoliciesClient;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

public class DeleteDiskConsistencyGroup {

public static void main(String[] args)
throws IOException, ExecutionException, InterruptedException {
// 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";
// Name of the region in which your consistency group is located.
String region = "us-central1";
// Name of the consistency group you want to delete.
String consistencyGroupName = "YOUR_CONSISTENCY_GROUP_NAME";

deleteDiskConsistencyGroup(project, region, consistencyGroupName);
}

// Deletes a disk consistency group resource policy in the specified project and region.
public static void deleteDiskConsistencyGroup(
String project, String region, String consistencyGroupName)
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 (ResourcePoliciesClient regionResourcePoliciesClient = ResourcePoliciesClient.create()) {
Operation response = regionResourcePoliciesClient
.deleteAsync(project, region, consistencyGroupName).get();

if (response.hasError()) {
return;
}
System.out.println(
"Disk consistency group resource policy deleted successfully: "
+ consistencyGroupName);
}
}
}
// [END compute_consistency_group_delete]
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* 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_remove_disk]
import com.google.cloud.compute.v1.Disk;
import com.google.cloud.compute.v1.Operation;
import com.google.cloud.compute.v1.RegionDisksClient;
import com.google.cloud.compute.v1.RegionDisksRemoveResourcePoliciesRequest;
import com.google.cloud.compute.v1.RemoveResourcePoliciesRegionDiskRequest;
// If your disk has zonal location uncomment these lines
//import com.google.cloud.compute.v1.DisksClient;
//import com.google.cloud.compute.v1.DisksRemoveResourcePoliciesRequest;
//import com.google.cloud.compute.v1.RemoveResourcePoliciesDiskRequest;
import java.io.IOException;
import java.util.Arrays;
import java.util.concurrent.ExecutionException;

public class RemoveDiskFromConsistencyGroup {

public static void main(String[] args)
throws IOException, ExecutionException, InterruptedException {
// TODO(developer): Replace these variables before running the sample.
// The project that contains the disk.
String project = "YOUR_PROJECT_ID";
// The zone or region of the disk.
String location = "us-central1";
// The name of the disk.
String diskName = "DISK_NAME";
// The name of the consistency group.
String consistencyGroupName = "CONSISTENCY_GROUP";
// The region of the consistency group.
String consistencyGroupLocation = "us-central1";
removeDiskFromConsistencyGroup(
project, location, diskName, consistencyGroupName, consistencyGroupLocation);
}

// Removes a disk from a Consistency Group.
public static Disk removeDiskFromConsistencyGroup(
String project, String location, String diskName,
String consistencyGroupName, String consistencyGroupLocation)
throws IOException, ExecutionException, InterruptedException {
String consistencyGroupUrl = String.format(
"https://www.googleapis.com/compute/v1/projects/%s/regions/%s/resourcePolicies/%s",
project, consistencyGroupLocation, consistencyGroupName);
// If your disk has zonal location uncomment these lines
// try (DisksClient disksClient = DisksClient.create()) {
// RemoveResourcePoliciesDiskRequest request =
// RemoveResourcePoliciesDiskRequest.newBuilder()
// .setDisk(diskName)
// .setDisksRemoveResourcePoliciesRequestResource(
// DisksRemoveResourcePoliciesRequest.newBuilder()
// .addAllResourcePolicies(Arrays.asList(consistencyGroupUrl))
// .build())
// .setProject(project)
// .setZone(location)
// .build();

try (RegionDisksClient disksClient = RegionDisksClient.create()) {
RemoveResourcePoliciesRegionDiskRequest disksRequest =
RemoveResourcePoliciesRegionDiskRequest.newBuilder()
.setDisk(diskName)
.setRegion(location)
.setProject(project)
.setRegionDisksRemoveResourcePoliciesRequestResource(
RegionDisksRemoveResourcePoliciesRequest.newBuilder()
.addAllResourcePolicies(Arrays.asList(consistencyGroupUrl))
.build())
.build();

Operation response = disksClient.removeResourcePoliciesAsync(disksRequest).get();
if (response.hasError()) {
return null;
}
return disksClient.get(project, location, diskName);
}
}
}
// [END compute_consistency_group_remove_disk]
Loading