Search in sources :

Example 1 with TemplateJoinVO

use of com.cloud.api.query.vo.TemplateJoinVO in project cloudstack by apache.

the class QueryManagerImpl method listTemplates.

@Override
public ListResponse<TemplateResponse> listTemplates(ListTemplatesCmd cmd) {
    Pair<List<TemplateJoinVO>, Integer> result = searchForTemplatesInternal(cmd);
    ListResponse<TemplateResponse> response = new ListResponse<TemplateResponse>();
    ResponseView respView = ResponseView.Restricted;
    if (cmd instanceof ListTemplatesCmdByAdmin) {
        respView = ResponseView.Full;
    }
    List<TemplateResponse> templateResponses = ViewResponseHelper.createTemplateResponse(cmd.getDetails(), respView, result.first().toArray(new TemplateJoinVO[result.first().size()]));
    response.setResponses(templateResponses, result.second());
    return response;
}
Also used : ResponseView(org.apache.cloudstack.api.ResponseObject.ResponseView) ListResponse(org.apache.cloudstack.api.response.ListResponse) ListTemplatesCmdByAdmin(org.apache.cloudstack.api.command.admin.template.ListTemplatesCmdByAdmin) TemplateJoinVO(com.cloud.api.query.vo.TemplateJoinVO) ArrayList(java.util.ArrayList) List(java.util.List) TemplateResponse(org.apache.cloudstack.api.response.TemplateResponse)

Example 2 with TemplateJoinVO

use of com.cloud.api.query.vo.TemplateJoinVO in project cloudstack by apache.

the class QueryManagerImpl method searchForTemplatesInternal.

