Search in sources :

Example 1 with ResourceLimitVO

use of com.cloud.configuration.ResourceLimitVO in project cloudstack by apache.

the class ResourceLimitManagerImpl method searchForLimits.

@Override
public List<ResourceLimitVO> searchForLimits(Long id, Long accountId, Long domainId, Integer type, Long startIndex, Long pageSizeVal) {
    Account caller = CallContext.current().getCallingAccount();
    List<ResourceLimitVO> limits = new ArrayList<ResourceLimitVO>();
    boolean isAccount = true;
    if (!_accountMgr.isAdmin(caller.getId())) {
        accountId = caller.getId();
        domainId = null;
    } else {
        if (domainId != null) {
            // verify domain information and permissions
            Domain domain = _domainDao.findById(domainId);
            if (domain == null) {
                // return empty set
                return limits;
            }
            _accountMgr.checkAccess(caller, domain);
            if (accountId != null) {
                // Verify account information and permissions
                Account account = _accountDao.findById(accountId);
                if (account == null) {
                    // return empty set
                    return limits;
                }
                _accountMgr.checkAccess(caller, null, true, account);
                domainId = null;
            }
        }
    }
    // Map resource type
    ResourceType resourceType = null;
    if (type != null) {
        try {
            resourceType = ResourceType.values()[type];
        } catch (ArrayIndexOutOfBoundsException e) {
            throw new InvalidParameterValueException("Please specify a valid resource type.");
        }
    }
    // If id is passed in, get the record and return it if permission check has passed
    if (id != null) {
        ResourceLimitVO vo = _resourceLimitDao.findById(id);
        if (vo.getAccountId() != null) {
            _accountMgr.checkAccess(caller, null, true, _accountDao.findById(vo.getAccountId()));
            limits.add(vo);
        } else if (vo.getDomainId() != null) {
            _accountMgr.checkAccess(caller, _domainDao.findById(vo.getDomainId()));
            limits.add(vo);
        }
        return limits;
    }
    // If account is not specified, default it to caller account
    if (accountId == null) {
        if (domainId == null) {
            accountId = caller.getId();
            isAccount = true;
        } else {
            isAccount = false;
        }
    } else {
        isAccount = true;
    }
    SearchBuilder<ResourceLimitVO> sb = _resourceLimitDao.createSearchBuilder();
    sb.and("accountId", sb.entity().getAccountId(), SearchCriteria.Op.EQ);
    sb.and("domainId", sb.entity().getDomainId(), SearchCriteria.Op.EQ);
    sb.and("type", sb.entity().getType(), SearchCriteria.Op.EQ);
    SearchCriteria<ResourceLimitVO> sc = sb.create();
    Filter filter = new Filter(ResourceLimitVO.class, "id", true, startIndex, pageSizeVal);
    if (accountId != null) {
        sc.setParameters("accountId", accountId);
    }
    if (domainId != null) {
        sc.setParameters("domainId", domainId);
        sc.setParameters("accountId", (Object[]) null);
    }
    if (resourceType != null) {
        sc.setParameters("type", resourceType);
    }
    List<ResourceLimitVO> foundLimits = _resourceLimitDao.search(sc, filter);
    if (resourceType != null) {
        if (foundLimits.isEmpty()) {
            if (isAccount) {
                limits.add(new ResourceLimitVO(resourceType, findCorrectResourceLimitForAccount(_accountMgr.getAccount(accountId), resourceType), accountId, ResourceOwnerType.Account));
            } else {
                limits.add(new ResourceLimitVO(resourceType, findCorrectResourceLimitForDomain(_domainDao.findById(domainId), resourceType), domainId, ResourceOwnerType.Domain));
            }
        } else {
            limits.addAll(foundLimits);
        }
    } else {
        limits.addAll(foundLimits);
        // see if any limits are missing from the table, and if yes - get it from the config table and add
        ResourceType[] resourceTypes = ResourceCount.ResourceType.values();
        if (foundLimits.size() != resourceTypes.length) {
            List<String> accountLimitStr = new ArrayList<String>();
            List<String> domainLimitStr = new ArrayList<String>();
            for (ResourceLimitVO foundLimit : foundLimits) {
                if (foundLimit.getAccountId() != null) {
                    accountLimitStr.add(foundLimit.getType().toString());
                } else {
                    domainLimitStr.add(foundLimit.getType().toString());
                }
            }
            // get default from config values
            if (isAccount) {
                if (accountLimitStr.size() < resourceTypes.length) {
                    for (ResourceType rt : resourceTypes) {
                        if (!accountLimitStr.contains(rt.toString()) && rt.supportsOwner(ResourceOwnerType.Account)) {
                            limits.add(new ResourceLimitVO(rt, findCorrectResourceLimitForAccount(_accountMgr.getAccount(accountId), rt), accountId, ResourceOwnerType.Account));
                        }
                    }
                }
            } else {
                if (domainLimitStr.size() < resourceTypes.length) {
                    for (ResourceType rt : resourceTypes) {
                        if (!domainLimitStr.contains(rt.toString()) && rt.supportsOwner(ResourceOwnerType.Domain)) {
                            limits.add(new ResourceLimitVO(rt, findCorrectResourceLimitForDomain(_domainDao.findById(domainId), rt), domainId, ResourceOwnerType.Domain));
                        }
                    }
                }
            }
        }
    }
    return limits;
}
Also used : Account(com.cloud.user.Account) ArrayList(java.util.ArrayList) ResourceType(com.cloud.configuration.Resource.ResourceType) ResourceLimitVO(com.cloud.configuration.ResourceLimitVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) Filter(com.cloud.utils.db.Filter) Domain(com.cloud.domain.Domain)

