mirror of
https://github.com/ansible/awx.git
synced 2026-05-09 10:27:37 -02:30
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:
0
awx/main/notifications/__init__.py
Normal file
0
awx/main/notifications/__init__.py
Normal file
11
awx/main/notifications/email_backend.py
Normal file
11
awx/main/notifications/email_backend.py
Normal 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")
|
||||
46
awx/main/notifications/slack_backend.py
Normal file
46
awx/main/notifications/slack_backend.py
Normal 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
|
||||
42
awx/main/notifications/twilio_backend.py
Normal file
42
awx/main/notifications/twilio_backend.py
Normal 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
|
||||
Reference in New Issue
Block a user