1
+ /*
2
+ * Copyright 2024 Google LLC
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ package compute .reservation ;
18
+
19
+ // [START compute_reservation_create_template]
20
+
21
+ import com .google .cloud .compute .v1 .AllocationSpecificSKUReservation ;
22
+ import com .google .cloud .compute .v1 .Operation ;
23
+ import com .google .cloud .compute .v1 .Reservation ;
24
+ import com .google .cloud .compute .v1 .ReservationsClient ;
25
+ import java .io .IOException ;
26
+ import java .util .concurrent .ExecutionException ;
27
+ import java .util .concurrent .TimeUnit ;
28
+ import java .util .concurrent .TimeoutException ;
29
+
30
+ public class CreateReservationForInstanceTemplate {
31
+
32
+ public static void main (String [] args )
33
+ throws IOException , ExecutionException , InterruptedException , TimeoutException {
34
+ // TODO(developer): Replace these variables before running the sample.
35
+ // Project ID or project number of the Cloud project you want to use.
36
+ String projectId = "YOUR_PROJECT_ID" ;
37
+ // Name of the zone in which you want to create the reservation.
38
+ String zone = "us-central1-a" ;
39
+ // Name of the reservation you want to create.
40
+ String reservationName = "YOUR_RESERVATION_NAME" ;
41
+ // The number of virtual machines you want to create.
42
+ int numberOfVms = 3 ;
43
+ // The URI of the instance template with GLOBAL location
44
+ // to be used for creating the reservation.
45
+ String instanceTemplateUri =
46
+ "projects/YOUR_PROJECT_ID/global/instanceTemplates/YOUR_INSTANCE_TEMPLATE_NAME" ;
47
+
48
+ // The URI of the instance template with REGIONAL location
49
+ // to be used for creating the reservation. For us-central1 region in this case.
50
+ // String instanceTemplateUri =
51
+ // "projects/YOUR_PROJECT_ID/regions/us-central1/instanceTemplates/YOUR_INSTANCE_TEMPLATE_NAME"
52
+
53
+ createReservationForInstanceTemplate (
54
+ projectId , reservationName , instanceTemplateUri , numberOfVms , zone );
55
+ }
56
+
57
+ // Creates a reservation in a project for the instance template.
58
+ public static void createReservationForInstanceTemplate (
59
+ String projectId , String reservationName , String instanceTemplateUri ,
60
+ int numberOfVms , String zone )
61
+ throws IOException , ExecutionException , InterruptedException , TimeoutException {
62
+ // Initialize client that will be used to send requests. This client only needs to be created
63
+ // once, and can be reused for multiple requests.
64
+ try (ReservationsClient reservationsClient = ReservationsClient .create ()) {
65
+
66
+ // Create the reservation.
67
+ Reservation reservation =
68
+ Reservation .newBuilder ()
69
+ .setName (reservationName )
70
+ .setZone (zone )
71
+ .setSpecificReservation (
72
+ AllocationSpecificSKUReservation .newBuilder ()
73
+ // Set the number of instances
74
+ .setCount (numberOfVms )
75
+ // Set the instance template to be used for creating the reservation.
76
+ .setSourceInstanceTemplate (instanceTemplateUri )
77
+ .build ())
78
+ .build ();
79
+
80
+ // Wait for the create reservation operation to complete.
81
+ Operation response =
82
+ reservationsClient .insertAsync (projectId , zone , reservation ).get (3 , TimeUnit .MINUTES );
83
+
84
+ if (response .hasError ()) {
85
+ System .out .println ("Reservation creation failed!" + response );
86
+ return ;
87
+ }
88
+ System .out .println ("Reservation created. Operation Status: " + response .getStatus ());
89
+ }
90
+ }
91
+ }
92
+ // [END compute_reservation_create_template]
0 commit comments