private Pair<List<TemplateJoinVO>, Integer> searchForTemplatesInternal(Long templateId, String name, String keyword, TemplateFilter templateFilter, boolean isIso, Boolean bootable, Long pageSize, Long startIndex, Long zoneId, HypervisorType hyperType, boolean showDomr, boolean onlyReady, List<Account> permittedAccounts, Account caller, ListProjectResourcesCriteria listProjectResourcesCriteria, Map<String, String> tags, boolean showRemovedTmpl, List<Long> ids, Long parentTemplateId, Boolean showUnique) {
    // check if zone is configured, if not, just return empty list
    List<HypervisorType> hypers = null;
    if (!isIso) {
        hypers = _resourceMgr.listAvailHypervisorInZone(null, null);
        if (hypers == null || hypers.isEmpty()) {
            return new Pair<List<TemplateJoinVO>, Integer>(new ArrayList<TemplateJoinVO>(), 0);
        }
    }
    VMTemplateVO template = null;
    Filter searchFilter = new Filter(TemplateJoinVO.class, "sortKey", SortKeyAscending.value(), startIndex, pageSize);
    searchFilter.addOrderBy(TemplateJoinVO.class, "tempZonePair", SortKeyAscending.value());
    SearchBuilder<TemplateJoinVO> sb = _templateJoinDao.createSearchBuilder();
    if (showUnique) {
        // select distinct templateId
        sb.select(null, Func.DISTINCT, sb.entity().getId());
    } else {
        // select distinct (templateId, zoneId) pair
        sb.select(null, Func.DISTINCT, sb.entity().getTempZonePair());
    }
    if (ids != null && !ids.isEmpty()) {
        sb.and("idIN", sb.entity().getId(), SearchCriteria.Op.IN);
    }
    SearchCriteria<TemplateJoinVO> sc = sb.create();
    // verify templateId parameter and specially handle it
    if (templateId != null) {
        // Done for backward compatibility - Bug-5221
        template = _templateDao.findByIdIncludingRemoved(templateId);
        if (template == null) {
            throw new InvalidParameterValueException("Please specify a valid template ID.");
        }
        // If ISO requested then it should be ISO.
        if (isIso && template.getFormat() != ImageFormat.ISO) {
            s_logger.error("Template Id " + templateId + " is not an ISO");
            InvalidParameterValueException ex = new InvalidParameterValueException("Specified Template Id is not an ISO");
            ex.addProxyObject(template.getUuid(), "templateId");
            throw ex;
        }
        // If ISO not requested then it shouldn't be an ISO.
        if (!isIso && template.getFormat() == ImageFormat.ISO) {
            s_logger.error("Incorrect format of the template id " + templateId);
            InvalidParameterValueException ex = new InvalidParameterValueException("Incorrect format " + template.getFormat() + " of the specified template id");
            ex.addProxyObject(template.getUuid(), "templateId");
            throw ex;
        }
        if (!template.isPublicTemplate() && caller.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN) {
            Account template_acc = _accountMgr.getAccount(template.getAccountId());
            DomainVO domain = _domainDao.findById(template_acc.getDomainId());
            _accountMgr.checkAccess(caller, domain);
        } else // if template is not public, perform permission check here
        if (!template.isPublicTemplate() && caller.getType() != Account.ACCOUNT_TYPE_ADMIN) {
            _accountMgr.checkAccess(caller, null, false, template);
        }
        // if templateId is specified, then we will just use the id to
        // search and ignore other query parameters
        sc.addAnd("id", SearchCriteria.Op.EQ, templateId);
    } else {
        DomainVO domain = null;
        if (!permittedAccounts.isEmpty()) {
            domain = _domainDao.findById(permittedAccounts.get(0).getDomainId());
        } else {
            domain = _domainDao.findById(Domain.ROOT_DOMAIN);
        }
        setIdsListToSearchCriteria(sc, ids);
        // add criteria for project or not
        if (listProjectResourcesCriteria == ListProjectResourcesCriteria.SkipProjectResources) {
            sc.addAnd("accountType", SearchCriteria.Op.NEQ, Account.ACCOUNT_TYPE_PROJECT);
        } else if (listProjectResourcesCriteria == ListProjectResourcesCriteria.ListProjectResourcesOnly) {
            sc.addAnd("accountType", SearchCriteria.Op.EQ, Account.ACCOUNT_TYPE_PROJECT);
        }
        // add criteria for domain path in case of domain admin
        if ((templateFilter == TemplateFilter.self || templateFilter == TemplateFilter.selfexecutable) && (caller.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN || caller.getType() == Account.ACCOUNT_TYPE_RESOURCE_DOMAIN_ADMIN)) {
            sc.addAnd("domainPath", SearchCriteria.Op.LIKE, domain.getPath() + "%");
        }
        List<Long> relatedDomainIds = new ArrayList<Long>();
        List<Long> permittedAccountIds = new ArrayList<Long>();
        if (!permittedAccounts.isEmpty()) {
            for (Account account : permittedAccounts) {
                permittedAccountIds.add(account.getId());
                boolean publicTemplates = (templateFilter == TemplateFilter.featured || templateFilter == TemplateFilter.community);
                // get all parent domain ID's all the way till root domain
                DomainVO domainTreeNode = null;
                // if template filter is featured, or community, all child domains should be included in search
                if (publicTemplates) {
                    domainTreeNode = _domainDao.findById(Domain.ROOT_DOMAIN);
                } else {
                    domainTreeNode = _domainDao.findById(account.getDomainId());
                }
                relatedDomainIds.add(domainTreeNode.getId());
                while (domainTreeNode.getParent() != null) {
                    domainTreeNode = _domainDao.findById(domainTreeNode.getParent());
                    relatedDomainIds.add(domainTreeNode.getId());
                }
                // get all child domain ID's
                if (_accountMgr.isAdmin(account.getId()) || publicTemplates) {
                    List<DomainVO> allChildDomains = _domainDao.findAllChildren(domainTreeNode.getPath(), domainTreeNode.getId());
                    for (DomainVO childDomain : allChildDomains) {
                        relatedDomainIds.add(childDomain.getId());
                    }
                }
            }
        }
        // control different template filters
        if (templateFilter == TemplateFilter.featured || templateFilter == TemplateFilter.community) {
            sc.addAnd("publicTemplate", SearchCriteria.Op.EQ, true);
            if (templateFilter == TemplateFilter.featured) {
                sc.addAnd("featured", SearchCriteria.Op.EQ, true);
            } else {
                sc.addAnd("featured", SearchCriteria.Op.EQ, false);
            }
            if (!permittedAccounts.isEmpty()) {
                SearchCriteria<TemplateJoinVO> scc = _templateJoinDao.createSearchCriteria();
                scc.addOr("domainId", SearchCriteria.Op.IN, relatedDomainIds.toArray());
                scc.addOr("domainId", SearchCriteria.Op.NULL);
                sc.addAnd("domainId", SearchCriteria.Op.SC, scc);
            }
        } else if (templateFilter == TemplateFilter.self || templateFilter == TemplateFilter.selfexecutable) {
            if (!permittedAccounts.isEmpty()) {
                sc.addAnd("accountId", SearchCriteria.Op.IN, permittedAccountIds.toArray());
            }
        } else if (templateFilter == TemplateFilter.sharedexecutable || templateFilter == TemplateFilter.shared) {
            // only show templates shared by others
            sc.addAnd("sharedAccountId", SearchCriteria.Op.IN, permittedAccountIds.toArray());
        } else if (templateFilter == TemplateFilter.executable) {
            SearchCriteria<TemplateJoinVO> scc = _templateJoinDao.createSearchCriteria();
            scc.addOr("publicTemplate", SearchCriteria.Op.EQ, true);
            if (!permittedAccounts.isEmpty()) {
                scc.addOr("accountId", SearchCriteria.Op.IN, permittedAccountIds.toArray());
            }
            sc.addAnd("publicTemplate", SearchCriteria.Op.SC, scc);
        } else if (templateFilter == TemplateFilter.all && caller.getType() != Account.ACCOUNT_TYPE_ADMIN) {
            SearchCriteria<TemplateJoinVO> scc = _templateJoinDao.createSearchCriteria();
            scc.addOr("publicTemplate", SearchCriteria.Op.EQ, true);
            if (listProjectResourcesCriteria == ListProjectResourcesCriteria.SkipProjectResources) {
                scc.addOr("domainPath", SearchCriteria.Op.LIKE, _domainDao.findById(caller.getDomainId()).getPath() + "%");
            } else {
                if (!permittedAccounts.isEmpty()) {
                    scc.addOr("accountId", SearchCriteria.Op.IN, permittedAccountIds.toArray());
                    scc.addOr("sharedAccountId", SearchCriteria.Op.IN, permittedAccountIds.toArray());
                }
            }
            sc.addAnd("publicTemplate", SearchCriteria.Op.SC, scc);
        }
    }
    return templateChecks(isIso, hypers, tags, name, keyword, hyperType, onlyReady, bootable, zoneId, showDomr, showRemovedTmpl, parentTemplateId, showUnique, searchFilter, sc);
}
Also used : Account(com.cloud.user.Account) VMTemplateVO(com.cloud.storage.VMTemplateVO) ArrayList(java.util.ArrayList) SearchCriteria(com.cloud.utils.db.SearchCriteria) HypervisorType(com.cloud.hypervisor.Hypervisor.HypervisorType) DomainVO(com.cloud.domain.DomainVO) TemplateFilter(com.cloud.template.VirtualMachineTemplate.TemplateFilter) Filter(com.cloud.utils.db.Filter) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) TemplateJoinVO(com.cloud.api.query.vo.TemplateJoinVO) Pair(com.cloud.utils.Pair)

