Search in sources :

Example 31 with SearchCriteria

use of com.cloud.utils.db.SearchCriteria in project cloudstack by apache.

the class HostDetailsDaoImpl method deleteDetails.

@Override
public void deleteDetails(long hostId) {
    SearchCriteria sc = HostSearch.create();
    sc.setParameters("hostId", hostId);
    List<DetailVO> results = search(sc, null);
    for (DetailVO result : results) {
        remove(result.getId());
    }
}
Also used : DetailVO(com.cloud.host.DetailVO) SearchCriteria(com.cloud.utils.db.SearchCriteria)

Example 32 with SearchCriteria

use of com.cloud.utils.db.SearchCriteria in project cloudstack by apache.

the class ResourceLimitManagerImpl method checkResourceLimit.

@Override
@DB
public void checkResourceLimit(final Account account, final ResourceType type, long... count) throws ResourceAllocationException {
    final long numResources = ((count.length == 0) ? 1 : count[0]);
    Project project = null;
    // Don't place any limits on system or root admin accounts
    if (_accountMgr.isRootAdmin(account.getId())) {
        return;
    }
    if (account.getType() == Account.ACCOUNT_TYPE_PROJECT) {
        project = _projectDao.findByProjectAccountId(account.getId());
    }
    final Project projectFinal = project;
    Transaction.execute(new TransactionCallbackWithExceptionNoReturn<ResourceAllocationException>() {

        @Override
        public void doInTransactionWithoutResult(TransactionStatus status) throws ResourceAllocationException {
            // Lock all rows first so nobody else can read it
            Set<Long> rowIdsToLock = _resourceCountDao.listAllRowsToUpdate(account.getId(), ResourceOwnerType.Account, type);
            SearchCriteria<ResourceCountVO> sc = ResourceCountSearch.create();
            sc.setParameters("id", rowIdsToLock.toArray());
            _resourceCountDao.lockRows(sc, null, true);
            // Check account limits
            long accountLimit = findCorrectResourceLimitForAccount(account, type);
            long potentialCount = _resourceCountDao.getResourceCount(account.getId(), ResourceOwnerType.Account, type) + numResources;
            if (accountLimit != Resource.RESOURCE_UNLIMITED && potentialCount > accountLimit) {
                String message = "Maximum number of resources of type '" + type + "' for account name=" + account.getAccountName() + " in domain id=" + account.getDomainId() + " has been exceeded.";
                if (projectFinal != null) {
                    message = "Maximum number of resources of type '" + type + "' for project name=" + projectFinal.getName() + " in domain id=" + account.getDomainId() + " has been exceeded.";
                }
                ResourceAllocationException e = new ResourceAllocationException(message, type);
                ;
                s_logger.error(message, e);
                throw e;
            }
            // check all domains in the account's domain hierarchy
            Long domainId = null;
            if (projectFinal != null) {
                domainId = projectFinal.getDomainId();
            } else {
                domainId = account.getDomainId();
            }
            while (domainId != null) {
                DomainVO domain = _domainDao.findById(domainId);
                // no limit check if it is ROOT domain
                if (domainId != Domain.ROOT_DOMAIN) {
                    long domainLimit = findCorrectResourceLimitForDomain(domain, type);
                    long domainCount = _resourceCountDao.getResourceCount(domainId, ResourceOwnerType.Domain, type) + numResources;
                    if (domainLimit != Resource.RESOURCE_UNLIMITED && domainCount > domainLimit) {
                        throw new ResourceAllocationException("Maximum number of resources of type '" + type + "' for domain id=" + domainId + " has been exceeded.", type);
                    }
                }
                domainId = domain.getParent();
            }
        }
    });
}
Also used : Project(com.cloud.projects.Project) DomainVO(com.cloud.domain.DomainVO) Set(java.util.Set) TransactionStatus(com.cloud.utils.db.TransactionStatus) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) SearchCriteria(com.cloud.utils.db.SearchCriteria) DB(com.cloud.utils.db.DB)

Example 33 with SearchCriteria

use of com.cloud.utils.db.SearchCriteria in project cloudstack by apache.

the class HostDetailsDaoImpl method deleteDetails.

@Override
public void deleteDetails(long hostId) {
    SearchCriteria sc = HostSearch.create();
    sc.setParameters("hostId", hostId);
    List<DetailVO> results = search(sc, null);
    for (DetailVO result : results) {
        remove(result.getId());
    }
}
Also used : DetailVO(com.cloud.host.DetailVO) SearchCriteria(com.cloud.utils.db.SearchCriteria)

Aggregations

SearchCriteria (com.cloud.utils.db.SearchCriteria)33 List (java.util.List)25 Account (com.cloud.user.Account)17 ArrayList (java.util.ArrayList)16 TransactionStatus (com.cloud.utils.db.TransactionStatus)8 DB (com.cloud.utils.db.DB)7 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)5 Filter (com.cloud.utils.db.Filter)5 DomainVO (com.cloud.domain.DomainVO)4 ExcludeList (com.cloud.deploy.DeploymentPlanner.ExcludeList)3 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)3 TemplateFilter (com.cloud.template.VirtualMachineTemplate.TemplateFilter)3 TransactionCallbackNoReturn (com.cloud.utils.db.TransactionCallbackNoReturn)3 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)3 Map (java.util.Map)3 DedicatedResourceVO (com.cloud.dc.DedicatedResourceVO)2 InsufficientAddressCapacityException (com.cloud.exception.InsufficientAddressCapacityException)2 DetailVO (com.cloud.host.DetailVO)2 HostVO (com.cloud.host.HostVO)2 NoTransitionException (com.cloud.utils.fsm.NoTransitionException)2