Search in sources :

Example 31 with Ternary

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

the class QueryManagerImpl method searchForAffinityGroupsInternal.

public Pair<List<AffinityGroupJoinVO>, Integer> searchForAffinityGroupsInternal(ListAffinityGroupsCmd cmd) {
    final Long affinityGroupId = cmd.getId();
    final String affinityGroupName = cmd.getAffinityGroupName();
    final String affinityGroupType = cmd.getAffinityGroupType();
    final Long vmId = cmd.getVirtualMachineId();
    final String accountName = cmd.getAccountName();
    Long domainId = cmd.getDomainId();
    final Long projectId = cmd.getProjectId();
    Boolean isRecursive = cmd.isRecursive();
    final Boolean listAll = cmd.listAll();
    final Long startIndex = cmd.getStartIndex();
    final Long pageSize = cmd.getPageSizeVal();
    final String keyword = cmd.getKeyword();
    Account caller = CallContext.current().getCallingAccount();
    if (vmId != null) {
        UserVmVO userVM = _userVmDao.findById(vmId);
        if (userVM == null) {
            throw new InvalidParameterValueException("Unable to list affinity groups for virtual machine instance " + vmId + "; instance not found.");
        }
        _accountMgr.checkAccess(caller, null, true, userVM);
        return listAffinityGroupsByVM(vmId.longValue(), startIndex, pageSize);
    }
    List<Long> permittedAccounts = new ArrayList<Long>();
    Ternary<Long, Boolean, ListProjectResourcesCriteria> ternary = new Ternary<Long, Boolean, ListProjectResourcesCriteria>(domainId, isRecursive, null);
    _accountMgr.buildACLSearchParameters(caller, affinityGroupId, accountName, projectId, permittedAccounts, ternary, listAll, false);
    domainId = ternary.first();
    isRecursive = ternary.second();
    ListProjectResourcesCriteria listProjectResourcesCriteria = ternary.third();
    Filter searchFilter = new Filter(AffinityGroupJoinVO.class, ID_FIELD, true, startIndex, pageSize);
    SearchCriteria<AffinityGroupJoinVO> sc = buildAffinityGroupSearchCriteria(domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria, affinityGroupId, affinityGroupName, affinityGroupType, keyword);
    Pair<List<AffinityGroupJoinVO>, Integer> uniqueGroupsPair = _affinityGroupJoinDao.searchAndCount(sc, searchFilter);
    // search group details by ids
    List<AffinityGroupJoinVO> affinityGroups = new ArrayList<AffinityGroupJoinVO>();
    Integer count = uniqueGroupsPair.second();
    if (count.intValue() != 0) {
        List<AffinityGroupJoinVO> uniqueGroups = uniqueGroupsPair.first();
        Long[] vrIds = new Long[uniqueGroups.size()];
        int i = 0;
        for (AffinityGroupJoinVO v : uniqueGroups) {
            vrIds[i++] = v.getId();
        }
        affinityGroups = _affinityGroupJoinDao.searchByIds(vrIds);
    }
    if (!permittedAccounts.isEmpty()) {
        // add domain level affinity groups
        if (domainId != null) {
            SearchCriteria<AffinityGroupJoinVO> scDomain = buildAffinityGroupSearchCriteria(null, isRecursive, new ArrayList<Long>(), listProjectResourcesCriteria, affinityGroupId, affinityGroupName, affinityGroupType, keyword);
            affinityGroups.addAll(listDomainLevelAffinityGroups(scDomain, searchFilter, domainId));
        } else {
            for (Long permAcctId : permittedAccounts) {
                Account permittedAcct = _accountDao.findById(permAcctId);
                SearchCriteria<AffinityGroupJoinVO> scDomain = buildAffinityGroupSearchCriteria(null, isRecursive, new ArrayList<Long>(), listProjectResourcesCriteria, affinityGroupId, affinityGroupName, affinityGroupType, keyword);
                affinityGroups.addAll(listDomainLevelAffinityGroups(scDomain, searchFilter, permittedAcct.getDomainId()));
            }
        }
    } else if (((permittedAccounts.isEmpty()) && (domainId != null) && isRecursive)) {
        // list all domain level affinity groups for the domain admin case
        SearchCriteria<AffinityGroupJoinVO> scDomain = buildAffinityGroupSearchCriteria(null, isRecursive, new ArrayList<Long>(), listProjectResourcesCriteria, affinityGroupId, affinityGroupName, affinityGroupType, keyword);
        affinityGroups.addAll(listDomainLevelAffinityGroups(scDomain, searchFilter, domainId));
    }
    return new Pair<List<AffinityGroupJoinVO>, Integer>(affinityGroups, affinityGroups.size());
}
Also used : Account(com.cloud.user.Account) UserVmVO(com.cloud.vm.UserVmVO) Ternary(com.cloud.utils.Ternary) ArrayList(java.util.ArrayList) SearchCriteria(com.cloud.utils.db.SearchCriteria) ListProjectResourcesCriteria(com.cloud.projects.Project.ListProjectResourcesCriteria) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) TemplateFilter(com.cloud.template.VirtualMachineTemplate.TemplateFilter) Filter(com.cloud.utils.db.Filter) AffinityGroupJoinVO(com.cloud.api.query.vo.AffinityGroupJoinVO) ArrayList(java.util.ArrayList) List(java.util.List) Pair(com.cloud.utils.Pair)

