Search in sources :

Example 61 with InvalidParameterValueException

use of com.cloud.exception.InvalidParameterValueException 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 62 with InvalidParameterValueException

use of com.cloud.exception.InvalidParameterValueException 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)

Example 63 with InvalidParameterValueException

use of com.cloud.exception.InvalidParameterValueException in project cloudstack by apache.

the class ProjectManagerImpl method deleteProject.

@Override
@ActionEvent(eventType = EventTypes.EVENT_PROJECT_DELETE, eventDescription = "deleting project", async = true)
public boolean deleteProject(long projectId) {
    CallContext ctx = CallContext.current();
    ProjectVO project = getProject(projectId);
    //verify input parameters
    if (project == null) {
        throw new InvalidParameterValueException("Unable to find project by id " + projectId);
    }
    _accountMgr.checkAccess(ctx.getCallingAccount(), AccessType.ModifyProject, true, _accountMgr.getAccount(project.getProjectAccountId()));
    return deleteProject(ctx.getCallingAccount(), ctx.getCallingUserId(), project);
}
Also used : InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CallContext(org.apache.cloudstack.context.CallContext) ActionEvent(com.cloud.event.ActionEvent)

Example 64 with InvalidParameterValueException

use of com.cloud.exception.InvalidParameterValueException in project cloudstack by apache.

the class StartRouterCmd method execute.

@Override
public void execute() throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException {
    CallContext.current().setEventDetails("Router Id: " + getId());
    VirtualRouter result = null;
    VirtualRouter router = _routerService.findRouter(getId());
    if (router == null || router.getRole() != Role.VIRTUAL_ROUTER) {
        throw new InvalidParameterValueException("Can't find router by id");
    } else {
        result = _routerService.startRouter(getId());
    }
    if (result != null) {
        DomainRouterResponse routerResponse = _responseGenerator.createDomainRouterResponse(result);
        routerResponse.setResponseName(getCommandName());
        setResponseObject(routerResponse);
    } else {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to start router");
    }
}
Also used : ServerApiException(org.apache.cloudstack.api.ServerApiException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) DomainRouterResponse(org.apache.cloudstack.api.response.DomainRouterResponse) VirtualRouter(com.cloud.network.router.VirtualRouter)

Example 65 with InvalidParameterValueException

use of com.cloud.exception.InvalidParameterValueException in project cloudstack by apache.

the class StopRouterCmd method execute.

@Override
public void execute() throws ConcurrentOperationException, ResourceUnavailableException {
    CallContext.current().setEventDetails("Router Id: " + getId());
    VirtualRouter result = null;
    VirtualRouter router = _routerService.findRouter(getId());
    if (router == null || router.getRole() != Role.VIRTUAL_ROUTER) {
        throw new InvalidParameterValueException("Can't find router by id");
    } else {
        result = _routerService.stopRouter(getId(), isForced());
    }
    if (result != null) {
        DomainRouterResponse response = _responseGenerator.createDomainRouterResponse(result);
        response.setResponseName(getCommandName());
        setResponseObject(response);
    } else {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to stop router");
    }
}
Also used : ServerApiException(org.apache.cloudstack.api.ServerApiException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) DomainRouterResponse(org.apache.cloudstack.api.response.DomainRouterResponse) VirtualRouter(com.cloud.network.router.VirtualRouter)

Aggregations

InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)725 Account (com.cloud.user.Account)242 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)229 ArrayList (java.util.ArrayList)186 ActionEvent (com.cloud.event.ActionEvent)171 DB (com.cloud.utils.db.DB)139 ServerApiException (org.apache.cloudstack.api.ServerApiException)110 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)94 TransactionStatus (com.cloud.utils.db.TransactionStatus)88 List (java.util.List)80 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)69 PhysicalNetworkVO (com.cloud.network.dao.PhysicalNetworkVO)63 Network (com.cloud.network.Network)58 HashMap (java.util.HashMap)58 ConfigurationException (javax.naming.ConfigurationException)53 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)52 Pair (com.cloud.utils.Pair)50 HostVO (com.cloud.host.HostVO)46 NetworkVO (com.cloud.network.dao.NetworkVO)46 DataCenterVO (com.cloud.dc.DataCenterVO)44