Search in sources :

Example 11 with Ternary

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

the class RabbitMQEventBus method configure.

@Override
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
    try {
        if (amqpHost == null || amqpHost.isEmpty()) {
            throw new ConfigurationException("Unable to get the AMQP server details");
        }
        if (username == null || username.isEmpty()) {
            throw new ConfigurationException("Unable to get the username details");
        }
        if (password == null || password.isEmpty()) {
            throw new ConfigurationException("Unable to get the password details");
        }
        if (amqpExchangeName == null || amqpExchangeName.isEmpty()) {
            throw new ConfigurationException("Unable to get the _exchange details on the AMQP server");
        }
        if (port == null) {
            throw new ConfigurationException("Unable to get the port details of AMQP server");
        }
        if (useSsl != null && !useSsl.isEmpty()) {
            if (!useSsl.equalsIgnoreCase("true") && !useSsl.equalsIgnoreCase("false")) {
                throw new ConfigurationException("Invalid configuration parameter for 'ssl'.");
            }
        }
        if (retryInterval == null) {
            // default to 10s to try out reconnect
            retryInterval = 10000;
        }
    } catch (NumberFormatException e) {
        throw new ConfigurationException("Invalid port number/retry interval");
    }
    s_subscribers = new ConcurrentHashMap<String, Ternary<String, Channel, EventSubscriber>>();
    executorService = Executors.newCachedThreadPool();
    disconnectHandler = new DisconnectHandler();
    blockedConnectionHandler = new BlockedConnectionHandler();
    return true;
}
Also used : ConfigurationException(javax.naming.ConfigurationException) Ternary(com.cloud.utils.Ternary)

Example 12 with Ternary

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

the class NetworkACLServiceImpl method listNetworkACLs.

