Search in sources :

Example 16 with Disk

use of com.google.cloud.compute.v1.Disk 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)

Example 17 with Disk

use of com.google.cloud.compute.v1.Disk in project java-docs-samples by GoogleCloudPlatform.

the class InstancesAdvancedIT method createSnapshot.

private static Snapshot createSnapshot(Disk srcDisk) throws IOException, InterruptedException, ExecutionException, TimeoutException {
    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(3, TimeUnit.MINUTES);
        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)

Example 18 with Disk

use of com.google.cloud.compute.v1.Disk in project java-docs-samples by GoogleCloudPlatform.

the class InstancesAdvancedIT method createImage.

private static Image createImage(Disk srcDisk) throws IOException, InterruptedException, ExecutionException, TimeoutException {
    try (ImagesClient imagesClient = ImagesClient.create()) {
        Image image = Image.newBuilder().setName("test-img-" + UUID.randomUUID()).setSourceDisk(srcDisk.getSelfLink()).build();
        OperationFuture<Operation, Operation> operation = imagesClient.insertAsync(PROJECT_ID, image);
        operation.get(3, TimeUnit.MINUTES);
        return imagesClient.get(PROJECT_ID, image.getName());
    }
}
Also used : Operation(com.google.cloud.compute.v1.Operation) ImagesClient(com.google.cloud.compute.v1.ImagesClient) Image(com.google.cloud.compute.v1.Image)

Example 19 with Disk

use of com.google.cloud.compute.v1.Disk in project java-docs-samples by GoogleCloudPlatform.

the class InstancesAdvancedIT method createSourceDisk.

private static Disk createSourceDisk() throws IOException, ExecutionException, InterruptedException, TimeoutException {
    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(3, TimeUnit.MINUTES);
        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 20 with Disk

use of com.google.cloud.compute.v1.Disk in project java-docs-samples by GoogleCloudPlatform.

the class CreateEncryptedInstance method createEncryptedInstance.

// Create a new encrypted instance with the provided "instanceName" value and encryption key
// in the specified project and zone.
public static void createEncryptedInstance(String project, String zone, String instanceName, String diskEncryptionKey) throws IOException, InterruptedException, ExecutionException, TimeoutException {
    /* Below are sample values that can be replaced.
       machineType: machine type of the VM being created.
       (This value uses the format zones/{zone}/machineTypes/{type_name}.
       For a list of machine types, see https://cloud.google.com/compute/docs/machine-types)
       sourceImage: path to the operating system image to mount.
       (For details about images you can mount, see https://cloud.google.com/compute/docs/images)
       diskSizeGb: storage size of the boot disk to attach to the instance.
       networkName: network interface to associate with the instance. */
    String machineType = String.format("zones/%s/machineTypes/n1-standard-1", zone);
    String sourceImage = String.format("projects/debian-cloud/global/images/family/%s", "debian-11");
    long diskSizeGb = 10L;
    String networkName = "default";
    /* 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 `instancesClient.close()` method on the client to safely
       clean up any remaining background resources. */
    try (InstancesClient instancesClient = InstancesClient.create()) {
        // Instance creation requires at least one persistent disk and one network interface.
        AttachedDisk disk = AttachedDisk.newBuilder().setBoot(true).setAutoDelete(true).setType(Type.PERSISTENT.toString()).setInitializeParams(AttachedDiskInitializeParams.newBuilder().setSourceImage(sourceImage).setDiskSizeGb(diskSizeGb).build()).setDiskEncryptionKey(CustomerEncryptionKey.newBuilder().setRawKey(diskEncryptionKey).build()).build();
        // Use the network interface provided in the networkName argument.
        NetworkInterface networkInterface = NetworkInterface.newBuilder().setName(networkName).build();
        // Bind `instanceName`, `machineType`, `disk`, and `networkInterface` to an instance.
        Instance instanceResource = Instance.newBuilder().setName(instanceName).setMachineType(machineType).addDisks(disk).addNetworkInterfaces(networkInterface).build();
        System.out.printf("Creating instance: %s at %s ", instanceName, zone);
        // Insert the instance in the specified project and zone.
        InsertInstanceRequest insertInstanceRequest = InsertInstanceRequest.newBuilder().setProject(project).setZone(zone).setInstanceResource(instanceResource).build();
        OperationFuture<Operation, Operation> operation = instancesClient.insertAsync(insertInstanceRequest);
        // Wait for the operation to complete.
        Operation response = operation.get(3, TimeUnit.MINUTES);
        if (response.hasError()) {
            System.out.println("Instance creation failed ! ! " + response);
            return;
        }
        System.out.println("Operation Status: " + response.getStatus());
    }
}
Also used : InsertInstanceRequest(com.google.cloud.compute.v1.InsertInstanceRequest) Instance(com.google.cloud.compute.v1.Instance) InstancesClient(com.google.cloud.compute.v1.InstancesClient) AttachedDisk(com.google.cloud.compute.v1.AttachedDisk) NetworkInterface(com.google.cloud.compute.v1.NetworkInterface) Operation(com.google.cloud.compute.v1.Operation)

Aggregations

Operation (com.google.cloud.compute.v1.Operation)22 AttachedDisk (com.google.cloud.compute.v1.AttachedDisk)12 Instance (com.google.cloud.compute.v1.Instance)12 InstancesClient (com.google.cloud.compute.v1.InstancesClient)11 InsertInstanceRequest (com.google.cloud.compute.v1.InsertInstanceRequest)8 DisksClient (com.google.cloud.compute.v1.DisksClient)7 NetworkInterface (com.google.cloud.compute.v1.NetworkInterface)7 Image (com.google.cloud.compute.v1.Image)5 ImagesClient (com.google.cloud.compute.v1.ImagesClient)5 InstanceTemplatesClient (com.google.cloud.compute.v1.InstanceTemplatesClient)5 InsertInstanceTemplateRequest (com.google.cloud.compute.v1.InsertInstanceTemplateRequest)4 InstanceTemplate (com.google.cloud.compute.v1.InstanceTemplate)4 Disk (com.google.cloud.compute.v1.Disk)3 GlobalOperationsClient (com.google.cloud.compute.v1.GlobalOperationsClient)3 Test (org.junit.Test)3 InstanceProperties (com.google.cloud.compute.v1.InstanceProperties)2 Snapshot (com.google.cloud.compute.v1.Snapshot)2 SnapshotsClient (com.google.cloud.compute.v1.SnapshotsClient)2 Vector (java.util.Vector)2 CustomerEncryptionKeyProtectedDisk (com.google.cloud.compute.v1.CustomerEncryptionKeyProtectedDisk)1