|
| 1 | +/* |
| 2 | + * Copyright 2024 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package compute.disks; |
| 18 | + |
| 19 | +// [START compute_instance_attach_regional_disk_force] |
| 20 | +import com.google.cloud.compute.v1.AttachDiskInstanceRequest; |
| 21 | +import com.google.cloud.compute.v1.AttachedDisk; |
| 22 | +import com.google.cloud.compute.v1.InstancesClient; |
| 23 | +import com.google.cloud.compute.v1.Operation; |
| 24 | +import com.google.cloud.compute.v1.Operation.Status; |
| 25 | +import java.io.IOException; |
| 26 | +import java.util.concurrent.ExecutionException; |
| 27 | +import java.util.concurrent.TimeUnit; |
| 28 | +import java.util.concurrent.TimeoutException; |
| 29 | + |
| 30 | +public class AttachRegionalDiskForce { |
| 31 | + public static void main(String[] args) |
| 32 | + throws IOException, InterruptedException, ExecutionException, TimeoutException { |
| 33 | + // TODO(developer): Replace these variables before running the sample. |
| 34 | + // Project ID or project number of the Cloud project you want to use. |
| 35 | + String projectId = "YOUR_PROJECT_ID"; |
| 36 | + // Name of the zone of your compute instance. |
| 37 | + String zone = "us-central1-a"; |
| 38 | + // The name of the compute instance where you are adding the replicated disk. |
| 39 | + String instanceName = "YOUR_INSTANCE_NAME"; |
| 40 | + // The region where your replicated disk is located. |
| 41 | + String region = "us-central1"; |
| 42 | + // The name of the replicated disk. |
| 43 | + String diskName = "YOUR_DISK_NAME"; |
| 44 | + |
| 45 | + attachRegionalDiskForce(projectId, zone, instanceName, region, diskName); |
| 46 | + } |
| 47 | + |
| 48 | + // Attaches a regional disk to the instance, |
| 49 | + // forcing the attachment even if other VMs are using the disk. |
| 50 | + public static Status attachRegionalDiskForce(String projectId, |
| 51 | + String zone, String instanceName, String region, String diskName) |
| 52 | + throws IOException, InterruptedException, ExecutionException, TimeoutException { |
| 53 | + String diskLink = String.format("projects/%s/regions/%s/disks/%s", |
| 54 | + projectId, region, diskName); |
| 55 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 56 | + // once, and can be reused for multiple requests. |
| 57 | + try (InstancesClient instancesClient = InstancesClient.create()) { |
| 58 | + AttachedDisk attachedDisk = AttachedDisk.newBuilder() |
| 59 | + .setSource(diskLink) |
| 60 | + .setMode(AttachedDisk.Mode.READ_WRITE.toString()) |
| 61 | + .build(); |
| 62 | + |
| 63 | + AttachDiskInstanceRequest attachDiskRequest = AttachDiskInstanceRequest.newBuilder() |
| 64 | + .setProject(projectId) |
| 65 | + .setZone(zone) |
| 66 | + .setInstance(instanceName) |
| 67 | + .setAttachedDiskResource(attachedDisk) |
| 68 | + .setForceAttach(true) // Force the attachment |
| 69 | + .build(); |
| 70 | + |
| 71 | + Operation response = instancesClient.attachDiskAsync(attachDiskRequest) |
| 72 | + .get(3, TimeUnit.MINUTES); |
| 73 | + |
| 74 | + if (response.hasError()) { |
| 75 | + throw new Error("Error attaching regional disk! " + response); |
| 76 | + } |
| 77 | + return response.getStatus(); |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | +// [END compute_instance_attach_regional_disk_force] |
0 commit comments