Search in sources :

Example 6 with TemplateJoinVO

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

the class ViewResponseHelper method createTemplateResponse.

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

Example 7 with TemplateJoinVO

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

the class TemplateJoinDaoImpl method searchByTemplateZonePair.

@Override
public List<TemplateJoinVO> searchByTemplateZonePair(Boolean showRemoved, String... idPairs) {
    // set detail batch query size
    int DETAILS_BATCH_SIZE = 2000;
    String batchCfg = _configDao.getValue("detail.batch.query.size");
    if (batchCfg != null) {
        DETAILS_BATCH_SIZE = Integer.parseInt(batchCfg);
    }
    // query details by batches
    Filter searchFilter = new Filter(TemplateJoinVO.class, "sortKey", QueryService.SortKeyAscending.value(), null, null);
    searchFilter.addOrderBy(TemplateJoinVO.class, "tempZonePair", QueryService.SortKeyAscending.value());
    List<TemplateJoinVO> uvList = new ArrayList<TemplateJoinVO>();
    // query details by batches
    int curr_index = 0;
    if (idPairs.length > DETAILS_BATCH_SIZE) {
        while ((curr_index + DETAILS_BATCH_SIZE) <= idPairs.length) {
            String[] labels = new String[DETAILS_BATCH_SIZE];
            for (int k = 0, j = curr_index; j < curr_index + DETAILS_BATCH_SIZE; j++, k++) {
                labels[k] = idPairs[j];
            }
            SearchCriteria<TemplateJoinVO> sc = tmpltIdPairSearch.create();
            if (!showRemoved) {
                sc.setParameters("templateState", VirtualMachineTemplate.State.Active);
            }
            sc.setParameters("tempZonePairIN", labels);
            List<TemplateJoinVO> vms = searchIncludingRemoved(sc, searchFilter, null, false);
            if (vms != null) {
                uvList.addAll(vms);
            }
            curr_index += DETAILS_BATCH_SIZE;
        }
    }
    if (curr_index < idPairs.length) {
        int batch_size = (idPairs.length - curr_index);
        String[] labels = new String[batch_size];
        for (int k = 0, j = curr_index; j < curr_index + batch_size; j++, k++) {
            labels[k] = idPairs[j];
        }
        SearchCriteria<TemplateJoinVO> sc = tmpltIdPairSearch.create();
        if (!showRemoved) {
            sc.setParameters("templateState", VirtualMachineTemplate.State.Active, VirtualMachineTemplate.State.UploadAbandoned, VirtualMachineTemplate.State.UploadError, VirtualMachineTemplate.State.NotUploaded, VirtualMachineTemplate.State.UploadInProgress);
        }
        sc.setParameters("tempZonePairIN", labels);
        List<TemplateJoinVO> vms = searchIncludingRemoved(sc, searchFilter, null, false);
        if (vms != null) {
            uvList.addAll(vms);
        }
    }
    return uvList;
}
Also used : Filter(com.cloud.utils.db.Filter) TemplateJoinVO(com.cloud.api.query.vo.TemplateJoinVO) ArrayList(java.util.ArrayList)

Example 8 with TemplateJoinVO

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

the class KubernetesVersionManagerImpl method createKubernetesSupportedVersionResponse.

