Search in sources :

Example 1 with DiskOfferingJoinVO

use of com.cloud.api.query.vo.DiskOfferingJoinVO in project cloudstack by apache.

the class QueryManagerImpl method searchForDiskOfferingsInternal.

private Pair<List<DiskOfferingJoinVO>, Integer> searchForDiskOfferingsInternal(ListDiskOfferingsCmd cmd) {
    // Note
    // The list method for offerings is being modified in accordance with
    // discussion with Will/Kevin
    // For now, we will be listing the following based on the usertype
    // 1. For root, we will list all offerings
    // 2. For domainAdmin and regular users, we will list everything in
    // their domains+parent domains ... all the way
    // till
    // root
    Filter searchFilter = new Filter(DiskOfferingJoinVO.class, "sortKey", SortKeyAscending.value(), cmd.getStartIndex(), cmd.getPageSizeVal());
    searchFilter.addOrderBy(DiskOfferingJoinVO.class, "id", true);
    SearchCriteria<DiskOfferingJoinVO> sc = _diskOfferingJoinDao.createSearchCriteria();
    sc.addAnd("computeOnly", Op.EQ, false);
    Account account = CallContext.current().getCallingAccount();
    Object name = cmd.getDiskOfferingName();
    Object id = cmd.getId();
    Object keyword = cmd.getKeyword();
    Long domainId = cmd.getDomainId();
    Boolean isRootAdmin = _accountMgr.isRootAdmin(account.getAccountId());
    Boolean isRecursive = cmd.isRecursive();
    Long zoneId = cmd.getZoneId();
    Long volumeId = cmd.getVolumeId();
    Long storagePoolId = cmd.getStoragePoolId();
    // associated with this domain
    if (domainId != null) {
        if (_accountMgr.isRootAdmin(account.getId()) || isPermissible(account.getDomainId(), domainId)) {
            // check if the user's domain == do's domain || user's domain is
            // a child of so's domain for non-root users
            sc.addAnd("domainId", Op.FIND_IN_SET, String.valueOf(domainId));
            if (!isRootAdmin) {
                sc.addAnd("displayOffering", SearchCriteria.Op.EQ, 1);
            }
            return _diskOfferingJoinDao.searchAndCount(sc, searchFilter);
        } else {
            throw new PermissionDeniedException("The account:" + account.getAccountName() + " does not fall in the same domain hierarchy as the disk offering");
        }
    }
    // and everything above till root
    if ((_accountMgr.isNormalUser(account.getId()) || _accountMgr.isDomainAdmin(account.getId())) || account.getType() == Account.ACCOUNT_TYPE_RESOURCE_DOMAIN_ADMIN) {
        if (isRecursive) {
            // domain + all sub-domains
            if (account.getType() == Account.ACCOUNT_TYPE_NORMAL) {
                throw new InvalidParameterValueException("Only ROOT admins and Domain admins can list disk offerings with isrecursive=true");
            }
        }
    }
    if (volumeId != null && storagePoolId != null) {
        throw new InvalidParameterValueException("Both volume ID and storage pool ID are not allowed at the same time");
    }
    if (keyword != null) {
        SearchCriteria<DiskOfferingJoinVO> ssc = _diskOfferingJoinDao.createSearchCriteria();
        ssc.addOr("displayText", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("name", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        sc.addAnd("name", SearchCriteria.Op.SC, ssc);
    }
    if (id != null) {
        sc.addAnd("id", SearchCriteria.Op.EQ, id);
    }
    if (name != null) {
        sc.addAnd("name", SearchCriteria.Op.EQ, name);
    }
    if (zoneId != null) {
        SearchBuilder<DiskOfferingJoinVO> sb = _diskOfferingJoinDao.createSearchBuilder();
        sb.and("zoneId", sb.entity().getZoneId(), Op.FIND_IN_SET);
        sb.or("zId", sb.entity().getZoneId(), Op.NULL);
        sb.done();
        SearchCriteria<DiskOfferingJoinVO> zoneSC = sb.create();
        zoneSC.setParameters("zoneId", String.valueOf(zoneId));
        sc.addAnd("zoneId", SearchCriteria.Op.SC, zoneSC);
    }
    DiskOffering currentDiskOffering = null;
    if (volumeId != null) {
        Volume volume = volumeDao.findById(volumeId);
        if (volume == null) {
            throw new InvalidParameterValueException(String.format("Unable to find a volume with specified id %s", volumeId));
        }
        currentDiskOffering = _diskOfferingDao.findByIdIncludingRemoved(volume.getDiskOfferingId());
        if (!currentDiskOffering.isComputeOnly() && currentDiskOffering.getDiskSizeStrictness()) {
            SearchCriteria<DiskOfferingJoinVO> ssc = _diskOfferingJoinDao.createSearchCriteria();
            ssc.addOr("diskSize", Op.EQ, volume.getSize());
            ssc.addOr("customized", SearchCriteria.Op.EQ, true);
            sc.addAnd("diskSizeOrCustomized", SearchCriteria.Op.SC, ssc);
        }
        sc.addAnd("id", SearchCriteria.Op.NEQ, currentDiskOffering.getId());
        sc.addAnd("diskSizeStrictness", Op.EQ, currentDiskOffering.getDiskSizeStrictness());
    }
    // Filter offerings that are not associated with caller's domain
    // Fetch the offering ids from the details table since theres no smart way to filter them in the join ... yet!
    Account caller = CallContext.current().getCallingAccount();
    if (caller.getType() != Account.ACCOUNT_TYPE_ADMIN) {
        Domain callerDomain = _domainDao.findById(caller.getDomainId());
        List<Long> domainIds = findRelatedDomainIds(callerDomain, isRecursive);
        List<Long> ids = _diskOfferingDetailsDao.findOfferingIdsByDomainIds(domainIds);
        SearchBuilder<DiskOfferingJoinVO> sb = _diskOfferingJoinDao.createSearchBuilder();
        if (ids != null && !ids.isEmpty()) {
            sb.and("id", sb.entity().getId(), Op.IN);
        }
        sb.or("domainId", sb.entity().getDomainId(), Op.NULL);
        sb.done();
        SearchCriteria<DiskOfferingJoinVO> scc = sb.create();
        if (ids != null && !ids.isEmpty()) {
            scc.setParameters("id", ids.toArray());
        }
        sc.addAnd("domainId", SearchCriteria.Op.SC, scc);
    }
    Pair<List<DiskOfferingJoinVO>, Integer> result = _diskOfferingJoinDao.searchAndCount(sc, searchFilter);
    String[] requiredTagsArray = new String[0];
    if (CollectionUtils.isNotEmpty(result.first()) && VolumeApiServiceImpl.MatchStoragePoolTagsWithDiskOffering.valueIn(zoneId)) {
        if (volumeId != null) {
            Volume volume = volumeDao.findById(volumeId);
            currentDiskOffering = _diskOfferingDao.findByIdIncludingRemoved(volume.getDiskOfferingId());
            requiredTagsArray = currentDiskOffering.getTagsArray();
        } else if (storagePoolId != null) {
            requiredTagsArray = _storageTagDao.getStoragePoolTags(storagePoolId).toArray(new String[0]);
        }
    }
    if (requiredTagsArray.length != 0) {
        ListIterator<DiskOfferingJoinVO> iteratorForTagsChecking = result.first().listIterator();
        while (iteratorForTagsChecking.hasNext()) {
            DiskOfferingJoinVO offering = iteratorForTagsChecking.next();
            String offeringTags = offering.getTags();
            String[] offeringTagsArray = (offeringTags == null || offeringTags.isEmpty()) ? new String[0] : offeringTags.split(",");
            if (!CollectionUtils.isSubCollection(Arrays.asList(requiredTagsArray), Arrays.asList(offeringTagsArray))) {
                iteratorForTagsChecking.remove();
            }
        }
    }
    return new Pair<>(result.first(), result.second());
}
Also used : Account(com.cloud.user.Account) DiskOffering(com.cloud.offering.DiskOffering) DiskOfferingJoinVO(com.cloud.api.query.vo.DiskOfferingJoinVO) TemplateFilter(com.cloud.template.VirtualMachineTemplate.TemplateFilter) Filter(com.cloud.utils.db.Filter) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) Volume(com.cloud.storage.Volume) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) ArrayList(java.util.ArrayList) List(java.util.List) Domain(com.cloud.domain.Domain) Pair(com.cloud.utils.Pair)

