@Override
public ListenableFuture<Image> createImage(ImageTemplate template) {
checkState(template instanceof CloneImageTemplate,
" cloudstack only currently supports creating images through cloning.");
CloneImageTemplate cloneTemplate = (CloneImageTemplate) template;
VirtualMachine vm = client.getVirtualMachineApi().getVirtualMachine(cloneTemplate.getSourceNodeId());
String stopJob = client.getVirtualMachineApi().stopVirtualMachine(vm.getId());
jobComplete.apply(stopJob);
Set<Volume> volumes = client.getVolumeApi().listVolumes(ListVolumesOptions.Builder.virtualMachineId(vm.getId()));
Volume volume = Iterables.getOnlyElement(volumes);
CreateTemplateOptions options = CreateTemplateOptions.Builder.volumeId(volume.getId());
AsyncCreateResponse templateJob = client.getTemplateApi().createTemplate(TemplateMetadata.builder()
.name(cloneTemplate.getName())
.osTypeId(vm.getGuestOSId())
.displayText(cloneTemplate.getName())
.build(), options);
Template newTemplate = blockUntilJobCompletesAndReturnResult.<Template>apply(templateJob);
logger.info(">> Registered new template %s, waiting for it to become available.", newTemplate.getId());
final AtomicReference<Image> image = Atomics.newReference(new ImageBuilder()
.location(find(locations.get(), idEquals(vm.getZoneId())))
.id(newTemplate.getId())
.providerId(newTemplate.getId())
.description(cloneTemplate.getName())
.operatingSystem(OperatingSystem.builder().description(cloneTemplate.getName()).build())
.status(Image.Status.PENDING).build());
return userExecutor.submit(new Callable<Image>() {
@Override
public Image call() throws Exception {