Search in sources :

Example 56 with ListProjectResourcesCriteria

use of com.cloud.projects.Project.ListProjectResourcesCriteria in project cloudstack by apache.

the class QueryManagerImpl method getUserListInternal.

private Pair<List<UserAccountJoinVO>, Integer> getUserListInternal(Account caller, List<Long> permittedAccounts, boolean listAll, Long id, Object username, Object type, String accountName, Object state, Object keyword, Long domainId, boolean recursive, Filter searchFilter) {
    Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<Long, Boolean, ListProjectResourcesCriteria>(domainId, recursive, null);
    _accountMgr.buildACLSearchParameters(caller, id, accountName, null, permittedAccounts, domainIdRecursiveListProject, listAll, false);
    domainId = domainIdRecursiveListProject.first();
    Boolean isRecursive = domainIdRecursiveListProject.second();
    ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third();
    SearchBuilder<UserAccountJoinVO> sb = _userAccountJoinDao.createSearchBuilder();
    _accountMgr.buildACLViewSearchBuilder(sb, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    sb.and("username", sb.entity().getUsername(), Op.LIKE);
    if (id != null && id == 1) {
        // system user should NOT be searchable
        List<UserAccountJoinVO> emptyList = new ArrayList<UserAccountJoinVO>();
        return new Pair<List<UserAccountJoinVO>, Integer>(emptyList, 0);
    } else if (id != null) {
        sb.and("id", sb.entity().getId(), Op.EQ);
    } else {
        // this condition is used to exclude system user from the search
        // results
        sb.and("id", sb.entity().getId(), Op.NEQ);
    }
    sb.and("type", sb.entity().getAccountType(), Op.EQ);
    sb.and("domainId", sb.entity().getDomainId(), Op.EQ);
    sb.and("accountName", sb.entity().getAccountName(), Op.EQ);
    sb.and("state", sb.entity().getState(), Op.EQ);
    if ((accountName == null) && (domainId != null)) {
        sb.and("domainPath", sb.entity().getDomainPath(), Op.LIKE);
    }
    SearchCriteria<UserAccountJoinVO> sc = sb.create();
    // building ACL condition
    _accountMgr.buildACLViewSearchCriteria(sc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    if (keyword != null) {
        SearchCriteria<UserAccountJoinVO> ssc = _userAccountJoinDao.createSearchCriteria();
        ssc.addOr("username", Op.LIKE, "%" + keyword + "%");
        ssc.addOr("firstname", Op.LIKE, "%" + keyword + "%");
        ssc.addOr("lastname", Op.LIKE, "%" + keyword + "%");
        ssc.addOr("email", Op.LIKE, "%" + keyword + "%");
        ssc.addOr("state", Op.LIKE, "%" + keyword + "%");
        ssc.addOr("accountName", Op.LIKE, "%" + keyword + "%");
        ssc.addOr("accountType", Op.LIKE, "%" + keyword + "%");
        sc.addAnd("username", Op.SC, ssc);
    }
    if (username != null) {
        sc.setParameters("username", username);
    }
    if (id != null) {
        sc.setParameters("id", id);
    } else {
        // Don't return system user, search builder with NEQ
        sc.setParameters("id", 1);
    }
    if (type != null) {
        sc.setParameters("type", type);
    }
    if (accountName != null) {
        sc.setParameters("accountName", accountName);
        if (domainId != null) {
            sc.setParameters("domainId", domainId);
        }
    } else if (domainId != null) {
        DomainVO domainVO = _domainDao.findById(domainId);
        sc.setParameters("domainPath", domainVO.getPath() + "%");
    }
    if (state != null) {
        sc.setParameters("state", state);
    }
    return _userAccountJoinDao.searchAndCount(sc, searchFilter);
}
Also used : ListProjectResourcesCriteria(com.cloud.projects.Project.ListProjectResourcesCriteria) DomainVO(com.cloud.domain.DomainVO) UserAccountJoinVO(com.cloud.api.query.vo.UserAccountJoinVO) Ternary(com.cloud.utils.Ternary) ArrayList(java.util.ArrayList) Pair(com.cloud.utils.Pair)

Example 57 with ListProjectResourcesCriteria

use of com.cloud.projects.Project.ListProjectResourcesCriteria in project cloudstack by apache.

the class QueryManagerImpl method searchForRoutersInternal.

private Pair<List<DomainRouterJoinVO>, Integer> searchForRoutersInternal(BaseListProjectAndAccountResourcesCmd cmd, Long id, String name, String state, Long zoneId, Long podId, Long clusterId, Long hostId, String keyword, Long networkId, Long vpcId, Boolean forVpc, String role, String version, Boolean isHealthCheckFailed) {
    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(DomainRouterJoinVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal());
    SearchBuilder<DomainRouterJoinVO> sb = _routerJoinDao.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().getInstanceName(), SearchCriteria.Op.LIKE);
    sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
    sb.and("accountId", sb.entity().getAccountId(), SearchCriteria.Op.IN);
    sb.and("state", sb.entity().getState(), SearchCriteria.Op.EQ);
    sb.and("dataCenterId", sb.entity().getDataCenterId(), SearchCriteria.Op.EQ);
    sb.and("podId", sb.entity().getPodId(), SearchCriteria.Op.EQ);
    sb.and("clusterId", sb.entity().getClusterId(), SearchCriteria.Op.EQ);
    sb.and("hostId", sb.entity().getHostId(), SearchCriteria.Op.EQ);
    sb.and("vpcId", sb.entity().getVpcId(), SearchCriteria.Op.EQ);
    sb.and("role", sb.entity().getRole(), SearchCriteria.Op.EQ);
    sb.and("version", sb.entity().getTemplateVersion(), SearchCriteria.Op.LIKE);
    if (forVpc != null) {
        if (forVpc) {
            sb.and("forVpc", sb.entity().getVpcId(), SearchCriteria.Op.NNULL);
        } else {
            sb.and("forVpc", sb.entity().getVpcId(), SearchCriteria.Op.NULL);
        }
    }
    if (networkId != null) {
        sb.and("networkId", sb.entity().getNetworkId(), SearchCriteria.Op.EQ);
    }
    List<Long> routersWithFailures = null;
    if (isHealthCheckFailed != null) {
        GenericSearchBuilder<RouterHealthCheckResultVO, Long> routerHealthCheckResultSearch = routerHealthCheckResultDao.createSearchBuilder(Long.class);
        routerHealthCheckResultSearch.and("checkResult", routerHealthCheckResultSearch.entity().getCheckResult(), SearchCriteria.Op.EQ);
        routerHealthCheckResultSearch.selectFields(routerHealthCheckResultSearch.entity().getRouterId());
        routerHealthCheckResultSearch.done();
        SearchCriteria<Long> ssc = routerHealthCheckResultSearch.create();
        ssc.setParameters("checkResult", false);
        routersWithFailures = routerHealthCheckResultDao.customSearch(ssc, null);
        if (routersWithFailures != null && !routersWithFailures.isEmpty()) {
            if (isHealthCheckFailed) {
                sb.and("routerId", sb.entity().getId(), SearchCriteria.Op.IN);
            } else {
                sb.and("routerId", sb.entity().getId(), SearchCriteria.Op.NIN);
            }
        } else if (isHealthCheckFailed) {
            return new Pair<List<DomainRouterJoinVO>, Integer>(Collections.emptyList(), 0);
        }
    }
    SearchCriteria<DomainRouterJoinVO> sc = sb.create();
    _accountMgr.buildACLViewSearchCriteria(sc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    if (keyword != null) {
        SearchCriteria<DomainRouterJoinVO> ssc = _routerJoinDao.createSearchCriteria();
        ssc.addOr("name", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("instanceName", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("state", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("networkName", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("vpcName", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("redundantState", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        sc.addAnd("instanceName", SearchCriteria.Op.SC, ssc);
    }
    if (name != null) {
        sc.setParameters("name", "%" + name + "%");
    }
    if (id != null) {
        sc.setParameters("id", id);
    }
    if (state != null) {
        sc.setParameters("state", state);
    }
    if (zoneId != null) {
        sc.setParameters("dataCenterId", zoneId);
    }
    if (podId != null) {
        sc.setParameters("podId", podId);
    }
    if (clusterId != null) {
        sc.setParameters("clusterId", clusterId);
    }
    if (hostId != null) {
        sc.setParameters("hostId", hostId);
    }
    if (networkId != null) {
        sc.setParameters("networkId", networkId);
    }
    if (vpcId != null) {
        sc.setParameters("vpcId", vpcId);
    }
    if (role != null) {
        sc.setParameters("role", role);
    }
    if (version != null) {
        sc.setParameters("version", "Cloudstack Release " + version + "%");
    }
    if (routersWithFailures != null && !routersWithFailures.isEmpty()) {
        sc.setParameters("routerId", routersWithFailures.toArray(new Object[routersWithFailures.size()]));
    }
    // search VR details by ids
    Pair<List<DomainRouterJoinVO>, Integer> uniqueVrPair = _routerJoinDao.searchAndCount(sc, searchFilter);
    Integer count = uniqueVrPair.second();
    if (count.intValue() == 0) {
        // empty result
        return uniqueVrPair;
    }
    List<DomainRouterJoinVO> uniqueVrs = uniqueVrPair.first();
    Long[] vrIds = new Long[uniqueVrs.size()];
    int i = 0;
    for (DomainRouterJoinVO v : uniqueVrs) {
        vrIds[i++] = v.getId();
    }
    List<DomainRouterJoinVO> vrs = _routerJoinDao.searchByIds(vrIds);
    return new Pair<List<DomainRouterJoinVO>, Integer>(vrs, count);
}
Also used : Account(com.cloud.user.Account) Ternary(com.cloud.utils.Ternary) ArrayList(java.util.ArrayList) DomainRouterJoinVO(com.cloud.api.query.vo.DomainRouterJoinVO) 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) RouterHealthCheckResultVO(com.cloud.network.dao.RouterHealthCheckResultVO) Pair(com.cloud.utils.Pair)

Example 58 with ListProjectResourcesCriteria

use of com.cloud.projects.Project.ListProjectResourcesCriteria in project cloudstack by apache.

the class QueryManagerImpl method listTagsInternal.

private Pair<List<ResourceTagJoinVO>, Integer> listTagsInternal(ListTagsCmd cmd) {
    Account caller = CallContext.current().getCallingAccount();
    List<Long> permittedAccounts = new ArrayList<Long>();
    String key = cmd.getKey();
    String value = cmd.getValue();
    String resourceId = cmd.getResourceId();
    String resourceType = cmd.getResourceType();
    String customerName = cmd.getCustomer();
    boolean listAll = cmd.listAll();
    Long projectId = cmd.getProjectId();
    if (projectId == null && ResourceObjectType.Project.name().equalsIgnoreCase(resourceType) && StringUtils.isNotEmpty(resourceId)) {
        try {
            projectId = Long.parseLong(resourceId);
        } catch (final NumberFormatException e) {
            final ProjectVO project = _projectDao.findByUuidIncludingRemoved(resourceId);
            if (project != null) {
                projectId = project.getId();
            }
        }
    }
    Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<Long, Boolean, ListProjectResourcesCriteria>(cmd.getDomainId(), cmd.isRecursive(), null);
    _accountMgr.buildACLSearchParameters(caller, null, cmd.getAccountName(), projectId, permittedAccounts, domainIdRecursiveListProject, listAll, false);
    Long domainId = domainIdRecursiveListProject.first();
    Boolean isRecursive = domainIdRecursiveListProject.second();
    ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third();
    Filter searchFilter = new Filter(ResourceTagJoinVO.class, "resourceType", false, cmd.getStartIndex(), cmd.getPageSizeVal());
    SearchBuilder<ResourceTagJoinVO> sb = _resourceTagJoinDao.createSearchBuilder();
    _accountMgr.buildACLViewSearchBuilder(sb, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    sb.and("key", sb.entity().getKey(), SearchCriteria.Op.EQ);
    sb.and("value", sb.entity().getValue(), SearchCriteria.Op.EQ);
    if (resourceId != null) {
        sb.and("resourceId", sb.entity().getResourceId(), SearchCriteria.Op.EQ);
        sb.and("resourceUuid", sb.entity().getResourceUuid(), SearchCriteria.Op.EQ);
    }
    sb.and("resourceType", sb.entity().getResourceType(), SearchCriteria.Op.EQ);
    sb.and("customer", sb.entity().getCustomer(), SearchCriteria.Op.EQ);
    // now set the SC criteria...
    SearchCriteria<ResourceTagJoinVO> sc = sb.create();
    _accountMgr.buildACLViewSearchCriteria(sc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    if (key != null) {
        sc.setParameters("key", key);
    }
    if (value != null) {
        sc.setParameters("value", value);
    }
    if (resourceId != null) {
        try {
            long rid = Long.parseLong(resourceId);
            sc.setParameters("resourceId", rid);
        } catch (NumberFormatException ex) {
            // internal id instead of resource id is passed
            sc.setParameters("resourceUuid", resourceId);
        }
    }
    if (resourceType != null) {
        sc.setParameters("resourceType", resourceType);
    }
    if (customerName != null) {
        sc.setParameters("customer", customerName);
    }
    Pair<List<ResourceTagJoinVO>, Integer> result = _resourceTagJoinDao.searchAndCount(sc, searchFilter);
    return result;
}
Also used : Account(com.cloud.user.Account) Ternary(com.cloud.utils.Ternary) ArrayList(java.util.ArrayList) ProjectVO(com.cloud.projects.ProjectVO) ListProjectResourcesCriteria(com.cloud.projects.Project.ListProjectResourcesCriteria) TemplateFilter(com.cloud.template.VirtualMachineTemplate.TemplateFilter) Filter(com.cloud.utils.db.Filter) ResourceTagJoinVO(com.cloud.api.query.vo.ResourceTagJoinVO) ArrayList(java.util.ArrayList) List(java.util.List)

Example 59 with ListProjectResourcesCriteria

use of com.cloud.projects.Project.ListProjectResourcesCriteria in project cloudstack by apache.

the class ManagementServerImpl method searchForIPAddresses.

@Override
public Pair<List<? extends IpAddress>, Integer> searchForIPAddresses(final ListPublicIpAddressesCmd cmd) {
    final Long associatedNetworkId = cmd.getAssociatedNetworkId();
    final Long zone = cmd.getZoneId();
    final Long vlan = cmd.getVlanId();
    final Boolean forVirtualNetwork = cmd.isForVirtualNetwork();
    final Long ipId = cmd.getId();
    final Long networkId = cmd.getNetworkId();
    final Long vpcId = cmd.getVpcId();
    final String state = cmd.getState();
    Boolean isAllocated = cmd.isAllocatedOnly();
    if (isAllocated == null) {
        if (state != null && state.equalsIgnoreCase(IpAddress.State.Free.name())) {
            isAllocated = Boolean.FALSE;
        } else {
            // default
            isAllocated = Boolean.TRUE;
        }
    } else {
        if (state != null && state.equalsIgnoreCase(IpAddress.State.Free.name())) {
            if (isAllocated) {
                throw new InvalidParameterValueException("Conflict: allocatedonly is true but state is Free");
            }
        } else if (state != null && state.equalsIgnoreCase(IpAddress.State.Allocated.name())) {
            isAllocated = Boolean.TRUE;
        }
    }
    VlanType vlanType = null;
    if (forVirtualNetwork != null) {
        vlanType = forVirtualNetwork ? VlanType.VirtualNetwork : VlanType.DirectAttached;
    } else {
        vlanType = VlanType.VirtualNetwork;
    }
    final Account caller = getCaller();
    List<IPAddressVO> addrs = new ArrayList<>();
    if (vlanType == VlanType.DirectAttached && networkId == null && ipId == null) {
        // only root admin can list public ips in all shared networks
        if (caller.getType() != Account.ACCOUNT_TYPE_ADMIN) {
            isAllocated = true;
        }
    } else if (vlanType == VlanType.DirectAttached) {
        // list public ip address on shared network
        // access control. admin: all Ips, domain admin/user: all Ips in shared network in the domain/sub-domain/user
        NetworkVO network = null;
        if (networkId == null) {
            IPAddressVO ip = _publicIpAddressDao.findById(ipId);
            if (ip == null) {
                throw new InvalidParameterValueException("Please specify a valid ipaddress id");
            }
            network = _networkDao.findById(ip.getSourceNetworkId());
        } else {
            network = _networkDao.findById(networkId);
        }
        if (network == null || network.getGuestType() != Network.GuestType.Shared) {
            throw new InvalidParameterValueException("Please specify a valid network id");
        }
        if (network.getAclType() == ControlledEntity.ACLType.Account) {
            NetworkAccountVO networkMap = _networkAccountDao.getAccountNetworkMapByNetworkId(network.getId());
            if (networkMap == null) {
                return new Pair<>(addrs, 0);
            }
            _accountMgr.checkAccess(caller, null, false, _accountDao.findById(networkMap.getAccountId()));
        } else {
            // Domain level
            NetworkDomainVO networkMap = _networkDomainDao.getDomainNetworkMapByNetworkId(network.getId());
            if (networkMap == null) {
                return new Pair<>(addrs, 0);
            }
            if (caller.getType() == Account.ACCOUNT_TYPE_NORMAL || caller.getType() == Account.ACCOUNT_TYPE_PROJECT) {
                if (_networkMgr.isNetworkAvailableInDomain(network.getId(), caller.getDomainId())) {
                    isAllocated = Boolean.TRUE;
                } else {
                    return new Pair<>(addrs, 0);
                }
            } else if (caller.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN || caller.getType() == Account.ACCOUNT_TYPE_RESOURCE_DOMAIN_ADMIN) {
                if (caller.getDomainId() == networkMap.getDomainId() || _domainDao.isChildDomain(caller.getDomainId(), networkMap.getDomainId())) {
                    s_logger.debug("Caller " + caller.getUuid() + " has permission to access the network : " + network.getUuid());
                } else {
                    if (_networkMgr.isNetworkAvailableInDomain(network.getId(), caller.getDomainId())) {
                        isAllocated = Boolean.TRUE;
                    } else {
                        return new Pair<>(addrs, 0);
                    }
                }
            }
        }
    }
    final Filter searchFilter = new Filter(IPAddressVO.class, "address", false, null, null);
    final SearchBuilder<IPAddressVO> sb = _publicIpAddressDao.createSearchBuilder();
    Long domainId = null;
    Boolean isRecursive = null;
    final List<Long> permittedAccounts = new ArrayList<>();
    ListProjectResourcesCriteria listProjectResourcesCriteria = null;
    if (isAllocated || (vlanType == VlanType.VirtualNetwork && (caller.getType() != Account.ACCOUNT_TYPE_ADMIN || cmd.getDomainId() != null))) {
        final Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<>(cmd.getDomainId(), cmd.isRecursive(), null);
        _accountMgr.buildACLSearchParameters(caller, cmd.getId(), cmd.getAccountName(), cmd.getProjectId(), permittedAccounts, domainIdRecursiveListProject, cmd.listAll(), false);
        domainId = domainIdRecursiveListProject.first();
        isRecursive = domainIdRecursiveListProject.second();
        listProjectResourcesCriteria = domainIdRecursiveListProject.third();
        _accountMgr.buildACLSearchBuilder(sb, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    }
    buildParameters(sb, cmd, vlanType == VlanType.VirtualNetwork ? true : isAllocated);
    SearchCriteria<IPAddressVO> sc = sb.create();
    setParameters(sc, cmd, vlanType);
    if (isAllocated || (vlanType == VlanType.VirtualNetwork && (caller.getType() != Account.ACCOUNT_TYPE_ADMIN || cmd.getDomainId() != null))) {
        _accountMgr.buildACLSearchCriteria(sc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    }
    if (associatedNetworkId != null) {
        _accountMgr.checkAccess(caller, null, false, _networkDao.findById(associatedNetworkId));
        sc.setParameters("associatedNetworkIdEq", associatedNetworkId);
    }
    if (vpcId != null) {
        _accountMgr.checkAccess(caller, null, false, _vpcDao.findById(vpcId));
        sc.setParameters("vpcId", vpcId);
    }
    // Allocated
    addrs = _publicIpAddressDao.search(sc, searchFilter);
    // Free IP addresses in system IP ranges
    List<Long> freeAddrIds = new ArrayList<>();
    if (!(isAllocated || vlanType == VlanType.DirectAttached)) {
        Long zoneId = zone;
        Account owner = _accountMgr.finalizeOwner(CallContext.current().getCallingAccount(), cmd.getAccountName(), cmd.getDomainId(), cmd.getProjectId());
        if (associatedNetworkId != null) {
            NetworkVO guestNetwork = _networkDao.findById(associatedNetworkId);
            if (zoneId == null) {
                zoneId = guestNetwork.getDataCenterId();
            } else if (zoneId != guestNetwork.getDataCenterId()) {
                InvalidParameterValueException ex = new InvalidParameterValueException("Please specify a valid associated network id in the specified zone.");
                throw ex;
            }
            owner = _accountDao.findById(guestNetwork.getAccountId());
        }
        List<DataCenterVO> dcList = new ArrayList<>();
        if (zoneId == null) {
            dcList = ApiDBUtils.listZones();
        } else {
            dcList.add(ApiDBUtils.findZoneById(zoneId));
        }
        List<Long> vlanDbIds = null;
        if (vlan != null) {
            vlanDbIds = new ArrayList<>();
            vlanDbIds.add(vlan);
        }
        List<IPAddressVO> freeAddrs = new ArrayList<>();
        for (DataCenterVO dc : dcList) {
            long dcId = dc.getId();
            try {
                freeAddrs.addAll(_ipAddressMgr.listAvailablePublicIps(dcId, null, vlanDbIds, owner, VlanType.VirtualNetwork, associatedNetworkId, false, false, false, null, null, false, cmd.getVpcId(), cmd.isDisplay(), false, // Free
                false));
            } catch (InsufficientAddressCapacityException e) {
                s_logger.warn("no free address is found in zone " + dcId);
            }
        }
        for (IPAddressVO addr : freeAddrs) {
            freeAddrIds.add(addr.getId());
        }
    }
    if (freeAddrIds.size() > 0) {
        final SearchBuilder<IPAddressVO> sb2 = _publicIpAddressDao.createSearchBuilder();
        buildParameters(sb2, cmd, false);
        sb2.and("ids", sb2.entity().getId(), SearchCriteria.Op.IN);
        SearchCriteria<IPAddressVO> sc2 = sb2.create();
        setParameters(sc2, cmd, vlanType);
        sc2.setParameters("ids", freeAddrIds.toArray());
        // Allocated + Free
        addrs.addAll(_publicIpAddressDao.search(sc2, searchFilter));
    }
    Collections.sort(addrs, Comparator.comparing(IPAddressVO::getAddress));
    List<? extends IpAddress> wPagination = com.cloud.utils.StringUtils.applyPagination(addrs, cmd.getStartIndex(), cmd.getPageSizeVal());
    if (wPagination != null) {
        return new Pair<List<? extends IpAddress>, Integer>(wPagination, addrs.size());
    }
    return new Pair<>(addrs, addrs.size());
}
Also used : Account(com.cloud.user.Account) InsufficientAddressCapacityException(com.cloud.exception.InsufficientAddressCapacityException) ArrayList(java.util.ArrayList) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) NetworkDomainVO(com.cloud.network.dao.NetworkDomainVO) VlanType(com.cloud.dc.Vlan.VlanType) Pair(com.cloud.utils.Pair) SSHKeyPair(com.cloud.user.SSHKeyPair) DataCenterVO(com.cloud.dc.DataCenterVO) NetworkVO(com.cloud.network.dao.NetworkVO) Ternary(com.cloud.utils.Ternary) ListProjectResourcesCriteria(com.cloud.projects.Project.ListProjectResourcesCriteria) Filter(com.cloud.utils.db.Filter) IPAddressVO(com.cloud.network.dao.IPAddressVO) NetworkAccountVO(com.cloud.network.dao.NetworkAccountVO)

Example 60 with ListProjectResourcesCriteria

use of com.cloud.projects.Project.ListProjectResourcesCriteria in project cloudstack by apache.

the class ManagementServerImpl method listSSHKeyPairs.

@Override
public Pair<List<? extends SSHKeyPair>, Integer> listSSHKeyPairs(final ListSSHKeyPairsCmd cmd) {
    final Long id = cmd.getId();
    final String name = cmd.getName();
    final String fingerPrint = cmd.getFingerprint();
    final String keyword = cmd.getKeyword();
    final Account caller = getCaller();
    final List<Long> permittedAccounts = new ArrayList<Long>();
    final Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<Long, Boolean, ListProjectResourcesCriteria>(cmd.getDomainId(), cmd.isRecursive(), null);
    _accountMgr.buildACLSearchParameters(caller, null, 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 SearchBuilder<SSHKeyPairVO> sb = _sshKeyPairDao.createSearchBuilder();
    _accountMgr.buildACLSearchBuilder(sb, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    final Filter searchFilter = new Filter(SSHKeyPairVO.class, "id", false, cmd.getStartIndex(), cmd.getPageSizeVal());
    final SearchCriteria<SSHKeyPairVO> sc = sb.create();
    _accountMgr.buildACLSearchCriteria(sc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    if (id != null) {
        sc.addAnd("id", SearchCriteria.Op.EQ, id);
    }
    if (name != null) {
        sc.addAnd("name", SearchCriteria.Op.EQ, name);
    }
    if (fingerPrint != null) {
        sc.addAnd("fingerprint", SearchCriteria.Op.EQ, fingerPrint);
    }
    if (keyword != null) {
        final SearchCriteria<SSHKeyPairVO> ssc = _sshKeyPairDao.createSearchCriteria();
        ssc.addOr("name", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("fingerprint", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        sc.addAnd("name", SearchCriteria.Op.SC, ssc);
    }
    final Pair<List<SSHKeyPairVO>, Integer> result = _sshKeyPairDao.searchAndCount(sc, searchFilter);
    return new Pair<List<? extends SSHKeyPair>, Integer>(result.first(), result.second());
}
Also used : Account(com.cloud.user.Account) Ternary(com.cloud.utils.Ternary) ArrayList(java.util.ArrayList) SSHKeyPairVO(com.cloud.user.SSHKeyPairVO) ListProjectResourcesCriteria(com.cloud.projects.Project.ListProjectResourcesCriteria) Filter(com.cloud.utils.db.Filter) ArrayList(java.util.ArrayList) ExcludeList(com.cloud.deploy.DeploymentPlanner.ExcludeList) List(java.util.List) Pair(com.cloud.utils.Pair) SSHKeyPair(com.cloud.user.SSHKeyPair)

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