Search in sources :

Example 16 with Ternary

use of com.cloud.utils.Ternary in project cloudstack by apache.

the class QueryManagerImpl method searchForIsosInternal.

private Pair<List<TemplateJoinVO>, Integer> searchForIsosInternal(ListIsosCmd cmd) {
    TemplateFilter isoFilter = TemplateFilter.valueOf(cmd.getIsoFilter());
    Long id = cmd.getId();
    Map<String, String> tags = cmd.getTags();
    boolean showRemovedISO = cmd.getShowRemoved();
    Account caller = CallContext.current().getCallingAccount();
    boolean listAll = false;
    if (isoFilter != null && isoFilter == TemplateFilter.all) {
        if (caller.getType() == Account.ACCOUNT_TYPE_NORMAL) {
            throw new InvalidParameterValueException("Filter " + TemplateFilter.all + " can be specified by admin only");
        }
        listAll = true;
    }
    List<Long> permittedAccountIds = new ArrayList<Long>();
    Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<Long, Boolean, ListProjectResourcesCriteria>(cmd.getDomainId(), cmd.isRecursive(), null);
    _accountMgr.buildACLSearchParameters(caller, id, cmd.getAccountName(), cmd.getProjectId(), permittedAccountIds, domainIdRecursiveListProject, listAll, false);
    ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third();
    List<Account> permittedAccounts = new ArrayList<Account>();
    for (Long accountId : permittedAccountIds) {
        permittedAccounts.add(_accountMgr.getAccount(accountId));
    }
    HypervisorType hypervisorType = HypervisorType.getType(cmd.getHypervisor());
    return searchForTemplatesInternal(cmd.getId(), cmd.getIsoName(), cmd.getKeyword(), isoFilter, true, cmd.isBootable(), cmd.getPageSizeVal(), cmd.getStartIndex(), cmd.getZoneId(), hypervisorType, true, cmd.listInReadyState(), permittedAccounts, caller, listProjectResourcesCriteria, tags, showRemovedISO, null);
}
Also used : Account(com.cloud.user.Account) Ternary(com.cloud.utils.Ternary) ArrayList(java.util.ArrayList) TemplateFilter(com.cloud.template.VirtualMachineTemplate.TemplateFilter) HypervisorType(com.cloud.hypervisor.Hypervisor.HypervisorType) ListProjectResourcesCriteria(com.cloud.projects.Project.ListProjectResourcesCriteria) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException)

Example 17 with Ternary

use of com.cloud.utils.Ternary in project cloudstack by apache.

the class QueryManagerImpl method searchForVolumesInternal.

