Search in sources :

Example 1 with ResourceType

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

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

the class ResourceCountDaoImpl method createResourceCounts.

@Override
@DB
public void createResourceCounts(long ownerId, ResourceLimit.ResourceOwnerType ownerType) {
    TransactionLegacy txn = TransactionLegacy.currentTxn();
    txn.start();
    ResourceType[] resourceTypes = Resource.ResourceType.values();
    for (ResourceType resourceType : resourceTypes) {
        if (!resourceType.supportsOwner(ownerType)) {
            continue;
        }
        ResourceCountVO resourceCountVO = new ResourceCountVO(resourceType, 0, ownerId, ownerType);
        persist(resourceCountVO);
    }
    txn.commit();
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) ResourceCountVO(com.cloud.configuration.ResourceCountVO) ResourceType(com.cloud.configuration.Resource.ResourceType) DB(com.cloud.utils.db.DB)

Example 3 with ResourceType

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

the class ConfigurationServerImpl method updateResourceCount.

@DB
public void updateResourceCount() {
    ResourceType[] resourceTypes = Resource.ResourceType.values();
    List<AccountVO> accounts = _accountDao.listAll();
    List<DomainVO> domains = _domainDao.listAll();
    List<ResourceCountVO> domainResourceCount = _resourceCountDao.listResourceCountByOwnerType(ResourceOwnerType.Domain);
    List<ResourceCountVO> accountResourceCount = _resourceCountDao.listResourceCountByOwnerType(ResourceOwnerType.Account);
    final List<ResourceType> accountSupportedResourceTypes = new ArrayList<ResourceType>();
    final List<ResourceType> domainSupportedResourceTypes = new ArrayList<ResourceType>();
    for (ResourceType resourceType : resourceTypes) {
        if (resourceType.supportsOwner(ResourceOwnerType.Account)) {
            accountSupportedResourceTypes.add(resourceType);
        }
        if (resourceType.supportsOwner(ResourceOwnerType.Domain)) {
            domainSupportedResourceTypes.add(resourceType);
        }
    }
    final int accountExpectedCount = accountSupportedResourceTypes.size();
    final int domainExpectedCount = domainSupportedResourceTypes.size();
    if ((domainResourceCount.size() < domainExpectedCount * domains.size())) {
        s_logger.debug("resource_count table has records missing for some domains...going to insert them");
        for (final DomainVO domain : domains) {
            // Lock domain
            Transaction.execute(new TransactionCallbackNoReturn() {

                @Override
                public void doInTransactionWithoutResult(TransactionStatus status) {
                    _domainDao.lockRow(domain.getId(), true);
                    List<ResourceCountVO> domainCounts = _resourceCountDao.listByOwnerId(domain.getId(), ResourceOwnerType.Domain);
                    List<String> domainCountStr = new ArrayList<String>();
                    for (ResourceCountVO domainCount : domainCounts) {
                        domainCountStr.add(domainCount.getType().toString());
                    }
                    if (domainCountStr.size() < domainExpectedCount) {
                        for (ResourceType resourceType : domainSupportedResourceTypes) {
                            if (!domainCountStr.contains(resourceType.toString())) {
                                ResourceCountVO resourceCountVO = new ResourceCountVO(resourceType, 0, domain.getId(), ResourceOwnerType.Domain);
                                s_logger.debug("Inserting resource count of type " + resourceType + " for domain id=" + domain.getId());
                                _resourceCountDao.persist(resourceCountVO);
                            }
                        }
                    }
                }
            });
        }
    }
    if ((accountResourceCount.size() < accountExpectedCount * accounts.size())) {
        s_logger.debug("resource_count table has records missing for some accounts...going to insert them");
        for (final AccountVO account : accounts) {
            // lock account
            Transaction.execute(new TransactionCallbackNoReturn() {

                @Override
                public void doInTransactionWithoutResult(TransactionStatus status) {
                    _accountDao.lockRow(account.getId(), true);
                    List<ResourceCountVO> accountCounts = _resourceCountDao.listByOwnerId(account.getId(), ResourceOwnerType.Account);
                    List<String> accountCountStr = new ArrayList<String>();
                    for (ResourceCountVO accountCount : accountCounts) {
                        accountCountStr.add(accountCount.getType().toString());
                    }
                    if (accountCountStr.size() < accountExpectedCount) {
                        for (ResourceType resourceType : accountSupportedResourceTypes) {
                            if (!accountCountStr.contains(resourceType.toString())) {
                                ResourceCountVO resourceCountVO = new ResourceCountVO(resourceType, 0, account.getId(), ResourceOwnerType.Account);
                                s_logger.debug("Inserting resource count of type " + resourceType + " for account id=" + account.getId());
                                _resourceCountDao.persist(resourceCountVO);
                            }
                        }
                    }
                }
            });
        }
    }
}
Also used : ArrayList(java.util.ArrayList) TransactionStatus(com.cloud.utils.db.TransactionStatus) ResourceType(com.cloud.configuration.Resource.ResourceType) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) AccountVO(com.cloud.user.AccountVO) DomainVO(com.cloud.domain.DomainVO) ResourceCountVO(com.cloud.configuration.ResourceCountVO) ArrayList(java.util.ArrayList) List(java.util.List) DB(com.cloud.utils.db.DB)

