Skip to content

Commit 82a49bb

Browse files
Fixed code
1 parent 7d82ec4 commit 82a49bb

File tree

3 files changed

+40
-40
lines changed

3 files changed

+40
-40
lines changed

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

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,66 +44,73 @@ public static void main(String[] args)
4444
String zone = "us-central1-a";
4545
// Name of the VM instance you want to query.
4646
String instanceName = "YOUR_INSTANCE_NAME";
47-
// Machine type of the instances in the reservation.
48-
String machineType = "n2-standard-32";
4947

50-
createInstanceNotConsumeReservation(projectId, zone, instanceName, machineType);
48+
createInstanceNotConsumeReservation(projectId, zone, instanceName);
5149
}
5250

5351
// Create a virtual machine that explicitly doesn't consume reservations
5452
public static void createInstanceNotConsumeReservation(
55-
String projectId, String zone, String instanceName, String machineType)
53+
String project, String zone, String instanceName)
5654
throws IOException, InterruptedException, ExecutionException, TimeoutException {
5755
// Below are sample values that can be replaced.
56+
// machineType: machine type of the VM being created.
57+
// * This value uses the format zones/{zone}/machineTypes/{type_name}.
58+
// * For a list of machine types, see https://cloud.google.com/compute/docs/machine-types
5859
// sourceImage: path to the operating system image to mount.
5960
// * For details about images you can mount, see https://cloud.google.com/compute/docs/images
60-
// Network interface to associate with the instance.
61+
// diskSizeGb: storage size of the boot disk to attach to the instance.
62+
// networkName: network interface to associate with the instance.
63+
String machineType = String.format("zones/%s/machineTypes/n1-standard-1", zone);
6164
String sourceImage = String
6265
.format("projects/debian-cloud/global/images/family/%s", "debian-11");
63-
String network = "global/networks/default"; // Example network
6466
long diskSizeGb = 10L;
67+
String networkName = "default";
68+
6569
// Initialize client that will be used to send requests. This client only needs to be created
6670
// once, and can be reused for multiple requests.
6771
try (InstancesClient instancesClient = InstancesClient.create()) {
68-
69-
// Create the attached disk object
70-
AttachedDisk attachedDisk =
72+
// Instance creation requires at least one persistent disk and one network interface.
73+
AttachedDisk disk =
7174
AttachedDisk.newBuilder()
7275
.setBoot(true)
7376
.setAutoDelete(true)
7477
.setType(AttachedDisk.Type.PERSISTENT.toString())
78+
.setDeviceName("disk-1")
7579
.setInitializeParams(
7680
AttachedDiskInitializeParams.newBuilder()
7781
.setSourceImage(sourceImage)
7882
.setDiskSizeGb(diskSizeGb)
7983
.build())
8084
.build();
8185

82-
// Create the network interface object
83-
NetworkInterface networkInterface =
84-
NetworkInterface.newBuilder().setName(network).build();
86+
// Use the network interface provided in the networkName argument.
87+
NetworkInterface networkInterface = NetworkInterface.newBuilder()
88+
.setName(networkName)
89+
.build();
8590

8691
// Set reservation affinity to "none"
8792
ReservationAffinity reservationAffinity =
8893
ReservationAffinity.newBuilder()
8994
.setConsumeReservationType(NO_RESERVATION.toString())
9095
.build();
9196

92-
// Create the instance object
93-
Instance instance =
97+
// Bind `instanceName`, `machineType`, `disk`, and `networkInterface` to an instance.
98+
Instance instanceResource =
9499
Instance.newBuilder()
95100
.setName(instanceName)
96-
.setMachineType("zones/" + zone + "/machineTypes/" + machineType)
97-
.addDisks(attachedDisk)
101+
.setMachineType(machineType)
102+
.addDisks(disk)
98103
.addNetworkInterfaces(networkInterface)
99104
.setReservationAffinity(reservationAffinity)
100105
.build();
101106

107+
System.out.printf("Creating instance: %s at %s %n", instanceName, zone);
108+
102109
// Insert the instance in the specified project and zone.
103110
InsertInstanceRequest insertInstanceRequest = InsertInstanceRequest.newBuilder()
104-
.setProject(projectId)
111+
.setProject(project)
105112
.setZone(zone)
106-
.setInstanceResource(instance)
113+
.setInstanceResource(instanceResource)
107114
.build();
108115

109116
OperationFuture<Operation, Operation> operation = instancesClient.insertAsync(
@@ -114,6 +121,7 @@ public static void createInstanceNotConsumeReservation(
114121

115122
if (response.hasError()) {
116123
System.out.println("Instance creation failed ! ! " + response);
124+
return;
117125
}
118126
System.out.println("Operation Status: " + response.getStatus());
119127
}

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,18 @@ public static void main(String[] args)
4343
String projectId = "YOUR_PROJECT_ID";
4444
// Name of the template you want to query.
4545
String templateName = "YOUR_INSTANCE_TEMPLATE_NAME";
46-
// Machine type of the template.
47-
String machineType = "n2-standard-32";
4846

49-
createTemplateNotConsumeReservation(projectId, templateName, machineType);
47+
createTemplateNotConsumeReservation(projectId, templateName);
5048
}
5149

50+
5251
// Create a template that explicitly doesn't consume any reservations.
53-
public static void createTemplateNotConsumeReservation(
54-
String projectId, String templateName, String machineType)
55-
throws IOException, InterruptedException, ExecutionException, TimeoutException {
52+
public static void createTemplateNotConsumeReservation(String projectId, String templateName)
53+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
5654
try (InstanceTemplatesClient instanceTemplatesClient = InstanceTemplatesClient.create()) {
57-
// Below are sample values that can be replaced.
58-
// sourceImage: path to the operating system image to mount.
59-
// * For details about images you can mount, see https://cloud.google.com/compute/docs/images
60-
// Network interface to associate with the instance.
61-
String sourceImage = String
62-
.format("projects/debian-cloud/global/images/family/%s", "debian-11");
55+
56+
String machineType = "e2-standard-4";
57+
String sourceImage = "projects/debian-cloud/global/images/family/debian-11";
6358

6459
// The template describes the size and source image of the boot disk
6560
// to attach to the instance.
@@ -106,6 +101,7 @@ public static void createTemplateNotConsumeReservation(
106101

107102
if (response.hasError()) {
108103
System.out.println("Instance Template creation failed ! ! " + response);
104+
return;
109105
}
110106
System.out
111107
.printf("Instance Template Operation Status %s: %s", templateName, response.getStatus());

compute/cloud-client/src/test/java/compute/reservation/ConsumeReservationIT.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import com.google.cloud.compute.v1.InstanceTemplate;
2424
import com.google.cloud.compute.v1.InstanceTemplatesClient;
2525
import com.google.cloud.compute.v1.InstancesClient;
26-
import compute.CreateInstance;
26+
import compute.DeleteInstance;
2727
import compute.DeleteInstanceTemplate;
2828
import compute.Util;
2929
import java.io.IOException;
@@ -34,7 +34,6 @@
3434
import org.junit.jupiter.api.AfterAll;
3535
import org.junit.jupiter.api.Assertions;
3636
import org.junit.jupiter.api.BeforeAll;
37-
import org.junit.jupiter.api.Disabled;
3837
import org.junit.jupiter.api.Test;
3938
import org.junit.jupiter.api.Timeout;
4039
import org.junit.runner.RunWith;
@@ -45,7 +44,7 @@
4544
public class ConsumeReservationIT {
4645

4746
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
48-
private static final String ZONE = "europe-southwest1-a";
47+
private static final String ZONE = "us-central1-a";
4948
private static InstancesClient instancesClient;
5049
private static InstanceTemplatesClient instanceTemplatesClient;
5150
static String javaVersion = System.getProperty("java.version").substring(0, 2);
@@ -55,7 +54,6 @@ public class ConsumeReservationIT {
5554
private static final String TEMPLATE_NOT_CONSUME_RESERVATION_NAME =
5655
"test-template-not-consume-" + javaVersion + "-"
5756
+ UUID.randomUUID().toString().substring(0, 8);
58-
private static final String MACHINE_TYPE = "n2-standard-32";
5957

6058
// Check if the required environment variables are set.
6159
public static void requireEnvVar(String envVarName) {
@@ -78,19 +76,18 @@ public static void setUp()
7876
Util.cleanUpExistingInstanceTemplates("test-template-not-consume-" + javaVersion, PROJECT_ID);
7977

8078
// Create resources for testing.
81-
CreateInstance.createInstance(PROJECT_ID, ZONE, INSTANCE_NOT_CONSUME_RESERVATION_NAME);
82-
// CreateInstanceNotConsumeReservation.createInstanceNotConsumeReservation(
83-
// PROJECT_ID, ZONE, INSTANCE_NOT_CONSUME_RESERVATION_NAME, MACHINE_TYPE);
79+
CreateInstanceNotConsumeReservation.createInstanceNotConsumeReservation(
80+
PROJECT_ID, ZONE, INSTANCE_NOT_CONSUME_RESERVATION_NAME);
8481
CreateTemplateNotConsumeReservation.createTemplateNotConsumeReservation(
85-
PROJECT_ID, TEMPLATE_NOT_CONSUME_RESERVATION_NAME, MACHINE_TYPE);
82+
PROJECT_ID, TEMPLATE_NOT_CONSUME_RESERVATION_NAME);
8683
TimeUnit.SECONDS.sleep(30);
8784
}
8885

8986
@AfterAll
9087
public static void cleanup()
9188
throws IOException, ExecutionException, InterruptedException, TimeoutException {
9289
// Delete the instance created for testing.
93-
//DeleteInstance.deleteInstance(PROJECT_ID, ZONE, INSTANCE_NOT_CONSUME_RESERVATION_NAME);
90+
DeleteInstance.deleteInstance(PROJECT_ID, ZONE, INSTANCE_NOT_CONSUME_RESERVATION_NAME);
9491
DeleteInstanceTemplate.deleteInstanceTemplate(
9592
PROJECT_ID, TEMPLATE_NOT_CONSUME_RESERVATION_NAME);
9693

@@ -99,7 +96,6 @@ public static void cleanup()
9996
instanceTemplatesClient.close();
10097
}
10198

102-
@Disabled
10399
@Test
104100
public void testCreateInstanceNotConsumeReservation() {
105101
Instance instance = instancesClient.get(

0 commit comments

Comments
 (0)