private KubernetesSupportedVersionResponse createKubernetesSupportedVersionResponse(final KubernetesSupportedVersion kubernetesSupportedVersion) {
    KubernetesSupportedVersionResponse response = new KubernetesSupportedVersionResponse();
    response.setObjectName("kubernetessupportedversion");
    response.setId(kubernetesSupportedVersion.getUuid());
    response.setName(kubernetesSupportedVersion.getName());
    response.setSemanticVersion(kubernetesSupportedVersion.getSemanticVersion());
    if (kubernetesSupportedVersion.getState() != null) {
        response.setState(kubernetesSupportedVersion.getState().toString());
    }
    response.setMinimumCpu(kubernetesSupportedVersion.getMinimumCpu());
    response.setMinimumRamSize(kubernetesSupportedVersion.getMinimumRamSize());
    DataCenterVO zone = dataCenterDao.findById(kubernetesSupportedVersion.getZoneId());
    if (zone != null) {
        response.setZoneId(zone.getUuid());
        response.setZoneName(zone.getName());
    }
    response.setSupportsHA(compareSemanticVersions(kubernetesSupportedVersion.getSemanticVersion(), KubernetesClusterService.MIN_KUBERNETES_VERSION_HA_SUPPORT) >= 0);
    response.setSupportsAutoscaling(versionSupportsAutoscaling(kubernetesSupportedVersion));
    TemplateJoinVO template = templateJoinDao.findById(kubernetesSupportedVersion.getIsoId());
    if (template != null) {
        response.setIsoId(template.getUuid());
        response.setIsoName(template.getName());
        response.setIsoState(template.getState().toString());
    }
    return response;
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) TemplateJoinVO(com.cloud.api.query.vo.TemplateJoinVO) KubernetesSupportedVersionResponse(org.apache.cloudstack.api.response.KubernetesSupportedVersionResponse)

Example 9 with TemplateJoinVO

use of com.cloud.api.query.vo.TemplateJoinVO in project cosmic by MissionCriticalCloud.

the class QueryManagerImpl method searchForTemplatesInternal.

private Pair<List<TemplateJoinVO>, Integer> searchForTemplatesInternal(final Long templateId, final String name, final String keyword, final TemplateFilter templateFilter, final boolean isIso, final Boolean bootable, final Long pageSize, final Long startIndex, final Long zoneId, final HypervisorType hyperType, final boolean showDomr, final boolean onlyReady, final List<Account> permittedAccounts, final Account caller, final ListProjectResourcesCriteria listProjectResourcesCriteria, final Map<String, String> tags, final boolean showRemovedTmpl) {
    // 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<>(new ArrayList<>(), 0);
        }
    }
    final VMTemplateVO template;
    Boolean isAscending = Boolean.parseBoolean(_configDao.getValue("sortkey.algorithm"));
    isAscending = isAscending == null ? Boolean.TRUE : isAscending;
    final Filter searchFilter = new Filter(TemplateJoinVO.class, "sortKey", isAscending, startIndex, pageSize);
    searchFilter.addOrderBy(TemplateJoinVO.class, "tempZonePair", isAscending);
    final SearchBuilder<TemplateJoinVO> sb = _templateJoinDao.createSearchBuilder();
    // select distinct (templateId, zoneId) pair
    sb.select(null, Func.DISTINCT, sb.entity().getTempZonePair());
    final 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");
            final 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);
            final InvalidParameterValueException ex = new InvalidParameterValueException("Incorrect format " + template.getFormat() + " of the specified template id");
            ex.addProxyObject(template.getUuid(), "templateId");
            throw ex;
        }
        // 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 {
        final DomainVO domain;
        if (!permittedAccounts.isEmpty()) {
            domain = _domainDao.findById(permittedAccounts.get(0).getDomainId());
        } else {
            domain = _domainDao.findById(Domain.ROOT_DOMAIN);
        }
        // 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() + "%");
        }
        final List<Long> relatedDomainIds = new ArrayList<>();
        final List<Long> permittedAccountIds = new ArrayList<>();
        if (!permittedAccounts.isEmpty()) {
            for (final Account account : permittedAccounts) {
                permittedAccountIds.add(account.getId());
                final boolean publicTemplates = templateFilter == TemplateFilter.featured || templateFilter == TemplateFilter.community;
                // get all parent domain ID's all the way till root domain
                DomainVO domainTreeNode;
                // 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) {
                    final List<DomainVO> allChildDomains = _domainDao.findAllChildren(domainTreeNode.getPath(), domainTreeNode.getId());
                    for (final DomainVO childDomain : allChildDomains) {
                        relatedDomainIds.add(childDomain.getId());
                    }
                }
            }
        }
        if (!isIso) {
            // add hypervisor criteria for template case
            if (hypers != null && !hypers.isEmpty()) {
                final 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);
            }
        }
        // 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()) {
                final 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) {
            final 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) {
            final 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);
        }
        // add tags criteria
        if (tags != null && !tags.isEmpty()) {
            final SearchCriteria<TemplateJoinVO> scc = _templateJoinDao.createSearchCriteria();
            for (final Map.Entry<String, String> entry : tags.entrySet()) {
                final 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);
        }
        if (isIso) {
            sc.addAnd("format", SearchCriteria.Op.EQ, "ISO");
        } else {
            sc.addAnd("format", SearchCriteria.Op.NEQ, "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) {
            final SearchCriteria<TemplateJoinVO> readySc = _templateJoinDao.createSearchCriteria();
            readySc.addOr("state", SearchCriteria.Op.EQ, TemplateState.Ready);
            final 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) {
        final SearchCriteria<TemplateJoinVO> zoneSc = _templateJoinDao.createSearchCriteria();
        zoneSc.addOr("dataCenterId", SearchCriteria.Op.EQ, zoneId);
        zoneSc.addOr("dataStoreScope", SearchCriteria.Op.EQ, ScopeType.REGION);
        // handle the case where xs-tools.iso do not have data_center information in template_view
        final 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);
    }
    // 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
    final Pair<List<TemplateJoinVO>, Integer> uniqueTmplPair;
    if (showRemovedTmpl) {
        uniqueTmplPair = _templateJoinDao.searchIncludingRemovedAndCount(sc, searchFilter);
    } else {
        sc.addAnd("templateState", SearchCriteria.Op.IN, new State[] { State.Active, State.NotUploaded, State.UploadInProgress });
        uniqueTmplPair = _templateJoinDao.searchAndCount(sc, searchFilter);
    }
    final Integer count = uniqueTmplPair.second();
    if (count.intValue() == 0) {
        // empty result
        return uniqueTmplPair;
    }
    final List<TemplateJoinVO> uniqueTmpls = uniqueTmplPair.first();
    final String[] tzIds = new String[uniqueTmpls.size()];
    int i = 0;
    for (final TemplateJoinVO v : uniqueTmpls) {
        tzIds[i++] = v.getTempZonePair();
    }
    final List<TemplateJoinVO> vrs = _templateJoinDao.searchByTemplateZonePair(showRemovedTmpl, tzIds);
    return new Pair<>(vrs, count);
