Search in sources :

Example 41 with ListProjectResourcesCriteria

use of com.cloud.projects.Project.ListProjectResourcesCriteria in project cosmic by MissionCriticalCloud.

the class LoadBalancingRulesManagerImpl method searchForLoadBalancers.

@Override
public Pair<List<? extends LoadBalancer>, Integer> searchForLoadBalancers(final ListLoadBalancerRulesCmd cmd) {
    final Long ipId = cmd.getPublicIpId();
    final Long zoneId = cmd.getZoneId();
    final Long id = cmd.getId();
    final String name = cmd.getLoadBalancerRuleName();
    final String keyword = cmd.getKeyword();
    final Long instanceId = cmd.getVirtualMachineId();
    final Long networkId = cmd.getNetworkId();
    final Map<String, String> tags = cmd.getTags();
    final Boolean forDisplay = cmd.getDisplay();
    final Account caller = CallContext.current().getCallingAccount();
    final List<Long> permittedAccounts = new ArrayList<>();
    final Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<>(cmd.getDomainId(), cmd.isRecursive(), null);
    _accountMgr.buildACLSearchParameters(caller, id, cmd.getAccountName(), cmd.getProjectId(), permittedAccounts, domainIdRecursiveListProject, cmd.listAll(), false);
    final Long domainId = domainIdRecursiveListProject.first();
    final Boolean isRecursive = domainIdRecursiveListProject.second();
    final ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third();
    final Filter searchFilter = new Filter(LoadBalancerVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal());
    final SearchBuilder<LoadBalancerVO> sb = _lbDao.createSearchBuilder();
    _accountMgr.buildACLSearchBuilder(sb, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
    sb.and("name", sb.entity().getName(), SearchCriteria.Op.LIKE);
    sb.and("sourceIpAddress", sb.entity().getSourceIpAddressId(), SearchCriteria.Op.EQ);
    sb.and("networkId", sb.entity().getNetworkId(), SearchCriteria.Op.EQ);
    sb.and("scheme", sb.entity().getScheme(), SearchCriteria.Op.EQ);
    sb.and("display", sb.entity().isDisplay(), SearchCriteria.Op.EQ);
    if (instanceId != null) {
        final SearchBuilder<LoadBalancerVMMapVO> lbVMSearch = _lb2VmMapDao.createSearchBuilder();
        lbVMSearch.and("instanceId", lbVMSearch.entity().getInstanceId(), SearchCriteria.Op.EQ);
        sb.join("lbVMSearch", lbVMSearch, sb.entity().getId(), lbVMSearch.entity().getLoadBalancerId(), JoinBuilder.JoinType.INNER);
    }
    if (zoneId != null) {
        final SearchBuilder<IPAddressVO> ipSearch = _ipAddressDao.createSearchBuilder();
        ipSearch.and("zoneId", ipSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
        sb.join("ipSearch", ipSearch, sb.entity().getSourceIpAddressId(), ipSearch.entity().getId(), JoinBuilder.JoinType.INNER);
    }
    if (tags != null && !tags.isEmpty()) {
        final SearchBuilder<ResourceTagVO> tagSearch = _resourceTagDao.createSearchBuilder();
        for (int count = 0; count < tags.size(); count++) {
            tagSearch.or().op("key" + String.valueOf(count), tagSearch.entity().getKey(), SearchCriteria.Op.EQ);
            tagSearch.and("value" + String.valueOf(count), tagSearch.entity().getValue(), SearchCriteria.Op.EQ);
            tagSearch.cp();
        }
        tagSearch.and("resourceType", tagSearch.entity().getResourceType(), SearchCriteria.Op.EQ);
        sb.groupBy(sb.entity().getId());
        sb.join("tagSearch", tagSearch, sb.entity().getId(), tagSearch.entity().getResourceId(), JoinBuilder.JoinType.INNER);
    }
    final SearchCriteria<LoadBalancerVO> sc = sb.create();
    _accountMgr.buildACLSearchCriteria(sc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    if (keyword != null) {
        final SearchCriteria<LoadBalancerVO> ssc = _lbDao.createSearchCriteria();
        ssc.addOr("name", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("description", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        sc.addAnd("name", SearchCriteria.Op.SC, ssc);
    }
    if (name != null) {
        sc.setParameters("name", "%" + name + "%");
    }
    if (id != null) {
        sc.setParameters("id", id);
    }
    if (ipId != null) {
        sc.setParameters("sourceIpAddress", ipId);
    }
    if (instanceId != null) {
        sc.setJoinParameters("lbVMSearch", "instanceId", instanceId);
    }
    if (zoneId != null) {
        sc.setJoinParameters("ipSearch", "zoneId", zoneId);
    }
    if (networkId != null) {
        sc.setParameters("networkId", networkId);
    }
    if (tags != null && !tags.isEmpty()) {
        int count = 0;
        sc.setJoinParameters("tagSearch", "resourceType", ResourceObjectType.LoadBalancer.toString());
        for (final String key : tags.keySet()) {
            sc.setJoinParameters("tagSearch", "key" + String.valueOf(count), key);
            sc.setJoinParameters("tagSearch", "value" + String.valueOf(count), tags.get(key));
            count++;
        }
    }
    if (forDisplay != null) {
        sc.setParameters("display", forDisplay);
    }
    // list only Public load balancers using this command
    sc.setParameters("scheme", Scheme.Public);
    final Pair<List<LoadBalancerVO>, Integer> result = _lbDao.searchAndCount(sc, searchFilter);
    return new Pair<>(result.first(), result.second());
}
Also used : Account(com.cloud.user.Account) ArrayList(java.util.ArrayList) LoadBalancerVO(com.cloud.network.dao.LoadBalancerVO) LoadBalancerVMMapVO(com.cloud.network.dao.LoadBalancerVMMapVO) ResourceTagVO(com.cloud.tags.ResourceTagVO) ArrayList(java.util.ArrayList) List(java.util.List) Pair(com.cloud.utils.Pair) Ternary(com.cloud.utils.Ternary) ListProjectResourcesCriteria(com.cloud.projects.Project.ListProjectResourcesCriteria) Filter(com.cloud.utils.db.Filter) IPAddressVO(com.cloud.network.dao.IPAddressVO)

Example 42 with ListProjectResourcesCriteria

use of com.cloud.projects.Project.ListProjectResourcesCriteria in project cosmic by MissionCriticalCloud.

the class VMSnapshotManagerImpl method listVMSnapshots.

@Override
public List<VMSnapshotVO> listVMSnapshots(final ListVMSnapshotCmd cmd) {
    final Account caller = getCaller();
    final List<Long> permittedAccounts = new ArrayList<>();
    final boolean listAll = cmd.listAll();
    final Long id = cmd.getId();
    final Long vmId = cmd.getVmId();
    final String state = cmd.getState();
    final String keyword = cmd.getKeyword();
    final String name = cmd.getVmSnapshotName();
    final String accountName = cmd.getAccountName();
    final Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<>(cmd.getDomainId(), cmd.isRecursive(), null);
    _accountMgr.buildACLSearchParameters(caller, id, cmd.getAccountName(), cmd.getProjectId(), permittedAccounts, domainIdRecursiveListProject, listAll, false);
    final Long domainId = domainIdRecursiveListProject.first();
    final Boolean isRecursive = domainIdRecursiveListProject.second();
    final ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third();
    final Filter searchFilter = new Filter(VMSnapshotVO.class, "created", false, cmd.getStartIndex(), cmd.getPageSizeVal());
    final SearchBuilder<VMSnapshotVO> sb = _vmSnapshotDao.createSearchBuilder();
    _accountMgr.buildACLSearchBuilder(sb, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    sb.and("vm_id", sb.entity().getVmId(), SearchCriteria.Op.EQ);
    sb.and("domain_id", sb.entity().getDomainId(), SearchCriteria.Op.EQ);
    sb.and("status", sb.entity().getState(), SearchCriteria.Op.IN);
    sb.and("state", sb.entity().getState(), SearchCriteria.Op.EQ);
    sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
    sb.and("display_name", sb.entity().getDisplayName(), SearchCriteria.Op.EQ);
    sb.and("account_id", sb.entity().getAccountId(), SearchCriteria.Op.EQ);
    sb.done();
    final SearchCriteria<VMSnapshotVO> sc = sb.create();
    _accountMgr.buildACLSearchCriteria(sc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    if (accountName != null && cmd.getDomainId() != null) {
        final Account account = _accountMgr.getActiveAccountByName(accountName, cmd.getDomainId());
        sc.setParameters("account_id", account.getId());
    }
    if (vmId != null) {
        sc.setParameters("vm_id", vmId);
    }
    if (domainId != null) {
        sc.setParameters("domain_id", domainId);
    }
    if (state == null) {
        final VMSnapshot.State[] status = { VMSnapshot.State.Ready, VMSnapshot.State.Creating, VMSnapshot.State.Allocated, VMSnapshot.State.Error, VMSnapshot.State.Expunging, VMSnapshot.State.Reverting };
        sc.setParameters("status", (Object[]) status);
    } else {
        sc.setParameters("state", state);
    }
    if (name != null) {
        sc.setParameters("display_name", name);
    }
    if (keyword != null) {
        final SearchCriteria<VMSnapshotVO> ssc = _vmSnapshotDao.createSearchCriteria();
        ssc.addOr("name", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("display_name", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("description", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        sc.addAnd("name", SearchCriteria.Op.SC, ssc);
    }
    if (id != null) {
        sc.setParameters("id", id);
    }
    return _vmSnapshotDao.search(sc, searchFilter);
}
Also used : Account(com.cloud.user.Account) Ternary(com.cloud.utils.Ternary) ArrayList(java.util.ArrayList) ListProjectResourcesCriteria(com.cloud.projects.Project.ListProjectResourcesCriteria) Filter(com.cloud.utils.db.Filter)

Example 43 with ListProjectResourcesCriteria

use of com.cloud.projects.Project.ListProjectResourcesCriteria in project cosmic by MissionCriticalCloud.

the class QueryManagerImpl method searchForIsosInternal.

private Pair<List<TemplateJoinVO>, Integer> searchForIsosInternal(final ListIsosCmd cmd) {
    final TemplateFilter isoFilter = TemplateFilter.valueOf(cmd.getIsoFilter());
    final Long id = cmd.getId();
    final Map<String, String> tags = cmd.getTags();
    final boolean showRemovedISO = cmd.getShowRemoved();
    final 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;
    }
    final List<Long> permittedAccountIds = new ArrayList<>();
    final Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<>(cmd.getDomainId(), cmd.isRecursive(), null);
    _accountMgr.buildACLSearchParameters(caller, id, cmd.getAccountName(), cmd.getProjectId(), permittedAccountIds, domainIdRecursiveListProject, listAll, false);
    final ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third();
    final List<Account> permittedAccounts = new ArrayList<>();
    for (final Long accountId : permittedAccountIds) {
        permittedAccounts.add(_accountMgr.getAccount(accountId));
    }
    final 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);
}
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.utils.exception.InvalidParameterValueException)

Example 44 with ListProjectResourcesCriteria

use of com.cloud.projects.Project.ListProjectResourcesCriteria in project cosmic by MissionCriticalCloud.

the class QueryManagerImpl method searchForUserVMsInternal.

private Pair<List<UserVmJoinVO>, Integer> searchForUserVMsInternal(final ListVMsCmd cmd) {
    final Account caller = CallContext.current().getCallingAccount();
    final List<Long> permittedAccounts = new ArrayList<>();
    final boolean listAll = cmd.listAll();
    final Long id = cmd.getId();
    final Long userId = cmd.getUserId();
    final Map<String, String> tags = cmd.getTags();
    final Boolean display = cmd.getDisplay();
    final Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<>(cmd.getDomainId(), cmd.isRecursive(), null);
    _accountMgr.buildACLSearchParameters(caller, id, cmd.getAccountName(), cmd.getProjectId(), permittedAccounts, domainIdRecursiveListProject, listAll, false);
    final Long domainId = domainIdRecursiveListProject.first();
    final Boolean isRecursive = domainIdRecursiveListProject.second();
    final ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third();
    final Filter searchFilter = new Filter(UserVmJoinVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal());
    final List<Long> ids;
    if (cmd.getId() != null) {
        if (cmd.getIds() != null && !cmd.getIds().isEmpty()) {
            throw new InvalidParameterValueException("Specify either id or ids but not both parameters");
        }
        ids = new ArrayList<>();
        ids.add(cmd.getId());
    } else {
        ids = cmd.getIds();
    }
    // first search distinct vm id by using query criteria and pagination
    final SearchBuilder<UserVmJoinVO> sb = _userVmJoinDao.createSearchBuilder();
    // select distinct ids
    sb.select(null, Func.DISTINCT, sb.entity().getId());
    _accountMgr.buildACLViewSearchBuilder(sb, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    final String hypervisor = cmd.getHypervisor();
    final Object name = cmd.getName();
    final String state = cmd.getState();
    final Object zoneId = cmd.getZoneId();
    final Object keyword = cmd.getKeyword();
    boolean isAdmin = false;
    boolean isRootAdmin = false;
    if (_accountMgr.isAdmin(caller.getId())) {
        isAdmin = true;
    }
    if (_accountMgr.isRootAdmin(caller.getId())) {
        isRootAdmin = true;
    }
    final Object groupId = cmd.getGroupId();
    final Object networkId = cmd.getNetworkId();
    if (HypervisorType.getType(hypervisor) == HypervisorType.None && hypervisor != null) {
        // invalid hypervisor type input
        throw new InvalidParameterValueException("Invalid HypervisorType " + hypervisor);
    }
    final Object templateId = cmd.getTemplateId();
    final Object isoId = cmd.getIsoId();
    final Object vpcId = cmd.getVpcId();
    final Object affinityGroupId = cmd.getAffinityGroupId();
    final Object keyPairName = cmd.getKeyPairName();
    final Object serviceOffId = cmd.getServiceOfferingId();
    Object pod = null;
    Object hostId = null;
    Object storageId = null;
    if (cmd instanceof ListVMsCmdByAdmin) {
        final ListVMsCmdByAdmin adCmd = (ListVMsCmdByAdmin) cmd;
        pod = adCmd.getPodId();
        hostId = adCmd.getHostId();
        storageId = adCmd.getStorageId();
    }
    sb.and("displayName", sb.entity().getDisplayName(), SearchCriteria.Op.LIKE);
    sb.and("idIN", sb.entity().getId(), SearchCriteria.Op.IN);
    sb.and("name", sb.entity().getName(), SearchCriteria.Op.LIKE);
    sb.and("stateEQ", sb.entity().getState(), SearchCriteria.Op.EQ);
    sb.and("stateNEQ", sb.entity().getState(), SearchCriteria.Op.NEQ);
    sb.and("stateNIN", sb.entity().getState(), SearchCriteria.Op.NIN);
    sb.and("dataCenterId", sb.entity().getDataCenterId(), SearchCriteria.Op.EQ);
    sb.and("podId", sb.entity().getPodId(), SearchCriteria.Op.EQ);
    sb.and("hypervisorType", sb.entity().getHypervisorType(), SearchCriteria.Op.EQ);
    sb.and("hostIdEQ", sb.entity().getHostId(), SearchCriteria.Op.EQ);
    sb.and("templateId", sb.entity().getTemplateId(), SearchCriteria.Op.EQ);
    sb.and("isoId", sb.entity().getIsoId(), SearchCriteria.Op.EQ);
    sb.and("instanceGroupId", sb.entity().getInstanceGroupId(), SearchCriteria.Op.EQ);
    if (serviceOffId != null) {
        sb.and("serviceOfferingId", sb.entity().getServiceOfferingId(), SearchCriteria.Op.EQ);
    }
    if (display != null) {
        sb.and("display", sb.entity().isDisplayVm(), SearchCriteria.Op.EQ);
    }
    if (groupId != null && (Long) groupId != -1) {
        sb.and("instanceGroupId", sb.entity().getInstanceGroupId(), SearchCriteria.Op.EQ);
    }
    if (userId != null) {
        sb.and("userId", sb.entity().getUserId(), SearchCriteria.Op.EQ);
    }
    if (networkId != null) {
        sb.and("networkId", sb.entity().getNetworkId(), SearchCriteria.Op.EQ);
    }
    if (vpcId != null && networkId == null) {
        sb.and("vpcId", sb.entity().getVpcId(), SearchCriteria.Op.EQ);
    }
    if (storageId != null) {
        sb.and("poolId", sb.entity().getPoolId(), SearchCriteria.Op.EQ);
    }
    if (affinityGroupId != null) {
        sb.and("affinityGroupId", sb.entity().getAffinityGroupId(), SearchCriteria.Op.EQ);
    }
    if (keyPairName != null) {
        sb.and("keyPairName", sb.entity().getKeypairName(), SearchCriteria.Op.EQ);
    }
    if (!isRootAdmin) {
        sb.and("displayVm", sb.entity().isDisplayVm(), SearchCriteria.Op.EQ);
    }
    // populate the search criteria with the values passed in
    final SearchCriteria<UserVmJoinVO> sc = sb.create();
    // building ACL condition
    _accountMgr.buildACLViewSearchCriteria(sc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    if (tags != null && !tags.isEmpty()) {
        final SearchCriteria<UserVmJoinVO> tagSc = _userVmJoinDao.createSearchCriteria();
        for (final Map.Entry<String, String> entry : tags.entrySet()) {
            final SearchCriteria<UserVmJoinVO> tsc = _userVmJoinDao.createSearchCriteria();
            tsc.addAnd("tagKey", SearchCriteria.Op.EQ, entry.getKey());
            tsc.addAnd("tagValue", SearchCriteria.Op.EQ, entry.getValue());
            tagSc.addOr("tagKey", SearchCriteria.Op.SC, tsc);
        }
        sc.addAnd("tagKey", SearchCriteria.Op.SC, tagSc);
    }
    if (groupId != null && (Long) groupId != -1) {
        sc.setParameters("instanceGroupId", groupId);
    }
    if (keyword != null) {
        final SearchCriteria<UserVmJoinVO> ssc = _userVmJoinDao.createSearchCriteria();
        ssc.addOr("displayName", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("name", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("instanceName", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("state", SearchCriteria.Op.EQ, keyword);
        sc.addAnd("displayName", SearchCriteria.Op.SC, ssc);
    }
    if (serviceOffId != null) {
        sc.setParameters("serviceOfferingId", serviceOffId);
    }
    if (display != null) {
        sc.setParameters("display", display);
    }
    if (ids != null && !ids.isEmpty()) {
        sc.setParameters("idIN", ids.toArray());
    }
    if (templateId != null) {
        sc.setParameters("templateId", templateId);
    }
    if (isoId != null) {
        sc.setParameters("isoId", isoId);
    }
    if (userId != null) {
        sc.setParameters("userId", userId);
    }
    if (networkId != null) {
        sc.setParameters("networkId", networkId);
    }
    if (vpcId != null && networkId == null) {
        sc.setParameters("vpcId", vpcId);
    }
    if (name != null) {
        sc.setParameters("name", "%" + name + "%");
    }
    if (state != null) {
        if (state.equalsIgnoreCase("present")) {
            sc.setParameters("stateNIN", "Destroyed", "Expunging");
        } else {
            sc.setParameters("stateEQ", state);
        }
    }
    if (hypervisor != null) {
        sc.setParameters("hypervisorType", hypervisor);
    }
    // Don't show Destroyed and Expunging vms to the end user if the AllowUserViewDestroyedVM flag is not set.
    if (!isAdmin && !AllowUserViewDestroyedVM.valueIn(caller.getAccountId())) {
        sc.setParameters("stateNIN", "Destroyed", "Expunging");
    }
    if (zoneId != null) {
        sc.setParameters("dataCenterId", zoneId);
    }
    if (affinityGroupId != null) {
        sc.setParameters("affinityGroupId", affinityGroupId);
    }
    if (keyPairName != null) {
        sc.setParameters("keyPairName", keyPairName);
    }
    if (cmd instanceof ListVMsCmdByAdmin) {
        final ListVMsCmdByAdmin aCmd = (ListVMsCmdByAdmin) cmd;
        if (aCmd.getPodId() != null) {
            sc.setParameters("podId", pod);
            if (state == null) {
                sc.setParameters("stateNEQ", "Destroyed");
            }
        }
        if (hostId != null) {
            sc.setParameters("hostIdEQ", hostId);
        }
        if (storageId != null) {
            sc.setParameters("poolId", storageId);
        }
    }
    if (!isRootAdmin) {
        sc.setParameters("displayVm", 1);
    }
    // search vm details by ids
    final Pair<List<UserVmJoinVO>, Integer> uniqueVmPair = _userVmJoinDao.searchAndDistinctCount(sc, searchFilter);
    final Integer count = uniqueVmPair.second();
    if (count.intValue() == 0) {
        // handle empty result cases
        return uniqueVmPair;
    }
    final List<UserVmJoinVO> uniqueVms = uniqueVmPair.first();
    final Long[] vmIds = new Long[uniqueVms.size()];
    int i = 0;
    for (final UserVmJoinVO v : uniqueVms) {
        vmIds[i++] = v.getId();
    }
    final List<UserVmJoinVO> vms = _userVmJoinDao.searchByIds(vmIds);
    return new Pair<>(vms, count);
}
Also used : Account(com.cloud.user.Account) ArrayList(java.util.ArrayList) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) ArrayList(java.util.ArrayList) List(java.util.List) Pair(com.cloud.utils.Pair) Ternary(com.cloud.utils.Ternary) UserVmJoinVO(com.cloud.api.query.vo.UserVmJoinVO) ListProjectResourcesCriteria(com.cloud.projects.Project.ListProjectResourcesCriteria) TemplateFilter(com.cloud.template.VirtualMachineTemplate.TemplateFilter) Filter(com.cloud.utils.db.Filter) ListVMsCmdByAdmin(com.cloud.api.command.admin.vm.ListVMsCmdByAdmin) Map(java.util.Map)

Example 45 with ListProjectResourcesCriteria

use of com.cloud.projects.Project.ListProjectResourcesCriteria in project cosmic by MissionCriticalCloud.

the class QueryManagerImpl method searchForAsyncJobsInternal.

private Pair<List<AsyncJobJoinVO>, Integer> searchForAsyncJobsInternal(final ListAsyncJobsCmd cmd) {
    final Account caller = CallContext.current().getCallingAccount();
    final List<Long> permittedAccounts = new ArrayList<>();
    final Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<>(cmd.getDomainId(), cmd.isRecursive(), null);
    _accountMgr.buildACLSearchParameters(caller, null, cmd.getAccountName(), null, permittedAccounts, domainIdRecursiveListProject, cmd.listAll(), false);
    final Long domainId = domainIdRecursiveListProject.first();
    final Boolean isRecursive = domainIdRecursiveListProject.second();
    final ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third();
    final Filter searchFilter = new Filter(AsyncJobJoinVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal());
    final SearchBuilder<AsyncJobJoinVO> sb = _jobJoinDao.createSearchBuilder();
    sb.and("accountIdIN", sb.entity().getAccountId(), SearchCriteria.Op.IN);
    boolean accountJoinIsDone = false;
    if (permittedAccounts.isEmpty() && domainId != null) {
        sb.and("domainId", sb.entity().getDomainId(), SearchCriteria.Op.EQ);
        sb.and("path", sb.entity().getDomainPath(), SearchCriteria.Op.LIKE);
        accountJoinIsDone = true;
    }
    if (listProjectResourcesCriteria != null) {
        if (listProjectResourcesCriteria == Project.ListProjectResourcesCriteria.ListProjectResourcesOnly) {
            sb.and("type", sb.entity().getAccountType(), SearchCriteria.Op.EQ);
        } else if (listProjectResourcesCriteria == Project.ListProjectResourcesCriteria.SkipProjectResources) {
            sb.and("type", sb.entity().getAccountType(), SearchCriteria.Op.NEQ);
        }
        if (!accountJoinIsDone) {
            sb.and("domainId", sb.entity().getDomainId(), SearchCriteria.Op.EQ);
            sb.and("path", sb.entity().getDomainPath(), SearchCriteria.Op.LIKE);
        }
    }
    final Object keyword = cmd.getKeyword();
    final Object startDate = cmd.getStartDate();
    final SearchCriteria<AsyncJobJoinVO> sc = sb.create();
    if (listProjectResourcesCriteria != null) {
        sc.setParameters("type", Account.ACCOUNT_TYPE_PROJECT);
    }
    if (!permittedAccounts.isEmpty()) {
        sc.setParameters("accountIdIN", permittedAccounts.toArray());
    } else if (domainId != null) {
        final DomainVO domain = _domainDao.findById(domainId);
        if (isRecursive) {
            sc.setParameters("path", domain.getPath() + "%");
        } else {
            sc.setParameters("domainId", domainId);
        }
    }
    if (keyword != null) {
        sc.addAnd("cmd", SearchCriteria.Op.LIKE, "%" + keyword + "%");
    }
    if (startDate != null) {
        sc.addAnd("created", SearchCriteria.Op.GTEQ, startDate);
    }
    return _jobJoinDao.searchAndCount(sc, searchFilter);
}
Also used : Account(com.cloud.user.Account) Ternary(com.cloud.utils.Ternary) ArrayList(java.util.ArrayList) ListProjectResourcesCriteria(com.cloud.projects.Project.ListProjectResourcesCriteria) DomainVO(com.cloud.domain.DomainVO) AsyncJobJoinVO(com.cloud.api.query.vo.AsyncJobJoinVO) TemplateFilter(com.cloud.template.VirtualMachineTemplate.TemplateFilter) Filter(com.cloud.utils.db.Filter)

Aggregations

ListProjectResourcesCriteria (com.cloud.projects.Project.ListProjectResourcesCriteria)62 Ternary (com.cloud.utils.Ternary)62 ArrayList (java.util.ArrayList)62 Account (com.cloud.user.Account)61 Filter (com.cloud.utils.db.Filter)57 List (java.util.List)49 Pair (com.cloud.utils.Pair)48 TemplateFilter (com.cloud.template.VirtualMachineTemplate.TemplateFilter)24 ResourceTagVO (com.cloud.tags.ResourceTagVO)19 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)15 InvalidParameterValueException (com.cloud.utils.exception.InvalidParameterValueException)11 IPAddressVO (com.cloud.network.dao.IPAddressVO)10 HypervisorType (com.cloud.hypervisor.Hypervisor.HypervisorType)8 DomainVO (com.cloud.domain.DomainVO)6 Date (java.util.Date)6 LinkedList (java.util.LinkedList)6 UserVmVO (com.cloud.vm.UserVmVO)5 AffinityGroupJoinVO (com.cloud.api.query.vo.AffinityGroupJoinVO)4 AsyncJobJoinVO (com.cloud.api.query.vo.AsyncJobJoinVO)4 DomainRouterJoinVO (com.cloud.api.query.vo.DomainRouterJoinVO)4