Add unicode support to email notifications

Change-Id: I82a869a57781c19a041a5ca4a71d35e83daaf18d
This commit is contained in:
Ryan Brandt 2015-04-28 10:08:56 -06:00
parent ee6f2b5d8d
commit 36b37c2120
3 changed files with 25 additions and 17 deletions

View File

@ -42,13 +42,14 @@ class Notification(object):
retry_count, alarm):
"""Setup the notification object
The src_partition and src_offset allow the notification
to be linked to the alarm that it came from.
to be linked to the alarm which triggered it.
ntype - The notification type
name - Name used in sending
address - to send the notification to
address - where to send the notification
retry_count - number of times we've tried to send
alarm - info that caused the notification
notifications that come after this one to remain uncommitted.
Note that data may include unicode strings.
"""
self.address = address
self.name = name

View File

@ -76,8 +76,11 @@ class EmailNotifier(AbstractNotifier):
return False
def _sendmail(self, notification, msg):
self._smtp.sendmail(self._config['from_addr'], notification.address, msg.as_string())
self._log.debug("Sent email to {}, notification {}".format(notification.address, notification.to_json()))
self._smtp.sendmail(self._config['from_addr'],
notification.address,
msg.as_string())
self._log.debug("Sent email to {}, notification {}".format(notification.address,
notification.to_json()))
def _email_error(self, notification):
self._log.exception("Error sending Email Notification")
@ -113,7 +116,7 @@ class EmailNotifier(AbstractNotifier):
timestamp = time.asctime(time.gmtime(notification.alarm_timestamp))
if len(hostname) == 1: # Type 1
text = '''On host "{}" {}
text = u'''On host "{}" {}
Alarm "{}" transitioned to the {} state at {} UTC
alarm_id: {}'''.format(hostname[0],
@ -121,26 +124,26 @@ class EmailNotifier(AbstractNotifier):
notification.alarm_name,
notification.state,
timestamp,
notification.alarm_id)
notification.alarm_id).encode("utf-8")
msg = email.mime.text.MIMEText(text)
msg['Subject'] = '{} "{}" for Host: {}'.format(notification.state,
notification.alarm_name,
hostname[0])
msg['Subject'] = u'{} "{}" for Host: {}'.format(notification.state,
notification.alarm_name,
hostname[0]).encode("utf-8")
else: # Type 2
text = '''{}
text = u'''{}
Alarm "{}" transitioned to the {} state at {} UTC
Alarm_id: {}'''.format(notification.message,
Alarm_id: {}'''.format(notification.message.lower(),
notification.alarm_name,
notification.state,
timestamp,
notification.alarm_id)
notification.alarm_id).encode("utf-8")
msg = email.mime.text.MIMEText(text)
msg['Subject'] = '{} "{}" '.format(notification.state, notification.alarm_name)
msg['Subject'] = u'{} "{}" '.format(notification.state, notification.alarm_name).encode("utf-8")
msg['From'] = self._config['from_addr']
msg['To'] = notification.address

View File

@ -22,14 +22,17 @@ import unittest
from monasca_notification.notification import Notification
from monasca_notification.types import email_notifier
UNICODE_CHAR = unichr(2344)
UNICODE_CHAR_ENCODED = UNICODE_CHAR.encode("utf-8")
def alarm(metrics):
return {"tenantId": "0",
"alarmId": "0",
"alarmName": "test Alarm",
"alarmName": u"test Alarm " + UNICODE_CHAR,
"oldState": "OK",
"newState": "ALARM",
"stateChangeReason": "I am alarming!",
"stateChangeReason": u"I am alarming!" + UNICODE_CHAR,
"timestamp": time.time(),
"metrics": metrics}
@ -39,7 +42,7 @@ class smtpStub(object):
self.trap = trap
def sendmail(self, from_addr, to_addr, msg):
self.trap.append("%s %s %s" % (from_addr, to_addr, msg))
self.trap.append("{} {} {}".format(from_addr, to_addr, msg))
class smtpStubException(object):
@ -93,7 +96,7 @@ class TestEmail(unittest.TestCase):
"""
metrics = []
metric_data = {'dimensions': {'hostname': 'foo1', 'service': 'bar1'}}
metric_data = {'dimensions': {'hostname': u'foo1' + UNICODE_CHAR, u'service' + UNICODE_CHAR: 'bar1'}}
metrics.append(metric_data)
self.notify(self._smtpStub, metrics)
@ -105,6 +108,7 @@ class TestEmail(unittest.TestCase):
self.assertRegexpMatches(email, "Content-Type: text/plain")
self.assertRegexpMatches(email, "Alarm .test Alarm.")
self.assertRegexpMatches(email, "On host .foo1.")
self.assertRegexpMatches(email, UNICODE_CHAR_ENCODED)
return_value = self.trap.pop(0)
self.assertTrue(return_value)