Search in sources :

Example 41 with DomainVO

use of com.cloud.domain.DomainVO in project cloudstack by apache.

the class QuotaAlertManagerImplTest method testSendQuotaAlert.

@Test
public void testSendQuotaAlert() throws UnsupportedEncodingException, MessagingException {
    Mockito.doCallRealMethod().when(quotaAlertManager).sendQuotaAlert(Mockito.any(QuotaAlertManagerImpl.DeferredQuotaEmail.class));
    AccountVO account = new AccountVO();
    account.setId(2L);
    account.setDomainId(1L);
    account.setType(Account.ACCOUNT_TYPE_NORMAL);
    account.setAccountName("admin");
    account.setUuid("uuid");
    QuotaAccountVO quotaAccount = new QuotaAccountVO(2L);
    quotaAccount.setQuotaBalance(new BigDecimal(404));
    quotaAccount.setQuotaMinBalance(new BigDecimal(100));
    quotaAccount.setQuotaBalanceDate(new Date());
    quotaAccount.setQuotaAlertDate(null);
    quotaAccount.setQuotaEnforce(0);
    QuotaAlertManagerImpl.DeferredQuotaEmail email = new QuotaAlertManagerImpl.DeferredQuotaEmail(account, quotaAccount, new BigDecimal(100), QuotaConfig.QuotaEmailTemplateTypes.QUOTA_LOW);
    QuotaEmailTemplatesVO quotaEmailTemplatesVO = new QuotaEmailTemplatesVO();
    quotaEmailTemplatesVO.setTemplateSubject("Low quota");
    quotaEmailTemplatesVO.setTemplateBody("Low quota {{accountID}}");
    List<QuotaEmailTemplatesVO> emailTemplates = new ArrayList<>();
    emailTemplates.add(quotaEmailTemplatesVO);
    Mockito.when(quotaEmailTemplateDao.listAllQuotaEmailTemplates(Mockito.anyString())).thenReturn(emailTemplates);
    DomainVO domain = new DomainVO();
    domain.setUuid("uuid");
    domain.setName("/domain");
    Mockito.when(domainDao.findByIdIncludingRemoved(Mockito.anyLong())).thenReturn(new DomainVO());
    UserVO user = new UserVO();
    user.setUsername("user1");
    user.setEmail("user1@apache.org");
    List<UserVO> users = new ArrayList<>();
    users.add(user);
    Mockito.when(userDao.listByAccount(Mockito.anyLong())).thenReturn(users);
    quotaAlertManager.mailSender = Mockito.mock(SMTPMailSender.class);
    Mockito.doNothing().when(quotaAlertManager.mailSender).sendMail(Mockito.any());
    quotaAlertManager.sendQuotaAlert(email);
    assertTrue(email.getSendDate() != null);
    Mockito.verify(quotaAlertManager, Mockito.times(1)).sendQuotaAlert(Mockito.anyString(), Mockito.anyListOf(String.class), Mockito.anyString(), Mockito.anyString());
    Mockito.verify(quotaAlertManager.mailSender, Mockito.times(1)).sendMail(Mockito.any(SMTPMailProperties.class));
}
Also used : QuotaEmailTemplatesVO(org.apache.cloudstack.quota.vo.QuotaEmailTemplatesVO) SMTPMailProperties(org.apache.cloudstack.utils.mailing.SMTPMailProperties) ArrayList(java.util.ArrayList) QuotaAccountVO(org.apache.cloudstack.quota.vo.QuotaAccountVO) AccountVO(com.cloud.user.AccountVO) BigDecimal(java.math.BigDecimal) Date(java.util.Date) DomainVO(com.cloud.domain.DomainVO) UserVO(com.cloud.user.UserVO) QuotaAccountVO(org.apache.cloudstack.quota.vo.QuotaAccountVO) SMTPMailSender(org.apache.cloudstack.utils.mailing.SMTPMailSender) Test(org.junit.Test)

Example 42 with DomainVO

use of com.cloud.domain.DomainVO in project cloudstack by apache.

the class ExplicitDedicationProcessor method getDomainParentIds.

private List<Long> getDomainParentIds(long domainId) {
    DomainVO domainRecord = _domainDao.findById(domainId);
    List<Long> domainIds = new ArrayList<Long>();
    domainIds.add(domainRecord.getId());
    while (domainRecord.getParent() != null) {
        domainRecord = _domainDao.findById(domainRecord.getParent());
        domainIds.add(domainRecord.getId());
    }
    return domainIds;
}
Also used : DomainVO(com.cloud.domain.DomainVO) ArrayList(java.util.ArrayList)