Example 4 with ResourceType

use of com.cloud.configuration.Resource.ResourceType 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 5 with ResourceType

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

the class ResourceLimitManagerImpl method recalculateResourceCount.

@Override
public List<ResourceCountVO> recalculateResourceCount(Long accountId, Long domainId, Integer typeId) throws InvalidParameterValueException, CloudRuntimeException, PermissionDeniedException {
    Account callerAccount = CallContext.current().getCallingAccount();
    long count = 0;
    List<ResourceCountVO> counts = new ArrayList<ResourceCountVO>();
    List<ResourceType> resourceTypes = new ArrayList<ResourceType>();
    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");
        }
    }
    DomainVO domain = _domainDao.findById(domainId);
    if (domain == null) {
        throw new InvalidParameterValueException("Please specify a valid domain ID.");
    }
    _accountMgr.checkAccess(callerAccount, domain);
    if (resourceType != null) {
        resourceTypes.add(resourceType);
    } else {
        resourceTypes = Arrays.asList(Resource.ResourceType.values());
    }
    for (ResourceType type : resourceTypes) {
        if (accountId != null) {
            if (type.supportsOwner(ResourceOwnerType.Account)) {
                count = recalculateAccountResourceCount(accountId, type);
                counts.add(new ResourceCountVO(type, count, accountId, ResourceOwnerType.Account));
            }
        } else {
            if (type.supportsOwner(ResourceOwnerType.Domain)) {
                count = recalculateDomainResourceCount(domainId, type);
                counts.add(new ResourceCountVO(type, count, domainId, ResourceOwnerType.Domain));
            }
        }
    }
    return counts;
}
Also used : Account(com.cloud.user.Account) DomainVO(com.cloud.domain.DomainVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ArrayList(java.util.ArrayList) ResourceCountVO(com.cloud.configuration.ResourceCountVO) ResourceType(com.cloud.configuration.Resource.ResourceType)

Aggregations

ResourceType (com.cloud.configuration.Resource.ResourceType)7 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)4 Account (com.cloud.user.Account)4 ArrayList (java.util.ArrayList)4 ResourceCountVO (com.cloud.configuration.ResourceCountVO)3 DomainVO (com.cloud.domain.DomainVO)3 ResourceOwnerType (com.cloud.configuration.Resource.ResourceOwnerType)2 ResourceLimitVO (com.cloud.configuration.ResourceLimitVO)2 Domain (com.cloud.domain.Domain)2 DB (com.cloud.utils.db.DB)2 Filter (com.cloud.utils.db.Filter)2 List (java.util.List)2 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)1 UnsupportedServiceException (com.cloud.exception.UnsupportedServiceException)1 HypervisorType (com.cloud.hypervisor.Hypervisor.HypervisorType)1 ListProjectResourcesCriteria (com.cloud.projects.Project.ListProjectResourcesCriteria)1 ResourceObjectType (com.cloud.server.ResourceTag.ResourceObjectType)1 ScopeType (com.cloud.storage.ScopeType)1 Type (com.cloud.storage.Snapshot.Type)1 SnapshotVO (com.cloud.storage.SnapshotVO)1