2020
2121import static com .google .cloud .compute .v1 .ReservationAffinity .ConsumeReservationType .NO_RESERVATION ;
2222
23+ import com .google .api .gax .longrunning .OperationFuture ;
2324import com .google .cloud .compute .v1 .AttachedDisk ;
2425import com .google .cloud .compute .v1 .AttachedDiskInitializeParams ;
26+ import com .google .cloud .compute .v1 .InsertInstanceRequest ;
2527import com .google .cloud .compute .v1 .Instance ;
2628import com .google .cloud .compute .v1 .InstancesClient ;
2729import com .google .cloud .compute .v1 .NetworkInterface ;
@@ -59,7 +61,7 @@ public static void createInstanceNotConsumeReservation(
5961 String sourceImage = String
6062 .format ("projects/debian-cloud/global/images/family/%s" , "debian-11" );
6163 String network = "global/networks/default" ; // Example network
62-
64+ long diskSizeGb = 10L ;
6365 // Initialize client that will be used to send requests. This client only needs to be created
6466 // once, and can be reused for multiple requests.
6567 try (InstancesClient instancesClient = InstancesClient .create ()) {
@@ -72,6 +74,7 @@ public static void createInstanceNotConsumeReservation(
7274 .setInitializeParams (
7375 AttachedDiskInitializeParams .newBuilder ()
7476 .setSourceImage (sourceImage )
77+ .setDiskSizeGb (diskSizeGb )
7578 .build ())
7679 .build ();
7780
@@ -95,9 +98,18 @@ public static void createInstanceNotConsumeReservation(
9598 .setReservationAffinity (reservationAffinity )
9699 .build ();
97100
101+ // Insert the instance in the specified project and zone.
102+ InsertInstanceRequest insertInstanceRequest = InsertInstanceRequest .newBuilder ()
103+ .setProject (projectId )
104+ .setZone (zone )
105+ .setInstanceResource (instance )
106+ .build ();
107+
108+ OperationFuture <Operation , Operation > operation = instancesClient .insertAsync (
109+ insertInstanceRequest );
110+
98111 // Wait for the operation to complete.
99- Operation response = instancesClient .insertAsync (
100- projectId , zone , instance ).get (3 , TimeUnit .MINUTES );
112+ Operation response = operation .get (3 , TimeUnit .MINUTES );
101113
102114 if (response .hasError ()) {
103115 System .out .println ("Instance creation failed ! ! " + response );
0 commit comments