Search in sources :

Example 76 with Instance

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

the class CreateInstancesAdvanced method createFromPublicImage.

// [END compute_instances_create_from_image]
// [END compute_instances_create_from_custom_image]
// [END compute_instances_create_from_image_plus_empty_disk]
// [END compute_instances_create_from_snapshot]
// [END compute_instances_create_from_image_plus_snapshot_disk]
// [END compute_instances_create_with_subnet]
// [START compute_instances_create_from_image]
/**
 * Create a new VM instance with Debian 10 operating system.
 *
 * @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 createFromPublicImage(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()));
        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 77 with Instance

use of com.google.cloud.compute.v1.Instance 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 78 with Instance

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

Example 79 with Instance

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

the class CreateTemplateWithSubnet method createTemplateWithSubnet.

// Create an instance template that uses a provided subnet.
public static void createTemplateWithSubnet(String projectId, String network, String subnetwork, String templateName) throws IOException, ExecutionException, InterruptedException, TimeoutException {
    try (InstanceTemplatesClient instanceTemplatesClient = InstanceTemplatesClient.create();
        GlobalOperationsClient globalOperationsClient = GlobalOperationsClient.create()) {
        AttachedDisk disk = AttachedDisk.newBuilder().setInitializeParams(AttachedDiskInitializeParams.newBuilder().setSourceImage(String.format("projects/%s/global/images/family/%s", "debian-cloud", "debian-11")).setDiskSizeGb(250).build()).setAutoDelete(true).setBoot(true).build();
        InstanceProperties instanceProperties = InstanceProperties.newBuilder().addDisks(disk).setMachineType("e2-standard-4").addNetworkInterfaces(NetworkInterface.newBuilder().setNetwork(network).setSubnetwork(subnetwork).build()).build();
        InstanceTemplate instanceTemplate = InstanceTemplate.newBuilder().setName(templateName).setProperties(instanceProperties).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("Template creation from subnet failed ! ! " + response);
            return;
        }
        System.out.printf("Template creation from subnet operation status %s: %s", templateName, response.getStatus());
    }
}
Also used : InstanceProperties(com.google.cloud.compute.v1.InstanceProperties) AttachedDisk(com.google.cloud.compute.v1.AttachedDisk) GlobalOperationsClient(com.google.cloud.compute.v1.GlobalOperationsClient) Operation(com.google.cloud.compute.v1.Operation) InsertInstanceTemplateRequest(com.google.cloud.compute.v1.InsertInstanceTemplateRequest) InstanceTemplatesClient(com.google.cloud.compute.v1.InstanceTemplatesClient) InstanceTemplate(com.google.cloud.compute.v1.InstanceTemplate)

Example 80 with Instance

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

the class GetInstanceTemplate method getInstanceTemplate.

// Retrieve an instance template, which you can use to create virtual machine
// (VM) instances and managed instance groups (MIGs).
public static void getInstanceTemplate(String projectId, String templateName) throws IOException {
    try (InstanceTemplatesClient instanceTemplatesClient = InstanceTemplatesClient.create()) {
        GetInstanceTemplateRequest getInstanceTemplateRequest = GetInstanceTemplateRequest.newBuilder().setProject(projectId).setInstanceTemplate(templateName).build();
        InstanceTemplate instanceTemplate = instanceTemplatesClient.get(getInstanceTemplateRequest);
        System.out.println("Instance Template retrieved: " + instanceTemplate.getName());
    }
}
Also used : GetInstanceTemplateRequest(com.google.cloud.compute.v1.GetInstanceTemplateRequest) InstanceTemplatesClient(com.google.cloud.compute.v1.InstanceTemplatesClient) InstanceTemplate(com.google.cloud.compute.v1.InstanceTemplate)

Aggregations

Test (org.junit.Test)41 Instance (com.google.cloud.redis.v1beta1.Instance)34 CloudRedisClient (com.google.cloud.redis.v1beta1.CloudRedisClient)30 InstancesClient (com.google.cloud.compute.v1.InstancesClient)29 Operation (com.google.cloud.compute.v1.Operation)29 Instance (com.google.cloud.compute.v1.Instance)25 ByteString (com.google.protobuf.ByteString)17 AttachedDisk (com.google.cloud.compute.v1.AttachedDisk)16 AbstractMessage (com.google.protobuf.AbstractMessage)13 InsertInstanceRequest (com.google.cloud.compute.v1.InsertInstanceRequest)11 Instance (com.google.spanner.admin.instance.v1.Instance)11 Instance (com.google.bigtable.admin.v2.Instance)10 InstanceTemplatesClient (com.google.cloud.compute.v1.InstanceTemplatesClient)9 Instance (com.google.cloud.notebooks.v1beta1.Instance)9 ArrayList (java.util.ArrayList)8 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)7 InstanceTemplate (com.google.cloud.compute.v1.InstanceTemplate)7 NetworkInterface (com.google.cloud.compute.v1.NetworkInterface)7 Any (com.google.protobuf.Any)7 FieldMask (com.google.protobuf.FieldMask)7