Skip to content

Commit 1108ae1

Browse files
Added comments and code
1 parent 3aea8da commit 1108ae1

File tree

4 files changed

+34
-45
lines changed

4 files changed

+34
-45
lines changed

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

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package compute.reservation;
1818

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

2322
import com.google.api.gax.longrunning.OperationFuture;
@@ -40,6 +39,7 @@ public static void main(String[] args)
4039
// TODO(developer): Replace these variables before running the sample.
4140
// Project ID or project number of the Cloud project you want to use.
4241
String projectId = "YOUR_PROJECT_ID";
42+
// Name of the zone you want to use.
4343
String zone = "us-central1-a";
4444
// Name of the VM instance you want to query.
4545
String instanceName = "YOUR_INSTANCE_NAME";
@@ -48,7 +48,7 @@ public static void main(String[] args)
4848
}
4949

5050
// Create a virtual machine that explicitly doesn't consume reservations
51-
public static void createInstanceNotConsumeReservation(
51+
public static Instance createInstanceNotConsumeReservation(
5252
String project, String zone, String instanceName)
5353
throws IOException, InterruptedException, ExecutionException, TimeoutException {
5454
// Below are sample values that can be replaced.
@@ -60,15 +60,13 @@ public static void createInstanceNotConsumeReservation(
6060
// diskSizeGb: storage size of the boot disk to attach to the instance.
6161
// networkName: network interface to associate with the instance.
6262
String machineType = String.format("zones/%s/machineTypes/n1-standard-1", zone);
63-
String sourceImage = String
64-
.format("projects/debian-cloud/global/images/family/%s", "debian-11");
63+
String sourceImage = "projects/debian-cloud/global/images/family/debian-11";
6564
long diskSizeGb = 10L;
6665
String networkName = "default";
6766

6867
// Initialize client that will be used to send requests. This client only needs to be created
6968
// once, and can be reused for multiple requests.
7069
try (InstancesClient instancesClient = InstancesClient.create()) {
71-
// Instance creation requires at least one persistent disk and one network interface.
7270
AttachedDisk disk =
7371
AttachedDisk.newBuilder()
7472
.setBoot(true)
@@ -82,18 +80,15 @@ public static void createInstanceNotConsumeReservation(
8280
.build())
8381
.build();
8482

85-
// Use the network interface provided in the networkName argument.
8683
NetworkInterface networkInterface = NetworkInterface.newBuilder()
8784
.setName(networkName)
8885
.build();
8986

90-
// Set reservation affinity to "none"
9187
ReservationAffinity reservationAffinity =
9288
ReservationAffinity.newBuilder()
9389
.setConsumeReservationType(NO_RESERVATION.toString())
9490
.build();
9591

96-
// Bind `instanceName`, `machineType`, `disk`, and `networkInterface` to an instance.
9792
Instance instanceResource =
9893
Instance.newBuilder()
9994
.setName(instanceName)
@@ -103,9 +98,6 @@ public static void createInstanceNotConsumeReservation(
10398
.setReservationAffinity(reservationAffinity)
10499
.build();
105100

106-
System.out.printf("Creating instance: %s at %s %n", instanceName, zone);
107-
108-
// Insert the instance in the specified project and zone.
109101
InsertInstanceRequest insertInstanceRequest = InsertInstanceRequest.newBuilder()
110102
.setProject(project)
111103
.setZone(zone)
@@ -120,9 +112,10 @@ public static void createInstanceNotConsumeReservation(
120112

121113
if (response.hasError()) {
122114
System.out.println("Instance creation failed ! ! " + response);
123-
return;
115+
return null;
124116
}
125117
System.out.println("Operation Status: " + response.getStatus());
118+
return instancesClient.get(project, zone, instanceName);
126119
}
127120
}
128121
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package compute.reservation;
1818

1919
// [START compute_reservation_create]
20-
2120
import com.google.cloud.compute.v1.AcceleratorConfig;
2221
import com.google.cloud.compute.v1.AllocationSpecificSKUAllocationAllocatedInstancePropertiesReservedDisk;
2322
import com.google.cloud.compute.v1.AllocationSpecificSKUAllocationReservedInstanceProperties;

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

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package compute.reservation;
1818

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

2322
import com.google.cloud.compute.v1.AccessConfig;
@@ -49,34 +48,33 @@ public static void main(String[] args)
4948

5049

5150
// Create a template that explicitly doesn't consume any reservations.
52-
public static void createTemplateNotConsumeReservation(String projectId, String templateName)
51+
public static InstanceTemplate createTemplateNotConsumeReservation(
52+
String projectId, String templateName)
5353
throws IOException, ExecutionException, InterruptedException, TimeoutException {
5454
try (InstanceTemplatesClient instanceTemplatesClient = InstanceTemplatesClient.create()) {
5555

5656
String machineType = "e2-standard-4";
5757
String sourceImage = "projects/debian-cloud/global/images/family/debian-11";
5858

59-
// The template describes the size and source image of the boot disk
60-
// to attach to the instance.
6159
AttachedDisk attachedDisk = AttachedDisk.newBuilder()
6260
.setInitializeParams(AttachedDiskInitializeParams.newBuilder()
6361
.setSourceImage(sourceImage)
6462
.setDiskType("pd-balanced")
65-
.setDiskSizeGb(250).build())
63+
.setDiskSizeGb(250)
64+
.build())
6665
.setAutoDelete(true)
67-
.setBoot(true).build();
66+
.setBoot(true)
67+
.build();
6868

69-
// The template connects the instance to the `default` network,
70-
// without specifying a subnetwork.
7169
NetworkInterface networkInterface = NetworkInterface.newBuilder()
7270
.setName("global/networks/default")
73-
// The template lets the instance use an external IP address.
7471
.addAccessConfigs(AccessConfig.newBuilder()
7572
.setName("External NAT")
7673
.setType(AccessConfig.Type.ONE_TO_ONE_NAT.toString())
77-
.setNetworkTier(AccessConfig.NetworkTier.PREMIUM.toString()).build()).build();
74+
.setNetworkTier(AccessConfig.NetworkTier.PREMIUM.toString())
75+
.build())
76+
.build();
7877

79-
// Set reservation affinity to "none"
8078
ReservationAffinity reservationAffinity =
8179
ReservationAffinity.newBuilder()
8280
.setConsumeReservationType(NO_RESERVATION.toString())
@@ -86,25 +84,28 @@ public static void createTemplateNotConsumeReservation(String projectId, String
8684
.addDisks(attachedDisk)
8785
.setMachineType(machineType)
8886
.setReservationAffinity(reservationAffinity)
89-
.addNetworkInterfaces(networkInterface).build();
87+
.addNetworkInterfaces(networkInterface)
88+
.build();
9089

9190
InsertInstanceTemplateRequest insertInstanceTemplateRequest = InsertInstanceTemplateRequest
9291
.newBuilder()
9392
.setProject(projectId)
9493
.setInstanceTemplateResource(InstanceTemplate.newBuilder()
9594
.setName(templateName)
96-
.setProperties(instanceProperties).build()).build();
95+
.setProperties(instanceProperties)
96+
.build())
97+
.build();
9798

98-
// Create the Instance Template.
9999
Operation response = instanceTemplatesClient.insertAsync(insertInstanceTemplateRequest)
100100
.get(3, TimeUnit.MINUTES);
101101

102102
if (response.hasError()) {
103103
System.out.println("Instance Template creation failed ! ! " + response);
104-
return;
104+
return null;
105105
}
106-
System.out
107-
.printf("Instance Template Operation Status %s: %s", templateName, response.getStatus());
106+
System.out.printf("Instance Template Operation Status %s: %s",
107+
templateName, response.getStatus());
108+
return instanceTemplatesClient.get(projectId, templateName);
108109
}
109110
}
110111
}

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

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
import org.junit.runners.JUnit4;
4141

4242
@RunWith(JUnit4.class)
43-
@Timeout(value = 25, unit = TimeUnit.MINUTES)
43+
@Timeout(value = 3, unit = TimeUnit.MINUTES)
4444
public class ConsumeReservationIT {
4545

4646
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
@@ -74,13 +74,6 @@ public static void setUp()
7474
// Cleanup existing stale resources.
7575
Util.cleanUpExistingInstances("test-instance-not-consume-" + javaVersion, PROJECT_ID, ZONE);
7676
Util.cleanUpExistingInstanceTemplates("test-template-not-consume-" + javaVersion, PROJECT_ID);
77-
78-
// Create resources for testing.
79-
CreateInstanceNotConsumeReservation.createInstanceNotConsumeReservation(
80-
PROJECT_ID, ZONE, INSTANCE_NOT_CONSUME_RESERVATION_NAME);
81-
CreateTemplateNotConsumeReservation.createTemplateNotConsumeReservation(
82-
PROJECT_ID, TEMPLATE_NOT_CONSUME_RESERVATION_NAME);
83-
TimeUnit.SECONDS.sleep(30);
8477
}
8578

8679
@AfterAll
@@ -97,21 +90,24 @@ public static void cleanup()
9790
}
9891