@Override
public Pair<List<? extends NetworkACL>, Integer> listNetworkACLs(final ListNetworkACLListsCmd cmd) {
    final Long id = cmd.getId();
    final String name = cmd.getName();
    final Long networkId = cmd.getNetworkId();
    final Long vpcId = cmd.getVpcId();
    final String keyword = cmd.getKeyword();
    final Boolean display = cmd.getDisplay();
    final SearchBuilder<NetworkACLVO> sb = _networkACLDao.createSearchBuilder();
    sb.and("id", sb.entity().getId(), Op.EQ);
    sb.and("name", sb.entity().getName(), Op.EQ);
    sb.and("vpcId", sb.entity().getVpcId(), Op.IN);
    sb.and("display", sb.entity().isDisplay(), Op.EQ);
    final Account caller = CallContext.current().getCallingAccount();
    if (networkId != null) {
        final SearchBuilder<NetworkVO> network = _networkDao.createSearchBuilder();
        network.and("networkId", network.entity().getId(), Op.EQ);
        sb.join("networkJoin", network, sb.entity().getId(), network.entity().getNetworkACLId(), JoinBuilder.JoinType.INNER);
    }
    final SearchCriteria<NetworkACLVO> sc = sb.create();
    if (keyword != null) {
        final SearchCriteria<NetworkACLVO> ssc = _networkACLDao.createSearchCriteria();
        ssc.addOr("name", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("description", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        sc.addAnd("name", SearchCriteria.Op.SC, ssc);
    }
    if (display != null) {
        sc.setParameters("display", display);
    }
    if (id != null) {
        sc.setParameters("id", id);
    }
    if (name != null) {
        sc.setParameters("name", name);
    }
    if (vpcId != null) {
        final Vpc vpc = _entityMgr.findById(Vpc.class, vpcId);
        if (vpc == null) {
            throw new InvalidParameterValueException("Unable to find VPC");
        }
        _accountMgr.checkAccess(caller, null, true, vpc);
        //Include vpcId 0 to list default ACLs
        sc.setParameters("vpcId", vpcId, 0);
    } else {
        //ToDo: Add accountId to network_acl table for permission check
        // VpcId is not specified. Find permitted VPCs for the caller
        // and list ACLs belonging to the permitted VPCs
        final List<Long> permittedAccounts = new ArrayList<Long>();
        Long domainId = cmd.getDomainId();
        boolean isRecursive = cmd.isRecursive();
        final String accountName = cmd.getAccountName();
        final Long projectId = cmd.getProjectId();
        final boolean listAll = cmd.listAll();
        final Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<Long, Boolean, ListProjectResourcesCriteria>(domainId, isRecursive, null);
        _accountMgr.buildACLSearchParameters(caller, id, accountName, projectId, permittedAccounts, domainIdRecursiveListProject, listAll, false);
        domainId = domainIdRecursiveListProject.first();
        isRecursive = domainIdRecursiveListProject.second();
        final ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third();
        final SearchBuilder<VpcVO> sbVpc = _vpcDao.createSearchBuilder();
        _accountMgr.buildACLSearchBuilder(sbVpc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
        final SearchCriteria<VpcVO> scVpc = sbVpc.create();
        _accountMgr.buildACLSearchCriteria(scVpc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
        final List<VpcVO> vpcs = _vpcDao.search(scVpc, null);
        final List<Long> vpcIds = new ArrayList<Long>();
        for (final VpcVO vpc : vpcs) {
            vpcIds.add(vpc.getId());
        }
        //Add vpc_id 0 to list default ACLs
        vpcIds.add(0L);
        sc.setParameters("vpcId", vpcIds.toArray());
    }
    if (networkId != null) {
        sc.setJoinParameters("networkJoin", "networkId", networkId);
    }
    final Filter filter = new Filter(NetworkACLVO.class, "id", false, null, null);
    final Pair<List<NetworkACLVO>, Integer> acls = _networkACLDao.searchAndCount(sc, filter);
    return new Pair<List<? extends NetworkACL>, Integer>(acls.first(), acls.second());
}
Also used : Account(com.cloud.user.Account) ArrayList(java.util.ArrayList) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ArrayList(java.util.ArrayList) List(java.util.List) Pair(com.cloud.utils.Pair) NetworkVO(com.cloud.network.dao.NetworkVO) Ternary(com.cloud.utils.Ternary) ListProjectResourcesCriteria(com.cloud.projects.Project.ListProjectResourcesCriteria) Filter(com.cloud.utils.db.Filter)

Example 13 with Ternary

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

the class NetworkACLServiceImpl method listNetworkACLItems.

@Override
public Pair<List<? extends NetworkACLItem>, Integer> listNetworkACLItems(final ListNetworkACLsCmd cmd) {
    final Long networkId = cmd.getNetworkId();
    final Long id = cmd.getId();
    Long aclId = cmd.getAclId();
    final String trafficType = cmd.getTrafficType();
    final String protocol = cmd.getProtocol();
    final String action = cmd.getAction();
    final Map<String, String> tags = cmd.getTags();
    final Account caller = CallContext.current().getCallingAccount();
    final Filter filter = new Filter(NetworkACLItemVO.class, "id", false, cmd.getStartIndex(), cmd.getPageSizeVal());
    final SearchBuilder<NetworkACLItemVO> sb = _networkACLItemDao.createSearchBuilder();
    sb.and("id", sb.entity().getId(), Op.EQ);
    sb.and("aclId", sb.entity().getAclId(), Op.EQ);
    sb.and("trafficType", sb.entity().getTrafficType(), Op.EQ);
    sb.and("protocol", sb.entity().getProtocol(), Op.EQ);
    sb.and("action", sb.entity().getAction(), Op.EQ);
    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(), Op.EQ);
            tagSearch.and("value" + String.valueOf(count), tagSearch.entity().getValue(), Op.EQ);
            tagSearch.cp();
        }
        tagSearch.and("resourceType", tagSearch.entity().getResourceType(), Op.EQ);
        sb.groupBy(sb.entity().getId());
        sb.join("tagSearch", tagSearch, sb.entity().getId(), tagSearch.entity().getResourceId(), JoinBuilder.JoinType.INNER);
    }
    if (aclId == null) {
        //Join with network_acl table when aclId is not specified to list acl_items within permitted VPCs
        final SearchBuilder<NetworkACLVO> vpcSearch = _networkACLDao.createSearchBuilder();
        vpcSearch.and("vpcId", vpcSearch.entity().getVpcId(), Op.IN);
        sb.join("vpcSearch", vpcSearch, sb.entity().getAclId(), vpcSearch.entity().getId(), JoinBuilder.JoinType.INNER);
    }
    final SearchCriteria<NetworkACLItemVO> sc = sb.create();
    if (id != null) {
        sc.setParameters("id", id);
    }
    if (networkId != null) {
        final Network network = _networkDao.findById(networkId);
        aclId = network.getNetworkACLId();
        if (aclId == null) {
            //Return empty list
            return new Pair(new ArrayList<NetworkACLItem>(), 0);
        }
    }
    if (trafficType != null) {
        sc.setParameters("trafficType", trafficType);
    }
    if (aclId != null) {
        // Get VPC and check access
        final NetworkACL acl = _networkACLDao.findById(aclId);
        if (acl.getVpcId() != 0) {
            final Vpc vpc = _vpcDao.findById(acl.getVpcId());
            if (vpc == null) {
                throw new InvalidParameterValueException("Unable to find VPC associated with acl");
            }
            _accountMgr.checkAccess(caller, null, true, vpc);
        }
        sc.setParameters("aclId", aclId);
    } else {
        //ToDo: Add accountId to network_acl_item table for permission check
        // aclId is not specified
        // List permitted VPCs and filter aclItems
        final List<Long> permittedAccounts = new ArrayList<Long>();
        Long domainId = cmd.getDomainId();
        boolean isRecursive = cmd.isRecursive();
        final String accountName = cmd.getAccountName();
        final Long projectId = cmd.getProjectId();
        final boolean listAll = cmd.listAll();
        final Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<Long, Boolean, ListProjectResourcesCriteria>(domainId, isRecursive, null);
        _accountMgr.buildACLSearchParameters(caller, id, accountName, projectId, permittedAccounts, domainIdRecursiveListProject, listAll, false);
        domainId = domainIdRecursiveListProject.first();
        isRecursive = domainIdRecursiveListProject.second();
        final ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third();
        final SearchBuilder<VpcVO> sbVpc = _vpcDao.createSearchBuilder();
        _accountMgr.buildACLSearchBuilder(sbVpc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
        final SearchCriteria<VpcVO> scVpc = sbVpc.create();
        _accountMgr.buildACLSearchCriteria(scVpc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
        final List<VpcVO> vpcs = _vpcDao.search(scVpc, null);
        final List<Long> vpcIds = new ArrayList<Long>();
        for (final VpcVO vpc : vpcs) {
            vpcIds.add(vpc.getId());
        }
        //Add vpc_id 0 to list acl_items in default ACL
        vpcIds.add(0L);
        sc.setJoinParameters("vpcSearch", "vpcId", vpcIds.toArray());
    }
    if (protocol != null) {
        sc.setParameters("protocol", protocol);
    }
    if (action != null) {
        sc.setParameters("action", action);
    }
    if (tags != null && !tags.isEmpty()) {
        int count = 0;
        sc.setJoinParameters("tagSearch", "resourceType", ResourceObjectType.NetworkACL.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++;
        }
    }
    final Pair<List<NetworkACLItemVO>, Integer> result = _networkACLItemDao.searchAndCount(sc, filter);
    final List<NetworkACLItemVO> aclItemVOs = result.first();
    for (final NetworkACLItemVO item : aclItemVOs) {
        _networkACLItemDao.loadCidrs(item);
    }
    return new Pair<List<? extends NetworkACLItem>, Integer>(aclItemVOs, result.second());
}
Also used : Account(com.cloud.user.Account) ArrayList(java.util.ArrayList) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) Network(com.cloud.network.Network) 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)

Example 14 with Ternary

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

the class QueryManagerImpl method searchForAsyncJobsInternal.

private Pair<List<AsyncJobJoinVO>, Integer> searchForAsyncJobsInternal(ListAsyncJobsCmd cmd) {
    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, null, cmd.getAccountName(), null, permittedAccounts, domainIdRecursiveListProject, cmd.listAll(), false);
    Long domainId = domainIdRecursiveListProject.first();
    Boolean isRecursive = domainIdRecursiveListProject.second();
    ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third();
    Filter searchFilter = new Filter(AsyncJobJoinVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal());
    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);
        }
    }
    Object keyword = cmd.getKeyword();
    Object startDate = cmd.getStartDate();
    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) {
        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)