Example 2 with DiskOfferingJoinVO

use of com.cloud.api.query.vo.DiskOfferingJoinVO in project cloudstack by apache.

the class DomainManagerImpl method cleanupDomainOfferings.

protected void cleanupDomainOfferings(Long domainId) {
    if (domainId == null) {
        return;
    }
    String domainIdString = String.valueOf(domainId);
    List<Long> diskOfferingsDetailsToRemove = new ArrayList<>();
    List<Long> serviceOfferingsDetailsToRemove = new ArrayList<>();
    // delete the service and disk offerings associated with this domain
    List<DiskOfferingJoinVO> diskOfferingsForThisDomain = diskOfferingJoinDao.findByDomainId(domainId);
    for (DiskOfferingJoinVO diskOffering : diskOfferingsForThisDomain) {
        if (domainIdString.equals(diskOffering.getDomainId())) {
            diskOfferingDao.remove(diskOffering.getId());
        } else {
            diskOfferingsDetailsToRemove.add(diskOffering.getId());
        }
    }
    List<ServiceOfferingJoinVO> serviceOfferingsForThisDomain = serviceOfferingJoinDao.findByDomainId(domainId);
    for (ServiceOfferingJoinVO serviceOffering : serviceOfferingsForThisDomain) {
        if (domainIdString.equals(serviceOffering.getDomainId())) {
            serviceOfferingDao.remove(serviceOffering.getId());
        } else {
            serviceOfferingsDetailsToRemove.add(serviceOffering.getId());
        }
    }
    // Remove domain IDs for offerings which may be multi-domain
    for (final Long diskOfferingId : diskOfferingsDetailsToRemove) {
        diskOfferingDetailsDao.removeDetail(diskOfferingId, ApiConstants.DOMAIN_ID, domainIdString);
    }
    for (final Long serviceOfferingId : serviceOfferingsDetailsToRemove) {
        serviceOfferingDetailsDao.removeDetail(serviceOfferingId, ApiConstants.DOMAIN_ID, domainIdString);
    }
}
Also used : ServiceOfferingJoinVO(com.cloud.api.query.vo.ServiceOfferingJoinVO) ArrayList(java.util.ArrayList) DiskOfferingJoinVO(com.cloud.api.query.vo.DiskOfferingJoinVO)

