Search in sources :

Example 26 with Project

use of com.cloud.projects.Project in project CloudStack-archive by CloudStack-extras.

the class SuspendProjectCmd method execute.

/////////////////////////////////////////////////////
/////////////// API Implementation///////////////////
/////////////////////////////////////////////////////
@Override
public void execute() throws ConcurrentOperationException, ResourceUnavailableException {
    UserContext.current().setEventDetails("Project Id: " + id);
    Project project = _projectService.suspendProject(id);
    if (project != null) {
        ProjectResponse response = _responseGenerator.createProjectResponse(project);
        response.setResponseName(getCommandName());
        this.setResponseObject(response);
    } else {
        throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to suspend a project");
    }
}
Also used : Project(com.cloud.projects.Project) ProjectResponse(com.cloud.api.response.ProjectResponse) ServerApiException(com.cloud.api.ServerApiException)

Example 27 with Project

use of com.cloud.projects.Project in project CloudStack-archive by CloudStack-extras.

the class UpdateProjectCmd method execute.

/////////////////////////////////////////////////////
/////////////// API Implementation///////////////////
/////////////////////////////////////////////////////
@Override
public void execute() throws ResourceAllocationException {
    UserContext.current().setEventDetails("Project id: " + getId());
    Project project = _projectService.updateProject(getId(), getDisplayText(), getAccountName());
    if (project != null) {
        ProjectResponse response = _responseGenerator.createProjectResponse(project);
        response.setResponseName(getCommandName());
        this.setResponseObject(response);
    } else {
        throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to update a project");
    }
}
Also used : Project(com.cloud.projects.Project) ProjectResponse(com.cloud.api.response.ProjectResponse) ServerApiException(com.cloud.api.ServerApiException)

Example 28 with Project

use of com.cloud.projects.Project in project cloudstack by apache.

the class AddAccountToProjectCmdTest method testGetEntityOwnerIdForProject.

/****
     * Condition not handled in the code
     *
     *****/
/*
     * @Test public void testGetEntityOwnerIdForNullProject() {
     *
     * ProjectService projectService = Mockito.mock(ProjectService.class);
     * Mockito
     * .when(projectService.getProject(Mockito.anyLong())).thenReturn(null);
     * addAccountToProjectCmd._projectService = projectService;
     *
     * try { addAccountToProjectCmd.getEntityOwnerId(); }
     * catch(InvalidParameterValueException exception) {
     * Assert.assertEquals("Unable to find project by id 2",
     * exception.getLocalizedMessage()); }
     *
     * }
     */
@Test
public void testGetEntityOwnerIdForProject() {
    Project project = Mockito.mock(Project.class);
    Mockito.when(project.getId()).thenReturn(2L);
    ProjectService projectService = Mockito.mock(ProjectService.class);
    Account account = Mockito.mock(Account.class);
    Mockito.when(account.getId()).thenReturn(2L);
    Mockito.when(projectService.getProject(Matchers.anyLong())).thenReturn(project);
    Mockito.when(projectService.getProjectOwner(Matchers.anyLong())).thenReturn(account);
    addAccountToProjectCmd._projectService = projectService;
    Assert.assertEquals(2L, addAccountToProjectCmd.getEntityOwnerId());
}
Also used : Project(com.cloud.projects.Project) Account(com.cloud.user.Account) ProjectService(com.cloud.projects.ProjectService) Test(org.junit.Test)

Example 29 with Project

use of com.cloud.projects.Project in project cloudstack by apache.

the class AssociateIPAddrCmd method getEntityOwnerId.

@Override
public long getEntityOwnerId() {
    Account caller = CallContext.current().getCallingAccount();
    if (accountName != null && domainId != null) {
        Account account = _accountService.finalizeOwner(caller, accountName, domainId, projectId);
        return account.getId();
    } else if (projectId != null) {
        Project project = _projectService.getProject(projectId);
        if (project != null) {
            if (project.getState() == Project.State.Active) {
                return project.getProjectAccountId();
            } else {
                throw new PermissionDeniedException("Can't add resources to the project with specified projectId in state=" + project.getState() + " as it's no longer active");
            }
        } else {
            throw new InvalidParameterValueException("Unable to find project by ID");
        }
    } else if (networkId != null) {
        Network network = _networkService.getNetwork(networkId);
        if (network == null) {
            throw new InvalidParameterValueException("Unable to find network by network id specified");
        }
        NetworkOffering offering = _entityMgr.findById(NetworkOffering.class, network.getNetworkOfferingId());
        DataCenter zone = _entityMgr.findById(DataCenter.class, network.getDataCenterId());
        if (zone.getNetworkType() == NetworkType.Basic && offering.getElasticIp() && offering.getElasticLb()) {
            // shared network with EIP/ELB service.
            return caller.getAccountId();
        }
        return network.getAccountId();
    } else if (vpcId != null) {
        Vpc vpc = _entityMgr.findById(Vpc.class, getVpcId());
        if (vpc == null) {
            throw new InvalidParameterValueException("Can't find enabled VPC by ID specified");
        }
        return vpc.getAccountId();
    }
    return caller.getAccountId();
}
Also used : Account(com.cloud.user.Account) Project(com.cloud.projects.Project) DataCenter(com.cloud.dc.DataCenter) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) NetworkOffering(com.cloud.offering.NetworkOffering) Network(com.cloud.network.Network) Vpc(com.cloud.network.vpc.Vpc) PermissionDeniedException(com.cloud.exception.PermissionDeniedException)

Example 30 with Project

use of com.cloud.projects.Project in project cloudstack by apache.

the class ResizeVolumeCmd method getEntityOwnerId.

@Override
public long getEntityOwnerId() {
    Volume volume = _entityMgr.findById(Volume.class, getEntityId());
    if (volume == null) {
        throw new InvalidParameterValueException("Unable to find volume by id=" + id);
    }
    Account account = _accountService.getAccount(volume.getAccountId());
    //Can resize volumes for enabled projects/accounts only
    if (account.getType() == Account.ACCOUNT_TYPE_PROJECT) {
        Project project = _projectService.findByProjectAccountId(volume.getAccountId());
        if (project.getState() != Project.State.Active) {
            throw new PermissionDeniedException("Can't add resources to  project id=" + project.getId() + " in state=" + project.getState() + " as it's no longer active");
        }
    } else if (account.getState() == Account.State.disabled) {
        throw new PermissionDeniedException("The owner of volume " + id + "  is disabled: " + account);
    }
    return volume.getAccountId();
}
Also used : Account(com.cloud.user.Account) Project(com.cloud.projects.Project) Volume(com.cloud.storage.Volume) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException)

Aggregations

Project (com.cloud.projects.Project)48 Account (com.cloud.user.Account)30 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)25 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)16 Domain (com.cloud.domain.Domain)10 ArrayList (java.util.ArrayList)10 DomainVO (com.cloud.domain.DomainVO)7 Volume (com.cloud.storage.Volume)7 Pair (com.cloud.utils.Pair)6 List (java.util.List)6 ServerApiException (com.cloud.api.ServerApiException)5 ProjectResponse (com.cloud.api.response.ProjectResponse)5 PhysicalNetworkVO (com.cloud.network.dao.PhysicalNetworkVO)5 ProjectAccount (com.cloud.projects.ProjectAccount)5 UserAccount (com.cloud.user.UserAccount)5 ServerApiException (org.apache.cloudstack.api.ServerApiException)5 DataCenterVO (com.cloud.dc.DataCenterVO)4 DB (com.cloud.utils.db.DB)4 Filter (com.cloud.utils.db.Filter)4 VlanVO (com.cloud.dc.VlanVO)3