Search in sources :

Example 1 with QuotaAccountVO

use of org.apache.cloudstack.quota.vo.QuotaAccountVO in project cloudstack by apache.

the class QuotaResponseBuilderImpl method createQuotaSummaryResponse.

@Override
public Pair<List<QuotaSummaryResponse>, Integer> createQuotaSummaryResponse(Boolean listAll, final String keyword, final Long startIndex, final Long pageSize) {
    List<QuotaSummaryResponse> result = new ArrayList<QuotaSummaryResponse>();
    Integer count = 0;
    if (listAll) {
        Filter filter = new Filter(AccountVO.class, "accountName", true, startIndex, pageSize);
        Pair<List<AccountVO>, Integer> data = _accountDao.findAccountsLike(keyword, filter);
        count = data.second();
        for (final AccountVO account : data.first()) {
            QuotaSummaryResponse qr = getQuotaSummaryResponse(account);
            result.add(qr);
        }
    } else {
        Pair<List<QuotaAccountVO>, Integer> data = _quotaAccountDao.listAllQuotaAccount(startIndex, pageSize);
        count = data.second();
        for (final QuotaAccountVO quotaAccount : data.first()) {
            AccountVO account = _accountDao.findById(quotaAccount.getId());
            if (account == null) {
                continue;
            }
            QuotaSummaryResponse qr = getQuotaSummaryResponse(account);
            result.add(qr);
        }
    }
    return new Pair<>(result, count);
}
Also used : Filter(com.cloud.utils.db.Filter) ArrayList(java.util.ArrayList) QuotaAccountVO(org.apache.cloudstack.quota.vo.QuotaAccountVO) List(java.util.List) ArrayList(java.util.ArrayList) QuotaAccountVO(org.apache.cloudstack.quota.vo.QuotaAccountVO) AccountVO(com.cloud.user.AccountVO) Pair(com.cloud.utils.Pair)

Example 2 with QuotaAccountVO

use of org.apache.cloudstack.quota.vo.QuotaAccountVO in project cloudstack by apache.

the class QuotaServiceImpl method setLockAccount.

@Override
public void setLockAccount(Long accountId, Boolean enforce) {
    QuotaAccountVO acc = _quotaAcc.findByIdQuotaAccount(accountId);
    if (acc == null) {
        acc = new QuotaAccountVO(accountId);
        acc.setQuotaEnforce(enforce ? 1 : 0);
        _quotaAcc.persistQuotaAccount(acc);
    } else {
        acc.setQuotaEnforce(enforce ? 1 : 0);
        _quotaAcc.updateQuotaAccount(accountId, acc);
    }
}
Also used : QuotaAccountVO(org.apache.cloudstack.quota.vo.QuotaAccountVO)

Example 3 with QuotaAccountVO

use of org.apache.cloudstack.quota.vo.QuotaAccountVO in project cloudstack by apache.

the class QuotaServiceImplTest method testSetLockAccount.

@Test
public void testSetLockAccount() {
    // existing account
    QuotaAccountVO quotaAccountVO = new QuotaAccountVO();
    Mockito.when(quotaAcc.findByIdQuotaAccount(Mockito.anyLong())).thenReturn(quotaAccountVO);
    quotaService.setLockAccount(2L, true);
    Mockito.verify(quotaAcc, Mockito.times(0)).persistQuotaAccount(Mockito.any(QuotaAccountVO.class));
    Mockito.verify(quotaAcc, Mockito.times(1)).updateQuotaAccount(Mockito.anyLong(), Mockito.any(QuotaAccountVO.class));
    // new account
    Mockito.when(quotaAcc.findByIdQuotaAccount(Mockito.anyLong())).thenReturn(null);
    quotaService.setLockAccount(2L, true);
    Mockito.verify(quotaAcc, Mockito.times(1)).persistQuotaAccount(Mockito.any(QuotaAccountVO.class));
}
Also used : QuotaAccountVO(org.apache.cloudstack.quota.vo.QuotaAccountVO) Test(org.junit.Test)

Example 4 with QuotaAccountVO

use of org.apache.cloudstack.quota.vo.QuotaAccountVO in project cloudstack by apache.

the class QuotaAlertManagerImpl method checkAndSendQuotaAlertEmails.

