Fixing Notification Engines

Because the notifications mechanisms haven't been used much,
a change got through that effectively disabled the notification
engines.

* Fixes the conf to actually reflect what the code expects.
* Handling the no html template case.
* getting rid of broken .login() call for RT engine.

Change-Id: Ifc57d31a677cac9d120b43988da7dc455e4608d9
This commit is contained in:
adrian-turjak 2016-12-15 17:47:15 +13:00 committed by Dale Smith
parent 1f59bf51a3
commit d0a2816b72
3 changed files with 67 additions and 39 deletions

View File

@ -94,29 +94,29 @@ DEFAULT_TASK_SETTINGS:
template: completed.txt
# html_template: completed.txt
notifications:
standard:
EmailNotification:
EmailNotification:
standard:
emails:
- example@example.com
reply: no-reply@example.com
from: bounce+%(task_uuid)s@example.com
template: notification.txt
# html_template: completed.txt
RTNotification:
error:
emails:
- example@example.com
reply: no-reply@example.com
from: bounce+%(task_uuid)s@example.com
template: notification.txt
# html_template: completed.txt
RTNotification:
standard:
url: http://localhost/rt/REST/1.0/
queue: helpdesk
username: example@example.com
password: password
template: notification.txt
error:
EmailNotification:
emails:
- example@example.com
reply: no-reply@example.com
from: bounce+%(task_uuid)s@example.com
template: notification.txt
# html_template: completed.txt
RTNotification:
error:
url: http://localhost/rt/REST/1.0/
queue: errors
username: example@example.com
@ -149,17 +149,17 @@ TASK_SETTINGS:
subject: Your OpenStack signup has been completed
template: signup_completed.txt
notifications:
standard:
EmailNotification:
EmailNotification:
standard:
emails:
- signups@example.com
RTNotification:
error:
emails:
- signups@example.com
RTNotification:
standard:
queue: signups
error:
EmailNotification:
emails:
- signups@example.com
RTNotification:
error:
queue: signups
default_region: RegionOne
# If 'None' (null in yaml), will default to domain as parent.

View File

@ -13,7 +13,7 @@
# under the License.
from django.conf import settings
from django.core.mail import send_mail
from django.core.mail import EmailMultiAlternatives
from django.template import loader
from smtplib import SMTPException
from stacktask.api.models import Notification
@ -40,43 +40,73 @@ class EmailNotification(NotificationEngine):
Example conf:
<TaskView>:
notifications:
standard:
EmailNotification:
EmailNotification:
standard:
emails:
- example@example.com
reply: no-reply@example.com
template: notification.txt
html_template: completed.txt
error:
EmailNotification:
error:
emails:
- errors@example.com
reply: no-reply@example.com
template: notification.txt
html_template: completed.txt
<other notification>:
<other notification engine>:
...
"""
def _notify(self, task, notification):
template = loader.get_template(self.conf['template'])
html_template = loader.get_template(self.conf['html_template'])
template = loader.get_template(
self.conf['template'],
using='include_etc_templates')
html_template = self.conf.get('html_template', None)
if html_template:
html_template = loader.get_template(
html_template,
using='include_etc_templates')
context = {
'task': task, 'notification': notification}
# NOTE(adriant): Error handling?
message = template.render(context)
html_message = html_template.render(context)
if notification.error:
subject = "Error - %s notification" % task.task_type
else:
subject = "%s notification" % task.task_type
try:
send_mail(
subject, message, self.conf['reply'],
self.conf['emails'], fail_silently=False,
html_message=html_message)
message = template.render(context)
# from_email is the return-path and is distinct from the
# message headers
from_email = self.conf.get('from')
if not from_email:
from_email = self.conf['reply']
elif "%(task_uuid)s" in from_email:
from_email = from_email % {'task_uuid': task.uuid}
# these are the message headers which will be visible to
# the email client.
headers = {
'X-StackTask-Task-UUID': task.uuid,
# From needs to be set to be disctinct from return-path
'From': self.conf['reply'],
'Reply-To': self.conf['reply'],
}
email = EmailMultiAlternatives(
subject,
message,
from_email,
self.conf['emails'],
headers=headers,
)
if html_template:
email.attach_alternative(
html_template.render(context), "text/html")
email.send(fail_silently=False)
if not notification.error:
notification.acknowledge = True
notification.save()

View File

@ -52,7 +52,6 @@ class RTNotification(NotificationEngine):
tracker = RTResource(
self.conf['url'], self.conf['username'], self.conf['password'],
CookieAuthenticator)
tracker.login()
self.tracker = tracker
def _notify(self, task, notification):
@ -60,7 +59,6 @@ class RTNotification(NotificationEngine):
context = {'task': task, 'notification': notification}
# NOTE(adriant): Error handling?
message = template.render(context)
if notification.error:
@ -79,8 +77,8 @@ class RTNotification(NotificationEngine):
try:
self.tracker.post(path='ticket/new', payload=content)
if not notification.error:
notification.acknowledged = True
notification.save()
notification.acknowledged = True
notification.save()
except RTResourceError as e:
notes = {
'errors':