Skip to content

Commit cf99ff4

Browse files
Fixed code following recommendations
1 parent 1108ae1 commit cf99ff4

File tree

3 files changed

+37
-52
lines changed

3 files changed

+37
-52
lines changed
Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
import java.util.concurrent.TimeUnit;
3434
import java.util.concurrent.TimeoutException;
3535

36-
public class CreateInstanceNotConsumeReservation {
36+
public class CreateInstanceWithoutConsumingReservation {
3737
public static void main(String[] args)
3838
throws IOException, ExecutionException, InterruptedException, TimeoutException {
3939
// TODO(developer): Replace these variables before running the sample.
@@ -43,27 +43,29 @@ public static void main(String[] args)
4343
String zone = "us-central1-a";
4444
// Name of the VM instance you want to query.
4545
String instanceName = "YOUR_INSTANCE_NAME";
46-
47-
createInstanceNotConsumeReservation(projectId, zone, instanceName);
48-
}
49-
50-
// Create a virtual machine that explicitly doesn't consume reservations
51-
public static Instance createInstanceNotConsumeReservation(
52-
String project, String zone, String instanceName)
53-
throws IOException, InterruptedException, ExecutionException, TimeoutException {
54-
// Below are sample values that can be replaced.
5546
// machineType: machine type of the VM being created.
5647
// * This value uses the format zones/{zone}/machineTypes/{type_name}.
5748
// * For a list of machine types, see https://cloud.google.com/compute/docs/machine-types
49+
String machineTypeName = "n1-standard-1";
5850
// sourceImage: path to the operating system image to mount.
5951
// * For details about images you can mount, see https://cloud.google.com/compute/docs/images
60-
// diskSizeGb: storage size of the boot disk to attach to the instance.
61-
// networkName: network interface to associate with the instance.
62-
String machineType = String.format("zones/%s/machineTypes/n1-standard-1", zone);
6352
String sourceImage = "projects/debian-cloud/global/images/family/debian-11";
53+
// diskSizeGb: storage size of the boot disk to attach to the instance.
6454
long diskSizeGb = 10L;
55+
// networkName: network interface to associate with the instance.
6556
String networkName = "default";
6657

58+
createInstanceWithoutConsumingReservationAsync(projectId, zone, instanceName,
59+
machineTypeName, sourceImage, diskSizeGb, networkName);
60+
}
61+
62+
// Create a virtual machine that explicitly doesn't consume reservations
63+
public static Instance createInstanceWithoutConsumingReservationAsync(
64+
String project, String zone, String instanceName,
65+
String machineTypeName, String sourceImage, long diskSizeGb, String networkName)
66+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
67+
String machineType = String.format("zones/%s/machineTypes/%s", zone, machineTypeName);
68+
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 (InstancesClient instancesClient = InstancesClient.create()) {
@@ -111,10 +113,8 @@ public static Instance createInstanceNotConsumeReservation(
111113
Operation response = operation.get(3, TimeUnit.MINUTES);
112114

113115
if (response.hasError()) {
114-
System.out.println("Instance creation failed ! ! " + response);
115116
return null;
116117
}
117-
System.out.println("Operation Status: " + response.getStatus());
118118
return instancesClient.get(project, zone, instanceName);
119119
}
120120
}
Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,28 +34,29 @@
3434
import java.util.concurrent.TimeUnit;
3535
import java.util.concurrent.TimeoutException;
3636

37-
public class CreateTemplateNotConsumeReservation {
37+
public class CreateTemplateWithoutConsumingReservation {
3838
public static void main(String[] args)
3939
throws IOException, ExecutionException, InterruptedException, TimeoutException {
4040
// TODO(developer): Replace these variables before running the sample.
4141
// Project ID or project number of the Cloud project you want to use.
4242
String projectId = "YOUR_PROJECT_ID";
4343
// Name of the template you want to query.
4444
String templateName = "YOUR_INSTANCE_TEMPLATE_NAME";
45+
String machineType = "e2-standard-4";
46+
String sourceImage = "projects/debian-cloud/global/images/family/debian-11";
4547

46-
createTemplateNotConsumeReservation(projectId, templateName);
48+
createTemplateWithoutConsumingReservationAsync(
49+
projectId, templateName, machineType, sourceImage);
4750
}
4851

4952

5053
// Create a template that explicitly doesn't consume any reservations.
51-
public static InstanceTemplate createTemplateNotConsumeReservation(
52-
String projectId, String templateName)
54+
public static InstanceTemplate createTemplateWithoutConsumingReservationAsync(
55+
String projectId, String templateName, String machineType, String sourceImage)
5356
throws IOException, ExecutionException, InterruptedException, TimeoutException {
57+
// Initialize client that will be used to send requests. This client only needs to be created
58+
// once, and can be reused for multiple requests.
5459
try (InstanceTemplatesClient instanceTemplatesClient = InstanceTemplatesClient.create()) {
55-
56-
String machineType = "e2-standard-4";
57-
String sourceImage = "projects/debian-cloud/global/images/family/debian-11";
58-
5960
AttachedDisk attachedDisk = AttachedDisk.newBuilder()
6061
.setInitializeParams(AttachedDiskInitializeParams.newBuilder()
6162
.setSourceImage(sourceImage)
@@ -100,11 +101,8 @@ public static InstanceTemplate createTemplateNotConsumeReservation(
100101
.get(3, TimeUnit.MINUTES);
101102

102103
if (response.hasError()) {
103-
System.out.println("Instance Template creation failed ! ! " + response);
104104
return null;
105105
}
106-
System.out.printf("Instance Template Operation Status %s: %s",
107-
templateName, response.getStatus());
108106
return instanceTemplatesClient.get(projectId, templateName);
109107
}
110108
}

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

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,8 @@
2121

2222
import com.google.cloud.compute.v1.Instance;
2323
import com.google.cloud.compute.v1.InstanceTemplate;
24-
import com.google.cloud.compute.v1.InstanceTemplatesClient;
25-
import com.google.cloud.compute.v1.InstancesClient;
2624
import compute.DeleteInstance;
2725
import compute.DeleteInstanceTemplate;
28-
import compute.Util;
2926
import java.io.IOException;
3027
import java.util.UUID;
3128
import java.util.concurrent.ExecutionException;
@@ -45,15 +42,14 @@ public class ConsumeReservationIT {
4542

4643
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
4744
private static final String ZONE = "us-central1-a";
48-
private static InstancesClient instancesClient;
49-
private static InstanceTemplatesClient instanceTemplatesClient;
50-
static String javaVersion = System.getProperty("java.version").substring(0, 2);
5145
private static final String INSTANCE_NOT_CONSUME_RESERVATION_NAME =
52-
"test-instance-not-consume-" + javaVersion + "-"
53-
+ UUID.randomUUID().toString().substring(0, 8);
46+
"test-instance-not-consume-" + UUID.randomUUID().toString().substring(0, 8);
5447
private static final String TEMPLATE_NOT_CONSUME_RESERVATION_NAME =
55-
"test-template-not-consume-" + javaVersion + "-"
56-
+ UUID.randomUUID().toString().substring(0, 8);
48+
"test-template-not-consume-" + UUID.randomUUID().toString().substring(0, 8);
49+
private static final String MACHINE_TYPE_NAME = "n1-standard-1";
50+
private static final String SOURCE_IMAGE = "projects/debian-cloud/global/images/family/debian-11";
51+
private static final String NETWORK_NAME = "default";
52+
private static final long DISK_SIZE_GD = 10L;
5753

5854
// Check if the required environment variables are set.
5955
public static void requireEnvVar(String envVarName) {
@@ -66,14 +62,6 @@ public static void setUp()
6662
throws IOException, ExecutionException, InterruptedException, TimeoutException {
6763
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
6864
requireEnvVar("GOOGLE_CLOUD_PROJECT");
69-
70-
// Initialize clients once for all tests
71-
instancesClient = InstancesClient.create();
72-
instanceTemplatesClient = InstanceTemplatesClient.create();
73-
74-
// Cleanup existing stale resources.
75-
Util.cleanUpExistingInstances("test-instance-not-consume-" + javaVersion, PROJECT_ID, ZONE);
76-
Util.cleanUpExistingInstanceTemplates("test-template-not-consume-" + javaVersion, PROJECT_ID);
7765
}
7866

7967
@AfterAll
@@ -83,17 +71,15 @@ public static void cleanup()
8371
DeleteInstance.deleteInstance(PROJECT_ID, ZONE, INSTANCE_NOT_CONSUME_RESERVATION_NAME);
8472
DeleteInstanceTemplate.deleteInstanceTemplate(
8573
PROJECT_ID, TEMPLATE_NOT_CONSUME_RESERVATION_NAME);
86-
87-
// Close clients after all tests
88-
instancesClient.close();
89-
instanceTemplatesClient.close();
9074
}
9175

9276
@Test
9377
public void testCreateInstanceNotConsumeReservation()
9478
throws IOException, ExecutionException, InterruptedException, TimeoutException {
95-
Instance instance = CreateInstanceNotConsumeReservation.createInstanceNotConsumeReservation(
96-
PROJECT_ID, ZONE, INSTANCE_NOT_CONSUME_RESERVATION_NAME);
79+
Instance instance = CreateInstanceWithoutConsumingReservation
80+
.createInstanceWithoutConsumingReservationAsync(
81+
PROJECT_ID, ZONE, INSTANCE_NOT_CONSUME_RESERVATION_NAME, MACHINE_TYPE_NAME,
82+
SOURCE_IMAGE, DISK_SIZE_GD, NETWORK_NAME);
9783

9884
Assertions.assertNotNull(instance);
9985
Assertions.assertEquals(NO_RESERVATION.toString(),
@@ -104,8 +90,9 @@ public void testCreateInstanceNotConsumeReservation()
10490
public void testCreateTemplateNotConsumeReservation()
10591
throws IOException, ExecutionException, InterruptedException, TimeoutException {
10692
InstanceTemplate template =
107-
CreateTemplateNotConsumeReservation.createTemplateNotConsumeReservation(
108-
PROJECT_ID, TEMPLATE_NOT_CONSUME_RESERVATION_NAME);
93+
CreateTemplateWithoutConsumingReservation.createTemplateWithoutConsumingReservationAsync(
94+
PROJECT_ID, TEMPLATE_NOT_CONSUME_RESERVATION_NAME,
95+
MACHINE_TYPE_NAME, SOURCE_IMAGE);
10996

11097
Assertions.assertNotNull(template);
11198
Assertions.assertEquals(NO_RESERVATION.toString(),

0 commit comments

Comments
 (0)