| 
 | 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_vms_update]  | 
 | 20 | + | 
 | 21 | +import com.google.cloud.compute.v1.Operation;  | 
 | 22 | +import com.google.cloud.compute.v1.ReservationsClient;  | 
 | 23 | +import com.google.cloud.compute.v1.ReservationsResizeRequest;  | 
 | 24 | +import com.google.cloud.compute.v1.ResizeReservationRequest;  | 
 | 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 UpdateVmsForReservation {  | 
 | 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 | +    // The zone where the reservation is located.  | 
 | 38 | +    String zone =  "us-central1-a";  | 
 | 39 | +    // Name of the reservation to update.  | 
 | 40 | +    String reservationName = "YOUR_RESERVATION_NAME";  | 
 | 41 | +    // Number of instances to update in the reservation.  | 
 | 42 | +    int numberOfVms = 3;  | 
 | 43 | + | 
 | 44 | +    updateVmsForReservation(projectId, zone, reservationName, numberOfVms);  | 
 | 45 | +  }  | 
 | 46 | + | 
 | 47 | +  // Updates a reservation with new VM capacity.  | 
 | 48 | +  public static void updateVmsForReservation(  | 
 | 49 | +      String projectId, String zone, String reservationName, int numberOfVms)  | 
 | 50 | +      throws IOException, ExecutionException, InterruptedException, TimeoutException {  | 
 | 51 | +    // Initialize client that will be used to send requests. This client only needs to be created  | 
 | 52 | +    // once, and can be reused for multiple requests.  | 
 | 53 | +    try (ReservationsClient reservationsClient = ReservationsClient.create()) {  | 
 | 54 | + | 
 | 55 | +      ResizeReservationRequest resizeReservationRequest =  | 
 | 56 | +          ResizeReservationRequest.newBuilder()  | 
 | 57 | +              .setProject(projectId)  | 
 | 58 | +              .setZone(zone)  | 
 | 59 | +              .setReservation(reservationName)  | 
 | 60 | +              .setReservationsResizeRequestResource(ReservationsResizeRequest.newBuilder()  | 
 | 61 | +                  .setSpecificSkuCount(numberOfVms)  | 
 | 62 | +                  .build())  | 
 | 63 | +              .build();  | 
 | 64 | + | 
 | 65 | +      Operation response = reservationsClient.resizeAsync(resizeReservationRequest)  | 
 | 66 | +          .get(3, TimeUnit.MINUTES);  | 
 | 67 | + | 
 | 68 | +      if (response.hasError()) {  | 
 | 69 | +        System.out.println("Reservation update failed !!" + response);  | 
 | 70 | +        return;  | 
 | 71 | +      }  | 
 | 72 | +      System.out.println("Reservation updated successfully: " + response.getStatus());  | 
 | 73 | +    }  | 
 | 74 | +  }  | 
 | 75 | +}  | 
 | 76 | +// [END compute_reservation_vms_update]  | 
0 commit comments