Example #1
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));
  }
  /**
   * 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;
  }