use of com.google.cloud.compute.v1.Operation in project java-docs-samples by GoogleCloudPlatform.
the class PreemptibleIT method testListZoneOperations.
@Test
public void testListZoneOperations() throws IOException {
String filter = String.format("targetLink=\"https://www.googleapis.com/compute/v1/projects/%s/zones/%s/instances/%s\"", PROJECT_ID, ZONE, INSTANCE_NAME);
ListPagedResponse response = ListZoneOperations.listZoneOperations(PROJECT_ID, ZONE, filter);
boolean elementPresent = false;
for (Operation operation : response.iterateAll()) {
elementPresent = true;
break;
}
assertWithMessage("There should be at least one operation for this instance at this point.").that(elementPresent).isTrue();
}
use of com.google.cloud.compute.v1.Operation in project java-docs-samples by GoogleCloudPlatform.
the class WindowsOsImageIT method setup.
@BeforeAll
public static void setup() throws IOException, ExecutionException, InterruptedException, TimeoutException {
final PrintStream out = System.out;
ByteArrayOutputStream stdOut = new ByteArrayOutputStream();
System.setOut(new PrintStream(stdOut));
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
requireEnvVar("GOOGLE_CLOUD_PROJECT");
// Cleanup existing test instances.
Util.cleanUpExistingInstances("windowsimage-test-instance-", PROJECT_ID, ZONE);
String randomUUID = UUID.randomUUID().toString().split("-")[0];
INSTANCE_NAME = "windowsimage-test-instance-" + randomUUID;
DISK_NAME = "windowsimage-test-disk-" + randomUUID;
IMAGE_NAME = "windowsimage-test-image-" + randomUUID;
// Create Instance with Windows source image.
try (InstancesClient instancesClient = InstancesClient.create()) {
AttachedDisk attachedDisk = AttachedDisk.newBuilder().setDeviceName(DISK_NAME).setAutoDelete(true).setBoot(true).setType(AttachedDisk.Type.PERSISTENT.name()).setInitializeParams(AttachedDiskInitializeParams.newBuilder().setDiskName(DISK_NAME).setDiskSizeGb(64).setSourceImage("projects/windows-cloud/global/images/windows-server-2012-r2-dc-core-v20220314").build()).build();
Instance instance = Instance.newBuilder().setName(INSTANCE_NAME).setMachineType(String.format("zones/%s/machineTypes/n1-standard-1", ZONE)).addNetworkInterfaces(NetworkInterface.newBuilder().setName("global/networks/default").build()).addDisks(attachedDisk).build();
InsertInstanceRequest request = InsertInstanceRequest.newBuilder().setProject(PROJECT_ID).setZone(ZONE).setInstanceResource(instance).build();
Operation response = instancesClient.insertAsync(request).get();
Assert.assertFalse(response.hasError());
}
stdOut.close();
System.setOut(out);
}
use of com.google.cloud.compute.v1.Operation in project java-docs-samples by GoogleCloudPlatform.
the class WindowsOsImageIT method cleanUp.
@AfterAll
public static void cleanUp() throws IOException, ExecutionException, InterruptedException, TimeoutException {
final PrintStream out = System.out;
ByteArrayOutputStream stdOut = new ByteArrayOutputStream();
System.setOut(new PrintStream(stdOut));
// Delete image.
ImagesClient imagesClient = ImagesClient.create();
Operation operation = imagesClient.deleteAsync(PROJECT_ID, IMAGE_NAME).get();
if (operation.hasError()) {
System.out.println("Image not deleted.");
}
// Delete instance.
DeleteInstance.deleteInstance(PROJECT_ID, ZONE, INSTANCE_NAME);
stdOut.close();
System.setOut(out);
}
use of com.google.cloud.compute.v1.Operation in project java-docs-samples by GoogleCloudPlatform.
the class CreateTemplateFromInstance method createTemplateFromInstance.
// Create a new instance template based on an existing instance.
// This new template specifies a different boot disk.
public static void createTemplateFromInstance(String projectId, String templateName, String instance) throws IOException, ExecutionException, InterruptedException, TimeoutException {
try (InstanceTemplatesClient instanceTemplatesClient = InstanceTemplatesClient.create();
GlobalOperationsClient globalOperationsClient = GlobalOperationsClient.create()) {
SourceInstanceParams sourceInstanceParams = SourceInstanceParams.newBuilder().addDiskConfigs(DiskInstantiationConfig.newBuilder().setDeviceName("disk-1").setInstantiateFrom(InstantiateFrom.CUSTOM_IMAGE.toString()).setCustomImage(String.format("projects/%s/global/images/family/%s", "rocky-linux-cloud", "rocky-linux-8")).setAutoDelete(true).build()).build();
InstanceTemplate instanceTemplate = InstanceTemplate.newBuilder().setName(templateName).setSourceInstance(instance).setSourceInstanceParams(sourceInstanceParams).build();
InsertInstanceTemplateRequest insertInstanceTemplateRequest = InsertInstanceTemplateRequest.newBuilder().setProject(projectId).setInstanceTemplateResource(instanceTemplate).build();
Operation operation = instanceTemplatesClient.insertCallable().futureCall(insertInstanceTemplateRequest).get(3, TimeUnit.MINUTES);
Operation response = globalOperationsClient.wait(projectId, operation.getName());
if (response.hasError()) {
System.out.println("Instance Template creation failed ! ! " + response);
return;
}
System.out.printf("Instance Template creation operation status %s: %s", templateName, response.getStatus());
}
}
use of com.google.cloud.compute.v1.Operation in project java-docs-samples by GoogleCloudPlatform.
the class DeleteFirewallRule method deleteFirewallRule.
// Deletes a firewall rule from the project.
public static void deleteFirewallRule(String project, String firewallRuleName) throws IOException, ExecutionException, InterruptedException, TimeoutException {
/* Initialize client that will be used to send requests. This client only needs to be created
once, and can be reused for multiple requests. After completing all of your requests, call
the `firewallsClient.close()` method on the client to safely
clean up any remaining background resources. */
try (FirewallsClient firewallsClient = FirewallsClient.create()) {
OperationFuture<Operation, Operation> operation = firewallsClient.deleteAsync(project, firewallRuleName);
operation.get(3, TimeUnit.MINUTES);
System.out.println("Deleted firewall rule -> " + firewallRuleName);
}
}
Aggregations