Search in sources :

Example 11 with ImagesClient

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

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

ImagesClient (com.google.cloud.compute.v1.ImagesClient)12 Image (com.google.cloud.compute.v1.Image)9 Operation (com.google.cloud.compute.v1.Operation)6 AttachedDisk (com.google.cloud.compute.v1.AttachedDisk)4 Vector (java.util.Vector)4 ListImagesRequest (com.google.cloud.compute.v1.ListImagesRequest)2 Disk (com.google.cloud.compute.v1.Disk)1 DisksClient (com.google.cloud.compute.v1.DisksClient)1 ListPage (com.google.cloud.compute.v1.ImagesClient.ListPage)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 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 PrintStream (java.io.PrintStream)1 AfterAll (org.junit.jupiter.api.AfterAll)1