Example 3 with DiskOfferingJoinVO

use of com.cloud.api.query.vo.DiskOfferingJoinVO in project cosmic by MissionCriticalCloud.

the class QueryManagerImpl method searchForDiskOfferingsInternal.

private Pair<List<DiskOfferingJoinVO>, Integer> searchForDiskOfferingsInternal(final ListDiskOfferingsCmd cmd) {
    // Note
    // The list method for offerings is being modified in accordance with
    // discussion with Will/Kevin
    // For now, we will be listing the following based on the usertype
    // 1. For root, we will list all offerings
    // 2. For domainAdmin and regular users, we will list everything in
    // their domains+parent domains ... all the way
    // till
    // root
    Boolean isAscending = Boolean.parseBoolean(_configDao.getValue("sortkey.algorithm"));
    isAscending = isAscending == null ? true : isAscending;
    final Filter searchFilter = new Filter(DiskOfferingJoinVO.class, "sortKey", isAscending, cmd.getStartIndex(), cmd.getPageSizeVal());
    final SearchCriteria<DiskOfferingJoinVO> sc = _diskOfferingJoinDao.createSearchCriteria();
    sc.addAnd("type", Op.EQ, DiskOfferingVO.Type.Disk);
    final Account account = CallContext.current().getCallingAccount();
    final Object name = cmd.getDiskOfferingName();
    final Object id = cmd.getId();
    final Object keyword = cmd.getKeyword();
    final Long domainId = cmd.getDomainId();
    final Boolean isRootAdmin = _accountMgr.isRootAdmin(account.getAccountId());
    final Boolean isRecursive = cmd.isRecursive();
    // associated with this domain
    if (domainId != null) {
        if (_accountMgr.isRootAdmin(account.getId()) || isPermissible(account.getDomainId(), domainId)) {
            // check if the user's domain == do's domain || user's domain is
            // a child of so's domain for non-root users
            sc.addAnd("domainId", SearchCriteria.Op.EQ, domainId);
            if (!isRootAdmin) {
                sc.addAnd("displayOffering", SearchCriteria.Op.EQ, 1);
            }
            return _diskOfferingJoinDao.searchAndCount(sc, searchFilter);
        } else {
            throw new PermissionDeniedException("The account:" + account.getAccountName() + " does not fall in the same domain hierarchy as the disk offering");
        }
    }
    final List<Long> domainIds;
    // and everything above till root
    if (_accountMgr.isNormalUser(account.getId()) || _accountMgr.isDomainAdmin(account.getId()) || account.getType() == Account.ACCOUNT_TYPE_RESOURCE_DOMAIN_ADMIN) {
        if (isRecursive) {
            // domain + all sub-domains
            if (account.getType() == Account.ACCOUNT_TYPE_NORMAL) {
                throw new InvalidParameterValueException("Only ROOT admins and Domain admins can list disk offerings with isrecursive=true");
            }
            final DomainVO domainRecord = _domainDao.findById(account.getDomainId());
            sc.addAnd("domainPath", SearchCriteria.Op.LIKE, domainRecord.getPath() + "%");
        } else {
            // domain + all ancestors
            // find all domain Id up to root domain for this account
            domainIds = new ArrayList<>();
            DomainVO domainRecord = _domainDao.findById(account.getDomainId());
            if (domainRecord == null) {
                s_logger.error("Could not find the domainId for account:" + account.getAccountName());
                throw new CloudAuthenticationException("Could not find the domainId for account:" + account.getAccountName());
            }
            domainIds.add(domainRecord.getId());
            while (domainRecord.getParent() != null) {
                domainRecord = _domainDao.findById(domainRecord.getParent());
                domainIds.add(domainRecord.getId());
            }
            final SearchCriteria<DiskOfferingJoinVO> spc = _diskOfferingJoinDao.createSearchCriteria();
            spc.addOr("domainId", SearchCriteria.Op.IN, domainIds.toArray());
            // include public offering as where
            spc.addOr("domainId", SearchCriteria.Op.NULL);
            sc.addAnd("domainId", SearchCriteria.Op.SC, spc);
            // non-root users should not see system offering at all
            sc.addAnd("systemUse", SearchCriteria.Op.EQ, false);
        }
    }
    if (keyword != null) {
        final SearchCriteria<DiskOfferingJoinVO> ssc = _diskOfferingJoinDao.createSearchCriteria();
        ssc.addOr("displayText", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("name", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        sc.addAnd("name", SearchCriteria.Op.SC, ssc);
    }
    if (id != null) {
        sc.addAnd("id", SearchCriteria.Op.EQ, id);
    }
    if (name != null) {
        sc.addAnd("name", SearchCriteria.Op.EQ, name);
    }
    return _diskOfferingJoinDao.searchAndCount(sc, searchFilter);
}
Also used : Account(com.cloud.user.Account) CloudAuthenticationException(com.cloud.exception.CloudAuthenticationException) DiskOfferingJoinVO(com.cloud.api.query.vo.DiskOfferingJoinVO) DomainVO(com.cloud.domain.DomainVO) TemplateFilter(com.cloud.template.VirtualMachineTemplate.TemplateFilter) Filter(com.cloud.utils.db.Filter) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException)

Aggregations

DiskOfferingJoinVO (com.cloud.api.query.vo.DiskOfferingJoinVO)3 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)2 TemplateFilter (com.cloud.template.VirtualMachineTemplate.TemplateFilter)2 Account (com.cloud.user.Account)2 Filter (com.cloud.utils.db.Filter)2 ArrayList (java.util.ArrayList)2 ServiceOfferingJoinVO (com.cloud.api.query.vo.ServiceOfferingJoinVO)1 Domain (com.cloud.domain.Domain)1 DomainVO (com.cloud.domain.DomainVO)1 CloudAuthenticationException (com.cloud.exception.CloudAuthenticationException)1 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)1 DiskOffering (com.cloud.offering.DiskOffering)1 Volume (com.cloud.storage.Volume)1 Pair (com.cloud.utils.Pair)1 InvalidParameterValueException (com.cloud.utils.exception.InvalidParameterValueException)1 List (java.util.List)1