@@ -32,22 +32,24 @@ public static void main(String[] args)
3232 // TODO(developer): Replace these variables before running the sample.
3333 // Project ID or project number of the Cloud project you want to use.
3434 String projectId = "YOUR_PROJECT_ID" ;
35- // Name of the zone in which the instance you want to use resides .
36- String zone = "zone-name " ;
37- // Name of the compute instance you want to attach a disk to .
35+ // Name of the zone of your compute instance.
36+ String zone = "us-central1-a " ;
37+ // The name of the compute instance where you are adding the replicated disk.
3838 String instanceName = "YOUR_INSTANCE_NAME" ;
39- // Full or partial URL of a persistent disk that you want to attach.
40- String diskLink = String .format (
41- "projects/%s/regions/DISK_REGION/disks/YOUR_DISK_NAME" , projectId );
39+ // The region where your replicated disk is located.
40+ String region = "us-central1" ;
41+ // The name of the replicated disk.
42+ String diskName = "YOUR_DISK_NAME" ;
4243
43- attachRegionalDiskForce (projectId , zone , instanceName , diskLink );
44+ attachRegionalDiskForce (projectId , zone , instanceName , region , diskName );
4445 }
4546
4647 // Attaches a regional disk to the instance,
4748 // forcing the attachment even if other VMs are using the disk.
48- public static void attachRegionalDiskForce (
49- String projectId , String zone , String instanceName , String diskLink )
49+ public static Operation . Status attachRegionalDiskForce (
50+ String projectId , String zone , String instanceName , String region , String diskName )
5051 throws IOException , InterruptedException , ExecutionException , TimeoutException {
52+ String diskLink = String .format ("projects/%s/regions/%s/disks/%s" , projectId , region , diskName );
5153 // Initialize client that will be used to send requests. This client only needs to be created
5254 // once, and can be reused for multiple requests.
5355 try (InstancesClient instancesClient = InstancesClient .create ()) {
@@ -69,10 +71,9 @@ public static void attachRegionalDiskForce(
6971 .get (3 , TimeUnit .MINUTES );
7072
7173 if (response .hasError ()) {
72- System .out .printf ("Error attaching regional disk: %s%n" , response .getError ());
73- return ;
74+ throw new Error ("Error attaching regional disk! " + response );
7475 }
75- System . out . println ( "Regional disk attached successfully." );
76+ return response . getStatus ( );
7677 }
7778 }
7879}
0 commit comments