Skip to content

Commit dfeb198

Browse files
Fixed code following recommendations
1 parent a2eee20 commit dfeb198

File tree

6 files changed

+141
-347
lines changed

6 files changed

+141
-347
lines changed

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

Lines changed: 19 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,17 @@
1717
package compute.reservation;
1818

1919
// [START compute_consume_any_matching_reservation]
20-
2120
import static com.google.cloud.compute.v1.ReservationAffinity.ConsumeReservationType.ANY_RESERVATION;
2221

2322
import com.google.api.gax.longrunning.OperationFuture;
24-
import com.google.cloud.compute.v1.AllocationSpecificSKUAllocationReservedInstanceProperties;
25-
import com.google.cloud.compute.v1.AllocationSpecificSKUReservation;
2623
import com.google.cloud.compute.v1.AttachedDisk;
2724
import com.google.cloud.compute.v1.AttachedDiskInitializeParams;
2825
import com.google.cloud.compute.v1.InsertInstanceRequest;
2926
import com.google.cloud.compute.v1.Instance;
3027
import com.google.cloud.compute.v1.InstancesClient;
3128
import com.google.cloud.compute.v1.NetworkInterface;
3229
import com.google.cloud.compute.v1.Operation;
33-
import com.google.cloud.compute.v1.Reservation;
3430
import com.google.cloud.compute.v1.ReservationAffinity;
35-
import com.google.cloud.compute.v1.ReservationsClient;
3631
import java.io.IOException;
3732
import java.util.concurrent.ExecutionException;
3833
import java.util.concurrent.TimeUnit;
@@ -45,85 +40,38 @@ public static void main(String[] args)
4540
// TODO(developer): Replace these variables before running the sample.
4641
// Project ID or project number of the Cloud project you want to use.
4742
String projectId = "YOUR_PROJECT_ID";
48-
// Name of the zone where the reservation is located.
43+
// Zone where the VM instance will be created.
4944
String zone = "us-central1-a";
50-
// Name of the reservation you want to query.
51-
String reservationName = "YOUR_RESERVATION_NAME";
5245
// Name of the VM instance you want to query.
5346
String instanceName = "YOUR_INSTANCE_NAME";
54-
// Number of the instances.
55-
int numberOfVms = 2;
56-
57-
createReservation(projectId, reservationName, numberOfVms, zone);
58-
createInstance(projectId, zone, instanceName);
59-
}
60-
61-
// Creates reservation with properties that match the VM properties.
62-
public static void createReservation(String projectId, String reservationName,
63-
int numberOfVms, String zone)
64-
throws IOException, ExecutionException, InterruptedException, TimeoutException {
65-
// Minimum CPU platform of the instances.
66-
String minCpuPlatform = "Intel Skylake";
67-
// Machine type of the instances.
68-
String machineType = "n1-standard-4";
69-
70-
// Initialize client that will be used to send requests. This client only needs to be created
71-
// once, and can be reused for multiple requests.
72-
try (ReservationsClient reservationsClient = ReservationsClient.create()) {
73-
74-
Reservation reservation =
75-
Reservation.newBuilder()
76-
.setName(reservationName)
77-
.setZone(zone)
78-
.setSpecificReservation(
79-
AllocationSpecificSKUReservation.newBuilder()
80-
.setCount(numberOfVms)
81-
.setInstanceProperties(
82-
AllocationSpecificSKUAllocationReservedInstanceProperties.newBuilder()
83-
.setMachineType(machineType)
84-
.setMinCpuPlatform(minCpuPlatform)
85-
.build())
86-
.build())
87-
.build();
88-
89-
// Wait for the create reservation operation to complete.
90-
Operation response =
91-
reservationsClient.insertAsync(projectId, zone, reservation)
92-
.get(3, TimeUnit.MINUTES);
93-
94-
if (response.hasError()) {
95-
System.out.println("Reservation creation failed!" + response);
96-
return;
97-
}
98-
System.out.println("Reservation created. Operation Status: " + response.getStatus());
99-
}
100-
}
101-
102-
// Create a new instance with the provided "instanceName" value in the specified project and zone.
103-
// In this consumption model, existing and new VMs automatically consume a reservation
104-
// if their properties match the VM properties specified in the reservation.
105-
public static void createInstance(String projectId, String zone, String instanceName)
106-
throws IOException, InterruptedException, ExecutionException, TimeoutException {
107-
// Below are sample values that can be replaced.
10847
// machineType: machine type of the VM being created.
109-
// * This value uses the format zones/{zone}/machineTypes/{type_name}.
11048
// * For a list of machine types, see https://cloud.google.com/compute/docs/machine-types
49+
String machineTypeName = "n1-standard-4";
11150
// sourceImage: path to the operating system image to mount.
11251
// * For details about images you can mount, see https://cloud.google.com/compute/docs/images
52+
String sourceImage = "projects/debian-cloud/global/images/family/debian-11";
11353
// diskSizeGb: storage size of the boot disk to attach to the instance.
114-
// networkName: network interface to associate with the instance.
115-
// Minimum CPU platform of the instances.
116-
String machineType = String.format("zones/%s/machineTypes/n1-standard-4", zone);
117-
String sourceImage = String
118-
.format("projects/debian-cloud/global/images/family/%s", "debian-11");
11954
long diskSizeGb = 10L;
55+
// networkName: network interface to associate with the instance.
12056
String networkName = "default";
57+
// Minimum CPU platform of the instances.
12158
String minCpuPlatform = "Intel Skylake";
12259

60+
createInstance(projectId, zone, instanceName, machineTypeName, sourceImage,
61+
diskSizeGb, networkName, minCpuPlatform);
62+
}
63+
64+
// Create a virtual machine targeted with the reserveAffinity field.
65+
// In this consumption model, existing and new VMs automatically consume a reservation
66+
// if their properties match the VM properties specified in the reservation.
67+
public static Operation createInstance(String projectId, String zone,
68+
String instanceName, String machineTypeName, String sourceImage,
69+
long diskSizeGb, String networkName, String minCpuPlatform)
70+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
71+
String machineType = String.format("zones/%s/machineTypes/%s", zone, machineTypeName);
12372
// Initialize client that will be used to send requests. This client only needs to be created
12473
// once, and can be reused for multiple requests.
12574
try (InstancesClient instancesClient = InstancesClient.create()) {
126-
// Instance creation requires at least one persistent disk and one network interface.
12775
AttachedDisk disk =
12876
AttachedDisk.newBuilder()
12977
.setBoot(true)
@@ -137,18 +85,15 @@ public static void createInstance(String projectId, String zone, String instance
13785
.build())
13886
.build();
13987

140-
// Use the network interface provided in the networkName argument.
14188
NetworkInterface networkInterface = NetworkInterface.newBuilder()
14289
.setName(networkName)
14390
.build();
14491

145-
// Set Reservation Affinity
14692
ReservationAffinity reservationAffinity =
14793
ReservationAffinity.newBuilder()
14894
.setConsumeReservationType(ANY_RESERVATION.toString())
14995
.build();
15096

151-
// Bind `instanceName`, `machineType`, `disk`, and `networkInterface` to an instance.
15297
Instance instanceResource =
15398
Instance.newBuilder()
15499
.setName(instanceName)
@@ -159,9 +104,6 @@ public static void createInstance(String projectId, String zone, String instance
159104
.setReservationAffinity(reservationAffinity)
160105
.build();
161106

162-
System.out.printf("Creating instance: %s at %s %n", instanceName, zone);
163-
164-
// Insert the instance in the specified project and zone.
165107
InsertInstanceRequest insertInstanceRequest = InsertInstanceRequest.newBuilder()
166108
.setProject(projectId)
167109
.setZone(zone)
@@ -171,13 +113,12 @@ public static void createInstance(String projectId, String zone, String instance
171113
OperationFuture<Operation, Operation> operation = instancesClient.insertAsync(
172114
insertInstanceRequest);
173115

174-
// Wait for the operation to complete.
175116
Operation response = operation.get(3, TimeUnit.MINUTES);
176117

177118
if (response.hasError()) {
178-
System.out.println("Instance creation failed ! ! " + response);
119+
return null;
179120
}
180-
System.out.println("Operation Status: " + response.getStatus());
121+
return response;
181122
}
182123
}
183124
}

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

