18
18
19
19
// [START compute_reservation_create_shared]
20
20
import com .google .cloud .compute .v1 .AllocationSpecificSKUReservation ;
21
+ import com .google .cloud .compute .v1 .InsertReservationRequest ;
21
22
import com .google .cloud .compute .v1 .Operation ;
23
+ import com .google .cloud .compute .v1 .Operation .Status ;
22
24
import com .google .cloud .compute .v1 .Reservation ;
23
25
import com .google .cloud .compute .v1 .ReservationsClient ;
24
26
import com .google .cloud .compute .v1 .ShareSettings ;
29
31
import java .util .concurrent .TimeoutException ;
30
32
31
33
public class CreateSharedReservation {
32
- private final ReservationsClient reservationsClient ;
33
-
34
- // Constructor to inject the ReservationsClient
35
- public CreateSharedReservation (ReservationsClient reservationsClient ) {
36
- this .reservationsClient = reservationsClient ;
37
- }
38
34
39
35
public static void main (String [] args )
40
36
throws IOException , ExecutionException , InterruptedException , TimeoutException {
@@ -48,62 +44,66 @@ public static void main(String[] args)
48
44
// For more information visit this page:
49
45
// https://cloud.google.com/compute/docs/instances/reservations-shared#shared_reservation_constraint
50
46
String projectId = "YOUR_PROJECT_ID" ;
51
- // Zone in which the reservation resides .
47
+ // Zone in which to reserve resources .
52
48
String zone = "us-central1-a" ;
53
49
// Name of the reservation to be created.
54
50
String reservationName = "YOUR_RESERVATION_NAME" ;
55
51
// The URI of the global instance template to be used for creating the reservation.
56
52
String instanceTemplateUri = String .format (
57
- "projects/%s/global/instanceTemplates/YOUR_INSTANCE_TEMPLATE_NAME " , projectId );
53
+ "projects/%s/global/instanceTemplates/%s " , projectId , "YOUR_INSTANCE_TEMPLATE_NAME" );
58
54
// Number of instances for which capacity needs to be reserved.
59
55
int vmCount = 3 ;
60
- // In your main method, create ReservationsClient
61
- ReservationsClient client = ReservationsClient .create ();
62
- // Create an instance of your class, passing in the client
63
- CreateSharedReservation creator = new CreateSharedReservation (client );
64
56
65
- creator . createSharedReservation (projectId , zone , reservationName , instanceTemplateUri , vmCount );
57
+ createSharedReservation (projectId , zone , reservationName , instanceTemplateUri , vmCount );
66
58
}
67
59
68
60
// Creates a shared reservation with the given name in the given zone.
69
- public void createSharedReservation (
70
- String projectId , String zone ,
71
- String reservationN
ED4F
ame , String instanceTemplateUri , int vmCount )
72
- throws ExecutionException , InterruptedException , TimeoutException {
61
+ public static Status createSharedReservation (
62
+ String projectId , String zone ,
63
+ String reservationName , String instanceTemplateUri , int vmCount )
64
+ throws ExecutionException , InterruptedException , TimeoutException , IOException {
65
+
66
+ // Initialize client that will be used to send requests. This client only needs to be created
67
+ // once, and can be reused for multiple requests.
68
+ try (ReservationsClient reservationsClient = ReservationsClient .create ()) {
69
+ ShareSettings shareSettings = ShareSettings .newBuilder ()
70
+ .setShareType (String .valueOf (ShareSettings .ShareType .SPECIFIC_PROJECTS ))
71
+ // The IDs of projects that can consume this reservation. You can include up to
72
+ // 100 consumer projects. These projects must be in the same organization as
73
+ // the owner project. Don't include the owner project.
74
+ // By default, it is already allowed to consume the reservation.
75
+ .putProjectMap ("CONSUMER_PROJECT_1" , ShareSettingsProjectConfig .newBuilder ().build ())
76
+ .putProjectMap ("CONSUMER_PROJECT_2" , ShareSettingsProjectConfig .newBuilder ().build ())
77
+ .build ();
73
78
74
- ShareSettings shareSettings = ShareSettings .newBuilder ()
75
- .setShareType (String .valueOf (ShareSettings .ShareType .SPECIFIC_PROJECTS ))
76
- // The IDs of projects that can consume this reservation. You can include up to 100
77
- // consumer projects. These projects must be in the same organization as
78
- // the owner project. Don't include the owner project. By default, it is already allowed
79
- // to consume the reservation.
80
- .putProjectMap ("CONSUMER_PROJECT_ID_1" , ShareSettingsProjectConfig .newBuilder ().build ())
81
- .putProjectMap ("CONSUMER_PROJECT_ID_2" , ShareSettingsProjectConfig .newBuilder ().build ())
82
- .build ();
79
+ Reservation reservationResource =
80
+ Reservation .newBuilder ()
81
+ .setName (reservationName )
82
+ .setZone (zone )
83
+ .setSpecificReservationRequired (true )
84
+ .setShareSettings (shareSettings )
85
+ .setSpecificReservation (
86
+ AllocationSpecificSKUReservation .newBuilder ()
87
+ .setCount (vmCount )
88
+ .setSourceInstanceTemplate (instanceTemplateUri )
89
+ .build ())
90
+ .build ();
83
91
84
- // Create the reservation.
85
- Reservation reservation =
86
- Reservation .newBuilder ()
87
- .setName (reservationName )
88
- .setZone (zone )
89
- .setSpecificReservationRequired (true )
90
- .setShareSettings (shareSettings )
91
- .setSpecificReservation (
92
- AllocationSpecificSKUReservation .newBuilder ()
93
- .setCount (vmCount )
94
- .setSourceInstanceTemplate (instanceTemplateUri )
95
- .build ())
96
- .build ();
92
+ InsertReservationRequest request =
93
+ InsertReservationRequest .newBuilder ()
94
+ .setProject (projectId )
95
+ .setZone (zone )
96
+ .setReservationResource (reservationResource )
97
+ .build ();
97
98
98
- // Wait for the create reservation operation to complete.
99
- Operation response =
100
- this .reservationsClient .insertAsync (projectId , zone , reservation ).get (3 , TimeUnit .MINUTES );
99
+ Operation response = reservationsClient .insertAsync (request )
100
+ .get (3 , TimeUnit .MINUTES );
101
101
102
- if (response .hasError ()) {
103
- System .out .println ("Reservation creation failed!" + response );
104
- return ;
102
+ if (response .hasError ()) {
103
+ throw new Error ("Reservation creation failed!!" + response );
104
+ }
105
+ return response .getStatus ();
105
106
}
106
- System .out .println ("Reservation created. Operation Status: " + response .getStatus ());
107
107
}
108
108
}
109
109
// [END compute_reservation_create_shared]
0 commit comments