Adding some early Notifications stubs

* A basic NotificationTemplate model class with early notification type
  definitions
* Initial implementations of the Email, Slack, and Twilio Notification
  backends using the Django email backend system
* Some dependencies thereof
This commit is contained in:
Matthew Jones
2016-02-01 16:54:34 -05:00
parent 6d71fe49f2
commit 7385efef35
7 changed files with 139 additions and 0 deletions

View File

View File

@@ -0,0 +1,11 @@
# Copyright (c) 2016 Ansible, Inc.
# All Rights Reserved.
import logging
from django.core.mail.backends.smtp import EmailBackend
class CustomEmailBackend(EmailBackend):
init_parameters = ("host", "port", "username", "password",
"use_tls", "use_ssl")

View File

@@ -0,0 +1,46 @@
# Copyright (c) 2016 Ansible, Inc.
# All Rights Reserved.
import logging
from slackclient import SlackClient
from django.core.mail.backends.base import BaseEmailBackend
logger = logging.getLogger('awx.main.notifications.slack_backend')
class SlackBackend(BaseEmailBackend):
init_parameters = ('token',)
def __init__(self, token, fail_silently=False, **kwargs):
super(SlackBackend, self).__init__(fail_silently=fail_silently)
self.token = token
self.connection = None
def open(self):
if self.connection is not None:
return False
self.connection = SlackClient(self.token)
if not self.connection.rtm_connect():
if not self.fail_silently:
raise Exception("Slack Notification Token is invalid")
return True
def close(self):
if self.connection is None:
return
self.connection = None
def send_messages(self, messages):
if self.connection is None:
self.open()
sent_messages = 0
for m in messages:
try:
self.connection.rtm_send_message(m.to, m.body)
sent_messages += 1
except Exception as e:
if not self.fail_silently:
raise
logger.error("Exception sending messages: {}".format(e))
return sent_messages

View File

@@ -0,0 +1,42 @@
# Copyright (c) 2016 Ansible, Inc.
# All Rights Reserved.
import logging
from twilio.rest import TwilioRestClient
from django.core.mail.backends.base import BaseEmailBackend
logger = logging.getLogger('awx.main.notifications.twilio_backend')
class TwilioBackend(BaseEmailBackend):
init_parameters = ('account_sid', 'account_token', 'from_phone',)
def __init__(self, account_sid, account_token, from_phone, fail_silently=False, **kwargs):
super(TwilioBackend, self).__init__(fail_silently=fail_silently)
self.account_sid = account_sid
self.account_token = account_token
self.from_phone = from_phone
def send_messages(self, messages):
sent_messages = 0
try:
connection = TwilioRestClient(self.account_sid, self.account_token)
except Exception as e:
if not self.fail_silently:
raise
logger.error("Exception connecting to Twilio: {}".format(e))
for m in messages:
try:
connection.messages.create(
to=m.to,
from_=self.from_phone,
body=m.body)
sent_messages += 1
except Exception as e:
if not self.fail_silently:
raise
logger.error("Exception sending messages: {}".format(e))
return sent_messages