1818
1919// [START compute_reservation_create_shared]
2020import com .google .cloud .compute .v1 .AllocationSpecificSKUReservation ;
21+ import com .google .cloud .compute .v1 .InsertReservationRequest ;
2122import com .google .cloud .compute .v1 .Operation ;
23+ import com .google .cloud .compute .v1 .Operation .Status ;
2224import com .google .cloud .compute .v1 .Reservation ;
2325import com .google .cloud .compute .v1 .ReservationsClient ;
2426import com .google .cloud .compute .v1 .ShareSettings ;
2931import java .util .concurrent .TimeoutException ;
3032
3133public class CreateSharedReservation {
32- private final ReservationsClient reservationsClient ;
33-
34- // Constructor to inject the ReservationsClient
35- public CreateSharedReservation (ReservationsClient reservationsClient ) {
36- this .reservationsClient = reservationsClient ;
37- }
3834
3935 public static void main (String [] args )
4036 throws IOException , ExecutionException , InterruptedException , TimeoutException {
@@ -48,62 +44,66 @@ public static void main(String[] args)
4844 // For more information visit this page:
4945 // https://cloud.google.com/compute/docs/instances/reservations-shared#shared_reservation_constraint
5046 String projectId = "YOUR_PROJECT_ID" ;
51- // Zone in which the reservation resides .
47+ // Zone in which to reserve resources .
5248 String zone = "us-central1-a" ;
5349 // Name of the reservation to be created.
5450 String reservationName = "YOUR_RESERVATION_NAME" ;
5551 // The URI of the global instance template to be used for creating the reservation.
5652 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" );
5854 // Number of instances for which capacity needs to be reserved.
5955 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 );
6456
65- creator . createSharedReservation (projectId , zone , reservationName , instanceTemplateUri , vmCount );
57+ createSharedReservation (projectId , zone , reservationName , instanceTemplateUri , vmCount );
6658 }
6759
6860 // Creates a shared reservation with the given name in the given zone.
69- public void createSharedReservation (
70- String projectId , String zone ,
71- String reservationName , 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 ();
7378
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 ();
8391
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 ();
9798
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 );
101101
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 ();
105106 }
106- System .out .println ("Reservation created. Operation Status: " + response .getStatus ());
107107 }
108108}
109109// [END compute_reservation_create_shared]
0 commit comments