1818
1919//[START compute_disk_create_secondary_custom] 
2020import  com .google .cloud .compute .v1 .Disk ;
21+ import  com .google .cloud .compute .v1 .DiskAsyncReplication ;
2122import  com .google .cloud .compute .v1 .DisksClient ;
2223import  com .google .cloud .compute .v1 .GuestOsFeature ;
2324import  com .google .cloud .compute .v1 .Operation ;
@@ -36,63 +37,72 @@ public static void main(String[] args)
3637    // TODO(developer): Replace these variables before running the sample. 
3738    // The project that contains the primary disk. 
3839    String  projectId  = "YOUR_PROJECT_ID" ;
39-     // Name of the zone in which you want to create the secondary disk. 
40-     String  disksZone  = "us-central1-a" ;
40+     // Name of the primary disk you want to use. 
41+     String  primaryDiskName  = "PRIMARY_DISK_NAME" ;
42+     // Name of the zone in which your primary disk is located. 
43+     // Learn more about zones and regions: 
44+     // https://cloud.google.com/compute/docs/disks/async-pd/about#supported_region_pairs 
45+     String  primaryDiskZone  = "us-central1-a" ;
4146    // Name of the disk you want to create. 
4247    String  secondaryDiskName  = "SECONDARY_DISK_NAME" ;
48+     // Name of the zone in which you want to create the secondary disk. 
49+     String  secondaryDiskZone  = "us-east1-c" ;
4350    // Size of the new disk in gigabytes. 
4451    long  diskSizeGb  = 100 ;
45-     // Name of the primary disk you want to use as a source for the new disk. 
46-     String  primaryDiskName  = "PRIMARY_DISK_NAME" ;
4752    // The type of the disk you want to create. This value uses the following format: 
4853    // "projects/{projectId}/zones/{zone}/diskTypes/ 
4954    // (pd-standard|pd-ssd|pd-balanced|pd-extreme)". 
5055    String  diskType  = String .format (
51-         "projects/%s/zones/%s/diskTypes/pd-balanced" , projectId , disksZone );
56+         "projects/%s/zones/%s/diskTypes/pd-balanced" , projectId , secondaryDiskZone );
5257
53-     createDiskSecondaryCustom (projectId , secondaryDiskName ,  disksZone ,
54-         diskSizeGb ,  primaryDiskName ,  diskType );
58+     createDiskSecondaryCustom (projectId , primaryDiskName ,  secondaryDiskName ,
59+         primaryDiskZone ,  secondaryDiskZone ,  diskSizeGb ,   diskType );
5560  }
5661
5762  // Creates a secondary disk with specified custom parameters. 
58-   public  static  Disk  createDiskSecondaryCustom (String  projectId , String  secondaryDiskName ,
59-        String  disksZone , long  diskSizeGb , String  primaryDiskName , String  diskType )
63+   public  static  Disk  createDiskSecondaryCustom (String  projectId , String  primaryDiskName ,
64+       String  secondaryDiskName , String  primaryDiskZone , String  secondaryDiskZone ,
65+       long  diskSizeGb ,  String  diskType )
6066      throws  IOException , ExecutionException , InterruptedException , TimeoutException  {
67+     String  primaryDiskSource  = String .format ("projects/%s/zones/%s/disks/%s" ,
68+         projectId , primaryDiskZone , primaryDiskName );
69+ 
70+     DiskAsyncReplication  asyncReplication  = DiskAsyncReplication .newBuilder ()
71+         .setDisk (primaryDiskSource )
72+         .build ();
73+ 
74+     // Define the guest OS features. 
75+     List <GuestOsFeature > guestOsFeatures  = Arrays .asList (
76+         GuestOsFeature .newBuilder ().setType ("UEFI_COMPATIBLE" ).build (),
77+         GuestOsFeature .newBuilder ().setType ("GVNIC" ).build (),
78+         GuestOsFeature .newBuilder ().setType ("MULTI_IP_SUBNET" ).build ());
79+ 
80+     // Define the labels. 
81+     Map <String , String > labels  = new  HashMap <>();
82+     labels .put ("secondary-disk-for-replication" , "yes" );
83+ 
6184    // Initialize client that will be used to send requests. This client only needs to be created 
6285    // once, and can be reused for multiple requests. 
6386    try  (DisksClient  disksClient  = DisksClient .create ()) {
64-       String  primaryDiskSource  = String .format ("projects/%s/zones/%s/disks/%s" ,
65-           projectId , disksZone , primaryDiskName );
66- 
67-       // Define the guest OS features. 
68-       List <GuestOsFeature > guestOsFeatures  = Arrays .asList (
69-           GuestOsFeature .newBuilder ().setType ("UEFI_COMPATIBLE" ).build (),
70-           GuestOsFeature .newBuilder ().setType ("GVNIC" ).build (),
71-           GuestOsFeature .newBuilder ().setType ("MULTI_IP_SUBNET" ).build ()
72-       );
73-       // Define the labels. 
74-       Map <String , String > labels  = new  HashMap <>();
75-       labels .put ("secondary-disk-for-replication" , "yes" );
7687
77-       // Create the disk object with the source disk information. 
7888      Disk  disk  = Disk .newBuilder ()
7989          .setName (secondaryDiskName )
8090          .setSizeGb (diskSizeGb )
8191          .setType (diskType )
82-           .setZone (disksZone )
92+           .setZone (secondaryDiskZone )
8393          .addAllGuestOsFeatures (guestOsFeatures )
8494          .putAllLabels (labels )
85-           .setSourceDisk ( primaryDiskSource )
95+           .setAsyncPrimaryDisk ( asyncReplication )
8696          .build ();
8797
8898      // Wait for the create disk operation to complete. 
89-       Operation  response  = disksClient .insertAsync (projectId , disksZone , disk )
99+       Operation  response  = disksClient .insertAsync (projectId , secondaryDiskZone , disk )
90100          .get (3 , TimeUnit .MINUTES );
91101
92102      if  (response .hasError ()) {
93103        return  null ;
94104      }
95-       return  disksClient .get (projectId , disksZone , secondaryDiskName );
105+       return  disksClient .get (projectId , secondaryDiskZone , secondaryDiskName );
96106    }
97107  }
98108}
0 commit comments