Search in sources :

Example 71 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)

Example 72 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 73 with Project

use of com.cloud.projects.Project in project cosmic by MissionCriticalCloud.

the class AddAccountToProjectCmd method getEntityOwnerId.

@Override
public long getEntityOwnerId() {
    final Project project = _projectService.getProject(getProjectId());
    // verify input parameters
    if (project == null) {
        final InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find project with specified ID");
        ex.addProxyObject(getProjectId().toString(), "projectId");
        throw ex;
    }
    return _projectService.getProjectOwner(getProjectId()).getId();
}
Also used : Project(com.cloud.projects.Project) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException)

Example 74 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
            lockAccountAndOwnerDomainRows(account.getId(), type);
            // Check account limits
            checkAccountResourceLimit(account, projectFinal, type, numResources);
            // check all domains in the account's domain hierarchy
            checkDomainResourceLimit(account, projectFinal, type, numResources);
        }
    });
}
Also used : Project(com.cloud.projects.Project) TransactionStatus(com.cloud.utils.db.TransactionStatus) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) DB(com.cloud.utils.db.DB)

Example 75 with Project

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

the class NetworkServiceImpl method searchForNetworks.

@Override
public Pair<List<? extends Network>, Integer> searchForNetworks(ListNetworksCmd cmd) {
    Long id = cmd.getId();
    String keyword = cmd.getKeyword();
    Long zoneId = cmd.getZoneId();
    Account caller = CallContext.current().getCallingAccount();
    Long domainId = cmd.getDomainId();
    String accountName = cmd.getAccountName();
    String guestIpType = cmd.getGuestIpType();
    String trafficType = cmd.getTrafficType();
    Boolean isSystem = cmd.getIsSystem();
    String aclType = cmd.getAclType();
    Long projectId = cmd.getProjectId();
    List<Long> permittedAccounts = new ArrayList<Long>();
    String path = null;
    Long physicalNetworkId = cmd.getPhysicalNetworkId();
    List<String> supportedServicesStr = cmd.getSupportedServices();
    Boolean restartRequired = cmd.isRestartRequired();
    boolean listAll = cmd.listAll();
    boolean isRecursive = cmd.isRecursive();
    Boolean specifyIpRanges = cmd.isSpecifyIpRanges();
    Long vpcId = cmd.getVpcId();
    Boolean canUseForDeploy = cmd.canUseForDeploy();
    Map<String, String> tags = cmd.getTags();
    Boolean forVpc = cmd.getForVpc();
    Boolean display = cmd.getDisplay();
    Long networkOfferingId = cmd.getNetworkOfferingId();
    // 2) reset parameter to false if it's specified by a non-ROOT user
    if (isSystem == null || !_accountMgr.isRootAdmin(caller.getId())) {
        isSystem = false;
    }
    // Account/domainId parameters and isSystem are mutually exclusive
    if (isSystem != null && isSystem && (accountName != null || domainId != null)) {
        throw new InvalidParameterValueException("System network belongs to system, account and domainId parameters can't be specified");
    }
    if (domainId != null) {
        DomainVO domain = _domainDao.findById(domainId);
        if (domain == null) {
            // see DomainVO.java
            throw new InvalidParameterValueException("Specified domain id doesn't exist in the system");
        }
        _accountMgr.checkAccess(caller, domain);
        if (accountName != null) {
            Account owner = _accountMgr.getActiveAccountByName(accountName, domainId);
            if (owner == null) {
                // see DomainVO.java
                throw new InvalidParameterValueException("Unable to find account " + accountName + " in specified domain");
            }
            _accountMgr.checkAccess(caller, null, true, owner);
            permittedAccounts.add(owner.getId());
        }
    }
    if (!_accountMgr.isAdmin(caller.getId()) || (projectId != null && projectId.longValue() != -1 && domainId == null)) {
        permittedAccounts.add(caller.getId());
        domainId = caller.getDomainId();
    }
    // set project information
    boolean skipProjectNetworks = true;
    if (projectId != null) {
        if (projectId.longValue() == -1) {
            if (!_accountMgr.isAdmin(caller.getId())) {
                permittedAccounts.addAll(_projectMgr.listPermittedProjectAccounts(caller.getId()));
            }
        } else {
            permittedAccounts.clear();
            Project project = _projectMgr.getProject(projectId);
            if (project == null) {
                throw new InvalidParameterValueException("Unable to find project by specified id");
            }
            if (!_projectMgr.canAccessProjectAccount(caller, project.getProjectAccountId())) {
                // getProject() returns type ProjectVO.
                throwInvalidIdException("Account " + caller + " cannot access specified project id", project.getUuid(), "projectId");
            }
            // add project account
            permittedAccounts.add(project.getProjectAccountId());
            // add caller account (if admin)
            if (_accountMgr.isAdmin(caller.getId())) {
                permittedAccounts.add(caller.getId());
            }
        }
        skipProjectNetworks = false;
    }
    if (domainId != null) {
        path = _domainDao.findById(domainId).getPath();
    } else {
        path = _domainDao.findById(caller.getDomainId()).getPath();
    }
    if (listAll && domainId == null) {
        isRecursive = true;
    }
    Filter searchFilter = new Filter(NetworkVO.class, "id", false, null, null);
    SearchBuilder<NetworkVO> sb = _networksDao.createSearchBuilder();
    if (forVpc != null) {
        if (forVpc) {
            sb.and("vpc", sb.entity().getVpcId(), Op.NNULL);
        } else {
            sb.and("vpc", sb.entity().getVpcId(), Op.NULL);
        }
    }
    // Don't display networks created of system network offerings
    SearchBuilder<NetworkOfferingVO> networkOfferingSearch = _networkOfferingDao.createSearchBuilder();
    networkOfferingSearch.and("systemOnly", networkOfferingSearch.entity().isSystemOnly(), SearchCriteria.Op.EQ);
    if (isSystem != null && isSystem) {
        networkOfferingSearch.and("trafficType", networkOfferingSearch.entity().getTrafficType(), SearchCriteria.Op.EQ);
    }
    sb.join("networkOfferingSearch", networkOfferingSearch, sb.entity().getNetworkOfferingId(), networkOfferingSearch.entity().getId(), JoinBuilder.JoinType.INNER);
    SearchBuilder<DataCenterVO> zoneSearch = _dcDao.createSearchBuilder();
    zoneSearch.and("networkType", zoneSearch.entity().getNetworkType(), SearchCriteria.Op.EQ);
    sb.join("zoneSearch", zoneSearch, sb.entity().getDataCenterId(), zoneSearch.entity().getId(), JoinBuilder.JoinType.INNER);
    sb.and("removed", sb.entity().getRemoved(), Op.NULL);
    if (tags != null && !tags.isEmpty()) {
        SearchBuilder<ResourceTagVO> tagSearch = _resourceTagDao.createSearchBuilder();
        for (int count = 0; count < tags.size(); count++) {
            tagSearch.or().op("key" + String.valueOf(count), tagSearch.entity().getKey(), SearchCriteria.Op.EQ);
            tagSearch.and("value" + String.valueOf(count), tagSearch.entity().getValue(), SearchCriteria.Op.EQ);
            tagSearch.cp();
        }
        tagSearch.and("resourceType", tagSearch.entity().getResourceType(), SearchCriteria.Op.EQ);
        sb.groupBy(sb.entity().getId());
        sb.join("tagSearch", tagSearch, sb.entity().getId(), tagSearch.entity().getResourceId(), JoinBuilder.JoinType.INNER);
    }
    if (permittedAccounts.isEmpty()) {
        SearchBuilder<DomainVO> domainSearch = _domainDao.createSearchBuilder();
        domainSearch.and("path", domainSearch.entity().getPath(), SearchCriteria.Op.LIKE);
        sb.join("domainSearch", domainSearch, sb.entity().getDomainId(), domainSearch.entity().getId(), JoinBuilder.JoinType.INNER);
    }
    SearchBuilder<AccountVO> accountSearch = _accountDao.createSearchBuilder();
    accountSearch.and("typeNEQ", accountSearch.entity().getType(), SearchCriteria.Op.NEQ);
    accountSearch.and("typeEQ", accountSearch.entity().getType(), SearchCriteria.Op.EQ);
    sb.join("accountSearch", accountSearch, sb.entity().getAccountId(), accountSearch.entity().getId(), JoinBuilder.JoinType.INNER);
    List<NetworkVO> networksToReturn = new ArrayList<NetworkVO>();
    if (isSystem == null || !isSystem) {
        if (!permittedAccounts.isEmpty()) {
            // get account level networks
            networksToReturn.addAll(listAccountSpecificNetworks(buildNetworkSearchCriteria(sb, keyword, id, isSystem, zoneId, guestIpType, trafficType, physicalNetworkId, networkOfferingId, aclType, skipProjectNetworks, restartRequired, specifyIpRanges, vpcId, tags, display), searchFilter, permittedAccounts));
            // get domain level networks
            if (domainId != null) {
                networksToReturn.addAll(listDomainLevelNetworks(buildNetworkSearchCriteria(sb, keyword, id, isSystem, zoneId, guestIpType, trafficType, physicalNetworkId, networkOfferingId, aclType, true, restartRequired, specifyIpRanges, vpcId, tags, display), searchFilter, domainId, false));
            }
        } else {
            // add account specific networks
            networksToReturn.addAll(listAccountSpecificNetworksByDomainPath(buildNetworkSearchCriteria(sb, keyword, id, isSystem, zoneId, guestIpType, trafficType, physicalNetworkId, networkOfferingId, aclType, skipProjectNetworks, restartRequired, specifyIpRanges, vpcId, tags, display), searchFilter, path, isRecursive));
            // add domain specific networks of domain + parent domains
            networksToReturn.addAll(listDomainSpecificNetworksByDomainPath(buildNetworkSearchCriteria(sb, keyword, id, isSystem, zoneId, guestIpType, trafficType, physicalNetworkId, networkOfferingId, aclType, skipProjectNetworks, restartRequired, specifyIpRanges, vpcId, tags, display), searchFilter, path, isRecursive));
            // add networks of subdomains
            if (domainId == null) {
                networksToReturn.addAll(listDomainLevelNetworks(buildNetworkSearchCriteria(sb, keyword, id, isSystem, zoneId, guestIpType, trafficType, physicalNetworkId, networkOfferingId, aclType, true, restartRequired, specifyIpRanges, vpcId, tags, display), searchFilter, caller.getDomainId(), true));
            }
        }
    } else {
        networksToReturn = _networksDao.search(buildNetworkSearchCriteria(sb, keyword, id, isSystem, zoneId, guestIpType, trafficType, physicalNetworkId, networkOfferingId, null, skipProjectNetworks, restartRequired, specifyIpRanges, vpcId, tags, display), searchFilter);
    }
    if (supportedServicesStr != null && !supportedServicesStr.isEmpty() && !networksToReturn.isEmpty()) {
        List<NetworkVO> supportedNetworks = new ArrayList<NetworkVO>();
        Service[] suppportedServices = new Service[supportedServicesStr.size()];
        int i = 0;
        for (String supportedServiceStr : supportedServicesStr) {
            Service service = Service.getService(supportedServiceStr);
            if (service == null) {
                throw new InvalidParameterValueException("Invalid service specified " + supportedServiceStr);
            } else {
                suppportedServices[i] = service;
            }
            i++;
        }
        for (NetworkVO network : networksToReturn) {
            if (areServicesSupportedInNetwork(network.getId(), suppportedServices)) {
                supportedNetworks.add(network);
            }
        }
        networksToReturn = supportedNetworks;
    }
    if (canUseForDeploy != null) {
        List<NetworkVO> networksForDeploy = new ArrayList<NetworkVO>();
        for (NetworkVO network : networksToReturn) {
            if (_networkModel.canUseForDeploy(network) == canUseForDeploy) {
                networksForDeploy.add(network);
            }
        }
        networksToReturn = networksForDeploy;
    }
    // Now apply pagination
    List<? extends Network> wPagination = com.cloud.utils.StringUtils.applyPagination(networksToReturn, cmd.getStartIndex(), cmd.getPageSizeVal());
    if (wPagination != null) {
        Pair<List<? extends Network>, Integer> listWPagination = new Pair<List<? extends Network>, Integer>(wPagination, networksToReturn.size());
        return listWPagination;
    }
    return new Pair<List<? extends Network>, Integer>(networksToReturn, networksToReturn.size());
}
Also used : Account(com.cloud.user.Account) ArrayList(java.util.ArrayList) AccountVO(com.cloud.user.AccountVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ResourceTagVO(com.cloud.tags.ResourceTagVO) ArrayList(java.util.ArrayList) List(java.util.List) Pair(com.cloud.utils.Pair) DataCenterVO(com.cloud.dc.DataCenterVO) PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) NetworkVO(com.cloud.network.dao.NetworkVO) AccountService(com.cloud.user.AccountService) NetworkOrchestrationService(org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService) Service(com.cloud.network.Network.Service) SecurityGroupService(com.cloud.network.security.SecurityGroupService) ResourceLimitService(com.cloud.user.ResourceLimitService) InternalLoadBalancerElementService(org.apache.cloudstack.network.element.InternalLoadBalancerElementService) NetworkDomainVO(com.cloud.network.dao.NetworkDomainVO) DomainVO(com.cloud.domain.DomainVO) Project(com.cloud.projects.Project) Filter(com.cloud.utils.db.Filter) NetworkOfferingVO(com.cloud.offerings.NetworkOfferingVO)

Aggregations

Project (com.cloud.projects.Project)89 Account (com.cloud.user.Account)55 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)28 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)27 Domain (com.cloud.domain.Domain)20 ArrayList (java.util.ArrayList)20 InvalidParameterValueException (com.cloud.utils.exception.InvalidParameterValueException)18 DomainVO (com.cloud.domain.DomainVO)12 PhysicalNetworkVO (com.cloud.network.dao.PhysicalNetworkVO)11 Pair (com.cloud.utils.Pair)11 List (java.util.List)11 ServerApiException (com.cloud.api.ServerApiException)10 ProjectAccount (com.cloud.projects.ProjectAccount)10 Volume (com.cloud.storage.Volume)10 UserAccount (com.cloud.user.UserAccount)10 DB (com.cloud.utils.db.DB)10 ProjectResponse (com.cloud.api.response.ProjectResponse)9 Filter (com.cloud.utils.db.Filter)9 DataCenterVO (com.cloud.dc.DataCenterVO)8 VlanVO (com.cloud.dc.VlanVO)6