Example 43 with DomainVO

use of com.cloud.domain.DomainVO in project cloudstack by apache.

the class ManagementServerImpl method searchForVlans.

@Override
public Pair<List<? extends Vlan>, Integer> searchForVlans(final ListVlanIpRangesCmd cmd) {
    // If an account name and domain ID are specified, look up the account
    final String accountName = cmd.getAccountName();
    final Long domainId = cmd.getDomainId();
    Long accountId = null;
    final Long networkId = cmd.getNetworkId();
    final Boolean forVirtual = cmd.isForVirtualNetwork();
    String vlanType = null;
    final Long projectId = cmd.getProjectId();
    final Long physicalNetworkId = cmd.getPhysicalNetworkId();
    if (accountName != null && domainId != null) {
        if (projectId != null) {
            throw new InvalidParameterValueException("Account and projectId can't be specified together");
        }
        final Account account = _accountDao.findActiveAccount(accountName, domainId);
        if (account == null) {
            final InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find account " + accountName + " in specified domain");
            // Since we don't have a DomainVO object here, we directly set
            // tablename to "domain".
            final DomainVO domain = ApiDBUtils.findDomainById(domainId);
            String domainUuid = domainId.toString();
            if (domain != null) {
                domainUuid = domain.getUuid();
            }
            ex.addProxyObject(domainUuid, "domainId");
            throw ex;
        } else {
            accountId = account.getId();
        }
    }
    if (forVirtual != null) {
        if (forVirtual) {
            vlanType = VlanType.VirtualNetwork.toString();
        } else {
            vlanType = VlanType.DirectAttached.toString();
        }
    }
    // set project information
    if (projectId != null) {
        final Project project = _projectMgr.getProject(projectId);
        if (project == null) {
            final InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find project by id " + projectId);
            ex.addProxyObject(projectId.toString(), "projectId");
            throw ex;
        }
        accountId = project.getProjectAccountId();
    }
    final Filter searchFilter = new Filter(VlanVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal());
    final Object id = cmd.getId();
    final Object vlan = cmd.getVlan();
    final Object dataCenterId = cmd.getZoneId();
    final Object podId = cmd.getPodId();
    final Object keyword = cmd.getKeyword();
    final SearchBuilder<VlanVO> sb = _vlanDao.createSearchBuilder();
    sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
    sb.and("vlan", sb.entity().getVlanTag(), SearchCriteria.Op.EQ);
    sb.and("dataCenterId", sb.entity().getDataCenterId(), SearchCriteria.Op.EQ);
    sb.and("vlan", sb.entity().getVlanTag(), SearchCriteria.Op.EQ);
    sb.and("networkId", sb.entity().getNetworkId(), SearchCriteria.Op.EQ);
    sb.and("vlanType", sb.entity().getVlanType(), SearchCriteria.Op.EQ);
    sb.and("physicalNetworkId", sb.entity().getPhysicalNetworkId(), SearchCriteria.Op.EQ);
    if (accountId != null) {
        final SearchBuilder<AccountVlanMapVO> accountVlanMapSearch = _accountVlanMapDao.createSearchBuilder();
        accountVlanMapSearch.and("accountId", accountVlanMapSearch.entity().getAccountId(), SearchCriteria.Op.EQ);
        sb.join("accountVlanMapSearch", accountVlanMapSearch, sb.entity().getId(), accountVlanMapSearch.entity().getVlanDbId(), JoinBuilder.JoinType.INNER);
    }
    if (domainId != null) {
        DomainVO domain = ApiDBUtils.findDomainById(domainId);
        if (domain == null) {
            throw new InvalidParameterValueException("Unable to find domain with id " + domainId);
        }
        final SearchBuilder<DomainVlanMapVO> domainVlanMapSearch = _domainVlanMapDao.createSearchBuilder();
        domainVlanMapSearch.and("domainId", domainVlanMapSearch.entity().getDomainId(), SearchCriteria.Op.EQ);
        sb.join("domainVlanMapSearch", domainVlanMapSearch, sb.entity().getId(), domainVlanMapSearch.entity().getVlanDbId(), JoinType.INNER);
    }
    if (podId != null) {
        final SearchBuilder<PodVlanMapVO> podVlanMapSearch = _podVlanMapDao.createSearchBuilder();
        podVlanMapSearch.and("podId", podVlanMapSearch.entity().getPodId(), SearchCriteria.Op.EQ);
        sb.join("podVlanMapSearch", podVlanMapSearch, sb.entity().getId(), podVlanMapSearch.entity().getVlanDbId(), JoinBuilder.JoinType.INNER);
    }
    final SearchCriteria<VlanVO> sc = sb.create();
    if (keyword != null) {
        final SearchCriteria<VlanVO> ssc = _vlanDao.createSearchCriteria();
        ssc.addOr("vlanTag", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("ipRange", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        sc.addAnd("vlanTag", SearchCriteria.Op.SC, ssc);
    } else {
        if (id != null) {
            sc.setParameters("id", id);
        }
        if (vlan != null) {
            sc.setParameters("vlan", vlan);
        }
        if (dataCenterId != null) {
            sc.setParameters("dataCenterId", dataCenterId);
        }
        if (networkId != null) {
            sc.setParameters("networkId", networkId);
        }
        if (accountId != null) {
            sc.setJoinParameters("accountVlanMapSearch", "accountId", accountId);
        }
        if (podId != null) {
            sc.setJoinParameters("podVlanMapSearch", "podId", podId);
        }
        if (vlanType != null) {
            sc.setParameters("vlanType", vlanType);
        }
        if (physicalNetworkId != null) {
            sc.setParameters("physicalNetworkId", physicalNetworkId);
        }
        if (domainId != null) {
            sc.setJoinParameters("domainVlanMapSearch", "domainId", domainId);
        }
    }
    final Pair<List<VlanVO>, Integer> result = _vlanDao.searchAndCount(sc, searchFilter);
    return new Pair<List<? extends Vlan>, Integer>(result.first(), result.second());
}
Also used : Account(com.cloud.user.Account) AccountVlanMapVO(com.cloud.dc.AccountVlanMapVO) DomainVlanMapVO(com.cloud.dc.DomainVlanMapVO) NetworkDomainVO(com.cloud.network.dao.NetworkDomainVO) DomainVO(com.cloud.domain.DomainVO) Project(com.cloud.projects.Project) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) Filter(com.cloud.utils.db.Filter) PodVlanMapVO(com.cloud.dc.PodVlanMapVO) ArrayList(java.util.ArrayList) ExcludeList(com.cloud.deploy.DeploymentPlanner.ExcludeList) List(java.util.List) VlanVO(com.cloud.dc.VlanVO) Pair(com.cloud.utils.Pair) SSHKeyPair(com.cloud.user.SSHKeyPair)

Example 44 with DomainVO

use of com.cloud.domain.DomainVO in project cloudstack by apache.

the class ManagementServerImpl method deleteSSHKeyPair.

@Override
public boolean deleteSSHKeyPair(final DeleteSSHKeyPairCmd cmd) {
    final Account caller = getCaller();
    final String accountName = cmd.getAccountName();
    final Long domainId = cmd.getDomainId();
    final Long projectId = cmd.getProjectId();
    Account owner = null;
    try {
        owner = _accountMgr.finalizeOwner(caller, accountName, domainId, projectId);
    } catch (InvalidParameterValueException ex) {
        if (caller.getType() == Account.ACCOUNT_TYPE_ADMIN && accountName != null && domainId != null) {
            owner = _accountDao.findAccountIncludingRemoved(accountName, domainId);
        }
        if (owner == null) {
            throw ex;
        }
    }
    final SSHKeyPairVO s = _sshKeyPairDao.findByName(owner.getAccountId(), owner.getDomainId(), cmd.getName());
    if (s == null) {
        final InvalidParameterValueException ex = new InvalidParameterValueException("A key pair with name '" + cmd.getName() + "' does not exist for account " + owner.getAccountName() + " in specified domain id");
        final DomainVO domain = ApiDBUtils.findDomainById(owner.getDomainId());
        String domainUuid = String.valueOf(owner.getDomainId());
        if (domain != null) {
            domainUuid = domain.getUuid();
        }
        ex.addProxyObject(domainUuid, "domainId");
        throw ex;
    }
    annotationDao.removeByEntityType(AnnotationService.EntityType.SSH_KEYPAIR.name(), s.getUuid());
    return _sshKeyPairDao.deleteByName(owner.getAccountId(), owner.getDomainId(), cmd.getName());
}
Also used : Account(com.cloud.user.Account) NetworkDomainVO(com.cloud.network.dao.NetworkDomainVO) DomainVO(com.cloud.domain.DomainVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) SSHKeyPairVO(com.cloud.user.SSHKeyPairVO)

Example 45 with DomainVO

use of com.cloud.domain.DomainVO in project cloudstack by apache.

the class ProjectManagerImpl method addAccountToProject.

@Override
@ActionEvent(eventType = EventTypes.EVENT_PROJECT_ACCOUNT_ADD, eventDescription = "adding account to project", async = true)
public boolean addAccountToProject(long projectId, String accountName, String email, Long projectRoleId, Role projectRoleType) {
    Account caller = CallContext.current().getCallingAccount();
    // check that the project exists
    Project project = getProject(projectId);
    if (project == null) {
        InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find project with specified id");
        ex.addProxyObject(String.valueOf(projectId), "projectId");
        throw ex;
    }
    // User can be added to Active project only
    if (project.getState() != Project.State.Active) {
        InvalidParameterValueException ex = new InvalidParameterValueException("Can't add account to the specified project id in state=" + project.getState() + " as it's no longer active");
        ex.addProxyObject(project.getUuid(), "projectId");
        throw ex;
    }
    // check that account-to-add exists
    Account account = null;
    if (accountName != null) {
        account = _accountMgr.getActiveAccountByName(accountName, project.getDomainId());
        if (account == null) {
            InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find account name=" + accountName + " in specified domain id");
            DomainVO domain = ApiDBUtils.findDomainById(project.getDomainId());
            String domainUuid = String.valueOf(project.getDomainId());
            if (domain != null) {
                domainUuid = domain.getUuid();
            }
            ex.addProxyObject(domainUuid, "domainId");
            throw ex;
        }
        CallContext.current().setProject(project);
        // verify permissions - only project owner can assign
        _accountMgr.checkAccess(caller, AccessType.ModifyProject, true, _accountMgr.getAccount(project.getProjectAccountId()));
        // Check if the account already added to the project
        ProjectAccount projectAccount = _projectAccountDao.findByProjectIdAccountId(projectId, account.getId());
        if (projectAccount != null) {
            s_logger.debug("Account " + accountName + " already added to the project id=" + projectId);
            return true;
        }
    }
    if (projectRoleId != null && projectRoleId < 1L) {
        throw new InvalidParameterValueException("Invalid project role id provided");
    }
    ProjectRole projectRole = null;
    if (projectRoleId != null) {
        projectRole = projectRoleDao.findById(projectRoleId);
        if (projectRole == null || projectRole.getProjectId() != projectId) {
            throw new InvalidParameterValueException("Invalid project role ID for the given project");
        }
    }
    if (_invitationRequired) {
        return inviteAccountToProject(project, account, email, projectRoleType, projectRole);
    } else {
        if (account == null) {
            throw new InvalidParameterValueException("Account information is required for assigning account to the project");
        }
        if (assignAccountToProject(project, account.getId(), projectRoleType, null, Optional.ofNullable(projectRole).map(ProjectRole::getId).orElse(null)) != null) {
            return true;
        } else {
            s_logger.warn("Failed to add account " + accountName + " to project id=" + projectId);
            return false;
        }
    }
}
Also used : Account(com.cloud.user.Account) DomainVO(com.cloud.domain.DomainVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ProjectRole(org.apache.cloudstack.acl.ProjectRole) ActionEvent(com.cloud.event.ActionEvent)

Aggregations

DomainVO (com.cloud.domain.DomainVO)196 Account (com.cloud.user.Account)85 AccountVO (com.cloud.user.AccountVO)64 Test (org.junit.Test)56 ArrayList (java.util.ArrayList)53 DomainDao (com.cloud.domain.dao.DomainDao)30 Field (java.lang.reflect.Field)30 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)29 SslCertDao (com.cloud.network.dao.SslCertDao)29 AccountManager (com.cloud.user.AccountManager)29 SslCertVO (com.cloud.network.dao.SslCertVO)27 List (java.util.List)26 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)24 Pair (com.cloud.utils.Pair)24 Domain (com.cloud.domain.Domain)23 Filter (com.cloud.utils.db.Filter)23 File (java.io.File)23 IOException (java.io.IOException)23 FileUtils.readFileToString (org.apache.commons.io.FileUtils.readFileToString)23 InvalidParameterValueException (com.cloud.utils.exception.InvalidParameterValueException)22