Search in sources :

Example 1 with DisksClient

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());
    }
}
Also used : Operation(com.google.cloud.compute.v1.Operation) Disk(com.google.cloud.compute.v1.Disk) DisksClient(com.google.cloud.compute.v1.DisksClient)

Example 2 with DisksClient

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();
    }
}
Also used : Operation(com.google.cloud.compute.v1.Operation) DisksClient(com.google.cloud.compute.v1.DisksClient)

Example 3 with DisksClient

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);
    }
}
Also used : Operation(com.google.cloud.compute.v1.Operation) DisksClient(com.google.cloud.compute.v1.DisksClient)

Example 4 with DisksClient

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.");
    }
}
Also used : Instance(com.google.cloud.compute.v1.Instance) InstancesClient(com.google.cloud.compute.v1.InstancesClient) InsertImageRequest(com.google.cloud.compute.v1.InsertImageRequest) Operation(com.google.cloud.compute.v1.Operation) ImagesClient(com.google.cloud.compute.v1.ImagesClient) Image(com.google.cloud.compute.v1.Image) Disk(com.google.cloud.compute.v1.Disk) DisksClient(com.google.cloud.compute.v1.DisksClient)

Example 5 with DisksClient

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());
    }
}
Also used : Snapshot(com.google.cloud.compute.v1.Snapshot) SnapshotsClient(com.google.cloud.compute.v1.SnapshotsClient) Operation(com.google.cloud.compute.v1.Operation) DisksClient(com.google.cloud.compute.v1.DisksClient)

Aggregations

DisksClient (com.google.cloud.compute.v1.DisksClient)7 Operation (com.google.cloud.compute.v1.Operation)7 Disk (com.google.cloud.compute.v1.Disk)3 Snapshot (com.google.cloud.compute.v1.Snapshot)2 SnapshotsClient (com.google.cloud.compute.v1.SnapshotsClient)2 Image (com.google.cloud.compute.v1.Image)1 ImagesClient (com.google.cloud.compute.v1.ImagesClient)1 InsertImageRequest (com.google.cloud.compute.v1.InsertImageRequest)1 Instance (com.google.cloud.compute.v1.Instance)1 InstancesClient (com.google.cloud.compute.v1.InstancesClient)1