Lines changed: 19 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,17 @@
1717
package compute.reservation;
1818

1919
// [START compute_consume_single_project_reservation]
20-
2120
import static com.google.cloud.compute.v1.ReservationAffinity.ConsumeReservationType.SPECIFIC_RESERVATION;
2221

2322
import com.google.api.gax.longrunning.OperationFuture;
24-
import com.google.cloud.compute.v1.AllocationSpecificSKUAllocationReservedInstanceProperties;
25-
import com.google.cloud.compute.v1.AllocationSpecificSKUReservation;
2623
import com.google.cloud.compute.v1.AttachedDisk;
2724
import com.google.cloud.compute.v1.AttachedDiskInitializeParams;
2825
import com.google.cloud.compute.v1.InsertInstanceRequest;
2926
import com.google.cloud.compute.v1.Instance;
3027
import com.google.cloud.compute.v1.InstancesClient;
3128
import com.google.cloud.compute.v1.NetworkInterface;
3229
import com.google.cloud.compute.v1.Operation;
33-
import com.google.cloud.compute.v1.Reservation;
3430
import com.google.cloud.compute.v1.ReservationAffinity;
35-
import com.google.cloud.compute.v1.ReservationsClient;
3631
import java.io.IOException;
3732
import java.util.concurrent.ExecutionException;
3833
import java.util.concurrent.TimeUnit;
@@ -50,78 +45,33 @@ public static void main(String[] args)
5045
String reservationName = "YOUR_RESERVATION_NAME";
5146
// Name of the VM instance you want to query.
5247
String instanceName = "YOUR_INSTANCE_NAME";
53-
// Number of the instances.
54-
int numberOfVms = 2;
55-
56-
createReservation(projectId, reservationName, numberOfVms, zone);
57-
createInstance(projectId, zone, instanceName, reservationName);
58-
}
59-
60-
// Creates reservation with the given parameters.
61-
public static void createReservation(
62-
String projectId, String reservationName, int numberOfVms, String zone)
63-
throws IOException, ExecutionException, InterruptedException, TimeoutException {
64-
// Machine type of the instances.
65-
String machineType = "n1-standard-4";
66-
// Minimum CPU platform of the instances.
67-
String minCpuPlatform = "Intel Skylake";
68-
boolean specificReservationRequired = true;
69-
70-
// Initialize client that will be used to send requests. This client only needs to be created
71-
// once, and can be reused for multiple requests.
72-
try (ReservationsClient reservationsClient = ReservationsClient.create()) {
73-
74-
Reservation reservation =
75-
Reservation.newBuilder()
76-
.setName(reservationName)
77-
.setZone(zone)
78-
.setSpecificReservationRequired(specificReservationRequired)
79-
.setSpecificReservation(
80-
AllocationSpecificSKUReservation.newBuilder()
81-
.setCount(numberOfVms)
82-
.setInstanceProperties(
83-
AllocationSpecificSKUAllocationReservedInstanceProperties.newBuilder()
84-
.setMachineType(machineType)
85-
.setMinCpuPlatform(minCpuPlatform)
86-
.build())
87-
.build())
88-
.build();
89-
90-
// Wait for the create reservation operation to complete.
91-
Operation response =
92-
reservationsClient.insertAsync(projectId, zone, reservation).get(3, TimeUnit.MINUTES);
93-
94-
if (response.hasError()) {
95-
System.out.println("Reservation creation failed!" + response);
96-
return;
97-
}
98-
System.out.println("Reservation created. Operation Status: " + response.getStatus());
99-
}
100-
}
101-
102-
// Create a new instance with the provided "instanceName" value in the specified project and zone.
103-
// Ensure that the VM's properties match the reservation's VM properties.
104-
public static void createInstance(
105-
String projectId, String zone, String instanceName, String reservationName)
106-
throws IOException, InterruptedException, ExecutionException, TimeoutException {
107-
// Below are sample values that can be replaced.
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";
10851
// sourceImage: path to the operating system image to mount.
10952
// * 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";
11054
// diskSizeGb: storage size of the boot disk to attach to the instance.
111-
// networkName: network interface to associate with the instance.
112-
// Machine type of the instances.
113-
// Minimum CPU platform of the instances.
114-
String sourceImage = String
115-
.format("projects/debian-cloud/global/images/family/%s", "debian-11");
11655
long diskSizeGb = 10L;
56+
// networkName: network interface to associate with the instance.
11757
String networkName = "default";
118-
String machineType = String.format("zones/%s/machineTypes/n1-standard-4", zone);
58+
// Minimum CPU platform of the instances.
11959
String minCpuPlatform = "Intel Skylake";
12060

61+
createInstance(projectId, zone, instanceName, reservationName, machineTypeName, sourceImage,
62+
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 Operation createInstance(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);
12172
// Initialize client that will be used to send requests. This client only needs to be created
12273
// once, and can be reused for multiple requests.
12374
try (InstancesClient instancesClient = InstancesClient.create()) {
124-
// Instance creation requires at least one persistent disk and one network interface.
12575
AttachedDisk disk =
12676
AttachedDisk.newBuilder()
12777
.setBoot(true)
@@ -135,12 +85,10 @@ public static void createInstance(
13585
.build())
13686
.build();
13787

138-
// Use the network interface provided in the networkName argument.
13988
NetworkInterface networkInterface = NetworkInterface.newBuilder()
14089
.setName(networkName)
14190
.build();
14291

143-
// Set Reservation Affinity
14492
ReservationAffinity reservationAffinity =
14593
ReservationAffinity.newBuilder()
14694
.setConsumeReservationType(SPECIFIC_RESERVATION.toString())
@@ -149,7 +97,6 @@ public static void createInstance(
14997
.addValues(reservationName)
15098
.build();
15199

152-
// Bind `instanceName`, `machineType`, `disk`, and `networkInterface` to an instance.
153100
Instance instanceResource =
154101
Instance.newBuilder()
155102
.setName(instanceName)
@@ -160,9 +107,6 @@ public static void createInstance(
160107
.setReservationAffinity(reservationAffinity)
161108
.build();
162109

163-
System.out.printf("Creating instance: %s at %s %n", instanceName, zone);
164-
165-
// Insert the instance in the specified project and zone.
166110
InsertInstanceRequest insertInstanceRequest = InsertInstanceRequest.newBuilder()
167111
.setProject(projectId)
168112
.setZone(zone)
@@ -171,14 +115,12 @@ public static void createInstance(
171115

172116
OperationFuture<Operation, Operation> operation = instancesClient.insertAsync(
173117
insertInstanceRequest);
174-
175-
// Wait for the operation to complete.
176118
Operation response = operation.get(3, TimeUnit.MINUTES);
177119

178120
if (response.hasError()) {
179-
System.out.println("Instance creation failed ! ! " + response);
121+
return null;
180122
}
181-
System.out.println("Operation Status: " + response.getStatus());
123+
return response;
182124
}
183125
}
184126
}

0 commit comments

Comments
 (0)