Example 2 with ResourceLimitVO

use of com.cloud.configuration.ResourceLimitVO in project cloudstack by apache.

the class ResourceLimitManagerImpl method findCorrectResourceLimitForAccount.

@Override
public long findCorrectResourceLimitForAccount(Account account, ResourceType type) {
    // if resource limit is not found, then we treat it as unlimited
    long max = Resource.RESOURCE_UNLIMITED;
    // No limits for Root Admin accounts
    if (_accountMgr.isRootAdmin(account.getId())) {
        return max;
    }
    ResourceLimitVO limit = _resourceLimitDao.findByOwnerIdAndType(account.getId(), ResourceOwnerType.Account, type);
    // Check if limit is configured for account
    if (limit != null) {
        max = limit.getMax().longValue();
    } else {
        // If the account has an no limit set, then return global default account limits
        Long value = null;
        if (account.getType() == Account.ACCOUNT_TYPE_PROJECT) {
            value = projectResourceLimitMap.get(type);
        } else {
            value = accountResourceLimitMap.get(type);
        }
        if (value != null) {
            if (value < 0) {
                // return unlimit if value is set to negative
                return max;
            }
            // convert the value from GiB to bytes in case of primary or secondary storage.
            if (type == ResourceType.primary_storage || type == ResourceType.secondary_storage) {
                value = value * ResourceType.bytesToGiB;
            }
            return value;
        }
    }
    return max;
}
Also used : ResourceLimitVO(com.cloud.configuration.ResourceLimitVO)

Example 3 with ResourceLimitVO

use of com.cloud.configuration.ResourceLimitVO in project cloudstack by apache.

the class ResourceLimitManagerImpl method findCorrectResourceLimitForDomain.

@Override
public long findCorrectResourceLimitForDomain(Domain domain, ResourceType type) {
    long max = Resource.RESOURCE_UNLIMITED;
    // no limits on ROOT domain
    if (domain.getId() == Domain.ROOT_DOMAIN) {
        return Resource.RESOURCE_UNLIMITED;
    }
    // Check account
    ResourceLimitVO limit = _resourceLimitDao.findByOwnerIdAndType(domain.getId(), ResourceOwnerType.Domain, type);
    if (limit != null) {
        max = limit.getMax().longValue();
    } else {
        // check domain hierarchy
        Long domainId = domain.getParent();
        while ((domainId != null) && (limit == null)) {
            if (domainId == Domain.ROOT_DOMAIN) {
                break;
            }
            limit = _resourceLimitDao.findByOwnerIdAndType(domainId, ResourceOwnerType.Domain, type);
            DomainVO tmpDomain = _domainDao.findById(domainId);
            domainId = tmpDomain.getParent();
        }
        if (limit != null) {
            max = limit.getMax().longValue();
        } else {
            Long value = null;
            value = domainResourceLimitMap.get(type);
            if (value != null) {
                if (value < 0) {
                    // return unlimit if value is set to negative
                    return max;
                }
                if (type == ResourceType.primary_storage || type == ResourceType.secondary_storage) {
                    value = value * ResourceType.bytesToGiB;
                }
                return value;
            }
        }
    }
    return max;
}
Also used : DomainVO(com.cloud.domain.DomainVO) ResourceLimitVO(com.cloud.configuration.ResourceLimitVO)

Example 4 with ResourceLimitVO

use of com.cloud.configuration.ResourceLimitVO in project cloudstack by apache.

the class ResourceLimitManagerImpl method updateResourceLimit.

