Ejemplo n.º 1
0
  protected boolean isBlankTemplate() {
    if (getVmTemplate() != null) {
      return VmTemplateHandler.BLANK_VM_TEMPLATE_ID.equals(getVmTemplate().getId());
    }

    return false;
  }
  @Override
  protected boolean canDoAction() {
    Guid vmTemplateId = getVmTemplateId();
    VmTemplate template = getVmTemplate();

    if (!super.canDoAction()) {
      return false;
    }

    boolean isInstanceType = getVmTemplate().getTemplateType() == VmEntityType.INSTANCE_TYPE;

    if (getVdsGroup() == null && !isInstanceType) {
      addCanDoActionMessage(EngineMessage.ACTION_TYPE_FAILED_CLUSTER_CAN_NOT_BE_EMPTY);
      return false;
    }

    // check template exists
    if (!validate(templateExists())) {
      return false;
    }
    // check not blank template
    if (VmTemplateHandler.BLANK_VM_TEMPLATE_ID.equals(vmTemplateId)) {
      return failCanDoAction(EngineMessage.VMT_CANNOT_REMOVE_BLANK_TEMPLATE);
    }

    // check storage pool valid
    if (!isInstanceType && !validate(new StoragePoolValidator(getStoragePool()).isUp())) {
      return false;
    }

    // check if delete protected
    if (template.isDeleteProtected()) {
      return failCanDoAction(EngineMessage.ACTION_TYPE_FAILED_DELETE_PROTECTION_ENABLED);
    }

    if (!isInstanceType) {
      fetchImageTemplates();
    }

    // populate all the domains of the template
    Set<Guid> allDomainsList = getStorageDomainsByDisks(imageTemplates, true);
    getParameters().setStorageDomainsList(new ArrayList<>(allDomainsList));

    // check template images for selected domains
    ArrayList<String> canDoActionMessages = getReturnValue().getCanDoActionMessages();
    for (Guid domainId : getParameters().getStorageDomainsList()) {
      if (!isVmTemplateImagesReady(
          getVmTemplate(),
          domainId,
          canDoActionMessages,
          getParameters().getCheckDisksExists(),
          true,
          false,
          true,
          storageToDisksMap.get(domainId))) {
        return false;
      }
    }

    // check no vms from this template on selected domains
    List<VM> vms = getVmDao().getAllWithTemplate(vmTemplateId);
    List<String> problematicVmNames = new ArrayList<>();
    for (VM vm : vms) {
      problematicVmNames.add(vm.getName());
    }

    if (!problematicVmNames.isEmpty()) {
      return failCanDoAction(
          EngineMessage.VMT_CANNOT_REMOVE_DETECTED_DERIVED_VM,
          String.format("$vmsList %1$s", StringUtils.join(problematicVmNames, ",")));
    }

    if (template.isBaseTemplate() && !tryLockSubVersionIfExists()) {
      return false;
    }

    if (!isInstanceType && !validate(checkNoDisksBasedOnTemplateDisks())) {
      return false;
    }

    return true;
  }