mirror of
https://github.com/apache/cloudstack.git
synced 2025-10-26 08:42:29 +01:00
New configs for quota email's headers and footers (#7852)
Co-authored-by: João Jandre <joao@scclouds.com.br>
This commit is contained in:
parent
40d7da6707
commit
2f701e1f89
@ -36,6 +36,7 @@ import org.apache.cloudstack.quota.vo.QuotaAccountVO;
|
|||||||
import org.apache.cloudstack.quota.vo.QuotaEmailTemplatesVO;
|
import org.apache.cloudstack.quota.vo.QuotaEmailTemplatesVO;
|
||||||
import org.apache.commons.lang.StringEscapeUtils;
|
import org.apache.commons.lang.StringEscapeUtils;
|
||||||
import org.apache.commons.lang.text.StrSubstitutor;
|
import org.apache.commons.lang.text.StrSubstitutor;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.apache.log4j.Logger;
|
import org.apache.log4j.Logger;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
@ -223,7 +224,7 @@ public class QuotaAlertManagerImpl extends ManagerBase implements QuotaAlertMana
|
|||||||
final String body = bodySubstitutor.replace(emailTemplate.getTemplateBody());
|
final String body = bodySubstitutor.replace(emailTemplate.getTemplateBody());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
sendQuotaAlert(account.getUuid(), emailRecipients, subject, body);
|
sendQuotaAlert(account, emailRecipients, subject, body);
|
||||||
emailToBeSent.sentSuccessfully(_quotaAcc);
|
emailToBeSent.sentSuccessfully(_quotaAcc);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
s_logger.error(String.format("Unable to send quota alert email (subject=%s; body=%s) to account %s (%s) recipients (%s) due to error (%s)", subject, body, account.getAccountName(),
|
s_logger.error(String.format("Unable to send quota alert email (subject=%s; body=%s) to account %s (%s) recipients (%s) due to error (%s)", subject, body, account.getAccountName(),
|
||||||
@ -354,17 +355,20 @@ public class QuotaAlertManagerImpl extends ManagerBase implements QuotaAlertMana
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
protected void sendQuotaAlert(String accountUuid, List<String> emails, String subject, String body) {
|
protected void sendQuotaAlert(Account account, List<String> emails, String subject, String body) {
|
||||||
SMTPMailProperties mailProperties = new SMTPMailProperties();
|
SMTPMailProperties mailProperties = new SMTPMailProperties();
|
||||||
|
|
||||||
mailProperties.setSender(new MailAddress(senderAddress));
|
mailProperties.setSender(new MailAddress(senderAddress));
|
||||||
|
|
||||||
|
body = addHeaderAndFooter(body, QuotaConfig.QuotaEmailHeader.valueIn(account.getDomainId()), QuotaConfig.QuotaEmailFooter.valueIn(account.getDomainId()));
|
||||||
|
|
||||||
mailProperties.setSubject(subject);
|
mailProperties.setSubject(subject);
|
||||||
mailProperties.setContent(body);
|
mailProperties.setContent(body);
|
||||||
mailProperties.setContentType("text/html; charset=utf-8");
|
mailProperties.setContentType("text/html; charset=utf-8");
|
||||||
|
|
||||||
if (CollectionUtils.isEmpty(emails)) {
|
if (CollectionUtils.isEmpty(emails)) {
|
||||||
s_logger.warn(String.format("Account [%s] does not have users with email registered, "
|
s_logger.warn(String.format("Account [%s] does not have users with email registered, "
|
||||||
+ "therefore we are unable to send quota alert email with subject [%s] and content [%s].", accountUuid, subject, body));
|
+ "therefore we are unable to send quota alert email with subject [%s] and content [%s].", account.getUuid(), subject, body));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -378,4 +382,16 @@ public class QuotaAlertManagerImpl extends ManagerBase implements QuotaAlertMana
|
|||||||
mailSender.sendMail(mailProperties);
|
mailSender.sendMail(mailProperties);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected String addHeaderAndFooter(String body, String header, String footer) {
|
||||||
|
|
||||||
|
if (StringUtils.isNotEmpty(header)) {
|
||||||
|
body = String.format("%s%s", header, body);
|
||||||
|
}
|
||||||
|
if (StringUtils.isNotEmpty(footer)) {
|
||||||
|
body = String.format("%s%s", body, footer);
|
||||||
|
}
|
||||||
|
|
||||||
|
return body;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -63,6 +63,12 @@ public interface QuotaConfig {
|
|||||||
ConfigKey<Boolean> QuotaAccountEnabled = new ConfigKey<>("Advanced", Boolean.class, "quota.account.enabled", "true", "Indicates whether Quota plugin is enabled or not for " +
|
ConfigKey<Boolean> QuotaAccountEnabled = new ConfigKey<>("Advanced", Boolean.class, "quota.account.enabled", "true", "Indicates whether Quota plugin is enabled or not for " +
|
||||||
"the account.", true, ConfigKey.Scope.Account);
|
"the account.", true, ConfigKey.Scope.Account);
|
||||||
|
|
||||||
|
ConfigKey<String> QuotaEmailHeader = new ConfigKey<>("Advanced", String.class, "quota.email.header", "",
|
||||||
|
"Text to be added as a header for quota emails. Line breaks are not automatically inserted between this section and the body.", true, ConfigKey.Scope.Domain);
|
||||||
|
|
||||||
|
ConfigKey<String> QuotaEmailFooter = new ConfigKey<>("Advanced", String.class, "quota.email.footer", "",
|
||||||
|
"Text to be added as a footer for quota emails. Line breaks are not automatically inserted between this section and the body.", true, ConfigKey.Scope.Domain);
|
||||||
|
|
||||||
enum QuotaEmailTemplateTypes {
|
enum QuotaEmailTemplateTypes {
|
||||||
QUOTA_LOW, QUOTA_EMPTY, QUOTA_UNLOCK_ACCOUNT, QUOTA_STATEMENT
|
QUOTA_LOW, QUOTA_EMPTY, QUOTA_UNLOCK_ACCOUNT, QUOTA_STATEMENT
|
||||||
}
|
}
|
||||||
|
|||||||
@ -71,6 +71,21 @@ public class QuotaAlertManagerImplTest extends TestCase {
|
|||||||
@Mock
|
@Mock
|
||||||
private ConfigurationDao configDao;
|
private ConfigurationDao configDao;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private QuotaAccountVO quotaAccountVOMock;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private List<QuotaAlertManagerImpl.DeferredQuotaEmail> deferredQuotaEmailListMock;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private QuotaManagerImpl quotaManagerMock;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private Date balanceDateMock;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private AccountVO accountMock;
|
||||||
|
|
||||||
@Spy
|
@Spy
|
||||||
@InjectMocks
|
@InjectMocks
|
||||||
private QuotaAlertManagerImpl quotaAlertManager = new QuotaAlertManagerImpl();
|
private QuotaAlertManagerImpl quotaAlertManager = new QuotaAlertManagerImpl();
|
||||||
@ -162,10 +177,22 @@ public class QuotaAlertManagerImplTest extends TestCase {
|
|||||||
quotaAlertManager.sendQuotaAlert(email);
|
quotaAlertManager.sendQuotaAlert(email);
|
||||||
assertTrue(email.getSendDate() != null);
|
assertTrue(email.getSendDate() != null);
|
||||||
|
|
||||||
Mockito.verify(quotaAlertManager, Mockito.times(1)).sendQuotaAlert(Mockito.anyString(), Mockito.anyListOf(String.class), Mockito.anyString(), Mockito.anyString());
|
Mockito.verify(quotaAlertManager, Mockito.times(1)).sendQuotaAlert(Mockito.any(), Mockito.anyListOf(String.class), Mockito.anyString(), Mockito.anyString());
|
||||||
Mockito.verify(quotaAlertManager.mailSender, Mockito.times(1)).sendMail(Mockito.any(SMTPMailProperties.class));
|
Mockito.verify(quotaAlertManager.mailSender, Mockito.times(1)).sendMail(Mockito.any(SMTPMailProperties.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void addHeaderAndFooterTestIfHeaderAndFootersAreAdded() {
|
||||||
|
String body = quotaAlertManager.addHeaderAndFooter("body", "Header", "Footer");
|
||||||
|
assertEquals("HeaderbodyFooter", body);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void addHeaderAndFooterTestIfHeaderAndFootersAreNotAddedIfEmpty() {
|
||||||
|
String body = quotaAlertManager.addHeaderAndFooter("body", "", "");
|
||||||
|
assertEquals("body", body);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetDifferenceDays() {
|
public void testGetDifferenceDays() {
|
||||||
Date now = new Date();
|
Date now = new Date();
|
||||||
|
|||||||
@ -141,7 +141,8 @@ public class QuotaServiceImpl extends ManagerBase implements QuotaService, Confi
|
|||||||
@Override
|
@Override
|
||||||
public ConfigKey<?>[] getConfigKeys() {
|
public ConfigKey<?>[] getConfigKeys() {
|
||||||
return new ConfigKey<?>[] {QuotaPluginEnabled, QuotaEnableEnforcement, QuotaCurrencySymbol, QuotaStatementPeriod, QuotaSmtpHost, QuotaSmtpPort, QuotaSmtpTimeout,
|
return new ConfigKey<?>[] {QuotaPluginEnabled, QuotaEnableEnforcement, QuotaCurrencySymbol, QuotaStatementPeriod, QuotaSmtpHost, QuotaSmtpPort, QuotaSmtpTimeout,
|
||||||
QuotaSmtpUser, QuotaSmtpPassword, QuotaSmtpAuthType, QuotaSmtpSender, QuotaSmtpEnabledSecurityProtocols, QuotaSmtpUseStartTLS, QuotaActivationRuleTimeout, QuotaAccountEnabled};
|
QuotaSmtpUser, QuotaSmtpPassword, QuotaSmtpAuthType, QuotaSmtpSender, QuotaSmtpEnabledSecurityProtocols, QuotaSmtpUseStartTLS, QuotaActivationRuleTimeout, QuotaAccountEnabled,
|
||||||
|
QuotaEmailHeader, QuotaEmailFooter};
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user