Skip to content

Commit 4af7c89

Browse files
Fixed code
1 parent 9f14195 commit 4af7c89

File tree

4 files changed

+117
-83
lines changed

4 files changed

+117
-83
lines changed

compute/cloud-client/src/main/java/compute/reservation/ConsumeAnyMatchingReservation.java

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@
2020

2121
import static com.google.cloud.compute.v1.ReservationAffinity.ConsumeReservationType.ANY_RESERVATION;
2222

23+
import com.google.api.gax.longrunning.OperationFuture;
2324
import com.google.cloud.compute.v1.AllocationSpecificSKUAllocationReservedInstanceProperties;
2425
import com.google.cloud.compute.v1.AllocationSpecificSKUReservation;
2526
import com.google.cloud.compute.v1.AttachedDisk;
2627
import com.google.cloud.compute.v1.AttachedDiskInitializeParams;
28+
import com.google.cloud.compute.v1.InsertInstanceRequest;
2729
import com.google.cloud.compute.v1.Instance;
2830
import com.google.cloud.compute.v1.InstancesClient;
2931
import com.google.cloud.compute.v1.NetworkInterface;
@@ -51,19 +53,19 @@ public static void main(String[] args)
5153
String instanceName = "YOUR_INSTANCE_NAME";
5254
// Number of the instances.
5355
int numberOfVms = 2;
54-
// Machine type of the instances.
55-
String machineType = "n2-standard-32";
56-
// Minimum CPU platform of the instances.
57-
String minCpuPlatform = "Intel Cascade Lake";
5856

59-
createReservation(projectId, reservationName, numberOfVms, zone, machineType, minCpuPlatform);
60-
createInstance(projectId, zone, instanceName, machineType, minCpuPlatform);
57+
createReservation(projectId, reservationName, numberOfVms, zone);
58+
createInstance(projectId, zone, instanceName);
6159
}
6260