private Pair<List<VolumeJoinVO>, Integer> searchForVolumesInternal(ListVolumesCmd cmd) {
    Account caller = CallContext.current().getCallingAccount();
    List<Long> permittedAccounts = new ArrayList<Long>();
    Long id = cmd.getId();
    Long vmInstanceId = cmd.getVirtualMachineId();
    String name = cmd.getVolumeName();
    String keyword = cmd.getKeyword();
    String type = cmd.getType();
    Map<String, String> tags = cmd.getTags();
    Long storageId = cmd.getStorageId();
    Long diskOffId = cmd.getDiskOfferingId();
    Boolean display = cmd.getDisplay();
    Long zoneId = cmd.getZoneId();
    Long podId = cmd.getPodId();
    List<Long> ids = getIdsListFromCmd(cmd.getId(), cmd.getIds());
    Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<Long, Boolean, ListProjectResourcesCriteria>(cmd.getDomainId(), cmd.isRecursive(), null);
    _accountMgr.buildACLSearchParameters(caller, id, cmd.getAccountName(), cmd.getProjectId(), permittedAccounts, domainIdRecursiveListProject, cmd.listAll(), false);
    Long domainId = domainIdRecursiveListProject.first();
    Boolean isRecursive = domainIdRecursiveListProject.second();
    ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third();
    Filter searchFilter = new Filter(VolumeJoinVO.class, "created", false, cmd.getStartIndex(), cmd.getPageSizeVal());
    // hack for now, this should be done better but due to needing a join I
    // opted to
    // do this quickly and worry about making it pretty later
    SearchBuilder<VolumeJoinVO> sb = _volumeJoinDao.createSearchBuilder();
    // select distinct
    sb.select(null, Func.DISTINCT, sb.entity().getId());
    // ids to get
    // number of
    // records with
    // pagination
    _accountMgr.buildACLViewSearchBuilder(sb, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    sb.and("name", sb.entity().getName(), SearchCriteria.Op.EQ);
    sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
    sb.and("idIN", sb.entity().getId(), SearchCriteria.Op.IN);
    sb.and("volumeType", sb.entity().getVolumeType(), SearchCriteria.Op.LIKE);
    sb.and("instanceId", sb.entity().getVmId(), SearchCriteria.Op.EQ);
    sb.and("dataCenterId", sb.entity().getDataCenterId(), SearchCriteria.Op.EQ);
    sb.and("podId", sb.entity().getPodId(), SearchCriteria.Op.EQ);
    sb.and("storageId", sb.entity().getPoolId(), SearchCriteria.Op.EQ);
    sb.and("diskOfferingId", sb.entity().getDiskOfferingId(), SearchCriteria.Op.EQ);
    sb.and("display", sb.entity().isDisplayVolume(), SearchCriteria.Op.EQ);
    // Only return volumes that are not destroyed
    sb.and("state", sb.entity().getState(), SearchCriteria.Op.NEQ);
    sb.and("systemUse", sb.entity().isSystemUse(), SearchCriteria.Op.NEQ);
    // display UserVM volumes only
    sb.and().op("type", sb.entity().getVmType(), SearchCriteria.Op.NIN);
    sb.or("nulltype", sb.entity().getVmType(), SearchCriteria.Op.NULL);
    sb.cp();
    // now set the SC criteria...
    SearchCriteria<VolumeJoinVO> sc = sb.create();
    _accountMgr.buildACLViewSearchCriteria(sc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    if (keyword != null) {
        SearchCriteria<VolumeJoinVO> ssc = _volumeJoinDao.createSearchCriteria();
        ssc.addOr("name", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("volumeType", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        sc.addAnd("name", SearchCriteria.Op.SC, ssc);
    }
    if (name != null) {
        sc.setParameters("name", name);
    }
    if (display != null) {
        sc.setParameters("display", display);
    }
    setIdsListToSearchCriteria(sc, ids);
    sc.setParameters("systemUse", 1);
    if (tags != null && !tags.isEmpty()) {
        SearchCriteria<VolumeJoinVO> tagSc = _volumeJoinDao.createSearchCriteria();
        for (String key : tags.keySet()) {
            SearchCriteria<VolumeJoinVO> tsc = _volumeJoinDao.createSearchCriteria();
            tsc.addAnd("tagKey", SearchCriteria.Op.EQ, key);
            tsc.addAnd("tagValue", SearchCriteria.Op.EQ, tags.get(key));
            tagSc.addOr("tagKey", SearchCriteria.Op.SC, tsc);
        }
        sc.addAnd("tagKey", SearchCriteria.Op.SC, tagSc);
    }
    if (diskOffId != null) {
        sc.setParameters("diskOfferingId", diskOffId);
    }
    if (id != null) {
        sc.setParameters("id", id);
    }
    if (type != null) {
        sc.setParameters("volumeType", "%" + type + "%");
    }
    if (vmInstanceId != null) {
        sc.setParameters("instanceId", vmInstanceId);
    }
    if (zoneId != null) {
        sc.setParameters("dataCenterId", zoneId);
    }
    if (podId != null) {
        sc.setParameters("podId", podId);
    }
    if (storageId != null) {
        sc.setParameters("storageId", storageId);
    }
    // Don't return DomR and ConsoleProxy volumes
    sc.setParameters("type", VirtualMachine.Type.ConsoleProxy, VirtualMachine.Type.SecondaryStorageVm, VirtualMachine.Type.DomainRouter);
    // Only return volumes that are not destroyed
    sc.setParameters("state", Volume.State.Destroy);
    // search Volume details by ids
    Pair<List<VolumeJoinVO>, Integer> uniqueVolPair = _volumeJoinDao.searchAndCount(sc, searchFilter);
    Integer count = uniqueVolPair.second();
    if (count.intValue() == 0) {
        // empty result
        return uniqueVolPair;
    }
    List<VolumeJoinVO> uniqueVols = uniqueVolPair.first();
    Long[] vrIds = new Long[uniqueVols.size()];
    int i = 0;
    for (VolumeJoinVO v : uniqueVols) {
        vrIds[i++] = v.getId();
    }
    List<VolumeJoinVO> vrs = _volumeJoinDao.searchByIds(vrIds);
    return new Pair<List<VolumeJoinVO>, Integer>(vrs, count);
}
Also used : Account(com.cloud.user.Account) Ternary(com.cloud.utils.Ternary) ArrayList(java.util.ArrayList) ListProjectResourcesCriteria(com.cloud.projects.Project.ListProjectResourcesCriteria) TemplateFilter(com.cloud.template.VirtualMachineTemplate.TemplateFilter) Filter(com.cloud.utils.db.Filter) ArrayList(java.util.ArrayList) List(java.util.List) VolumeJoinVO(com.cloud.api.query.vo.VolumeJoinVO) Pair(com.cloud.utils.Pair)

Example 18 with Ternary

use of com.cloud.utils.Ternary in project cloudstack by apache.

the class QueryManagerImpl method searchForTemplatesInternal.

private Pair<List<TemplateJoinVO>, Integer> searchForTemplatesInternal(ListTemplatesCmd cmd) {
    TemplateFilter templateFilter = TemplateFilter.valueOf(cmd.getTemplateFilter());
    Long id = cmd.getId();
    Map<String, String> tags = cmd.getTags();
    boolean showRemovedTmpl = cmd.getShowRemoved();
    Account caller = CallContext.current().getCallingAccount();
    boolean listAll = false;
    if (templateFilter != null && templateFilter == TemplateFilter.all) {
        if (caller.getType() == Account.ACCOUNT_TYPE_NORMAL) {
            throw new InvalidParameterValueException("Filter " + TemplateFilter.all + " can be specified by admin only");
        }
        listAll = true;
    }
    List<Long> permittedAccountIds = new ArrayList<Long>();
    Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<Long, Boolean, ListProjectResourcesCriteria>(cmd.getDomainId(), cmd.isRecursive(), null);
    _accountMgr.buildACLSearchParameters(caller, id, cmd.getAccountName(), cmd.getProjectId(), permittedAccountIds, domainIdRecursiveListProject, listAll, false);
    ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third();
    List<Account> permittedAccounts = new ArrayList<Account>();
    for (Long accountId : permittedAccountIds) {
        permittedAccounts.add(_accountMgr.getAccount(accountId));
    }
    boolean showDomr = ((templateFilter != TemplateFilter.selfexecutable) && (templateFilter != TemplateFilter.featured));
    HypervisorType hypervisorType = HypervisorType.getType(cmd.getHypervisor());
    return searchForTemplatesInternal(id, cmd.getTemplateName(), cmd.getKeyword(), templateFilter, false, null, cmd.getPageSizeVal(), cmd.getStartIndex(), cmd.getZoneId(), hypervisorType, showDomr, cmd.listInReadyState(), permittedAccounts, caller, listProjectResourcesCriteria, tags, showRemovedTmpl, cmd.getIds());
}
Also used : Account(com.cloud.user.Account) Ternary(com.cloud.utils.Ternary) ArrayList(java.util.ArrayList) TemplateFilter(com.cloud.template.VirtualMachineTemplate.TemplateFilter) HypervisorType(com.cloud.hypervisor.Hypervisor.HypervisorType) ListProjectResourcesCriteria(com.cloud.projects.Project.ListProjectResourcesCriteria) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException)

Example 19 with Ternary

use of com.cloud.utils.Ternary in project cloudstack by apache.

the class QueryManagerImpl method listProjectInvitationsInternal.

public Pair<List<ProjectInvitationJoinVO>, Integer> listProjectInvitationsInternal(ListProjectInvitationsCmd cmd) {
    Long id = cmd.getId();
    Long projectId = cmd.getProjectId();
    String accountName = cmd.getAccountName();
    Long domainId = cmd.getDomainId();
    String state = cmd.getState();
    boolean activeOnly = cmd.isActiveOnly();
    Long startIndex = cmd.getStartIndex();
    Long pageSizeVal = cmd.getPageSizeVal();
    boolean isRecursive = cmd.isRecursive();
    boolean listAll = cmd.listAll();
    Account caller = CallContext.current().getCallingAccount();
    List<Long> permittedAccounts = new ArrayList<Long>();
    Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<Long, Boolean, ListProjectResourcesCriteria>(domainId, isRecursive, null);
    _accountMgr.buildACLSearchParameters(caller, id, accountName, projectId, permittedAccounts, domainIdRecursiveListProject, listAll, true);
    domainId = domainIdRecursiveListProject.first();
    isRecursive = domainIdRecursiveListProject.second();
    ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third();
    Filter searchFilter = new Filter(ProjectInvitationJoinVO.class, "id", true, startIndex, pageSizeVal);
    SearchBuilder<ProjectInvitationJoinVO> sb = _projectInvitationJoinDao.createSearchBuilder();
    _accountMgr.buildACLViewSearchBuilder(sb, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    sb.and("projectId", sb.entity().getProjectId(), SearchCriteria.Op.EQ);
    sb.and("state", sb.entity().getState(), SearchCriteria.Op.EQ);
    sb.and("created", sb.entity().getCreated(), SearchCriteria.Op.GT);
    sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
    SearchCriteria<ProjectInvitationJoinVO> sc = sb.create();
    _accountMgr.buildACLViewSearchCriteria(sc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    if (projectId != null) {
        sc.setParameters("projectId", projectId);
    }
    if (state != null) {
        sc.setParameters("state", state);
    }
    if (id != null) {
        sc.setParameters("id", id);
    }
    if (activeOnly) {
        sc.setParameters("state", ProjectInvitation.State.Pending);
        sc.setParameters("created", new Date((DateUtil.currentGMTTime().getTime()) - _projectMgr.getInvitationTimeout()));
    }
    return _projectInvitationJoinDao.searchAndCount(sc, searchFilter);
}
Also used : Account(com.cloud.user.Account) Ternary(com.cloud.utils.Ternary) ArrayList(java.util.ArrayList) ProjectInvitationJoinVO(com.cloud.api.query.vo.ProjectInvitationJoinVO) Date(java.util.Date) ListProjectResourcesCriteria(com.cloud.projects.Project.ListProjectResourcesCriteria) TemplateFilter(com.cloud.template.VirtualMachineTemplate.TemplateFilter) Filter(com.cloud.utils.db.Filter)

Example 20 with Ternary

use of com.cloud.utils.Ternary in project cloudstack by apache.

the class QueryManagerImpl method searchForVmGroupsInternal.

private Pair<List<InstanceGroupJoinVO>, Integer> searchForVmGroupsInternal(ListVMGroupsCmd cmd) {
    Long id = cmd.getId();
    String name = cmd.getGroupName();
    String keyword = cmd.getKeyword();
    Account caller = CallContext.current().getCallingAccount();
    List<Long> permittedAccounts = new ArrayList<Long>();
    Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<Long, Boolean, ListProjectResourcesCriteria>(cmd.getDomainId(), cmd.isRecursive(), null);
    _accountMgr.buildACLSearchParameters(caller, id, cmd.getAccountName(), cmd.getProjectId(), permittedAccounts, domainIdRecursiveListProject, cmd.listAll(), false);
    Long domainId = domainIdRecursiveListProject.first();
    Boolean isRecursive = domainIdRecursiveListProject.second();
    ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third();
    Filter searchFilter = new Filter(InstanceGroupJoinVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal());
    SearchBuilder<InstanceGroupJoinVO> sb = _vmGroupJoinDao.createSearchBuilder();
    _accountMgr.buildACLViewSearchBuilder(sb, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
    sb.and("name", sb.entity().getName(), SearchCriteria.Op.LIKE);
    SearchCriteria<InstanceGroupJoinVO> sc = sb.create();
    _accountMgr.buildACLViewSearchCriteria(sc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    if (keyword != null) {
        SearchCriteria<InstanceGroupJoinVO> ssc = _vmGroupJoinDao.createSearchCriteria();
        ssc.addOr("name", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        sc.addAnd("name", SearchCriteria.Op.SC, ssc);
    }
    if (id != null) {
        sc.setParameters("id", id);
    }
    if (name != null) {
        sc.setParameters("name", "%" + name + "%");
    }
    return _vmGroupJoinDao.searchAndCount(sc, searchFilter);
}
Also used : Account(com.cloud.user.Account) Ternary(com.cloud.utils.Ternary) ArrayList(java.util.ArrayList) InstanceGroupJoinVO(com.cloud.api.query.vo.InstanceGroupJoinVO) ListProjectResourcesCriteria(com.cloud.projects.Project.ListProjectResourcesCriteria) TemplateFilter(com.cloud.template.VirtualMachineTemplate.TemplateFilter) Filter(com.cloud.utils.db.Filter)

Aggregations

Ternary (com.cloud.utils.Ternary)53 ArrayList (java.util.ArrayList)40 Account (com.cloud.user.Account)34 ListProjectResourcesCriteria (com.cloud.projects.Project.ListProjectResourcesCriteria)32 Filter (com.cloud.utils.db.Filter)30 List (java.util.List)29 Pair (com.cloud.utils.Pair)28 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)15 TemplateFilter (com.cloud.template.VirtualMachineTemplate.TemplateFilter)13 ResourceTagVO (com.cloud.tags.ResourceTagVO)9 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)9 IPAddressVO (com.cloud.network.dao.IPAddressVO)6 HashMap (java.util.HashMap)6 OVAProcessor (com.cloud.storage.template.OVAProcessor)4 Script (com.cloud.utils.script.Script)4 File (java.io.File)4 RemoteException (java.rmi.RemoteException)4 ExcludeList (com.cloud.deploy.DeploymentPlanner.ExcludeList)3 HypervisorType (com.cloud.hypervisor.Hypervisor.HypervisorType)3 SSHKeyPair (com.cloud.user.SSHKeyPair)3