use of com.google.cloud.compute.v1.DisksClient in project java-docs-samples by GoogleCloudPlatform.
the class SnippetsIT method createSourceDisk.
private static Disk createSourceDisk() throws IOException, ExecutionException, InterruptedException {
try (DisksClient disksClient = DisksClient.create()) {
Disk disk = Disk.newBuilder().setSourceImage(getActiveDebian().getSelfLink()).setName("test-disk-" + UUID.randomUUID()).build();
OperationFuture<Operation, Operation> operation = disksClient.insertAsync(PROJECT_ID, ZONE, disk);
// Wait for the operation to complete.
operation.get();
return disksClient.get(PROJECT_ID, ZONE, disk.getName());
}
}
use of com.google.cloud.compute.v1.DisksClient in project java-docs-samples by GoogleCloudPlatform.
the class SnippetsIT method deleteDisk.
private static void deleteDisk(Disk disk) throws IOException, InterruptedException, ExecutionException {
try (DisksClient disksClient = DisksClient.create()) {
OperationFuture<Operation, Operation> operation = disksClient.deleteAsync(PROJECT_ID, ZONE, disk.getName());
operation.get();
}
}
use of com.google.cloud.compute.v1.DisksClient in project java-docs-samples by GoogleCloudPlatform.
the class InstancesAdvancedIT method deleteDisk.
private static void deleteDisk(Disk disk) throws IOException, InterruptedException, ExecutionException, TimeoutException {
try (DisksClient disksClient = DisksClient.create()) {
OperationFuture<Operation, Operation> operation = disksClient.deleteAsync(PROJECT_ID, ZONE, disk.getName());
operation.get(3, TimeUnit.MINUTES);
}
}
use of com.google.cloud.compute.v1.DisksClient in project java-docs-samples by GoogleCloudPlatform.
the class CreateWindowsOsImage method createWindowsOsImage.
// Creates a new Windows image from the specified source disk.
public static void createWindowsOsImage(String project, String zone, String sourceDiskName, String imageName, String storageLocation, boolean forceCreate) throws IOException, ExecutionException, InterruptedException, TimeoutException {
try (ImagesClient imagesClient = ImagesClient.create();
InstancesClient instancesClient = InstancesClient.create();
DisksClient disksClient = DisksClient.create()) {
Disk disk = disksClient.get(project, zone, sourceDiskName);
// Getting instances where source disk is attached.
for (String fullInstanceName : disk.getUsersList()) {
Map<String, String> instanceInfo = parseInstanceName(fullInstanceName);
Instance instance = instancesClient.get(instanceInfo.get("instanceProjectId"), instanceInfo.get("instanceZone"), instanceInfo.get("instanceName"));
// Сhecking whether the instances is stopped.
if (!Arrays.asList("TERMINATED", "STOPPED").contains(instance.getStatus()) && !forceCreate) {
throw new IllegalStateException(String.format("Instance %s should be stopped. Please stop the instance using GCESysprep command or set forceCreate parameter to true (not recommended). More information here: https://cloud.google.com/compute/docs/instances/windows/creating-windows-os-image#api.", instanceInfo.get("instanceName")));
}
}
if (forceCreate) {
System.out.println("Warning: forceCreate option compromise the integrity of your image. " + "Stop the instance before you create the image if possible.");
}
// Create Image.
Image image = Image.newBuilder().setName(imageName).setSourceDisk(String.format("/zones/%s/disks/%s", zone, sourceDiskName)).addStorageLocations(storageLocation.isEmpty() ? "" : storageLocation).build();
InsertImageRequest insertImageRequest = InsertImageRequest.newBuilder().setProject(project).setForceCreate(forceCreate).setImageResource(image).build();
Operation response = imagesClient.insertAsync(insertImageRequest).get(3, TimeUnit.MINUTES);
if (response.hasError()) {
System.out.println("Windows OS Image creation failed ! ! " + response);
return;
}
System.out.println("Image created.");
}
}
use of com.google.cloud.compute.v1.DisksClient in project java-docs-samples by GoogleCloudPlatform.
the class SnippetsIT method createSnapshot.
private static Snapshot createSnapshot(Disk srcDisk) throws IOException, InterruptedException, ExecutionException {
try (SnapshotsClient snapshotsClient = SnapshotsClient.create();
DisksClient disksClient = DisksClient.create()) {
Snapshot snapshot = Snapshot.newBuilder().setName("test-snap-" + UUID.randomUUID()).build();
OperationFuture<Operation, Operation> operation = disksClient.createSnapshotAsync(PROJECT_ID, ZONE, srcDisk.getName(), snapshot);
operation.get();
return snapshotsClient.get(PROJECT_ID, snapshot.getName());
}
}
Aggregations