Search in sources :

Example 21 with Disk

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

the class CreateInstanceTemplate method createInstanceTemplate.

/*
    Create a new instance template with the provided name and a specific
    instance configuration.
   */
public static void createInstanceTemplate(String projectId, String templateName) throws IOException, ExecutionException, InterruptedException, TimeoutException {
    try (InstanceTemplatesClient instanceTemplatesClient = InstanceTemplatesClient.create()) {
        String machineType = "e2-standard-4";
        String sourceImage = "projects/debian-cloud/global/images/family/debian-11";
        // The template describes the size and source image of the boot disk
        // to attach to the instance.
        AttachedDisk attachedDisk = AttachedDisk.newBuilder().setInitializeParams(AttachedDiskInitializeParams.newBuilder().setSourceImage(sourceImage).setDiskSizeGb(250).build()).setAutoDelete(true).setBoot(true).build();
        // The template connects the instance to the `default` network,
        // without specifying a subnetwork.
        NetworkInterface networkInterface = NetworkInterface.newBuilder().setName("global/networks/default").addAccessConfigs(AccessConfig.newBuilder().setName("External NAT").setType(AccessConfig.Type.ONE_TO_ONE_NAT.toString()).setNetworkTier(NetworkTier.PREMIUM.toString()).build()).build();
        InstanceProperties instanceProperties = InstanceProperties.newBuilder().addDisks(attachedDisk).setMachineType(machineType).addNetworkInterfaces(networkInterface).build();
        InsertInstanceTemplateRequest insertInstanceTemplateRequest = InsertInstanceTemplateRequest.newBuilder().setProject(projectId).setInstanceTemplateResource(InstanceTemplate.newBuilder().setName(templateName).setProperties(instanceProperties).build()).build();
        // Create the Instance Template.
        Operation response = instanceTemplatesClient.insertAsync(insertInstanceTemplateRequest).get(3, TimeUnit.MINUTES);
        if (response.hasError()) {
            System.out.println("Instance Template creation failed ! ! " + response);
            return;
        }
        System.out.printf("Instance Template Operation Status %s: %s", templateName, response.getStatus());
    }
}
Also used : InstanceProperties(com.google.cloud.compute.v1.InstanceProperties) AttachedDisk(com.google.cloud.compute.v1.AttachedDisk) NetworkInterface(com.google.cloud.compute.v1.NetworkInterface) Operation(com.google.cloud.compute.v1.Operation) InsertInstanceTemplateRequest(com.google.cloud.compute.v1.InsertInstanceTemplateRequest) InstanceTemplatesClient(com.google.cloud.compute.v1.InstanceTemplatesClient)

Example 22 with Disk

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

the class StartEncryptedInstance method startEncryptedInstance.

// Starts a stopped Google Compute Engine instance (with encrypted disks).
public static void startEncryptedInstance(String project, String zone, String instanceName, String key) 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 `instancesClient.close()` method on the client to safely
       clean up any remaining background resources. */
    try (InstancesClient instancesClient = InstancesClient.create()) {
        GetInstanceRequest getInstanceRequest = GetInstanceRequest.newBuilder().setProject(project).setZone(zone).setInstance(instanceName).build();
        Instance instance = instancesClient.get(getInstanceRequest);
        // Prepare the information about disk encryption.
        CustomerEncryptionKeyProtectedDisk protectedDisk = CustomerEncryptionKeyProtectedDisk.newBuilder().setDiskEncryptionKey(CustomerEncryptionKey.newBuilder().setRawKey(key).build()).setSource(instance.getDisks(0).getSource()).build();
        InstancesStartWithEncryptionKeyRequest startWithEncryptionKeyRequest = InstancesStartWithEncryptionKeyRequest.newBuilder().addDisks(protectedDisk).build();
        StartWithEncryptionKeyInstanceRequest encryptionKeyInstanceRequest = StartWithEncryptionKeyInstanceRequest.newBuilder().setProject(project).setZone(zone).setInstance(instanceName).setInstancesStartWithEncryptionKeyRequestResource(startWithEncryptionKeyRequest).build();
        OperationFuture<Operation, Operation> operation = instancesClient.startWithEncryptionKeyAsync(encryptionKeyInstanceRequest);
        Operation response = operation.get(3, TimeUnit.MINUTES);
        if (response.getStatus() == Status.DONE) {
            System.out.println("Encrypted instance started successfully ! ");
        }
    }
}
Also used : GetInstanceRequest(com.google.cloud.compute.v1.GetInstanceRequest) Instance(com.google.cloud.compute.v1.Instance) StartWithEncryptionKeyInstanceRequest(com.google.cloud.compute.v1.StartWithEncryptionKeyInstanceRequest) InstancesClient(com.google.cloud.compute.v1.InstancesClient) InstancesStartWithEncryptionKeyRequest(com.google.cloud.compute.v1.InstancesStartWithEncryptionKeyRequest) Operation(com.google.cloud.compute.v1.Operation) CustomerEncryptionKeyProtectedDisk(com.google.cloud.compute.v1.CustomerEncryptionKeyProtectedDisk)

Example 23 with Disk

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

the class CreateInstancesAdvanced method createWithSnapshottedDataDisk.

// [END compute_instances_create_from_snapshot]
// [START compute_instances_create_from_image_plus_snapshot_disk]
/**
 * Create a new VM instance with Debian 10 operating system and data disk created from snapshot.
 *
 * @param project project ID or project number of the Cloud project you want to use.
 * @param zone name of the zone to create the instance in. For example: "us-west3-b"
 * @param instanceName name of the new virtual machine (VM) instance.
 * @param snapshotName link to the snapshot you want to use as the source of your data disk in the
 * form of: "projects/{project_name}/global/snapshots/{snapshot_name}"
 * @return Instance object.
 */