Example 3 with TemplateJoinVO

use of com.cloud.api.query.vo.TemplateJoinVO in project cloudstack by apache.

the class QueryManagerImpl method templateChecks.

private Pair<List<TemplateJoinVO>, Integer> templateChecks(boolean isIso, List<HypervisorType> hypers, Map<String, String> tags, String name, String keyword, HypervisorType hyperType, boolean onlyReady, Boolean bootable, Long zoneId, boolean showDomr, boolean showRemovedTmpl, Long parentTemplateId, Boolean showUnique, Filter searchFilter, SearchCriteria<TemplateJoinVO> sc) {
    if (!isIso) {
        // add hypervisor criteria for template case
        if (hypers != null && !hypers.isEmpty()) {
            String[] relatedHypers = new String[hypers.size()];
            for (int i = 0; i < hypers.size(); i++) {
                relatedHypers[i] = hypers.get(i).toString();
            }
            sc.addAnd("hypervisorType", SearchCriteria.Op.IN, relatedHypers);
        }
    }
    // add tags criteria
    if (tags != null && !tags.isEmpty()) {
        SearchCriteria<TemplateJoinVO> scc = _templateJoinDao.createSearchCriteria();
        for (Map.Entry<String, String> entry : tags.entrySet()) {
            SearchCriteria<TemplateJoinVO> scTag = _templateJoinDao.createSearchCriteria();
            scTag.addAnd("tagKey", SearchCriteria.Op.EQ, entry.getKey());
            scTag.addAnd("tagValue", SearchCriteria.Op.EQ, entry.getValue());
            if (isIso) {
                scTag.addAnd("tagResourceType", SearchCriteria.Op.EQ, ResourceObjectType.ISO);
            } else {
                scTag.addAnd("tagResourceType", SearchCriteria.Op.EQ, ResourceObjectType.Template);
            }
            scc.addOr("tagKey", SearchCriteria.Op.SC, scTag);
        }
        sc.addAnd("tagKey", SearchCriteria.Op.SC, scc);
    }
    if (keyword != null) {
        sc.addAnd("name", SearchCriteria.Op.LIKE, "%" + keyword + "%");
    } else if (name != null) {
        sc.addAnd("name", SearchCriteria.Op.EQ, name);
    }
    SearchCriteria.Op op = isIso ? Op.EQ : Op.NEQ;
    sc.addAnd("format", op, "ISO");
    if (!hyperType.equals(HypervisorType.None)) {
        sc.addAnd("hypervisorType", SearchCriteria.Op.EQ, hyperType);
    }
    if (bootable != null) {
        sc.addAnd("bootable", SearchCriteria.Op.EQ, bootable);
    }
    if (onlyReady) {
        SearchCriteria<TemplateJoinVO> readySc = _templateJoinDao.createSearchCriteria();
        readySc.addOr("state", SearchCriteria.Op.EQ, TemplateState.Ready);
        readySc.addOr("format", SearchCriteria.Op.EQ, ImageFormat.BAREMETAL);
        SearchCriteria<TemplateJoinVO> isoPerhostSc = _templateJoinDao.createSearchCriteria();
        isoPerhostSc.addAnd("format", SearchCriteria.Op.EQ, ImageFormat.ISO);
        isoPerhostSc.addAnd("templateType", SearchCriteria.Op.EQ, TemplateType.PERHOST);
        readySc.addOr("templateType", SearchCriteria.Op.SC, isoPerhostSc);
        sc.addAnd("state", SearchCriteria.Op.SC, readySc);
    }
    if (!showDomr) {
        // excluding system template
        sc.addAnd("templateType", SearchCriteria.Op.NEQ, Storage.TemplateType.SYSTEM);
    }
    if (zoneId != null) {
        SearchCriteria<TemplateJoinVO> zoneSc = _templateJoinDao.createSearchCriteria();
        zoneSc.addOr("dataCenterId", SearchCriteria.Op.EQ, zoneId);
        zoneSc.addOr("dataStoreScope", SearchCriteria.Op.EQ, ScopeType.REGION);
        // handle the case where TemplateManager.VMWARE_TOOLS_ISO and TemplateManager.VMWARE_TOOLS_ISO do not
        // have data_center information in template_view
        SearchCriteria<TemplateJoinVO> isoPerhostSc = _templateJoinDao.createSearchCriteria();
        isoPerhostSc.addAnd("format", SearchCriteria.Op.EQ, ImageFormat.ISO);
        isoPerhostSc.addAnd("templateType", SearchCriteria.Op.EQ, TemplateType.PERHOST);
        zoneSc.addOr("templateType", SearchCriteria.Op.SC, isoPerhostSc);
        sc.addAnd("dataCenterId", SearchCriteria.Op.SC, zoneSc);
    }
    if (parentTemplateId != null) {
        sc.addAnd("parentTemplateId", SearchCriteria.Op.EQ, parentTemplateId);
    }
    // don't return removed template, this should not be needed since we
    // changed annotation for removed field in TemplateJoinVO.
    // sc.addAnd("removed", SearchCriteria.Op.NULL);
    // search unique templates and find details by Ids
    Pair<List<TemplateJoinVO>, Integer> uniqueTmplPair = null;
    if (showRemovedTmpl) {
        uniqueTmplPair = _templateJoinDao.searchIncludingRemovedAndCount(sc, searchFilter);
    } else {
        sc.addAnd("templateState", SearchCriteria.Op.IN, new State[] { State.Active, State.UploadAbandoned, State.UploadError, State.NotUploaded, State.UploadInProgress });
        if (showUnique) {
            final String[] distinctColumns = { "id" };
            uniqueTmplPair = _templateJoinDao.searchAndDistinctCount(sc, searchFilter, distinctColumns);
        } else {
            final String[] distinctColumns = { "temp_zone_pair" };
            uniqueTmplPair = _templateJoinDao.searchAndDistinctCount(sc, searchFilter, distinctColumns);
        }
    }
    return findTemplatesByIdOrTempZonePair(uniqueTmplPair, showRemovedTmpl, showUnique);
// TODO: revisit the special logic for iso search in
// VMTemplateDaoImpl.searchForTemplates and understand why we need to
// specially handle ISO. The original logic is very twisted and no idea
// about what the code was doing.
}
Also used : Op(com.cloud.utils.db.SearchCriteria.Op) TemplateJoinVO(com.cloud.api.query.vo.TemplateJoinVO) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) HashMap(java.util.HashMap) SearchCriteria(com.cloud.utils.db.SearchCriteria)

