Search in sources :

Example 1 with InsertImageRequest

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

Aggregations

Disk (com.google.cloud.compute.v1.Disk)1 DisksClient (com.google.cloud.compute.v1.DisksClient)1 Image (com.google.cloud.compute.v1.Image)1 ImagesClient (com.google.cloud.compute.v1.ImagesClient)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 Operation (com.google.cloud.compute.v1.Operation)1