Search in sources :

Example 1 with ImagesClient

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

the class CreateInstancesAdvanced method createWithSubnetwork.

// [END compute_instances_create_from_image_plus_snapshot_disk]
// [START compute_instances_create_from_image]
/**
 * Create a new VM instance with Debian 10 operating system in specified network and subnetwork.
 *
 * @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 networkLink 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 subnetworkLink 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.
 */
public static Instance createWithSubnetwork(String project, String zone, String instanceName, String networkLink, String subnetworkLink) throws IOException, InterruptedException, ExecutionException {
    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", networkLink, subnetworkLink);
    }
}
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 2 with ImagesClient

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

the class ListImages method listImagesByPage.

// [END compute_images_list]
// [START compute_images_list_page]
// Prints a list of all non-deprecated image names available in a given project,
// divided into pages as returned by the Compute Engine API.
public static void listImagesByPage(String project, int pageSize) throws IOException {
    // safely clean up any remaining background resources.
    try (ImagesClient imagesClient = ImagesClient.create()) {
        // Listing only non-deprecated images to reduce the size of the reply.
        ListImagesRequest imagesRequest = ListImagesRequest.newBuilder().setProject(project).setMaxResults(pageSize).setFilter("deprecated.state != DEPRECATED").build();
        // Use the `iteratePages` attribute of returned iterable to have more granular control of
        // iteration over paginated results from the API. Each time you want to access the
        // next page, the library retrieves that page from the API.
        int pageNumber = 1;
        for (ListPage page : imagesClient.list(imagesRequest).iteratePages()) {
            System.out.println("Page Number: " + pageNumber++);
            for (Image image : page.getValues()) {
                System.out.println(image.getName());
            }
        }
    }
}
Also used : ListPage(com.google.cloud.compute.v1.ImagesClient.ListPage) ImagesClient(com.google.cloud.compute.v1.ImagesClient) ListImagesRequest(com.google.cloud.compute.v1.ListImagesRequest) Image(com.google.cloud.compute.v1.Image)

Example 3 with ImagesClient

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

the class ListImages method listImages.

// [START compute_images_list]
// Prints a list of all non-deprecated image names available in given project.
public static void listImages(String project) throws IOException {
    // safely clean up any remaining background resources.
    try (ImagesClient imagesClient = ImagesClient.create()) {
        // Listing only non-deprecated images to reduce the size of the reply.
        ListImagesRequest imagesRequest = ListImagesRequest.newBuilder().setProject(project).setMaxResults(100).setFilter("deprecated.state != DEPRECATED").build();
        // Although the `setMaxResults` parameter is specified in the request, the iterable returned
        // by the `list()` method hides the pagination mechanic. The library makes multiple
        // requests to the API for you, so you can simply iterate over all the images.
        int imageCount = 0;
        for (Image image : imagesClient.list(imagesRequest).iterateAll()) {
            imageCount++;
            System.out.println(image.getName());
        }
        System.out.printf("Image count in %s is: %s", project, imageCount);
    }
}
Also used : ImagesClient(com.google.cloud.compute.v1.ImagesClient) ListImagesRequest(com.google.cloud.compute.v1.ListImagesRequest) Image(com.google.cloud.compute.v1.Image)

Example 4 with ImagesClient

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

the class SnippetsIT method createImage.

private static Image createImage(Disk srcDisk) throws IOException, InterruptedException, ExecutionException {
    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();
        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 5 with ImagesClient

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

the class SnippetsIT method deleteImage.

private static void deleteImage(Image image) throws IOException, InterruptedException, ExecutionException {
    try (ImagesClient imagesClient = ImagesClient.create()) {
        OperationFuture<Operation, Operation> operation = imagesClient.deleteAsync(PROJECT_ID, image.getName());
        operation.get();
    }
}
Also used : Operation(com.google.cloud.compute.v1.Operation) ImagesClient(com.google.cloud.compute.v1.ImagesClient)

Aggregations

ImagesClient (com.google.cloud.compute.v1.ImagesClient)8 Image (com.google.cloud.compute.v1.Image)7 AttachedDisk (com.google.cloud.compute.v1.AttachedDisk)4 Vector (java.util.Vector)4 ListImagesRequest (com.google.cloud.compute.v1.ListImagesRequest)2 Operation (com.google.cloud.compute.v1.Operation)2 ListPage (com.google.cloud.compute.v1.ImagesClient.ListPage)1