9992
@Test
100-
public void testCreateInstanceNotConsumeReservation() {
101-
Instance instance = instancesClient.get(
102-
PROJECT_ID, ZONE, INSTANCE_NOT_CONSUME_RESERVATION_NAME);
93+
public void testCreateInstanceNotConsumeReservation()
94+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
95+
Instance instance = CreateInstanceNotConsumeReservation.createInstanceNotConsumeReservation(
96+
PROJECT_ID, ZONE, INSTANCE_NOT_CONSUME_RESERVATION_NAME);
10397

104-
// Verify that the instance was created with the correct consumeReservationType
98+
Assertions.assertNotNull(instance);
10599
Assertions.assertEquals(NO_RESERVATION.toString(),
106100
instance.getReservationAffinity().getConsumeReservationType());
107101
}
108102

109103
@Test
110-
public void testCreateTemplateNotConsumeReservation() {
111-
InstanceTemplate template = instanceTemplatesClient.get(
104+
public void testCreateTemplateNotConsumeReservation()
105+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
106+
InstanceTemplate template =
107+
CreateTemplateNotConsumeReservation.createTemplateNotConsumeReservation(
112108
PROJECT_ID, TEMPLATE_NOT_CONSUME_RESERVATION_NAME);
113109

114-
// Verify that the instance template was created with the correct consumeReservationType
110+
Assertions.assertNotNull(template);
115111
Assertions.assertEquals(NO_RESERVATION.toString(),
116112
template.getPropertiesOrBuilder().getReservationAffinity().getConsumeReservationType());
117113
}

0 commit comments

Comments
 (0)