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_from_vm]
20+
21+ import com .google .cloud .compute .v1 .AcceleratorConfig ;
22+ import com .google .cloud .compute .v1 .AllocationSpecificSKUAllocationAllocatedInstancePropertiesReservedDisk ;
23+ import com .google .cloud .compute .v1 .AllocationSpecificSKUAllocationReservedInstanceProperties ;
24+ import com .google .cloud .compute .v1 .AllocationSpecificSKUReservation ;
25+ import com .google .cloud .compute .v1 .AttachedDisk ;
26+ import com .google .cloud .compute .v1 .InsertReservationRequest ;
27+ import com .google .cloud .compute .v1 .Instance ;
28+ import com .google .cloud .compute .v1 .InstancesClient ;
29+ import com .google .cloud .compute .v1 .Operation ;
30+ import com .google .cloud .compute .v1 .Reservation ;
31+ import com .google .cloud .compute .v1 .ReservationsClient ;
32+ import java .io .IOException ;
33+ import java .util .ArrayList ;
34+ import java .util .List ;
35+ import java .util .concurrent .ExecutionException ;
36+ import java .util .concurrent .TimeUnit ;
37+ import java .util .concurrent .TimeoutException ;
38+
39+ public class CreateReservationFromVm {
40+
41+ public static void main (String [] args )
42+ throws IOException , ExecutionException , InterruptedException , TimeoutException {
43+ // TODO(developer): Replace these variables before running the sample.
44+ // Project ID or project number of the Cloud project you want to use.
45+ String project = "YOUR_PROJECT_ID" ;
46+ // The zone of the VM. In this zone the reservation will be created.
47+ String zone = "us-central1-a" ;
48+ // The name of the reservation to create.
49+ String reservationName = "YOUR_RESERVATION_NAME" ;
50+ // The name of the VM to create the reservation from.
51+ String vmName = "YOUR_VM_NAME" ;
52+
53+ createComputeReservationFromVm (project , zone , reservationName , vmName );
54+ }
55+
56+ // Creates a compute reservation from an existing VM.
57+ public static void createComputeReservationFromVm (
58+ String project , String zone , String reservationName , String vmName )
59+ throws IOException , ExecutionException , InterruptedException , TimeoutException {
60+ // Initialize client that will be used to send requests. This client only needs to be created
61+ // once, and can be reused for multiple requests.
62+ try (InstancesClient instancesClient = InstancesClient .create ();
63+ ReservationsClient reservationsClient = ReservationsClient .create ()) {
64+ Instance existingVm = instancesClient .get (project , zone , vmName );
65+
66+ // Extract properties from the existing VM
67+ List <AcceleratorConfig > guestAccelerators = new ArrayList <>();
68+ if (!existingVm .getGuestAcceleratorsList ().isEmpty ()) {
69+ for (AcceleratorConfig a : existingVm .getGuestAcceleratorsList ()) {
70+ guestAccelerators .add (
71+ AcceleratorConfig .newBuilder ()
72+ .setAcceleratorCount (a .getAcceleratorCount ())
73+ .setAcceleratorType (a .getAcceleratorType ()
74+ .substring (a .getAcceleratorType ().lastIndexOf ('/' ) + 1 ))
75+ .build ());
76+ }
77+ }
78+
79+ List <AllocationSpecificSKUAllocationAllocatedInstancePropertiesReservedDisk > localSsds =
80+ new ArrayList <>();
81+ if (!existingVm .getDisksList ().isEmpty ()) {
82+ for (AttachedDisk disk : existingVm .getDisksList ()) {
83+ if (disk .getDiskSizeGb () >= 375 ) {
84+ localSsds .add (
85+ AllocationSpecificSKUAllocationAllocatedInstancePropertiesReservedDisk .newBuilder ()
86+ .setDiskSizeGb (disk .getDiskSizeGb ())
87+ .setInterface (disk .getInterface ())
88+ .build ());
89+ }
90+ }
91+ }
92+
93+ AllocationSpecificSKUAllocationReservedInstanceProperties instanceProperties =
94+ AllocationSpecificSKUAllocationReservedInstanceProperties .newBuilder ()
95+ .setMachineType (
96+ existingVm .getMachineType ()
97+ .substring (existingVm .getMachineType ().lastIndexOf ('/' ) + 1 ))
98+ .setMinCpuPlatform (existingVm .getMinCpuPlatform ())
99+ .addAllLocalSsds (localSsds )
100+ .addAllGuestAccelerators (guestAccelerators )
101+ .build ();
102+
103+ Reservation reservation =
104+ Reservation .newBuilder ()
105+ .setName (reservationName )
106+ .setSpecificReservation (
107+ AllocationSpecificSKUReservation .newBuilder ()
108+ .setCount (3 )
109+ .setInstanceProperties (instanceProperties )
110+ .build ())
111+ .setSpecificReservationRequired (true )
112+ .build ();
113+
114+ InsertReservationRequest insertReservationRequest =
115+ InsertReservationRequest .newBuilder ()
116+ .setProject (project )
117+ .setZone (zone )
118+ .setReservationResource (reservation )
119+ .build ();
120+
121+ Operation response = reservationsClient
122+ .insertAsync (insertReservationRequest ).get (3 , TimeUnit .MINUTES );
123+
124+ if (response .hasError ()) {
125+ System .out .println ("Reservation creation failed ! ! " + response );
126+ return ;
127+ }
128+ System .out .println ("Operation completed successfully." );
129+ }
130+ }
131+ }
132+ // [END compute_reservation_create_from_vm]
0 commit comments