Search in sources :

Example 41 with Project

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

the class CreateProjectCmd method execute.

// ///////////////////////////////////////////////////
// ///////////// API Implementation///////////////////
// ///////////////////////////////////////////////////
@Override
public void execute() {
    Project project = _projectService.enableProject(this.getEntityId());
    if (project != null) {
        ProjectResponse response = _responseGenerator.createProjectResponse(project);
        response.setResponseName(getCommandName());
        this.setResponseObject(response);
    } else {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to create a project");
    }
}
Also used : Project(com.cloud.projects.Project) ProjectResponse(org.apache.cloudstack.api.response.ProjectResponse) ServerApiException(org.apache.cloudstack.api.ServerApiException)

Example 42 with Project

use of com.cloud.projects.Project 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 43 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 44 with Project

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

the class CreateSnapshotFromVMSnapshotCmd method getEntityOwnerId.

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

Example 45 with Project

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

the class CreateSnapshotPolicyCmd method getEntityOwnerId.

@Override
public long getEntityOwnerId() {
    Volume volume = _entityMgr.findById(Volume.class, getVolumeId());
    if (volume == null) {
        throw new InvalidParameterValueException("Unable to find volume by id=" + volumeId);
    }
    Account account = _accountService.getAccount(volume.getAccountId());
    //Can create templates for enabled projects/accounts only
    if (account.getType() == Account.ACCOUNT_TYPE_PROJECT) {
        Project project = _projectService.findByProjectAccountId(volume.getAccountId());
        if (project.getState() != Project.State.Active) {
            PermissionDeniedException ex = new PermissionDeniedException("Can't add resources to the specified project id in state=" + project.getState() + " as it's no longer active");
            ex.addProxyObject(project.getUuid(), "projectId");
            throw ex;
        }
    } else if (account.getState() == Account.State.disabled) {
        throw new PermissionDeniedException("The owner of template 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