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_consume_specific_shared_reservation]
20+ import static com .google .cloud .compute .v1 .ReservationAffinity .ConsumeReservationType .SPECIFIC_RESERVATION ;
21+
22+ import com .google .api .gax .longrunning .OperationFuture ;
23+ import com .google .cloud .compute .v1 .AttachedDisk ;
24+ import com .google .cloud .compute .v1 .AttachedDiskInitializeParams ;
25+ import com .google .cloud .compute .v1 .InsertInstanceRequest ;
26+ import com .google .cloud .compute .v1 .Instance ;
27+ import com .google .cloud .compute .v1 .InstancesClient ;
28+ import com .google .cloud .compute .v1 .NetworkInterface ;
29+ import com .google .cloud .compute .v1 .Operation ;
30+ import com .google .cloud .compute .v1 .ReservationAffinity ;
31+ import java .io .IOException ;
32+ import java .util .concurrent .ExecutionException ;
33+ import java .util .concurrent .TimeUnit ;
34+ import java .util .concurrent .TimeoutException ;
35+
36+ public class ConsumeSpecificSharedReservation {
37+ public static void main (String [] args )
38+ throws IOException , ExecutionException , InterruptedException , TimeoutException {
39+ // TODO(developer): Replace these variables before running the sample.
40+ // Project ID or project number of the Cloud project you want to use.
41+ String projectId = "YOUR_PROJECT_ID" ;
42+ // Name of the zone the reservation is located.
43+ String zone = "us-central1-a" ;
44+ // Name of the reservation you want to query.
45+ String reservationName = "YOUR_RESERVATION_NAME" ;
46+ // Name of the VM instance you want to query.
47+ String instanceName = "YOUR_INSTANCE_NAME" ;
48+ // machineType: machine type of the VM being created.
49+ // * For a list of machine types, see https://cloud.google.com/compute/docs/machine-types
50+ String machineTypeName = "n1-standard-4" ;
51+ // sourceImage: path to the operating system image to mount.
52+ // * For details about images you can mount, see https://cloud.google.com/compute/docs/images
53+ String sourceImage = "projects/debian-cloud/global/images/family/debian-11" ;
54+ // diskSizeGb: storage size of the boot disk to attach to the instance.
55+ long diskSizeGb = 10L ;
56+ // networkName: network interface to associate with the instance.
57+ String networkName = "default" ;
58+ // Minimum CPU platform of the instances.
59+ String minCpuPlatform = "Intel Skylake" ;
60+
61+ createInstanceAsync (projectId , zone , instanceName , reservationName , machineTypeName ,
62+ sourceImage , diskSizeGb , networkName , minCpuPlatform );
63+ }
64+
65+ // Create a virtual machine targeted with the reserveAffinity field.
66+ // Ensure that the VM's properties match the reservation's VM properties.
67+ public static Instance createInstanceAsync (String projectId , String zone , String instanceName ,
68+ String reservationName , String machineTypeName , String sourceImage , long diskSizeGb ,
69+ String networkName , String minCpuPlatform )
70+ throws IOException , InterruptedException , ExecutionException , TimeoutException {
71+ String machineType = String .format ("zones/%s/machineTypes/%s" , zone , machineTypeName );
72+ // To consume this reservation from any consumer projects that this reservation is shared with,
73+ // you must also specify the owner project of the reservation - the path to the reservation.
74+ String reservationPath =
75+ String .format ("projects/%s/reservations/%s" , projectId , reservationName );
76+ // Initialize client that will be used to send requests. This client only needs to be created
77+ // once, and can be reused for multiple requests.
78+ try (InstancesClient instancesClient = InstancesClient .create ()) {
79+ AttachedDisk disk =
80+ AttachedDisk .newBuilder ()
81+ .setBoot (true )
82+ .setAutoDelete (true )
83+ .setType (AttachedDisk .Type .PERSISTENT .toString ())
84+ .setDeviceName ("disk-1" )
85+ .setInitializeParams (
86+ AttachedDiskInitializeParams .newBuilder ()
87+ .setSourceImage (sourceImage )
88+ .setDiskSizeGb (diskSizeGb )
89+ .build ())
90+ .build ();
91+
92+ NetworkInterface networkInterface = NetworkInterface .newBuilder ()
93+ .setName (networkName )
94+ .build ();
95+
96+ ReservationAffinity reservationAffinity =
97+ ReservationAffinity .newBuilder ()
98+ .setConsumeReservationType (SPECIFIC_RESERVATION .toString ())
99+ .setKey ("compute.googleapis.com/reservation-name" )
100+ // Set specific reservation
101+ .addValues (reservationPath )
102+ .build ();
103+
104+ Instance instanceResource =
105+ Instance .newBuilder ()
106+ .setName (instanceName )
107+ .setMachineType (machineType )
108+ .addDisks (disk )
109+ .addNetworkInterfaces (networkInterface )
110+ .setMinCpuPlatform (minCpuPlatform )
111+ .setReservationAffinity (reservationAffinity )
112+ .build ();
113+
114+ InsertInstanceRequest insertInstanceRequest = InsertInstanceRequest .newBuilder ()
115+ .setProject (projectId )
116+ .setZone (zone )
117+ .setInstanceResource (instanceResource )
118+ .build ();
119+
120+ OperationFuture <Operation , Operation > operation = instancesClient .insertAsync (
121+ insertInstanceRequest );
122+ Operation response = operation .get (3 , TimeUnit .MINUTES );
123+
124+ if (response .hasError ()) {
125+ return null ;
126+ }
127+ return instancesClient .get (projectId , zone , instanceName );
128+ }
129+ }
130+ }
131+ // [END compute_consume_specific_shared_reservation]
0 commit comments