1717package compute .reservation ;
1818
1919// [START compute_consume_any_matching_reservation]
20-
2120import static com .google .cloud .compute .v1 .ReservationAffinity .ConsumeReservationType .ANY_RESERVATION ;
2221
2322import com .google .api .gax .longrunning .OperationFuture ;
24- import com .google .cloud .compute .v1 .AllocationSpecificSKUAllocationReservedInstanceProperties ;
25- import com .google .cloud .compute .v1 .AllocationSpecificSKUReservation ;
2623import com .google .cloud .compute .v1 .AttachedDisk ;
2724import com .google .cloud .compute .v1 .AttachedDiskInitializeParams ;
2825import com .google .cloud .compute .v1 .InsertInstanceRequest ;
2926import com .google .cloud .compute .v1 .Instance ;
3027import com .google .cloud .compute .v1 .InstancesClient ;
3128import com .google .cloud .compute .v1 .NetworkInterface ;
3229import com .google .cloud .compute .v1 .Operation ;
33- import com .google .cloud .compute .v1 .Reservation ;
3430import com .google .cloud .compute .v1 .ReservationAffinity ;
35- import com .google .cloud .compute .v1 .ReservationsClient ;
3631import java .io .IOException ;
3732import java .util .concurrent .ExecutionException ;
3833import java .util .concurrent .TimeUnit ;
@@ -45,85 +40,38 @@ public static void main(String[] args)
4540 // TODO(developer): Replace these variables before running the sample.
4641 // Project ID or project number of the Cloud project you want to use.
4742 String projectId = "YOUR_PROJECT_ID" ;
48- // Name of the zone where the reservation is located .
43+ // Zone where the VM instance will be created .
4944 String zone = "us-central1-a" ;
50- // Name of the reservation you want to query.
51- String reservationName = "YOUR_RESERVATION_NAME" ;
5245 // Name of the VM instance you want to query.
5346 String instanceName = "YOUR_INSTANCE_NAME" ;
54- // Number of the instances.
55- int numberOfVms = 2 ;
56-
57- createReservation (projectId , reservationName , numberOfVms , zone );
58- createInstance (projectId , zone , instanceName );
59- }
60-
61- // Creates reservation with properties that match the VM properties.
62- public static void createReservation (String projectId , String reservationName ,
63- int numberOfVms , String zone )
64- throws IOException , ExecutionException , InterruptedException , TimeoutException {
65- // Minimum CPU platform of the instances.
66- String minCpuPlatform = "Intel Skylake" ;
67- // Machine type of the instances.
68- String machineType = "n1-standard-4" ;
69-
70- // Initialize client that will be used to send requests. This client only needs to be created
71- // once, and can be reused for multiple requests.
72- try (ReservationsClient reservationsClient = ReservationsClient .create ()) {
73-
74- Reservation reservation =
75- Reservation .newBuilder ()
76- .setName (reservationName )
77- .setZone (zone )
78- .setSpecificReservation (
79- AllocationSpecificSKUReservation .newBuilder ()
80- .setCount (numberOfVms )
81- .setInstanceProperties (
82- AllocationSpecificSKUAllocationReservedInstanceProperties .newBuilder ()
83- .setMachineType (machineType )
84- .setMinCpuPlatform (minCpuPlatform )
85- .build ())
86- .build ())
87- .build ();
88-
89- // Wait for the create reservation operation to complete.
90- Operation response =
91- reservationsClient .insertAsync (projectId , zone , reservation )
92- .get (3 , TimeUnit .MINUTES );
93-
94- if (response .hasError ()) {
95- System .out .println ("Reservation creation failed!" + response );
96- return ;
97- }
98- System .out .println ("Reservation created. Operation Status: " + response .getStatus ());
99- }
100- }
101-
102- // Create a new instance with the provided "instanceName" value in the specified project and zone.
103- // In this consumption model, existing and new VMs automatically consume a reservation
104- // if their properties match the VM properties specified in the reservation.
105- public static void createInstance (String projectId , String zone , String instanceName )
106- throws IOException , InterruptedException , ExecutionException , TimeoutException {
107- // Below are sample values that can be replaced.
10847 // machineType: machine type of the VM being created.
109- // * This value uses the format zones/{zone}/machineTypes/{type_name}.
11048 // * For a list of machine types, see https://cloud.google.com/compute/docs/machine-types
49+ String machineTypeName = "n1-standard-4" ;
11150 // sourceImage: path to the operating system image to mount.
11251 // * For details about images you can mount, see https://cloud.google.com/compute/docs/images
52+ String sourceImage = "projects/debian-cloud/global/images/family/debian-11" ;
11353 // diskSizeGb: storage size of the boot disk to attach to the instance.
114- // networkName: network interface to associate with the instance.
115- // Minimum CPU platform of the instances.
116- String machineType = String .format ("zones/%s/machineTypes/n1-standard-4" , zone );
117- String sourceImage = String
118- .format ("projects/debian-cloud/global/images/family/%s" , "debian-11" );
11954 long diskSizeGb = 10L ;
55+ // networkName: network interface to associate with the instance.
12056 String networkName = "default" ;
57+ // Minimum CPU platform of the instances.
12158 String minCpuPlatform = "Intel Skylake" ;
12259
60+ createInstance (projectId , zone , instanceName , machineTypeName , sourceImage ,
61+ diskSizeGb , networkName , minCpuPlatform );
62+ }
63+
64+ // Create a virtual machine targeted with the reserveAffinity field.
65+ // In this consumption model, existing and new VMs automatically consume a reservation
66+ // if their properties match the VM properties specified in the reservation.
67+ public static Operation createInstance (String projectId , String zone ,
68+ String instanceName , String machineTypeName , String sourceImage ,
69+ long diskSizeGb , String networkName , String minCpuPlatform )
70+ throws IOException , InterruptedException , ExecutionException , TimeoutException {
71+ String machineType = String .format ("zones/%s/machineTypes/%s" , zone , machineTypeName );
12372 // Initialize client that will be used to send requests. This client only needs to be created
12473 // once, and can be reused for multiple requests.
12574 try (InstancesClient instancesClient = InstancesClient .create ()) {
126- // Instance creation requires at least one persistent disk and one network interface.
12775 AttachedDisk disk =
12876 AttachedDisk .newBuilder ()
12977 .setBoot (true )
@@ -137,18 +85,15 @@ public static void createInstance(String projectId, String zone, String instance
13785 .build ())
13886 .build ();
13987
140- // Use the network interface provided in the networkName argument.
14188 NetworkInterface networkInterface = NetworkInterface .newBuilder ()
14289 .setName (networkName )
14390 .build ();
14491
145- // Set Reservation Affinity
14692 ReservationAffinity reservationAffinity =
14793 ReservationAffinity .newBuilder ()
14894 .setConsumeReservationType (ANY_RESERVATION .toString ())
14995 .build ();
15096
151- // Bind `instanceName`, `machineType`, `disk`, and `networkInterface` to an instance.
15297 Instance instanceResource =
15398 Instance .newBuilder ()
15499 .setName (instanceName )
@@ -159,9 +104,6 @@ public static void createInstance(String projectId, String zone, String instance
159104 .setReservationAffinity (reservationAffinity )
160105 .build ();
161106
162- System .out .printf ("Creating instance: %s at %s %n" , instanceName , zone );
163-
164- // Insert the instance in the specified project and zone.
165107 InsertInstanceRequest insertInstanceRequest = InsertInstanceRequest .newBuilder ()
166108 .setProject (projectId )
167109 .setZone (zone )
@@ -171,13 +113,12 @@ public static void createInstance(String projectId, String zone, String instance
171113 OperationFuture <Operation , Operation > operation = instancesClient .insertAsync (
172114 insertInstanceRequest );
173115
174- // Wait for the operation to complete.
175116 Operation response = operation .get (3 , TimeUnit .MINUTES );
176117
177118 if (response .hasError ()) {
178- System . out . println ( "Instance creation failed ! ! " + response ) ;
119+ return null ;
179120 }
180- System . out . println ( "Operation Status: " + response . getStatus ()) ;
121+ return response ;
181122 }
182123 }
183124}
0 commit comments