mirror of
https://github.com/ansible/awx.git
synced 2026-02-16 02:30:01 -03:30
Pagerduty and Hipchat backends plus some cleanup
This commit is contained in:
46
awx/main/notifications/hipchat_backend.py
Normal file
46
awx/main/notifications/hipchat_backend.py
Normal file
@@ -0,0 +1,46 @@
|
||||
# Copyright (c) 2016 Ansible, Inc.
|
||||
# All Rights Reserved.
|
||||
|
||||
import logging
|
||||
|
||||
import requests
|
||||
|
||||
from django.core.mail.backends.base import BaseEmailBackend
|
||||
|
||||
logger = logging.getLogger('awx.main.notifications.hipchat_backend')
|
||||
|
||||
class HipChatBackend(BaseEmailBackend):
|
||||
|
||||
init_parameters = {"token": {"label": "Token", "type": "password"},
|
||||
"channels": {"label": "Destination Channels", "type": "list"},
|
||||
"color": {"label": "Notification Color", "type": "string"},
|
||||
"api_url": {"label": "API Url (e.g: https://mycompany.hipchat.com)", "type": "string"},
|
||||
"notify": {"label": "Notify channel", "type": "bool"},
|
||||
"message_from": {"label": "Label to be shown with notification", "type": "string"}}
|
||||
recipient_parameter = "channels"
|
||||
sender_parameter = "message_from"
|
||||
|
||||
def __init__(self, token, color, api_url, notify, fail_silently=False, **kwargs):
|
||||
super(HipChatBackend, self).__init__(fail_silently=fail_silently)
|
||||
self.token = token
|
||||
self.color = color
|
||||
self.api_url = api_url
|
||||
self.notify = notify
|
||||
|
||||
def send_messages(self, messages):
|
||||
sent_messages = 0
|
||||
|
||||
for m in messages:
|
||||
for rcp in m.recipients():
|
||||
r = requests.post("{}/v2/room/{}/notification".format(self.api_url, rcp),
|
||||
params={"auth_token": self.token},
|
||||
json={"color": self.color,
|
||||
"message": m.body,
|
||||
"notify": self.notify,
|
||||
"from": m.from_email,
|
||||
"message_format": "text"})
|
||||
if r.status_code != 204 and not self.fail_silently:
|
||||
logger.error("Error sending messages: {}".format(r.text))
|
||||
raise Exception("Error sending message to hipchat: {}".format(r.text))
|
||||
sent_messages += 1
|
||||
return sent_messages
|
||||
44
awx/main/notifications/pagerduty_backend.py
Normal file
44
awx/main/notifications/pagerduty_backend.py
Normal file
@@ -0,0 +1,44 @@
|
||||
# Copyright (c) 2016 Ansible, Inc.
|
||||
# All Rights Reserved.
|
||||
|
||||
import logging
|
||||
import pygerduty
|
||||
|
||||
from django.core.mail.backends.base import BaseEmailBackend
|
||||
|
||||
logger = logging.getLogger('awx.main.notifications.pagerduty_backend')
|
||||
|
||||
class PagerDutyBackend(BaseEmailBackend):
|
||||
|
||||
init_parameters = {"subdomain": {"label": "Pagerduty subdomain", "type": "string"},
|
||||
"token": {"label": "API Token", "type": "password"},
|
||||
"service_key": {"label": "API Service/Integration Key", "type": "string"},
|
||||
"client_name": {"label": "Client Identifier", "type": "string"}}
|
||||
recipient_parameter = "service_key"
|
||||
sender_parameter = "client_name"
|
||||
|
||||
def __init__(self, subdomain, token, fail_silently=False, **kwargs):
|
||||
super(PagerDutyBackend, self).__init__(fail_silently=fail_silently)
|
||||
self.subdomain = subdomain
|
||||
self.token = token
|
||||
|
||||
def send_messages(self, messages):
|
||||
sent_messages = 0
|
||||
|
||||
try:
|
||||
pager = pygerduty.PagerDuty(self.subdomain, self.token)
|
||||
except Exception as e:
|
||||
if not self.fail_silently:
|
||||
raise
|
||||
logger.error("Exception connecting to PagerDuty: {}".format(e))
|
||||
for m in messages:
|
||||
try:
|
||||
pager.trigger_incident(m.recipients()[0],
|
||||
description=m.subject,
|
||||
details=m.body,
|
||||
client=m.from_email)
|
||||
except Exception as e:
|
||||
logger.error("Exception sending messages: {}".format(e))
|
||||
if not self.fail_silently:
|
||||
raise
|
||||
return sent_messages
|
||||
@@ -41,7 +41,7 @@ class TwilioBackend(BaseEmailBackend):
|
||||
body=m.body)
|
||||
sent_messages += 1
|
||||
except Exception as e:
|
||||
logger.error("Exception sending messages: {}".format(e))
|
||||
if not self.fail_silently:
|
||||
raise
|
||||
logger.error("Exception sending messages: {}".format(e))
|
||||
return sent_messages
|
||||
|
||||
Reference in New Issue
Block a user