@Override
public ResourceLimitVO updateResourceLimit(Long accountId, Long domainId, Integer typeId, Long max) {
    Account caller = CallContext.current().getCallingAccount();
    if (max == null) {
        max = new Long(Resource.RESOURCE_UNLIMITED);
    } else if (max.longValue() < Resource.RESOURCE_UNLIMITED) {
        throw new InvalidParameterValueException("Please specify either '-1' for an infinite limit, or a limit that is at least '0'.");
    }
    // Map resource type
    ResourceType resourceType = null;
    if (typeId != null) {
        for (ResourceType type : Resource.ResourceType.values()) {
            if (type.getOrdinal() == typeId.intValue()) {
                resourceType = type;
            }
        }
        if (resourceType == null) {
            throw new InvalidParameterValueException("Please specify valid resource type");
        }
    }
    //Convert max storage size from GiB to bytes
    if ((resourceType == ResourceType.primary_storage || resourceType == ResourceType.secondary_storage) && max >= 0) {
        max = max * ResourceType.bytesToGiB;
    }
    ResourceOwnerType ownerType = null;
    Long ownerId = null;
    if (accountId != null) {
        Account account = _entityMgr.findById(Account.class, accountId);
        if (account == null) {
            throw new InvalidParameterValueException("Unable to find account " + accountId);
        }
        if (account.getId() == Account.ACCOUNT_ID_SYSTEM) {
            throw new InvalidParameterValueException("Can't update system account");
        }
        //only Unlimited value is accepted if account is  Root Admin
        if (_accountMgr.isRootAdmin(account.getId()) && max.shortValue() != Resource.RESOURCE_UNLIMITED) {
            throw new InvalidParameterValueException("Only " + Resource.RESOURCE_UNLIMITED + " limit is supported for Root Admin accounts");
        }
        if ((caller.getAccountId() == accountId.longValue()) && (_accountMgr.isDomainAdmin(caller.getId()) || caller.getType() == Account.ACCOUNT_TYPE_RESOURCE_DOMAIN_ADMIN)) {
            // If the admin is trying to update his own account, disallow.
            throw new PermissionDeniedException("Unable to update resource limit for his own account " + accountId + ", permission denied");
        }
        if (account.getType() == Account.ACCOUNT_TYPE_PROJECT) {
            _accountMgr.checkAccess(caller, AccessType.ModifyProject, true, account);
        } else {
            _accountMgr.checkAccess(caller, null, true, account);
        }
        ownerType = ResourceOwnerType.Account;
        ownerId = accountId;
    } else if (domainId != null) {
        Domain domain = _entityMgr.findById(Domain.class, domainId);
        _accountMgr.checkAccess(caller, domain);
        if (Domain.ROOT_DOMAIN == domainId.longValue()) {
            // no one can add limits on ROOT domain, disallow...
            throw new PermissionDeniedException("Cannot update resource limit for ROOT domain " + domainId + ", permission denied");
        }
        if ((caller.getDomainId() == domainId.longValue()) && caller.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN || caller.getType() == Account.ACCOUNT_TYPE_RESOURCE_DOMAIN_ADMIN) {
            // if the admin is trying to update their own domain, disallow...
            throw new PermissionDeniedException("Unable to update resource limit for domain " + domainId + ", permission denied");
        }
        Long parentDomainId = domain.getParent();
        if (parentDomainId != null) {
            DomainVO parentDomain = _domainDao.findById(parentDomainId);
            long parentMaximum = findCorrectResourceLimitForDomain(parentDomain, resourceType);
            if ((parentMaximum >= 0) && (max.longValue() > parentMaximum)) {
                throw new InvalidParameterValueException("Domain " + domain.getName() + "(id: " + parentDomain.getId() + ") has maximum allowed resource limit " + parentMaximum + " for " + resourceType + ", please specify a value less that or equal to " + parentMaximum);
            }
        }
        ownerType = ResourceOwnerType.Domain;
        ownerId = domainId;
    }
    if (ownerId == null) {
        throw new InvalidParameterValueException("AccountId or domainId have to be specified in order to update resource limit");
    }
    ResourceLimitVO limit = _resourceLimitDao.findByOwnerIdAndType(ownerId, ownerType, resourceType);
    if (limit != null) {
        // Update the existing limit
        _resourceLimitDao.update(limit.getId(), max);
        return _resourceLimitDao.findById(limit.getId());
    } else {
        return _resourceLimitDao.persist(new ResourceLimitVO(resourceType, max, ownerId, ownerType));
    }
}
Also used : Account(com.cloud.user.Account) DomainVO(com.cloud.domain.DomainVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ResourceOwnerType(com.cloud.configuration.Resource.ResourceOwnerType) ResourceType(com.cloud.configuration.Resource.ResourceType) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) Domain(com.cloud.domain.Domain) ResourceLimitVO(com.cloud.configuration.ResourceLimitVO)

Aggregations

ResourceLimitVO (com.cloud.configuration.ResourceLimitVO)4 ResourceType (com.cloud.configuration.Resource.ResourceType)2 Domain (com.cloud.domain.Domain)2 DomainVO (com.cloud.domain.DomainVO)2 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)2 Account (com.cloud.user.Account)2 ResourceOwnerType (com.cloud.configuration.Resource.ResourceOwnerType)1 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)1 Filter (com.cloud.utils.db.Filter)1 ArrayList (java.util.ArrayList)1