// 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 : Account(com.cloud.user.Account) VMTemplateVO(com.cloud.storage.VMTemplateVO) ArrayList(java.util.ArrayList) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) TemplateJoinVO(com.cloud.api.query.vo.TemplateJoinVO) ArrayList(java.util.ArrayList) List(java.util.List) Pair(com.cloud.utils.Pair) 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) Map(java.util.Map)

Example 10 with TemplateJoinVO

use of com.cloud.api.query.vo.TemplateJoinVO in project cosmic by MissionCriticalCloud.

the class QueryManagerImpl method listIsos.

@Override
public ListResponse<TemplateResponse> listIsos(final ListIsosCmd cmd) {
    final Pair<List<TemplateJoinVO>, Integer> result = searchForIsosInternal(cmd);
    final ListResponse<TemplateResponse> response = new ListResponse<>();
    ResponseView respView = ResponseView.Restricted;
    if (cmd instanceof ListIsosCmdByAdmin) {
        respView = ResponseView.Full;
    }
    final List<TemplateResponse> templateResponses = ViewResponseHelper.createIsoResponse(respView, result.first().toArray(new TemplateJoinVO[result.first().size()]));
    response.setResponses(templateResponses, result.second());
    return response;
}
Also used : ResponseView(com.cloud.api.ResponseObject.ResponseView) ListResponse(com.cloud.api.response.ListResponse) TemplateJoinVO(com.cloud.api.query.vo.TemplateJoinVO) ArrayList(java.util.ArrayList) List(java.util.List) TemplateResponse(com.cloud.api.response.TemplateResponse) ListIsosCmdByAdmin(com.cloud.api.command.admin.iso.ListIsosCmdByAdmin)

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