Source:
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.getVirtualMachineClient().getVirtualMachine(cloneTemplate.getSourceNodeId()); String stopJob = client.getVirtualMachineClient().stopVirtualMachine(vm.getId()); boolean stopComplete = jobComplete.apply(stopJob); Set<Volume> volumes = client.getVolumeClient().listVolumes(ListVolumesOptions.Builder.virtualMachineId(vm.getId())); Volume volume = Iterables.getOnlyElement(volumes); CreateTemplateOptions options = CreateTemplateOptions.Builder.volumeId(volume.getId()); AsyncCreateResponse templateJob = client.getTemplateClient().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()); System.out.println("locations: " + locations.get()); 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 { if (imageAvailablePredicate.apply(image)) return image.get(); throw new UncheckedTimeoutException("Image was not created within the time limit: " + image.get()); } }); }

Target:
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.getVirtualMachineClient().getVirtualMachine(cloneTemplate.getSourceNodeId()); String stopJob = client.getVirtualMachineClient().stopVirtualMachine(vm.getId()); jobComplete.apply(stopJob); Set<Volume> volumes = client.getVolumeClient().listVolumes(ListVolumesOptions.Builder.virtualMachineId(vm.getId())); Volume volume = Iterables.getOnlyElement(volumes); CreateTemplateOptions options = CreateTemplateOptions.Builder.volumeId(volume.getId()); AsyncCreateResponse templateJob = client.getTemplateClient().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 { if (imageAvailablePredicate.apply(image)) return image.get(); throw new UncheckedTimeoutException("Image was not created within the time limit: " + image.get()); } }); }