Search in sources :

Example 1 with UpdateTemplateCmd

use of org.apache.cloudstack.api.command.user.template.UpdateTemplateCmd in project cloudstack by apache.

the class TemplateManagerImpl method updateTemplateOrIso.

private VMTemplateVO updateTemplateOrIso(BaseUpdateTemplateOrIsoCmd cmd) {
    Long id = cmd.getId();
    String name = cmd.getTemplateName();
    String displayText = cmd.getDisplayText();
    String format = cmd.getFormat();
    Long guestOSId = cmd.getOsTypeId();
    Boolean passwordEnabled = cmd.getPasswordEnabled();
    Boolean sshKeyEnabled = cmd.isSshKeyEnabled();
    Boolean isDynamicallyScalable = cmd.isDynamicallyScalable();
    Boolean isRoutingTemplate = cmd.isRoutingType();
    Boolean bootable = cmd.getBootable();
    Boolean requiresHvm = cmd.getRequiresHvm();
    Integer sortKey = cmd.getSortKey();
    Map details = cmd.getDetails();
    Account account = CallContext.current().getCallingAccount();
    boolean cleanupDetails = cmd.isCleanupDetails();
    // verify that template exists
    VMTemplateVO template = _tmpltDao.findById(id);
    if (template == null || template.getRemoved() != null) {
        InvalidParameterValueException ex = new InvalidParameterValueException("unable to find template/iso with specified id");
        ex.addProxyObject(String.valueOf(id), "templateId");
        throw ex;
    }
    long oldGuestOSId = template.getGuestOSId();
    verifyTemplateId(id);
    // do a permission check
    _accountMgr.checkAccess(account, AccessType.OperateEntry, true, template);
    if (cmd.isRoutingType() != null) {
        if (!_accountService.isRootAdmin(account.getId())) {
            throw new PermissionDeniedException("Parameter isrouting can only be specified by a Root Admin, permission denied");
        }
    }
    // update template type
    TemplateType templateType = null;
    if (cmd instanceof UpdateTemplateCmd) {
        String newType = ((UpdateTemplateCmd) cmd).getTemplateType();
        if (newType != null) {
            if (!_accountService.isRootAdmin(account.getId())) {
                throw new PermissionDeniedException("Parameter templatetype can only be specified by a Root Admin, permission denied");
            }
            try {
                templateType = TemplateType.valueOf(newType.toUpperCase());
            } catch (IllegalArgumentException ex) {
                throw new InvalidParameterValueException("Please specify a valid templatetype: ROUTING / SYSTEM / USER / BUILTIN / PERHOST");
            }
        }
        if (templateType != null && cmd.isRoutingType() != null && (TemplateType.ROUTING.equals(templateType) != cmd.isRoutingType())) {
            throw new InvalidParameterValueException("Please specify a valid templatetype (consistent with isrouting parameter).");
        }
        if (templateType != null && (templateType == TemplateType.SYSTEM || templateType == TemplateType.BUILTIN) && !template.isCrossZones()) {
            throw new InvalidParameterValueException("System and Builtin templates must be cross zone");
        }
    }
    // update is needed if any of the fields below got filled by the user
    boolean updateNeeded = !(name == null && displayText == null && format == null && guestOSId == null && passwordEnabled == null && bootable == null && sshKeyEnabled == null && requiresHvm == null && sortKey == null && isDynamicallyScalable == null && isRoutingTemplate == null && templateType == null && // update details in every case except this one
    (!cleanupDetails && details == null));
    if (!updateNeeded) {
        return template;
    }
    template = _tmpltDao.findById(id);
    if (name != null) {
        template.setName(name);
    }
    if (displayText != null) {
        template.setDisplayText(displayText);
    }
    if (sortKey != null) {
        template.setSortKey(sortKey);
    }
    ImageFormat imageFormat = null;
    if (format != null) {
        try {
            imageFormat = ImageFormat.valueOf(format.toUpperCase());
        } catch (IllegalArgumentException e) {
            throw new InvalidParameterValueException("Image format: " + format + " is incorrect. Supported formats are " + EnumUtils.listValues(ImageFormat.values()));
        }
        template.setFormat(imageFormat);
    }
    if (guestOSId != null) {
        GuestOSVO guestOS = _guestOSDao.findById(guestOSId);
        if (guestOS == null) {
            throw new InvalidParameterValueException("Please specify a valid guest OS ID.");
        } else {
            template.setGuestOSId(guestOSId);
        }
        if (guestOSId != oldGuestOSId) {
            // vm guest os type need to be updated if template guest os id changes.
            SearchCriteria<VMInstanceVO> sc = _vmInstanceDao.createSearchCriteria();
            sc.addAnd("templateId", SearchCriteria.Op.EQ, id);
            sc.addAnd("state", SearchCriteria.Op.NEQ, State.Expunging);
            List<VMInstanceVO> vms = _vmInstanceDao.search(sc, null);
            if (vms != null && !vms.isEmpty()) {
                for (VMInstanceVO vm : vms) {
                    vm.setGuestOSId(guestOSId);
                    _vmInstanceDao.update(vm.getId(), vm);
                }
            }
        }
    }
    if (passwordEnabled != null) {
        template.setEnablePassword(passwordEnabled);
    }
    if (sshKeyEnabled != null) {
        template.setEnableSshKey(sshKeyEnabled);
    }
    if (bootable != null) {
        template.setBootable(bootable);
    }
    if (requiresHvm != null) {
        template.setRequiresHvm(requiresHvm);
    }
    if (isDynamicallyScalable != null) {
        template.setDynamicallyScalable(isDynamicallyScalable);
    }
    if (isRoutingTemplate != null) {
        if (isRoutingTemplate) {
            template.setTemplateType(TemplateType.ROUTING);
        } else if (templateType != null) {
            template.setTemplateType(templateType);
        } else {
            template.setTemplateType(TemplateType.USER);
        }
    } else if (templateType != null) {
        template.setTemplateType(templateType);
    }
    validateDetails(template, details);
    if (cleanupDetails) {
        template.setDetails(null);
        _tmpltDetailsDao.removeDetails(id);
    } else if (details != null && !details.isEmpty()) {
        template.setDetails(details);
        _tmpltDao.saveDetails(template);
    }
    _tmpltDao.update(id, template);
    return _tmpltDao.findById(id);
}
Also used : Account(com.cloud.user.Account) VMTemplateVO(com.cloud.storage.VMTemplateVO) VMInstanceVO(com.cloud.vm.VMInstanceVO) TemplateType(com.cloud.storage.Storage.TemplateType) GuestOSVO(com.cloud.storage.GuestOSVO) ImageFormat(com.cloud.storage.Storage.ImageFormat) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) UpdateTemplateCmd(org.apache.cloudstack.api.command.user.template.UpdateTemplateCmd) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)1 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)1 GuestOSVO (com.cloud.storage.GuestOSVO)1 ImageFormat (com.cloud.storage.Storage.ImageFormat)1 TemplateType (com.cloud.storage.Storage.TemplateType)1 VMTemplateVO (com.cloud.storage.VMTemplateVO)1 Account (com.cloud.user.Account)1 VMInstanceVO (com.cloud.vm.VMInstanceVO)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 UpdateTemplateCmd (org.apache.cloudstack.api.command.user.template.UpdateTemplateCmd)1