Example 32 with Ternary

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

the class QueryManagerImpl method searchForSecurityGroupsInternal.

private Pair<List<SecurityGroupJoinVO>, Integer> searchForSecurityGroupsInternal(ListSecurityGroupsCmd cmd) throws PermissionDeniedException, InvalidParameterValueException {
    Account caller = CallContext.current().getCallingAccount();
    Long instanceId = cmd.getVirtualMachineId();
    String securityGroup = cmd.getSecurityGroupName();
    Long id = cmd.getId();
    Object keyword = cmd.getKeyword();
    List<Long> permittedAccounts = new ArrayList<Long>();
    Map<String, String> tags = cmd.getTags();
    if (instanceId != null) {
        UserVmVO userVM = _userVmDao.findById(instanceId);
        if (userVM == null) {
            throw new InvalidParameterValueException("Unable to list network groups for virtual machine instance " + instanceId + "; instance not found.");
        }
        _accountMgr.checkAccess(caller, null, true, userVM);
        return listSecurityGroupRulesByVM(instanceId.longValue(), cmd.getStartIndex(), cmd.getPageSizeVal());
    }
    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(SecurityGroupJoinVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal());
    SearchBuilder<SecurityGroupJoinVO> sb = _securityGroupJoinDao.createSearchBuilder();
    // select distinct
    sb.select(null, Func.DISTINCT, sb.entity().getId());
    // ids
    _accountMgr.buildACLViewSearchBuilder(sb, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
    sb.and("name", sb.entity().getName(), SearchCriteria.Op.EQ);
    SearchCriteria<SecurityGroupJoinVO> sc = sb.create();
    _accountMgr.buildACLViewSearchCriteria(sc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    if (id != null) {
        sc.setParameters("id", id);
    }
    if (tags != null && !tags.isEmpty()) {
        SearchCriteria<SecurityGroupJoinVO> tagSc = _securityGroupJoinDao.createSearchCriteria();
        for (String key : tags.keySet()) {
            SearchCriteria<SecurityGroupJoinVO> tsc = _securityGroupJoinDao.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 (securityGroup != null) {
        sc.setParameters("name", securityGroup);
    }
    if (keyword != null) {
        SearchCriteria<SecurityGroupJoinVO> ssc = _securityGroupJoinDao.createSearchCriteria();
        ssc.addOr("name", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("description", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        sc.addAnd("name", SearchCriteria.Op.SC, ssc);
    }
    // search security group together with rules
    Pair<List<SecurityGroupJoinVO>, Integer> uniqueSgPair = _securityGroupJoinDao.searchAndCount(sc, searchFilter);
    Integer count = uniqueSgPair.second();
    if (count.intValue() == 0) {
        // handle empty result cases
        return uniqueSgPair;
    }
    List<SecurityGroupJoinVO> uniqueSgs = uniqueSgPair.first();
    Long[] sgIds = new Long[uniqueSgs.size()];
    int i = 0;
    for (SecurityGroupJoinVO v : uniqueSgs) {
        sgIds[i++] = v.getId();
    }
    List<SecurityGroupJoinVO> sgs = _securityGroupJoinDao.searchByIds(sgIds);
    return new Pair<List<SecurityGroupJoinVO>, Integer>(sgs, count);
}
Also used : Account(com.cloud.user.Account) UserVmVO(com.cloud.vm.UserVmVO) Ternary(com.cloud.utils.Ternary) ArrayList(java.util.ArrayList) SecurityGroupJoinVO(com.cloud.api.query.vo.SecurityGroupJoinVO) ListProjectResourcesCriteria(com.cloud.projects.Project.ListProjectResourcesCriteria) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) TemplateFilter(com.cloud.template.VirtualMachineTemplate.TemplateFilter) Filter(com.cloud.utils.db.Filter) ArrayList(java.util.ArrayList) List(java.util.List) Pair(com.cloud.utils.Pair)

Example 33 with Ternary

use of com.cloud.utils.Ternary 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();
    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, 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) 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 34 with Ternary

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

the class RemoteAccessVpnManagerImpl method searchForRemoteAccessVpns.

@Override
public Pair<List<? extends RemoteAccessVpn>, Integer> searchForRemoteAccessVpns(ListRemoteAccessVpnsCmd cmd) {
    // do some parameter validation
    Account caller = CallContext.current().getCallingAccount();
    Long ipAddressId = cmd.getPublicIpId();
    List<Long> permittedAccounts = new ArrayList<Long>();
    Long vpnId = cmd.getId();
    Long networkId = cmd.getNetworkId();
    if (ipAddressId != null) {
        PublicIpAddress publicIp = _networkMgr.getPublicIpAddress(ipAddressId);
        if (publicIp == null) {
            throw new InvalidParameterValueException("Unable to list remote access vpns, IP address " + ipAddressId + " not found.");
        } else {
            Long ipAddrAcctId = publicIp.getAccountId();
            if (ipAddrAcctId == null) {
                throw new InvalidParameterValueException("Unable to list remote access vpns, IP address " + ipAddressId + " is not associated with an account.");
            }
        }
        _accountMgr.checkAccess(caller, null, true, publicIp);
    }
    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);
    Long domainId = domainIdRecursiveListProject.first();
    Boolean isRecursive = domainIdRecursiveListProject.second();
    ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third();
    Filter filter = new Filter(RemoteAccessVpnVO.class, "serverAddressId", false, cmd.getStartIndex(), cmd.getPageSizeVal());
    SearchBuilder<RemoteAccessVpnVO> sb = _remoteAccessVpnDao.createSearchBuilder();
    _accountMgr.buildACLSearchBuilder(sb, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    sb.and("serverAddressId", sb.entity().getServerAddressId(), Op.EQ);
    sb.and("id", sb.entity().getId(), Op.EQ);
    sb.and("networkId", sb.entity().getNetworkId(), Op.EQ);
    sb.and("state", sb.entity().getState(), Op.EQ);
    sb.and("display", sb.entity().isDisplay(), Op.EQ);
    SearchCriteria<RemoteAccessVpnVO> sc = sb.create();
    _accountMgr.buildACLSearchCriteria(sc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    sc.setParameters("state", RemoteAccessVpn.State.Running);
    if (ipAddressId != null) {
        sc.setParameters("serverAddressId", ipAddressId);
    }
    if (vpnId != null) {
        sc.setParameters("id", vpnId);
    }
    if (networkId != null) {
        sc.setParameters("networkId", networkId);
    }
    Pair<List<RemoteAccessVpnVO>, Integer> result = _remoteAccessVpnDao.searchAndCount(sc, filter);
    return new Pair<List<? extends RemoteAccessVpn>, Integer>(result.first(), result.second());
}
Also used : Account(com.cloud.user.Account) RemoteAccessVpnVO(com.cloud.network.dao.RemoteAccessVpnVO) Ternary(com.cloud.utils.Ternary) ArrayList(java.util.ArrayList) PublicIpAddress(com.cloud.network.PublicIpAddress) ListProjectResourcesCriteria(com.cloud.projects.Project.ListProjectResourcesCriteria) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) Filter(com.cloud.utils.db.Filter) List(java.util.List) ArrayList(java.util.ArrayList) Pair(com.cloud.utils.Pair)

Example 35 with Ternary

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

the class VpcManagerImpl method listPrivateGateway.

@Override
public Pair<List<PrivateGateway>, Integer> listPrivateGateway(final ListPrivateGatewaysCmd cmd) {
    final String ipAddress = cmd.getIpAddress();
    final String vlan = cmd.getVlan();
    final Long vpcId = cmd.getVpcId();
    final Long id = cmd.getId();
    Boolean isRecursive = cmd.isRecursive();
    final Boolean listAll = cmd.listAll();
    Long domainId = cmd.getDomainId();
    final String accountName = cmd.getAccountName();
    final Account caller = CallContext.current().getCallingAccount();
    final List<Long> permittedAccounts = new ArrayList<Long>();
    final String state = cmd.getState();
    final Long projectId = cmd.getProjectId();
    final Filter searchFilter = new Filter(VpcGatewayVO.class, "id", false, cmd.getStartIndex(), cmd.getPageSizeVal());
    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<VpcGatewayVO> sb = _vpcGatewayDao.createSearchBuilder();
    _accountMgr.buildACLSearchBuilder(sb, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    if (vlan != null) {
        final SearchBuilder<NetworkVO> ntwkSearch = _ntwkDao.createSearchBuilder();
        ntwkSearch.and("vlan", ntwkSearch.entity().getBroadcastUri(), SearchCriteria.Op.EQ);
        sb.join("networkSearch", ntwkSearch, sb.entity().getNetworkId(), ntwkSearch.entity().getId(), JoinBuilder.JoinType.INNER);
    }
    final SearchCriteria<VpcGatewayVO> sc = sb.create();
    _accountMgr.buildACLSearchCriteria(sc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    if (id != null) {
        sc.addAnd("id", Op.EQ, id);
    }
    if (ipAddress != null) {
        sc.addAnd("ip4Address", Op.EQ, ipAddress);
    }
    if (state != null) {
        sc.addAnd("state", Op.EQ, state);
    }
    if (vpcId != null) {
        sc.addAnd("vpcId", Op.EQ, vpcId);
    }
    if (vlan != null) {
        sc.setJoinParameters("networkSearch", "vlan", BroadcastDomainType.Vlan.toUri(vlan));
    }
    final Pair<List<VpcGatewayVO>, Integer> vos = _vpcGatewayDao.searchAndCount(sc, searchFilter);
    final List<PrivateGateway> privateGtws = new ArrayList<PrivateGateway>(vos.first().size());
    for (final VpcGateway vo : vos.first()) {
        privateGtws.add(getPrivateGatewayProfile(vo));
    }
    return new Pair<List<PrivateGateway>, Integer>(privateGtws, vos.second());
}
Also used : Account(com.cloud.user.Account) NetworkVO(com.cloud.network.dao.NetworkVO) Ternary(com.cloud.utils.Ternary) ArrayList(java.util.ArrayList) ListProjectResourcesCriteria(com.cloud.projects.Project.ListProjectResourcesCriteria) Filter(com.cloud.utils.db.Filter) ArrayList(java.util.ArrayList) List(java.util.List) Pair(com.cloud.utils.Pair)

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