public static Instance createWithSnapshottedDataDisk(String project, String zone, String instanceName, String snapshotName) throws IOException, InterruptedException, ExecutionException, TimeoutException {
    try (ImagesClient imagesClient = ImagesClient.create()) {
        // List of public operating system (OS) images: https://cloud.google.com/compute/docs/images/os-details
        Image image = imagesClient.getFromFamily("debian-cloud", "debian-10");
        String diskType = String.format("zones/%s/diskTypes/pd-standard", zone);
        Vector<AttachedDisk> disks = new Vector<>();
        disks.add(diskFromImage(diskType, 10, true, image.getSelfLink()));
        disks.add(diskFromSnapshot(diskType, 11, false, snapshotName));
        return createWithDisks(project, zone, instanceName, disks, "n1-standard-1", "global/networks/default", null);
    }
}
Also used : AttachedDisk(com.google.cloud.compute.v1.AttachedDisk) ImagesClient(com.google.cloud.compute.v1.ImagesClient) Image(com.google.cloud.compute.v1.Image) Vector(java.util.Vector)

Example 24 with Disk

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

the class CreateInstancesAdvanced method createWithDisks.

// [END compute_instances_create_from_image_plus_snapshot_disk]
// [END compute_instances_create_from_snapshot]
// [START compute_instances_create_with_subnet]
// [START compute_instances_create_from_image_plus_snapshot_disk]
// [START compute_instances_create_from_snapshot]
// [START compute_instances_create_from_image_plus_empty_disk]
// [START compute_instances_create_from_custom_image]
// [START compute_instances_create_from_image]
/**
 * Send an instance creation request to the Compute Engine API and wait for it to complete.
 *
 * @param project project ID or project number of the Cloud project you want to use.
 * @param zone name of the zone to create the instance in. For example: "us-west3-b"
 * @param instanceName name of the new virtual machine (VM) instance.
 * @param disks a list of compute_v1.AttachedDisk objects describing the disks you want to attach
 * to your new instance.
 * @param machineType machine type of the VM being created. This value uses the following format:
 * "zones/{zone}/machineTypes/{type_name}".
 * For example: "zones/europe-west3-c/machineTypes/f1-micro"
 * @param network name of the network you want the new instance to use. For example:
 * "global/networks/default" represents the network named "default", which is created
 * automatically for each project.
 * @param subnetwork name of the subnetwork you want the new instance to use. This value uses the
 * following format: "regions/{region}/subnetworks/{subnetwork_name}"
 * @return Instance object.
 */
private static Instance createWithDisks(String project, String zone, String instanceName, Vector<AttachedDisk> disks, String machineType, String network, String subnetwork) throws IOException, InterruptedException, ExecutionException, TimeoutException {
    try (InstancesClient instancesClient = InstancesClient.create()) {
        // Use the network interface provided in the networkName argument.
        NetworkInterface networkInterface;
        if (subnetwork != null) {
            networkInterface = NetworkInterface.newBuilder().setName(network).setSubnetwork(subnetwork).build();
        } else {
            networkInterface = NetworkInterface.newBuilder().setName(network).build();
        }
        machineType = String.format("zones/%s/machineTypes/%s", zone, machineType);
        // Bind `instanceName`, `machineType`, `disk`, and `networkInterface` to an instance.
        Instance instanceResource = Instance.newBuilder().setName(instanceName).setMachineType(machineType).addAllDisks(disks).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 null;
        }
        System.out.println("Operation Status: " + response.getStatus());
        return instancesClient.get(project, zone, instanceName);
    }
}
Also used : InsertInstanceRequest(com.google.cloud.compute.v1.InsertInstanceRequest) Instance(com.google.cloud.compute.v1.Instance) InstancesClient(com.google.cloud.compute.v1.InstancesClient) NetworkInterface(com.google.cloud.compute.v1.NetworkInterface) Operation(com.google.cloud.compute.v1.Operation)

Example 25 with Disk

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

the class CreateInstancesAdvanced method createWithAdditionalDisk.

// [END compute_instances_create_from_custom_image]
// [START compute_instances_create_from_image_plus_empty_disk]
/**
 * Create a new VM instance with Debian 10 operating system and a 11 GB additional empty disk.
 *
 * @param project project ID or project number of the Cloud project you want to use.
 * @param zone name of the zone to create the instance in. For example: "us-west3-b"
 * @param instanceName name of the new virtual machine (VM) instance.
 * @return Instance object.
 */
public static Instance createWithAdditionalDisk(String project, String zone, String instanceName) throws IOException, InterruptedException, ExecutionException, TimeoutException {
    try (ImagesClient imagesClient = ImagesClient.create()) {
        // List of public operating system (OS) images: https://cloud.google.com/compute/docs/images/os-details
        Image image = imagesClient.getFromFamily("debian-cloud", "debian-10");
        String diskType = String.format("zones/%s/diskTypes/pd-standard", zone);
        Vector<AttachedDisk> disks = new Vector<>();
        disks.add(diskFromImage(diskType, 10, true, image.getSelfLink()));
        disks.add(emptyDisk(diskType, 11));
        return createWithDisks(project, zone, instanceName, disks, "n1-standard-1", "global/networks/default", null);
    }
}
Also used : AttachedDisk(com.google.cloud.compute.v1.AttachedDisk) ImagesClient(com.google.cloud.compute.v1.ImagesClient) Image(com.google.cloud.compute.v1.Image) Vector(java.util.Vector)

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