use of com.google.cloud.compute.v1.InstanceGroupManagersClient in project java-compute by googleapis.
the class ITSmokeInstancesTest method testResizeGroupToZero.
@Test
public void testResizeGroupToZero() throws IOException, ExecutionException, InterruptedException {
// We test here: 1)set body field to zero
// 2)set query param to zero
List<String> instanceGroupManagersToClean = new ArrayList<>();
List<String> instanceTemplatesToClean = new ArrayList<>();
InstanceTemplatesClient instanceTemplatesClient = InstanceTemplatesClient.create();
InstanceGroupManagersClient instanceGroupManagersClient = InstanceGroupManagersClient.create();
String templateName = generateRandomName("template");
String instanceGroupManagerName = generateRandomName("igm");
Instance instance = insertInstance();
InstanceTemplate instanceTemplate = InstanceTemplate.newBuilder().setSourceInstance(instance.getSelfLink()).setName(templateName).build();
Operation insertOperation = instanceTemplatesClient.insertAsync(DEFAULT_PROJECT, instanceTemplate).get();
instanceTemplatesToClean.add(templateName);
try {
InstanceGroupManager instanceGroupManager = InstanceGroupManager.newBuilder().setName(instanceGroupManagerName).setBaseInstanceName("java-gapic").setInstanceTemplate(insertOperation.getTargetLink()).setTargetSize(0).build();
instanceGroupManagersClient.insertAsync(DEFAULT_PROJECT, DEFAULT_ZONE, instanceGroupManager).get();
instanceGroupManagersToClean.add(instanceGroupManagerName);
InstanceGroupManager fetched = instanceGroupManagersClient.get(DEFAULT_PROJECT, DEFAULT_ZONE, instanceGroupManagerName);
Assert.assertEquals(0, fetched.getTargetSize());
instanceGroupManagersClient.resizeAsync(DEFAULT_PROJECT, DEFAULT_ZONE, instanceGroupManagerName, 1).get();
InstanceGroupManager resizedIGM = instanceGroupManagersClient.get(DEFAULT_PROJECT, DEFAULT_ZONE, instanceGroupManagerName);
Assert.assertEquals(1, resizedIGM.getTargetSize());
instanceGroupManagersClient.resizeAsync(DEFAULT_PROJECT, DEFAULT_ZONE, instanceGroupManagerName, 0).get();
InstanceGroupManager instanceGroupManagerResized = instanceGroupManagersClient.get(DEFAULT_PROJECT, DEFAULT_ZONE, instanceGroupManagerName);
Assert.assertEquals(0, instanceGroupManagerResized.getTargetSize());
} finally {
for (String name : instanceGroupManagersToClean) {
instanceGroupManagersClient.deleteAsync(DEFAULT_PROJECT, DEFAULT_ZONE, name).get();
}
for (String name : instanceTemplatesToClean) {
instanceTemplatesClient.deleteAsync(DEFAULT_PROJECT, name).get();
}
}
}
Aggregations