2020
2121import static com .google .cloud .compute .v1 .ReservationAffinity .ConsumeReservationType .SPECIFIC_RESERVATION ;
2222
23+ import com .google .api .gax .longrunning .OperationFuture ;
2324import com .google .cloud .compute .v1 .AllocationSpecificSKUAllocationReservedInstanceProperties ;
2425import com .google .cloud .compute .v1 .AllocationSpecificSKUReservation ;
2526import com .google .cloud .compute .v1 .AttachedDisk ;
2627import com .google .cloud .compute .v1 .AttachedDiskInitializeParams ;
28+ import com .google .cloud .compute .v1 .InsertInstanceRequest ;
2729import com .google .cloud .compute .v1 .Instance ;
2830import com .google .cloud .compute .v1 .InstancesClient ;
2931import com .google .cloud .compute .v1 .NetworkInterface ;
@@ -50,22 +52,21 @@ public static void main(String[] args)
5052 String instanceName = "YOUR_INSTANCE_NAME" ;
5153 // Number of the instances.
5254 int numberOfVms = 10 ;
53- // Machine type of the instances.
54- String machineType = "n2-standard-32" ;
55- // Minimum CPU platform of the instances.
56- String minCpuPlatform = "Intel Cascade Lake" ;
57- boolean specificReservationRequired = true ;
5855
59- createReservation (projectId , reservationName , numberOfVms ,
60- zone , machineType , minCpuPlatform , specificReservationRequired );
61- createInstance (projectId , zone , instanceName , machineType , minCpuPlatform , reservationName );
56+ createReservation (projectId , reservationName , numberOfVms , zone );
57+ createInstance (projectId , zone , instanceName , reservationName );
6258 }
6359
6460 // Creates reservation with the given parameters.
6561 public static void createReservation (
66- String projectId , String reservationName , int numberOfVms , String zone ,
67- String machineType , String minCpuPlatform , boolean specificReservationRequired )
62+ String projectId , String reservationName , int numberOfVms , String zone )
6863 throws IOException , ExecutionException , InterruptedException , TimeoutException {
64+ // Machine type of the instances.
65+ String machineType = "n2-standard-32" ;
66+ // Minimum CPU platform of the instances.
67+ String minCpuPlatform = "Intel Cascade Lake" ;
68+ boolean specificReservationRequired = true ;
69+
6970 // Initialize client that will be used to send requests. This client only needs to be created
7071 // once, and can be reused for multiple requests.
7172 try (ReservationsClient reservationsClient = ReservationsClient .create ()) {
@@ -99,18 +100,22 @@ public static void createReservation(
99100 }
100101
101102 // Create a new instance with the provided "instanceName" value in the specified project and zone.
102- public static void createInstance (String project , String zone ,
103- String instanceName , String machineType , String minCpuPlatform , String reservationName )
103+ public static void createInstance (
104+ String projectId , String zone , String instanceName , String reservationName )
104105 throws IOException , InterruptedException , ExecutionException , TimeoutException {
105106 // Below are sample values that can be replaced.
106107 // sourceImage: path to the operating system image to mount.
107108 // * For details about images you can mount, see https://cloud.google.com/compute/docs/images
108109 // diskSizeGb: storage size of the boot disk to attach to the instance.
109110 // networkName: network interface to associate with the instance.
111+ // Machine type of the instances.
112+ // Minimum CPU platform of the instances.
110113 String sourceImage = String
111114 .format ("projects/debian-cloud/global/images/family/%s" , "debian-11" );
112115 long diskSizeGb = 10L ;
113116 String networkName = "default" ;
117+ String machineType = String .format ("zones/%s/machineTypes/n2-standard-32" , zone );
118+ String minCpuPlatform = "Intel Cascade Lake" ;
114119
115120 // Initialize client that will be used to send requests. This client only needs to be created
116121 // once, and can be reused for multiple requests.
@@ -134,34 +139,43 @@ public static void createInstance(String project, String zone,
134139 .setName (networkName )
135140 .build ();
136141
142+ // Set Reservation Affinity
143+ ReservationAffinity reservationAffinity =
144+ ReservationAffinity .newBuilder ()
145+ .setConsumeReservationType (SPECIFIC_RESERVATION .toString ())
146+ .setKey ("compute.googleapis.com/reservation-name" )
147+ // Set specific reservation
148+ .addValues (reservationName )
149+ .build ();
150+
137151 // Bind `instanceName`, `machineType`, `disk`, and `networkInterface` to an instance.
138152 Instance instanceResource =
139153 Instance .newBuilder ()
140154 .setName (instanceName )
141- .setMachineType (String . format ( "zones/%s/machineTypes/%s" , zone , machineType ) )
155+ .setMachineType (machineType )
142156 .addDisks (disk )
143157 .addNetworkInterfaces (networkInterface )
144158 .setMinCpuPlatform (minCpuPlatform )
145- // Set Reservation Affinity
146- .setReservationAffinity (
147- ReservationAffinity .newBuilder ()
148- .setConsumeReservationType (
149- SPECIFIC_RESERVATION .name ())
150- .setKey ("compute.googleapis.com/reservation-name" )
151- // Set specific reservation
152- .addValues (reservationName )
153- .build ())
159+ .setReservationAffinity (reservationAffinity )
154160 .build ();
155161
156162 System .out .printf ("Creating instance: %s at %s %n" , instanceName , zone );
157163
164+ // Insert the instance in the specified project and zone.
165+ InsertInstanceRequest insertInstanceRequest = InsertInstanceRequest .newBuilder ()
166+ .setProject (projectId )
167+ .setZone (zone )
168+ .setInstanceResource (instanceResource )
169+ .build ();
170+
171+ OperationFuture <Operation , Operation > operation = instancesClient .insertAsync (
172+ insertInstanceRequest );
173+
158174 // Wait for the operation to complete.
159- Operation response = instancesClient .insertAsync (project , zone , instanceResource )
160- .get (3 , TimeUnit .MINUTES );
175+ Operation response = operation .get (3 , TimeUnit .MINUTES );
161176
162177 if (response .hasError ()) {
163178 System .out .println ("Instance creation failed ! ! " + response );
164- return ;
165179 }
166180 System .out .println ("Operation Status: " + response .getStatus ());
167181 }
0 commit comments