Example 4 with TemplateJoinVO

use of com.cloud.api.query.vo.TemplateJoinVO in project cloudstack by apache.

the class ViewResponseHelper method createTemplateUpdateResponse.

public static List<TemplateResponse> createTemplateUpdateResponse(ResponseView view, TemplateJoinVO... templates) {
    Hashtable<Long, TemplateResponse> vrDataList = new Hashtable<Long, TemplateResponse>();
    for (TemplateJoinVO vr : templates) {
        TemplateResponse vrData = vrDataList.get(vr.getId());
        if (vrData == null) {
            // first time encountering this volume
            vrData = ApiDBUtils.newTemplateUpdateResponse(vr);
        } else {
            // update tags
            vrData = ApiDBUtils.fillTemplateDetails(EnumSet.of(DomainDetails.all), view, vrData, vr);
        }
        vrDataList.put(vr.getId(), vrData);
    }
    return new ArrayList<TemplateResponse>(vrDataList.values());
}
Also used : Hashtable(java.util.Hashtable) TemplateJoinVO(com.cloud.api.query.vo.TemplateJoinVO) ArrayList(java.util.ArrayList) TemplateResponse(org.apache.cloudstack.api.response.TemplateResponse)

Example 5 with TemplateJoinVO

use of com.cloud.api.query.vo.TemplateJoinVO in project cloudstack by apache.

