Search in sources :

Example 1 with ResourceOwnerType

use of com.cloud.configuration.Resource.ResourceOwnerType in project cloudstack by apache.

the class ResourceCountDaoImpl method persist.

@Override
public ResourceCountVO persist(ResourceCountVO resourceCountVO) {
    ResourceOwnerType ownerType = resourceCountVO.getResourceOwnerType();
    ResourceType resourceType = resourceCountVO.getType();
    if (!resourceType.supportsOwner(ownerType)) {
        throw new UnsupportedServiceException("Resource type " + resourceType + " is not supported for owner of type " + ownerType.getName());
    }
    return super.persist(resourceCountVO);
}
Also used : UnsupportedServiceException(com.cloud.exception.UnsupportedServiceException) ResourceOwnerType(com.cloud.configuration.Resource.ResourceOwnerType) ResourceType(com.cloud.configuration.Resource.ResourceType)

Example 2 with ResourceOwnerType

use of com.cloud.configuration.Resource.ResourceOwnerType 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

ResourceOwnerType (com.cloud.configuration.Resource.ResourceOwnerType)2 ResourceType (com.cloud.configuration.Resource.ResourceType)2 ResourceLimitVO (com.cloud.configuration.ResourceLimitVO)1 Domain (com.cloud.domain.Domain)1 DomainVO (com.cloud.domain.DomainVO)1 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)1 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)1 UnsupportedServiceException (com.cloud.exception.UnsupportedServiceException)1 Account (com.cloud.user.Account)1