Search in sources :

Example 56 with InvalidParameterValueException

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

the class ProjectManagerImpl method createProject.

@Override
@ActionEvent(eventType = EventTypes.EVENT_PROJECT_CREATE, eventDescription = "creating project", create = true)
@DB
public Project createProject(final String name, final String displayText, String accountName, final Long domainId) throws ResourceAllocationException {
    Account caller = CallContext.current().getCallingAccount();
    Account owner = caller;
    //check if the user authorized to create the project
    if (_accountMgr.isNormalUser(caller.getId()) && !_allowUserToCreateProject) {
        throw new PermissionDeniedException("Regular user is not permitted to create a project");
    }
    //Verify request parameters
    if ((accountName != null && domainId == null) || (domainId != null && accountName == null)) {
        throw new InvalidParameterValueException("Account name and domain id must be specified together");
    }
    if (accountName != null) {
        owner = _accountMgr.finalizeOwner(caller, accountName, domainId, null);
    }
    //don't allow 2 projects with the same name inside the same domain
    if (_projectDao.findByNameAndDomain(name, owner.getDomainId()) != null) {
        throw new InvalidParameterValueException("Project with name " + name + " already exists in domain id=" + owner.getDomainId());
    }
    //do resource limit check
    _resourceLimitMgr.checkResourceLimit(owner, ResourceType.project);
    final Account ownerFinal = owner;
    return Transaction.execute(new TransactionCallback<Project>() {

        @Override
        public Project doInTransaction(TransactionStatus status) {
            //Create an account associated with the project
            StringBuilder acctNm = new StringBuilder("PrjAcct-");
            acctNm.append(name).append("-").append(ownerFinal.getDomainId());
            Account projectAccount = _accountMgr.createAccount(acctNm.toString(), Account.ACCOUNT_TYPE_PROJECT, null, domainId, null, null, UUID.randomUUID().toString());
            Project project = _projectDao.persist(new ProjectVO(name, displayText, ownerFinal.getDomainId(), projectAccount.getId()));
            //assign owner to the project
            assignAccountToProject(project, ownerFinal.getId(), ProjectAccount.Role.Admin);
            if (project != null) {
                CallContext.current().setEventDetails("Project id=" + project.getId());
                CallContext.current().putContextParameter(Project.class, project.getUuid());
            }
            //Increment resource count
            _resourceLimitMgr.incrementResourceCount(ownerFinal.getId(), ResourceType.project);
            return project;
        }
    });
}
Also used : Account(com.cloud.user.Account) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) TransactionStatus(com.cloud.utils.db.TransactionStatus) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) ActionEvent(com.cloud.event.ActionEvent) DB(com.cloud.utils.db.DB)

Example 57 with InvalidParameterValueException

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

the class ProjectManagerImpl method updateProject.

@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_PROJECT_UPDATE, eventDescription = "updating project", async = true)
public Project updateProject(final long projectId, final String displayText, final String newOwnerName) throws ResourceAllocationException {
    Account caller = CallContext.current().getCallingAccount();
    //check that the project exists
    final ProjectVO project = getProject(projectId);
    if (project == null) {
        throw new InvalidParameterValueException("Unable to find the project id=" + projectId);
    }
    //verify permissions
    _accountMgr.checkAccess(caller, AccessType.ModifyProject, true, _accountMgr.getAccount(project.getProjectAccountId()));
    Transaction.execute(new TransactionCallbackWithExceptionNoReturn<ResourceAllocationException>() {

        @Override
        public void doInTransactionWithoutResult(TransactionStatus status) throws ResourceAllocationException {
            if (displayText != null) {
                project.setDisplayText(displayText);
                _projectDao.update(projectId, project);
            }
            if (newOwnerName != null) {
                //check that the new owner exists
                Account futureOwnerAccount = _accountMgr.getActiveAccountByName(newOwnerName, project.getDomainId());
                if (futureOwnerAccount == null) {
                    throw new InvalidParameterValueException("Unable to find account name=" + newOwnerName + " in domain id=" + project.getDomainId());
                }
                Account currentOwnerAccount = getProjectOwner(projectId);
                if (currentOwnerAccount.getId() != futureOwnerAccount.getId()) {
                    ProjectAccountVO futureOwner = _projectAccountDao.findByProjectIdAccountId(projectId, futureOwnerAccount.getAccountId());
                    if (futureOwner == null) {
                        throw new InvalidParameterValueException("Account " + newOwnerName + " doesn't belong to the project. Add it to the project first and then change the project's ownership");
                    }
                    //do resource limit check
                    _resourceLimitMgr.checkResourceLimit(_accountMgr.getAccount(futureOwnerAccount.getId()), ResourceType.project);
                    //unset the role for the old owner
                    ProjectAccountVO currentOwner = _projectAccountDao.findByProjectIdAccountId(projectId, currentOwnerAccount.getId());
                    currentOwner.setAccountRole(Role.Regular);
                    _projectAccountDao.update(currentOwner.getId(), currentOwner);
                    _resourceLimitMgr.decrementResourceCount(currentOwnerAccount.getId(), ResourceType.project);
                    //set new owner
                    futureOwner.setAccountRole(Role.Admin);
                    _projectAccountDao.update(futureOwner.getId(), futureOwner);
                    _resourceLimitMgr.incrementResourceCount(futureOwnerAccount.getId(), ResourceType.project);
                } else {
                    s_logger.trace("Future owner " + newOwnerName + "is already the owner of the project id=" + projectId);
                }
            }
        }
    });
    return _projectDao.findById(projectId);
}
Also used : Account(com.cloud.user.Account) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) TransactionStatus(com.cloud.utils.db.TransactionStatus) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) ActionEvent(com.cloud.event.ActionEvent) DB(com.cloud.utils.db.DB)