Example 15 with Ternary

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

the class QueryManagerImpl method searchForUserVMsInternal.

private Pair<List<UserVmJoinVO>, Integer> searchForUserVMsInternal(ListVMsCmd cmd) {
    Account caller = CallContext.current().getCallingAccount();
    List<Long> permittedAccounts = new ArrayList<Long>();
    boolean listAll = cmd.listAll();
    Long id = cmd.getId();
    Long userId = cmd.getUserId();
    Map<String, String> tags = cmd.getTags();
    Boolean display = cmd.getDisplay();
    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, listAll, false);
    Long domainId = domainIdRecursiveListProject.first();
    Boolean isRecursive = domainIdRecursiveListProject.second();
    ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third();
    Filter searchFilter = new Filter(UserVmJoinVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal());
    List<Long> ids = null;
    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<Long>();
        ids.add(cmd.getId());
    } else {
        ids = cmd.getIds();
    }
    // first search distinct vm id by using query criteria and pagination
    SearchBuilder<UserVmJoinVO> sb = _userVmJoinDao.createSearchBuilder();
    // select distinct ids
    sb.select(null, Func.DISTINCT, sb.entity().getId());
    _accountMgr.buildACLViewSearchBuilder(sb, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    String hypervisor = cmd.getHypervisor();
    Object name = cmd.getName();
    String state = cmd.getState();
    Object zoneId = cmd.getZoneId();
    Object keyword = cmd.getKeyword();
    boolean isAdmin = false;
    boolean isRootAdmin = false;
    if (_accountMgr.isAdmin(caller.getId())) {
        isAdmin = true;
    }
    if (_accountMgr.isRootAdmin(caller.getId())) {
        isRootAdmin = true;
    }
    Object groupId = cmd.getGroupId();
    Object networkId = cmd.getNetworkId();
    if (HypervisorType.getType(hypervisor) == HypervisorType.None && hypervisor != null) {
        // invalid hypervisor type input
        throw new InvalidParameterValueException("Invalid HypervisorType " + hypervisor);
    }
    Object templateId = cmd.getTemplateId();
    Object isoId = cmd.getIsoId();
    Object vpcId = cmd.getVpcId();
    Object affinityGroupId = cmd.getAffinityGroupId();
    Object keyPairName = cmd.getKeyPairName();
    Object serviceOffId = cmd.getServiceOfferingId();
    Object pod = null;
    Object hostId = null;
    Object storageId = null;
    if (cmd instanceof ListVMsCmdByAdmin) {
        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
    SearchCriteria<UserVmJoinVO> sc = sb.create();
    // building ACL condition
    _accountMgr.buildACLViewSearchCriteria(sc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    if (tags != null && !tags.isEmpty()) {
        SearchCriteria<UserVmJoinVO> tagSc = _userVmJoinDao.createSearchCriteria();
        for (Map.Entry<String, String> entry : tags.entrySet()) {
            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) {
        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) {
        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
    Pair<List<UserVmJoinVO>, Integer> uniqueVmPair = _userVmJoinDao.searchAndDistinctCount(sc, searchFilter);
    Integer count = uniqueVmPair.second();
    if (count.intValue() == 0) {
        // handle empty result cases
        return uniqueVmPair;
    }
    List<UserVmJoinVO> uniqueVms = uniqueVmPair.first();
    Long[] vmIds = new Long[uniqueVms.size()];
    int i = 0;
    for (UserVmJoinVO v : uniqueVms) {
        vmIds[i++] = v.getId();
    }
    List<UserVmJoinVO> vms = _userVmJoinDao.searchByIds(vmIds);
    return new Pair<List<UserVmJoinVO>, Integer>(vms, count);
}
Also used : Account(com.cloud.user.Account) ArrayList(java.util.ArrayList) InvalidParameterValueException(com.cloud.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(org.apache.cloudstack.api.command.admin.vm.ListVMsCmdByAdmin) Map(java.util.Map)

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