6361
// Creates reservation with properties that match the VM properties.
6462
public static void createReservation(String projectId, String reservationName,
65-
int numberOfVms, String zone, String machineType, String minCpuPlatform)
63+
int numberOfVms, String zone)
6664
throws IOException, ExecutionException, InterruptedException, TimeoutException {
65+
// Minimum CPU platform of the instances.
66+
String minCpuPlatform = "Intel Cascade Lake";
67+
// Machine type of the instances.
68+
String machineType = "n2-standard-32";
6769
// Initialize client that will be used to send requests. This client only needs to be created
6870
// once, and can be reused for multiple requests.
6971
try (ReservationsClient reservationsClient = ReservationsClient.create()) {
@@ -99,18 +101,23 @@ public static void createReservation(String projectId, String reservationName,
99101
// Create a new instance with the provided "instanceName" value in the specified project and zone.
100102
// In this consumption model, existing and new VMs automatically consume a reservation
101103
// if their properties match the VM properties specified in the reservation.
102-
public static void createInstance(String project, String zone, String instanceName,
103-
String machineType, String minCpuPlatform)
104+
public static void createInstance(String projectId, String zone, String instanceName)
104105
throws IOException, InterruptedException, ExecutionException, TimeoutException {
105106
// Below are sample values that can be replaced.
107+
// machineType: machine type of the VM being created.
108+
// * This value uses the format zones/{zone}/machineTypes/{type_name}.
109+
// * For a list of machine types, see https://cloud.google.com/compute/docs/machine-types
106110
// sourceImage: path to the operating system image to mount.
107111
// * For details about images you can mount, see https://cloud.google.com/compute/docs/images
108112
// diskSizeGb: storage size of the boot disk to attach to the instance.
109113
// networkName: network interface to associate with the instance.
114+
// Minimum CPU platform of the instances.
115+
String machineType = String.format("zones/%s/machineTypes/n2-standard-32", zone);
110116
String sourceImage = String
111117
.format("projects/debian-cloud/global/images/family/%s", "debian-11");
112118
long diskSizeGb = 10L;
113119
String networkName = "default";
120+
String minCpuPlatform = "Intel Cascade Lake";
114121

115122
// Initialize client that will be used to send requests. This client only needs to be created
116123
// once, and can be reused for multiple requests.
@@ -134,30 +141,40 @@ public static void createInstance(String project, String zone, String instanceNa
134141
.setName(networkName)
135142
.build();
136143

144+
// Set Reservation Affinity to "ANY"
145+
ReservationAffinity reservationAffinity =
146+
ReservationAffinity.newBuilder()
147+
.setConsumeReservationType(ANY_RESERVATION.toString())
148+
.build();
149+
137150
// Bind `instanceName`, `machineType`, `disk`, and `networkInterface` to an instance.
138151
Instance instanceResource =
139152
Instance.newBuilder()
140153
.setName(instanceName)
141-
.setMachineType(String.format("zones/%s/machineTypes/%s", zone, machineType))
154+
.setMachineType(machineType)
142155
.addDisks(disk)
143156
.addNetworkInterfaces(networkInterface)
144157
.setMinCpuPlatform(minCpuPlatform)
145-
// Set Reservation Affinity to "ANY"
146-
.setReservationAffinity(
147-
ReservationAffinity.newBuilder()
148-
.setConsumeReservationType(
149-
ANY_RESERVATION.toString())
150-
.build())
158+
.setReservationAffinity(reservationAffinity)
151159
.build();
152160

153161
System.out.printf("Creating instance: %s at %s %n", instanceName, zone);
154162

163+
// Insert the instance in the specified project and zone.
164+
InsertInstanceRequest insertInstanceRequest = InsertInstanceRequest.newBuilder()
165+
.setProject(projectId)
166+
.setZone(zone)
167+
.setInstanceResource(instanceResource)
168+
.build();
169+
170+
OperationFuture<Operation, Operation> operation = instancesClient.insertAsync(
171+
insertInstanceRequest);
172+
155173
// Wait for the operation to complete.
156-
Operation response = instancesClient.insertAsync(project, zone, instanceResource)
157-
.get(3, TimeUnit.MINUTES);
174+
Operation response = operation.get(3, TimeUnit.MINUTES);
175+
158176
if (response.hasError()) {
159177
System.out.println("Instance creation failed ! ! " + response);
160-
return;
161178
}
162179
System.out.println("Operation Status: " + response.getStatus());
163180
}

compute/cloud-client/src/main/java/compute/reservation/ConsumeSingleProjectReservation.java

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@
2020

2121
import static com.google.cloud.compute.v1.ReservationAffinity.ConsumeReservationType.SPECIFIC_RESERVATION;
2222

23+
import com.google.api.gax.longrunning.OperationFuture;
2324
import com.google.cloud.compute.v1.AllocationSpecificSKUAllocationReservedInstanceProperties;
2425
import com.google.cloud.compute.v1.AllocationSpecificSKUReservation;
2526
import com.google.cloud.compute.v1.AttachedDisk;
2627
import com.google.cloud.compute.v1.AttachedDiskInitializeParams;
28+
import com.google.cloud.compute.v1.InsertInstanceRequest;
2729
import com.google.cloud.compute.v1.Instance;
2830
import com.google.cloud.compute.v1.InstancesClient;
2931
import com.google.cloud.compute.v1.NetworkInterface;
@@ -50,22 +52,21 @@ public static void main(String[] args)
5052
String instanceName = "YOUR_INSTANCE_NAME";
5153
// Number of the instances.
5254
int numberOfVms = 10;
53-
// Machine type of the instances.
54-
String machineType = "n2-standard-32";
55-
// Minimum CPU platform of the instances.
56-
String minCpuPlatform = "Intel Cascade Lake";
57-
boolean specificReservationRequired = true;
5855

59-
createReservation(projectId, reservationName, numberOfVms,
60-
zone, machineType, minCpuPlatform, specificReservationRequired);
61-
createInstance(projectId, zone, instanceName, machineType, minCpuPlatform, reservationName);
56+
createReservation(projectId, reservationName, numberOfVms, zone);
57+
createInstance(projectId, zone, instanceName, reservationName);
6258
}
6359

6460
// Creates reservation with the given parameters.
6561
public static void createReservation(
66-
String projectId, String reservationName, int numberOfVms, String zone,
67-
String machineType, String minCpuPlatform, boolean specificReservationRequired)
62+
String projectId, String reservationName, int numberOfVms, String zone)
6863
throws IOException, ExecutionException, InterruptedException, TimeoutException {
64+
// Machine type of the instances.
65+
String machineType = "n2-standard-32";
66+
// Minimum CPU platform of the instances.
67+
String minCpuPlatform = "Intel Cascade Lake";
68+
boolean specificReservationRequired = true;
69+
6970
// Initialize client that will be used to send requests. This client only needs to be created
7071
// once, and can be reused for multiple requests.
7172
try (ReservationsClient reservationsClient = ReservationsClient.create()) {
@@ -99,18 +100,22 @@ public static void createReservation(
99100
}
100101

101102
// Create a new instance with the provided "instanceName" value in the specified project and zone.
102-
public static void createInstance(String project, String zone,
103-
String instanceName, String machineType, String minCpuPlatform, String reservationName)
103+
public static void createInstance(
104+
String projectId, String zone, String instanceName, String reservationName)
104105
throws IOException, InterruptedException, ExecutionException, TimeoutException {
105106
// Below are sample values that can be replaced.
106107
// sourceImage: path to the operating system image to mount.
107108
// * For details about images you can mount, see https://cloud.google.com/compute/docs/images
108109
// diskSizeGb: storage size of the boot disk to attach to the instance.
109110
// networkName: network interface to associate with the instance.
111+
// Machine type of the instances.
112+
// Minimum CPU platform of the instances.
110113
String sourceImage = String
111114
.format("projects/debian-cloud/global/images/family/%s", "debian-11");
112115
long diskSizeGb = 10L;
113116
String networkName = "default";
117+
String machineType = String.format("zones/%s/machineTypes/n2-standard-32", zone);
118+
String minCpuPlatform = "Intel Cascade Lake";
114119

115120
// Initialize client that will be used to send requests. This client only needs to be created
116121
// once, and can be reused for multiple requests.
@@ -134,34 +139,43 @@ public static void createInstance(String project, String zone,
134139
.setName(networkName)
135140
.build();
136141

142+
// Set Reservation Affinity
143+
ReservationAffinity reservationAffinity =
144+
ReservationAffinity.newBuilder()
145+
.setConsumeReservationType(SPECIFIC_RESERVATION.toString())
146+
.setKey("compute.googleapis.com/reservation-name")
147+
// Set specific reservation
148+
.addValues(reservationName)
149+
.build();
150+
137151
// Bind `instanceName`, `machineType`, `disk`, and `networkInterface` to an instance.
138152
Instance instanceResource =
139153
Instance.newBuilder()
140154
.setName(instanceName)
141-
.setMachineType(String.format("zones/%s/machineTypes/%s", zone, machineType))
155+
.setMachineType(machineType)
142156
.addDisks(disk)
143157
.addNetworkInterfaces(networkInterface)
144158
.setMinCpuPlatform(minCpuPlatform)
145-
// Set Reservation Affinity
146-
.setReservationAffinity(
147-
ReservationAffinity.newBuilder()
148-
.setConsumeReservationType(
149-
SPECIFIC_RESERVATION.name())
150-
.setKey("compute.googleapis.com/reservation-name")
151-
// Set specific reservation
152-
.addValues(reservationName)
153-
.build())
159+
.setReservationAffinity(reservationAffinity)
154160
.build();
155161

156162
System.out.printf("Creating instance: %s at %s %n", instanceName, zone);
157163

164+
// Insert the instance in the specified project and zone.
165+
InsertInstanceRequest insertInstanceRequest = InsertInstanceRequest.newBuilder()
166+
.setProject(projectId)
167+
.setZone(zone)
168+
.setInstanceResource(instanceResource)
169+
.build();
170+
171+
OperationFuture<Operation, Operation> operation = instancesClient.insertAsync(
172+
insertInstanceRequest);
173+
158174
// Wait for the operation to complete.
159-
Operation response = instancesClient.insertAsync(project, zone, instanceResource)
160-
.get(3, TimeUnit.MINUTES);
175+
Operation response = operation.get(3, TimeUnit.MINUTES);
161176

162177
if (response.hasError()) {
163178
System.out.println("Instance creation failed ! ! " + response);
164-
return;
165179
}
166180
System.out.println("Operation Status: " + response.getStatus());
167181
}

compute/cloud-client/src/main/java/compute/reservation/ConsumeSpecificSharedReservation.java

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import static com.google.cloud.compute.v1.ReservationAffinity.ConsumeReservationType.SPECIFIC_RESERVATION;
2222

23+
import com.google.api.gax.longrunning.OperationFuture;
2324
import com.google.cloud.compute.v1.AllocationSpecificSKUAllocationReservedInstanceProperties;
2425
import com.google.cloud.compute.v1.AllocationSpecificSKUReservation;
2526
import com.google.cloud.compute.v1.AttachedDisk;
@@ -51,22 +52,21 @@ public static void main(String[] args)
5152
String instanceName = "YOUR_INSTANCE_NAME";
5253
// Number of instances.
5354
int numberOfVms = 3;
54-
// Machine type of the instances.
55-
String machineType = "n2-standard-32";
56-
// Minimum CPU platform of the instances.
57-
String minCpuPlatform = "Intel Cascade Lake";
58-
boolean specificReservationRequired = true;
5955

60-
createReservation(projectId, reservationName, numberOfVms,
61-
zone, machineType, minCpuPlatform, specificReservationRequired);
62-
createInstance(projectId, zone, instanceName, machineType, minCpuPlatform, reservationName);
56+
createReservation(projectId, reservationName, numberOfVms, zone);
57+
createInstance(projectId, zone, instanceName, reservationName);
6358
}
6459

6560
// Creates reservation with the given parameters.
6661
public static void createReservation(
67-
String projectId, String reservationName, int numberOfVms, String zone,
68-
String machineType, String minCpuPlatform, boolean specificReservationRequired)
62+
String projectId, String reservationName, int numberOfVms, String zone)
6963
throws IOException, ExecutionException, InterruptedException, TimeoutException {
64+
// Machine type of the instances.
65+
String machineType = "n2-standard-32";
66+
// Minimum CPU platform of the instances.
67+
String minCpuPlatform = "Intel Cascade Lake";
68+
boolean specificReservationRequired = true;
69+
7070
// Initialize client that will be used to send requests. This client only needs to be created
7171
// once, and can be reused for multiple requests.
7272
try (ReservationsClient reservationsClient = ReservationsClient.create()) {
@@ -101,18 +101,22 @@ public static void createReservation(
101101

102102
// Create a virtual machine targeted with the reserveAffinity field.
103103
// Ensure that the VM's properties match the reservation's VM properties.
104-
public static void createInstance(String projectId, String zone, String instanceName,
105-
String machineType, String minCpuPlatform, String reservationName)
104+
public static void createInstance(
105+
String projectId, String zone, String instanceName, String reservationName)
106106
throws IOException, InterruptedException, ExecutionException, TimeoutException {
107107
// Below are sample values that can be replaced.
108108
// sourceImage: path to the operating system image to mount.
109109
// * For details about images you can mount, see https://cloud.google.com/compute/docs/images
110110
// diskSizeGb: storage size of the boot disk to attach to the instance.
111111
// networkName: network interface to associate with the instance.
112+
// Machine type of the instances.
113+
// Minimum CPU platform of the instances.
112114
String sourceImage = String
113115
.format("projects/debian-cloud/global/images/family/%s", "debian-11");
114116
long diskSizeGb = 10L;
115117
String networkName = "default";
118+
String machineType = String.format("zones/%s/machineTypes/n2-standard-32", zone);
119+
String minCpuPlatform = "Intel Cascade Lake";
116120
// To consume this reservation from any consumer projects that this reservation is shared with,
117121
// you must also specify the owner project of the reservation - the path to the reservation.
118122
String reservationPath =
@@ -140,41 +144,43 @@ public static void createInstance(String projectId, String zone, String instance
140144
.setName(networkName)
141145
.build();
142146

147+
// Set Reservation Affinity
148+
ReservationAffinity reservationAffinity =
149+
ReservationAffinity.newBuilder()
150+
.setConsumeReservationType(SPECIFIC_RESERVATION.toString())
151+
.setKey("compute.googleapis.com/reservation-name")
152+
// Set specific reservation
153+
.addValues(reservationPath)
154+
.build();
155+
143156
// Bind `instanceName`, `machineType`, `disk`, and `networkInterface` to an instance.
144157
Instance instanceResource =
145158
Instance.newBuilder()
146159
.setName(instanceName)
147-
.setMachineType(String.format("zones/%s/machineTypes/%s", zone, machineType))
160+
.setMachineType(machineType)
148161
.addDisks(disk)
149162
.addNetworkInterfaces(networkInterface)
150163
.setMinCpuPlatform(minCpuPlatform)
151-
// Set Reservation Affinity
152-
.setReservationAffinity(
153-
ReservationAffinity.newBuilder()
154-
.setConsumeReservationType(
155-
SPECIFIC_RESERVATION.name())
156-
.setKey("compute.googleapis.com/reservation-name")
157-
// Set specific reservation
158-
.addValues(reservationPath)
159-
.build())
164+
.setReservationAffinity(reservationAffinity)
160165
.build();
161166

162167
System.out.printf("Creating instance: %s at %s %n", instanceName, zone);
163168

164-
// Insert the instance in the specified projectId and zone.
169+
// Insert the instance in the specified project and zone.
165170
InsertInstanceRequest insertInstanceRequest = InsertInstanceRequest.newBuilder()
166171
.setProject(projectId)
167172
.setZone(zone)
168173
.setInstanceResource(instanceResource)
169174
.build();
170175

176+
OperationFuture<Operation, Operation> operation = instancesClient.insertAsync(
177+
insertInstanceRequest);
178+
171179
// Wait for the operation to complete.
172-
Operation response = instancesClient.insertAsync(
173-
insertInstanceRequest).get(3, TimeUnit.MINUTES);
180+
Operation response = operation.get(3, TimeUnit.MINUTES);
174181

175182
if (response.hasError()) {
176183
System.out.println("Instance creation failed ! ! " + response);
177-
return;
178184
}
179185
System.out.println("Operation Status: " + response.getStatus());
180186
}

0 commit comments

Comments
 (0)