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.");
}
}
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();
}
}
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());
}
}
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);
}
}
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);
}
}
Aggregations