mirror of
https://github.com/ansible/awx.git
synced 2026-03-07 19:51:08 -03:30
Add webhook notification backend
This commit is contained in:
@@ -15,6 +15,7 @@ from awx.main.notifications.slack_backend import SlackBackend
|
|||||||
from awx.main.notifications.twilio_backend import TwilioBackend
|
from awx.main.notifications.twilio_backend import TwilioBackend
|
||||||
from awx.main.notifications.pagerduty_backend import PagerDutyBackend
|
from awx.main.notifications.pagerduty_backend import PagerDutyBackend
|
||||||
from awx.main.notifications.hipchat_backend import HipChatBackend
|
from awx.main.notifications.hipchat_backend import HipChatBackend
|
||||||
|
from awx.main.notifications.webhook_backend import WebhookBackend
|
||||||
|
|
||||||
# Django-JSONField
|
# Django-JSONField
|
||||||
from jsonfield import JSONField
|
from jsonfield import JSONField
|
||||||
@@ -29,7 +30,8 @@ class NotificationTemplate(CommonModel):
|
|||||||
('slack', _('Slack'), SlackBackend),
|
('slack', _('Slack'), SlackBackend),
|
||||||
('twilio', _('Twilio'), TwilioBackend),
|
('twilio', _('Twilio'), TwilioBackend),
|
||||||
('pagerduty', _('Pagerduty'), PagerDutyBackend),
|
('pagerduty', _('Pagerduty'), PagerDutyBackend),
|
||||||
('hipchat', _('HipChat'), HipChatBackend)]
|
('hipchat', _('HipChat'), HipChatBackend),
|
||||||
|
('webhook', _('Webhook'), WebhookBackend)]
|
||||||
NOTIFICATION_TYPE_CHOICES = [(x[0], x[1]) for x in NOTIFICATION_TYPES]
|
NOTIFICATION_TYPE_CHOICES = [(x[0], x[1]) for x in NOTIFICATION_TYPES]
|
||||||
CLASS_FOR_NOTIFICATION_TYPE = dict([(x[0], x[2]) for x in NOTIFICATION_TYPES])
|
CLASS_FOR_NOTIFICATION_TYPE = dict([(x[0], x[2]) for x in NOTIFICATION_TYPES])
|
||||||
|
|
||||||
|
|||||||
@@ -39,8 +39,9 @@ class HipChatBackend(BaseEmailBackend):
|
|||||||
"notify": self.notify,
|
"notify": self.notify,
|
||||||
"from": m.from_email,
|
"from": m.from_email,
|
||||||
"message_format": "text"})
|
"message_format": "text"})
|
||||||
if r.status_code != 204 and not self.fail_silently:
|
if r.status_code != 204:
|
||||||
logger.error("Error sending messages: {}".format(r.text))
|
logger.error("Error sending messages: {}".format(r.text))
|
||||||
raise Exception("Error sending message to hipchat: {}".format(r.text))
|
if not self.fail_silently:
|
||||||
sent_messages += 1
|
raise Exception("Error sending message to hipchat: {}".format(r.text))
|
||||||
|
sent_messages += 1
|
||||||
return sent_messages
|
return sent_messages
|
||||||
|
|||||||
32
awx/main/notifications/webhook_backend.py
Normal file
32
awx/main/notifications/webhook_backend.py
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
# 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.webhook_backend')
|
||||||
|
|
||||||
|
class WebhookBackend(BaseEmailBackend):
|
||||||
|
|
||||||
|
init_parameters = {"url": {"label": "Target URL", "type": "string"},
|
||||||
|
"headers": {"label": "HTTP Headers", "type": "object"}}
|
||||||
|
recipient_parameter = "url"
|
||||||
|
sender_parameter = None
|
||||||
|
|
||||||
|
def __init__(self, headers, fail_silently=False, **kwargs):
|
||||||
|
super(WebhookBackend, self).__init__(fail_silently=fail_silently)
|
||||||
|
|
||||||
|
def send_messages(self, messages):
|
||||||
|
sent_messages = 0
|
||||||
|
for m in messages:
|
||||||
|
r = requests.post("{}".format(m.recipients()[0]),
|
||||||
|
headers=self.headers)
|
||||||
|
if r.status_code >= 400:
|
||||||
|
logger.error("Error sending notification webhook: {}".format(r.text))
|
||||||
|
if not self.fail_silently:
|
||||||
|
raise Exception("Error sending notification webhook: {}".format(r.text))
|
||||||
|
sent_messages += 1
|
||||||
|
return sent_messages
|
||||||
Reference in New Issue
Block a user