Search in sources :

Example 6 with ImagesClient

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

the class CreateWindowsOsImage method createWindowsOsImage.

// Creates a new Windows image from the specified source disk.
public static void createWindowsOsImage(String project, String zone, String sourceDiskName, String imageName, String storageLocation, boolean forceCreate) throws IOException, ExecutionException, InterruptedException, TimeoutException {
    try (ImagesClient imagesClient = ImagesClient.create();
        InstancesClient instancesClient = InstancesClient.create();
        DisksClient disksClient = DisksClient.create()) {
        Disk disk = disksClient.get(project, zone, sourceDiskName);
        // Getting instances where source disk is attached.
        for (String fullInstanceName : disk.getUsersList()) {
            Map<String, String> instanceInfo = parseInstanceName(fullInstanceName);
            Instance instance = instancesClient.get(instanceInfo.get("instanceProjectId"), instanceInfo.get("instanceZone"), instanceInfo.get("instanceName"));
            // Сhecking whether the instances is stopped.
            if (!Arrays.asList("TERMINATED", "STOPPED").contains(instance.getStatus()) && !forceCreate) {
                throw new IllegalStateException(String.format("Instance %s should be stopped. Please stop the instance using GCESysprep command or set forceCreate parameter to true (not recommended). More information here: https://cloud.google.com/compute/docs/instances/windows/creating-windows-os-image#api.", instanceInfo.get("instanceName")));
            }
        }
        if (forceCreate) {
            System.out.println("Warning: forceCreate option compromise the integrity of your image. " + "Stop the instance before you create the image if possible.");
        }
        // Create Image.
        Image image = Image.newBuilder().setName(imageName).setSourceDisk(String.format("/zones/%s/disks/%s", zone, sourceDiskName)).addStorageLocations(storageLocation.isEmpty() ? "" : storageLocation).build();
        InsertImageRequest insertImageRequest = InsertImageRequest.newBuilder().setProject(project).setForceCreate(forceCreate).setImageResource(image).build();
        Operation response = imagesClient.insertAsync(insertImageRequest).get(3, TimeUnit.MINUTES);
        if (response.hasError()) {
            System.out.println("Windows OS Image creation failed ! ! " + response);
            return;
        }
        System.out.println("Image created.");
    }
}
Also used : Instance(com.google.cloud.compute.v1.Instance) InstancesClient(com.google.cloud.compute.v1.InstancesClient) InsertImageRequest(com.google.cloud.compute.v1.InsertImageRequest) Operation(com.google.cloud.compute.v1.Operation) ImagesClient(com.google.cloud.compute.v1.ImagesClient) Image(com.google.cloud.compute.v1.Image) Disk(com.google.cloud.compute.v1.Disk) DisksClient(com.google.cloud.compute.v1.DisksClient)

Example 7 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)

Example 8 with ImagesClient

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

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

the class InstancesAdvancedIT method deleteImage.

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

Example 10 with ImagesClient

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

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