the class ViewResponseHelper method createIsoResponse.

public static List<TemplateResponse> createIsoResponse(ResponseView view, TemplateJoinVO... templates) {
    Hashtable<String, TemplateResponse> vrDataList = new Hashtable<String, TemplateResponse>();
    for (TemplateJoinVO vr : templates) {
        TemplateResponse vrData = vrDataList.get(vr.getTempZonePair());
        if (vrData == null) {
            // first time encountering this volume
            vrData = ApiDBUtils.newIsoResponse(vr);
        } else {
            // update tags
            vrData = ApiDBUtils.fillTemplateDetails(EnumSet.of(DomainDetails.all), view, vrData, vr);
        }
        vrDataList.put(vr.getTempZonePair(), vrData);
    }
    return new ArrayList<TemplateResponse>(vrDataList.values());
}
Also used : Hashtable(java.util.Hashtable) TemplateJoinVO(com.cloud.api.query.vo.TemplateJoinVO) ArrayList(java.util.ArrayList) TemplateResponse(org.apache.cloudstack.api.response.TemplateResponse)

Aggregations

TemplateJoinVO (com.cloud.api.query.vo.TemplateJoinVO)21 ArrayList (java.util.ArrayList)17 List (java.util.List)6 TemplateResponse (org.apache.cloudstack.api.response.TemplateResponse)6 TemplateResponse (com.cloud.api.response.TemplateResponse)5 Filter (com.cloud.utils.db.Filter)5 Hashtable (java.util.Hashtable)4 SearchCriteria (com.cloud.utils.db.SearchCriteria)3 LinkedHashMap (java.util.LinkedHashMap)3 ResponseView (com.cloud.api.ResponseObject.ResponseView)2 ListResponse (com.cloud.api.response.ListResponse)2 DataCenterVO (com.cloud.dc.DataCenterVO)2 DomainVO (com.cloud.domain.DomainVO)2 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)2 HypervisorType (com.cloud.hypervisor.Hypervisor.HypervisorType)2 VMTemplateVO (com.cloud.storage.VMTemplateVO)2 TemplateFilter (com.cloud.template.VirtualMachineTemplate.TemplateFilter)2 Account (com.cloud.user.Account)2 Pair (com.cloud.utils.Pair)2 TransactionCallbackNoReturn (com.cloud.utils.db.TransactionCallbackNoReturn)2