@Override
public void checkAndSendQuotaAlertEmails() {
    List<DeferredQuotaEmail> deferredQuotaEmailList = new ArrayList<DeferredQuotaEmail>();
    final BigDecimal zeroBalance = new BigDecimal(0);
    for (final QuotaAccountVO quotaAccount : _quotaAcc.listAllQuotaAccount()) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("checkAndSendQuotaAlertEmails accId=" + quotaAccount.getId());
        }
        BigDecimal accountBalance = quotaAccount.getQuotaBalance();
        Date balanceDate = quotaAccount.getQuotaBalanceDate();
        Date alertDate = quotaAccount.getQuotaAlertDate();
        int lockable = quotaAccount.getQuotaEnforce();
        BigDecimal thresholdBalance = quotaAccount.getQuotaMinBalance();
        if (accountBalance != null) {
            AccountVO account = _accountDao.findById(quotaAccount.getId());
            if (account == null) {
                // the account is removed
                continue;
            }
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("checkAndSendQuotaAlertEmails: Check id=" + account.getId() + " bal=" + accountBalance + ", alertDate=" + alertDate + ", lockable=" + lockable);
            }
            if (accountBalance.compareTo(zeroBalance) < 0) {
                if (_lockAccountEnforcement && (lockable == 1)) {
                    if (_quotaManager.isLockable(account)) {
                        s_logger.info("Locking account " + account.getAccountName() + " due to quota < 0.");
                        lockAccount(account.getId());
                    }
                }
                if (alertDate == null || (balanceDate.after(alertDate) && getDifferenceDays(alertDate, new Date()) > 1)) {
                    s_logger.info("Sending alert " + account.getAccountName() + " due to quota < 0.");
                    deferredQuotaEmailList.add(new DeferredQuotaEmail(account, quotaAccount, QuotaConfig.QuotaEmailTemplateTypes.QUOTA_EMPTY));
                }
            } else if (accountBalance.compareTo(thresholdBalance) < 0) {
                if (alertDate == null || (balanceDate.after(alertDate) && getDifferenceDays(alertDate, new Date()) > 1)) {
                    s_logger.info("Sending alert " + account.getAccountName() + " due to quota below threshold.");
                    deferredQuotaEmailList.add(new DeferredQuotaEmail(account, quotaAccount, QuotaConfig.QuotaEmailTemplateTypes.QUOTA_LOW));
                }
            }
        }
    }
    for (DeferredQuotaEmail emailToBeSent : deferredQuotaEmailList) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("checkAndSendQuotaAlertEmails: Attempting to send quota alert email to users of account: " + emailToBeSent.getAccount().getAccountName());
        }
        sendQuotaAlert(emailToBeSent);
    }
}
Also used : ArrayList(java.util.ArrayList) QuotaAccountVO(org.apache.cloudstack.quota.vo.QuotaAccountVO) QuotaAccountVO(org.apache.cloudstack.quota.vo.QuotaAccountVO) AccountVO(com.cloud.user.AccountVO) BigDecimal(java.math.BigDecimal) Date(java.util.Date)

Example 5 with QuotaAccountVO

use of org.apache.cloudstack.quota.vo.QuotaAccountVO 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)

Aggregations

QuotaAccountVO (org.apache.cloudstack.quota.vo.QuotaAccountVO)13 AccountVO (com.cloud.user.AccountVO)7 ArrayList (java.util.ArrayList)7 BigDecimal (java.math.BigDecimal)6 Date (java.util.Date)5 Test (org.junit.Test)5 Calendar (java.util.Calendar)2 DomainVO (com.cloud.domain.DomainVO)1 UserVO (com.cloud.user.UserVO)1 Pair (com.cloud.utils.Pair)1 Filter (com.cloud.utils.db.Filter)1 List (java.util.List)1 DeferredQuotaEmail (org.apache.cloudstack.quota.QuotaAlertManagerImpl.DeferredQuotaEmail)1 QuotaEmailTemplatesVO (org.apache.cloudstack.quota.vo.QuotaEmailTemplatesVO)1 SMTPMailProperties (org.apache.cloudstack.utils.mailing.SMTPMailProperties)1 SMTPMailSender (org.apache.cloudstack.utils.mailing.SMTPMailSender)1