Refactor message generator

* Job object can now control the output and generate K:V output for
  notification types that can support it
* Notifications store the body as json/dict now to encode more
  information
* Notification Type can further compose the message based on what is
  sensible for the notification type
* This will also allow customizing the message template in the future
* All notification types use sane defaults for the level of detail now
This commit is contained in:
Matthew Jones
2016-02-22 17:09:36 -05:00
parent eb3d663d18
commit ab3669efa9
13 changed files with 76 additions and 37 deletions

View File

@@ -18,3 +18,10 @@ class CustomEmailBackend(EmailBackend):
recipient_parameter = "recipients"
sender_parameter = "sender"
def format_body(self, body):
body_actual = "{} #{} had status {} on Ansible Tower, view details at {}\n\n".format(body['friendly_name'],
body['id'],
body['status'],
body['url'])
body_actual += pprint.pformat(body, indent=4)
return body_actual

View File

@@ -5,11 +5,11 @@ import logging
import requests
from django.core.mail.backends.base import BaseEmailBackend
from awx.main.notifications.base import TowerBaseEmailBackend
logger = logging.getLogger('awx.main.notifications.hipchat_backend')
class HipChatBackend(BaseEmailBackend):
class HipChatBackend(TowerBaseEmailBackend):
init_parameters = {"token": {"label": "Token", "type": "password"},
"channels": {"label": "Destination Channels", "type": "list"},
@@ -35,7 +35,7 @@ class HipChatBackend(BaseEmailBackend):
r = requests.post("{}/v2/room/{}/notification".format(self.api_url, rcp),
params={"auth_token": self.token},
json={"color": self.color,
"message": m.body,
"message": m.subject,
"notify": self.notify,
"from": m.from_email,
"message_format": "text"})

View File

@@ -7,11 +7,11 @@ import logging
import irc.client
from django.core.mail.backends.base import BaseEmailBackend
from awx.main.notifications.base import TowerBaseEmailBackend
logger = logging.getLogger('awx.main.notifications.irc_backend')
class IrcBackend(BaseEmailBackend):
class IrcBackend(TowerBaseEmailBackend):
init_parameters = {"server": {"label": "IRC Server Address", "type": "string"},
"port": {"label": "IRC Server Port", "type": "int"},

View File

@@ -4,11 +4,11 @@
import logging
import pygerduty
from django.core.mail.backends.base import BaseEmailBackend
from awx.main.notifications.base import TowerBaseEmailBackend
logger = logging.getLogger('awx.main.notifications.pagerduty_backend')
class PagerDutyBackend(BaseEmailBackend):
class PagerDutyBackend(TowerBaseEmailBackend):
init_parameters = {"subdomain": {"label": "Pagerduty subdomain", "type": "string"},
"token": {"label": "API Token", "type": "password"},
@@ -22,6 +22,9 @@ class PagerDutyBackend(BaseEmailBackend):
self.subdomain = subdomain
self.token = token
def format_body(self, body):
return body
def send_messages(self, messages):
sent_messages = 0

View File

@@ -4,11 +4,11 @@
import logging
from slackclient import SlackClient
from django.core.mail.backends.base import BaseEmailBackend
from awx.main.notifications.base import TowerBaseEmailBackend
logger = logging.getLogger('awx.main.notifications.slack_backend')
class SlackBackend(BaseEmailBackend):
class SlackBackend(TowerBaseEmailBackend):
init_parameters = {"token": {"label": "Token", "type": "password"},
"channels": {"label": "Destination Channels", "type": "list"}}
@@ -41,7 +41,7 @@ class SlackBackend(BaseEmailBackend):
for m in messages:
try:
for r in m.recipients():
self.connection.rtm_send_message(r, m.body)
self.connection.rtm_send_message(r, m.subject)
sent_messages += 1
except Exception as e:
logger.error("Exception sending messages: {}".format(e))

View File

@@ -5,11 +5,11 @@ import logging
from twilio.rest import TwilioRestClient
from django.core.mail.backends.base import BaseEmailBackend
from awx.main.notifications.base import TowerBaseEmailBackend
logger = logging.getLogger('awx.main.notifications.twilio_backend')
class TwilioBackend(BaseEmailBackend):
class TwilioBackend(TowerBaseEmailBackend):
init_parameters = {"account_sid": {"label": "Account SID", "type": "string"},
"account_token": {"label": "Account Token", "type": "password"},
@@ -38,7 +38,7 @@ class TwilioBackend(BaseEmailBackend):
connection.messages.create(
to=m.to,
from_=m.from_email,
body=m.body)
body=m.subject)
sent_messages += 1
except Exception as e:
logger.error("Exception sending messages: {}".format(e))

View File

@@ -4,12 +4,12 @@
import logging
import requests
from django.core.mail.backends.base import BaseEmailBackend
import json
from awx.main.notifications.base import TowerBaseEmailBackend
logger = logging.getLogger('awx.main.notifications.webhook_backend')
class WebhookBackend(BaseEmailBackend):
class WebhookBackend(TowerBaseEmailBackend):
init_parameters = {"url": {"label": "Target URL", "type": "string"},
"headers": {"label": "HTTP Headers", "type": "object"}}
@@ -20,11 +20,16 @@ class WebhookBackend(BaseEmailBackend):
self.headers = headers
super(WebhookBackend, self).__init__(fail_silently=fail_silently)
def format_body(self, body):
logger.error("Generating body from {}".format(str(body)))
return body
def send_messages(self, messages):
sent_messages = 0
for m in messages:
logger.error("BODY: " + str(m.body))
r = requests.post("{}".format(m.recipients()[0]),
data=m.body,
data=json.dumps(m.body),
headers=self.headers)
if r.status_code >= 400:
logger.error("Error sending notification webhook: {}".format(r.text))