Example 58 with InvalidParameterValueException

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

the class ProjectManagerImpl method suspendProject.

@Override
@ActionEvent(eventType = EventTypes.EVENT_PROJECT_SUSPEND, eventDescription = "suspending project", async = true)
public Project suspendProject(long projectId) throws ConcurrentOperationException, ResourceUnavailableException {
    Account caller = CallContext.current().getCallingAccount();
    ProjectVO project = getProject(projectId);
    //verify input parameters
    if (project == null) {
        InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find project with specified id");
        ex.addProxyObject(String.valueOf(projectId), "projectId");
        throw ex;
    }
    _accountMgr.checkAccess(caller, AccessType.ModifyProject, true, _accountMgr.getAccount(project.getProjectAccountId()));
    if (suspendProject(project)) {
        s_logger.debug("Successfully suspended project id=" + projectId);
        return _projectDao.findById(projectId);
    } else {
        CloudRuntimeException ex = new CloudRuntimeException("Failed to suspend project with specified id");
        ex.addProxyObject(project.getUuid(), "projectId");
        throw ex;
    }
}
Also used : Account(com.cloud.user.Account) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ActionEvent(com.cloud.event.ActionEvent)

Example 59 with InvalidParameterValueException

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

the class UpdateClusterCmd method execute.

@Override
public void execute() {
    Cluster cluster = _resourceService.getCluster(getId());
    if (cluster == null) {
        throw new InvalidParameterValueException("Unable to find the cluster by id=" + getId());
    }
    Cluster result = _resourceService.updateCluster(cluster, getClusterType(), getHypervisor(), getAllocationState(), getManagedstate());
    if (result != null) {
        ClusterResponse clusterResponse = _responseGenerator.createClusterResponse(cluster, false);
        clusterResponse.setResponseName(getCommandName());
        this.setResponseObject(clusterResponse);
    } else {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to update cluster");
    }
}
Also used : ServerApiException(org.apache.cloudstack.api.ServerApiException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) Cluster(com.cloud.org.Cluster) ClusterResponse(org.apache.cloudstack.api.response.ClusterResponse)

Example 60 with InvalidParameterValueException

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

the class ScaleSystemVMCmd method execute.

@Override
public void execute() {
    CallContext.current().setEventDetails("SystemVm Id: " + getId());
    ServiceOffering serviceOffering = _entityMgr.findById(ServiceOffering.class, serviceOfferingId);
    if (serviceOffering == null) {
        throw new InvalidParameterValueException("Unable to find service offering: " + serviceOfferingId);
    }
    VirtualMachine result = null;
    try {
        result = _mgr.upgradeSystemVM(this);
    } catch (ResourceUnavailableException ex) {
        s_logger.warn("Exception: ", ex);
        throw new ServerApiException(ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, ex.getMessage());
    } catch (ConcurrentOperationException ex) {
        s_logger.warn("Exception: ", ex);
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage());
    } catch (ManagementServerException ex) {
        s_logger.warn("Exception: ", ex);
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage());
    } catch (VirtualMachineMigrationException ex) {
        s_logger.warn("Exception: ", ex);
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage());
    }
    if (result != null) {
        SystemVmResponse response = _responseGenerator.createSystemVmResponse(result);
        response.setResponseName(getCommandName());
        setResponseObject(response);
    } else {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to upgrade system vm");
    }
}
Also used : SystemVmResponse(org.apache.cloudstack.api.response.SystemVmResponse) ServerApiException(org.apache.cloudstack.api.ServerApiException) ManagementServerException(com.cloud.exception.ManagementServerException) ServiceOffering(com.cloud.offering.ServiceOffering) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) VirtualMachineMigrationException(com.cloud.exception.VirtualMachineMigrationException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) VirtualMachine(com.cloud.vm.VirtualMachine)

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