@Test public void testRemoveImageFromSnapshotConfiguration() throws OvfReaderException { Guid vmId = Guid.newGuid(); VM vm = new VM(); vm.setId(vmId); vm.setStoragePoolId(Guid.newGuid()); vm.setVmtName(RandomUtils.instance().nextString(10)); vm.setOrigin(OriginType.OVIRT); vm.setDbGeneration(1L); Guid vmSnapshotId = Guid.newGuid(); DiskImage disk1 = addTestDisk(vm, vmSnapshotId); DiskImage disk2 = addTestDisk(vm, vmSnapshotId); OvfManager ovfManager = new OvfManager(); ArrayList<DiskImage> disks = new ArrayList<DiskImage>(Arrays.asList(disk1, disk2)); String ovf = ovfManager.ExportVm(vm, disks, Version.v3_1); Snapshot snap = new Snapshot(); snap.setVmConfiguration(ovf); snap.setId(vmSnapshotId); when(snapshotDAO.get(vmSnapshotId)).thenReturn(snap); doReturn(disk2).when(cmd).getDiskImage(); doReturn(disk2).when(cmd).getImage(); doReturn(disk2.getId()).when(cmd).getImageId(); Snapshot actual = cmd.prepareSnapshotConfigWithoutImageSingleImage(vmSnapshotId, disk2.getImageId()); String actualOvf = actual.getVmConfiguration(); ArrayList<DiskImage> actualImages = new ArrayList<DiskImage>(); ovfManager.ImportVm(actualOvf, new VM(), actualImages, new ArrayList<VmNetworkInterface>()); assertEquals("Wrong number of disks", 1, actualImages.size()); assertEquals("Wrong disk", disk1, actualImages.get(0)); }
/** Adds the given vm metadata to the given map */ private String buildMetadataDictionaryForVm(VM vm) { ArrayList<DiskImage> AllVmImages = new ArrayList<DiskImage>(); List<DiskImage> filteredDisks = ImagesHandler.filterImageDisks(vm.getDiskList(), false, true, true); for (DiskImage diskImage : filteredDisks) { List<DiskImage> images = ImagesHandler.getAllImageSnapshots(diskImage.getImageId()); AllVmImages.addAll(images); } return ovfManager.ExportVm(vm, AllVmImages, ClusterUtils.getCompatibilityVersion(vm)); }
/** * parses a given ovf to a VmTemplate, initialized with images and interfaces. * * @param ovf * @return VmTemplate that represents the given ovf data * @throws OvfReaderException */ public VmTemplate readVmTemplateFromOvf(String ovf) throws OvfReaderException { ArrayList<DiskImage> diskImages = new ArrayList<DiskImage>(); ArrayList<VmNetworkInterface> interfaces = new ArrayList<VmNetworkInterface>(); VmTemplate template = new VmTemplate(); ovfManager.ImportTemplate(ovf, template, diskImages, interfaces); template.setInterfaces(interfaces); // add disk map for (DiskImage disk : diskImages) { template.getDiskTemplateMap().put(disk.getId(), disk); } return template; }
/** * Prepare a single {@link org.ovirt.engine.core.common.businessentities.Snapshot} object * representing a snapshot of a given VM without the given disk, substituting a new disk in its * place if a new disk is provided to the method. */ public static Snapshot prepareSnapshotConfigWithAlternateImage( Snapshot snapshot, Guid oldImageId, DiskImage newImage) { try { OvfManager ovfManager = new OvfManager(); String snapConfig = snapshot.getVmConfiguration(); if (snapshot.isVmConfigurationAvailable() && snapConfig != null) { VM vmSnapshot = new VM(); ArrayList<DiskImage> snapshotImages = new ArrayList<>(); ovfManager.importVm(snapConfig, vmSnapshot, snapshotImages, new ArrayList<>()); // Remove the image from the disk list Iterator<DiskImage> diskIter = snapshotImages.iterator(); while (diskIter.hasNext()) { DiskImage imageInList = diskIter.next(); if (imageInList.getImageId().equals(oldImageId)) { log.debug( "Recreating vmSnapshot '{}' without the image '{}'", snapshot.getId(), oldImageId); diskIter.remove(); break; } } if (newImage != null) { log.debug( "Adding image '{}' to vmSnapshot '{}'", newImage.getImageId(), snapshot.getId()); snapshotImages.add(newImage); } String newOvf = ovfManager.exportVm( vmSnapshot, snapshotImages, ClusterUtils.getCompatibilityVersion(vmSnapshot)); snapshot.setVmConfiguration(newOvf); } } catch (OvfReaderException e) { log.error("Can't remove image '{}' from snapshot '{}'", oldImageId, snapshot.getId()); } return snapshot; }
/** * parses a given ovf to a vm, intialized with images and interfaces. * * @param ovf * @return VM that represents the given ovf data * @throws OvfReaderException */ public VM readVmFromOvf(String ovf) throws OvfReaderException { VM vm = new VM(); ArrayList<DiskImage> diskImages = new ArrayList<DiskImage>(); ArrayList<VmNetworkInterface> interfaces = new ArrayList<VmNetworkInterface>(); ovfManager.ImportVm(ovf, vm, diskImages, interfaces); // add images vm.setImages(diskImages); // add interfaces vm.setInterfaces(interfaces); // add disk map Map<Guid, List<DiskImage>> images = ImagesHandler.getImagesLeaf(diskImages); for (Map.Entry<Guid, List<DiskImage>> entry : images.entrySet()) { List<DiskImage> list = entry.getValue(); vm.getDiskMap().put(entry.getKey(), list.get(list.size() - 1)); } return vm; }
public boolean isOvfTemplate(String ovfstring) throws OvfReaderException { return ovfManager.IsOvfTemplate(ovfstring); }