Search in sources :

Example 1 with ResourceOwnerType

use of com.cloud.legacymodel.configuration.Resource.ResourceOwnerType in project cosmic by MissionCriticalCloud.

the class ResourceCountDaoImpl method persist.

@Override
public ResourceCountVO persist(final ResourceCountVO resourceCountVO) {
    final ResourceOwnerType ownerType = resourceCountVO.getResourceOwnerType();
    final 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.legacymodel.exceptions.UnsupportedServiceException) ResourceOwnerType(com.cloud.legacymodel.configuration.Resource.ResourceOwnerType) ResourceType(com.cloud.legacymodel.configuration.Resource.ResourceType)

Example 2 with ResourceOwnerType

use of com.cloud.legacymodel.configuration.Resource.ResourceOwnerType in project cosmic by MissionCriticalCloud.

the class ResourceLimitManagerImpl method updateResourceLimit.

@Override
public ResourceLimitVO updateResourceLimit(final Long accountId, final Long domainId, final Integer typeId, Long max) {
    final 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 (final 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) {
        final Account account = this._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 (this._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()) && (this._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) {
            this._accountMgr.checkAccess(caller, AccessType.ModifyProject, true, account);
        } else {
            this._accountMgr.checkAccess(caller, null, true, account);
        }
        ownerType = ResourceOwnerType.Account;
        ownerId = accountId;
    } else if (domainId != null) {
        final Domain domain = this._entityMgr.findById(Domain.class, domainId);
        this._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");
        }
        final Long parentDomainId = domain.getParent();
        if (parentDomainId != null) {
            final DomainVO parentDomain = this._domainDao.findById(parentDomainId);
            final 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");
    }
    final ResourceLimitVO limit = this._resourceLimitDao.findByOwnerIdAndType(ownerId, ownerType, resourceType);
    if (limit != null) {
        // Update the existing limit
        this._resourceLimitDao.update(limit.getId(), max);
        return this._resourceLimitDao.findById(limit.getId());
    } else {
        return this._resourceLimitDao.persist(new ResourceLimitVO(resourceType, max, ownerId, ownerType));
    }
}
Also used : Account(com.cloud.legacymodel.user.Account) DomainVO(com.cloud.domain.DomainVO) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) ResourceOwnerType(com.cloud.legacymodel.configuration.Resource.ResourceOwnerType) ResourceType(com.cloud.legacymodel.configuration.Resource.ResourceType) PermissionDeniedException(com.cloud.legacymodel.exceptions.PermissionDeniedException) Domain(com.cloud.legacymodel.domain.Domain) ResourceLimitVO(com.cloud.configuration.ResourceLimitVO)

Aggregations

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