From 172207cd4ba129a5d1f4f654d1320bb25c9e1f15 Mon Sep 17 00:00:00 2001 From: Matthew Jones Date: Thu, 4 Feb 2016 11:30:40 -0500 Subject: [PATCH] Notification endpoints and url expositions Also some changes to the footprint of the notification handler classes --- awx/api/urls.py | 6 ++++++ awx/api/views.py | 13 +++++++++++++ awx/main/access.py | 13 +++++++++++++ awx/main/models/notifications.py | 8 ++++++++ awx/main/notifications/email_backend.py | 9 +++++++-- awx/main/notifications/slack_backend.py | 2 +- awx/main/notifications/twilio_backend.py | 4 +++- 7 files changed, 51 insertions(+), 4 deletions(-) diff --git a/awx/api/urls.py b/awx/api/urls.py index 2b3a93d852..8e48250560 100644 --- a/awx/api/urls.py +++ b/awx/api/urls.py @@ -209,6 +209,11 @@ system_job_urls = patterns('awx.api.views', url(r'^(?P[0-9]+)/cancel/$', 'system_job_cancel'), ) +notification_template_urls = patterns('awx.api.views', + url(r'^$', 'notification_template_list'), + url(r'^(?P[0-9]+)/$', 'notification_template_detail'), +) + schedule_urls = patterns('awx.api.views', url(r'^$', 'schedule_list'), url(r'^(?P[0-9]+)/$', 'schedule_detail'), @@ -257,6 +262,7 @@ v1_urls = patterns('awx.api.views', url(r'^ad_hoc_command_events/', include(ad_hoc_command_event_urls)), url(r'^system_job_templates/', include(system_job_template_urls)), url(r'^system_jobs/', include(system_job_urls)), + url(r'^notification_templates/', include(notification_template_urls)), url(r'^unified_job_templates/$', 'unified_job_template_list'), url(r'^unified_jobs/$', 'unified_job_list'), url(r'^activity_stream/', include(activity_stream_urls)), diff --git a/awx/api/views.py b/awx/api/views.py index 9a41e779ea..72e1fb606e 100644 --- a/awx/api/views.py +++ b/awx/api/views.py @@ -135,6 +135,7 @@ class ApiV1RootView(APIView): data['system_job_templates'] = reverse('api:system_job_template_list') data['system_jobs'] = reverse('api:system_job_list') data['schedules'] = reverse('api:schedule_list') + data['notification_templates'] = reverse('api:notification_template_list') data['unified_job_templates'] = reverse('api:unified_job_template_list') data['unified_jobs'] = reverse('api:unified_job_list') data['activity_stream'] = reverse('api:activity_stream_list') @@ -2919,6 +2920,18 @@ class AdHocCommandStdout(UnifiedJobStdout): model = AdHocCommand new_in_220 = True +class NotificationTemplateList(ListCreateAPIView): + + model = NotificationTemplate + serializer_class = NotificationTemplateSerializer + new_in_300 = True + +class NotificationTemplateDetail(RetrieveDestroyAPIView): + + model = NotificationTemplate + serializer_class = NotificationTemplateSerializer + new_in_300 = True + class ActivityStreamList(SimpleListAPIView): model = ActivityStream diff --git a/awx/main/access.py b/awx/main/access.py index e17fc59b02..e4ef4653a7 100644 --- a/awx/main/access.py +++ b/awx/main/access.py @@ -1484,6 +1484,18 @@ class ScheduleAccess(BaseAccess): else: return False +class NotificationTemplateAccess(BaseAccess): + ''' + I can see/use a notification template if I have permission to + ''' + model = NotificationTemplate + + def get_queryset(self): + qs = self.model.objects.filter(active=True).distinct() + if self.user.is_superuser: + return qs + return qs + class ActivityStreamAccess(BaseAccess): ''' I can see activity stream events only when I have permission on all objects included in the event @@ -1683,3 +1695,4 @@ register_access(UnifiedJob, UnifiedJobAccess) register_access(ActivityStream, ActivityStreamAccess) register_access(CustomInventoryScript, CustomInventoryScriptAccess) register_access(TowerSettings, TowerSettingsAccess) +register_access(NotificationTemplate, NotificationTemplateAccess) diff --git a/awx/main/models/notifications.py b/awx/main/models/notifications.py index e2539edc4b..81c5b31e7f 100644 --- a/awx/main/models/notifications.py +++ b/awx/main/models/notifications.py @@ -4,6 +4,9 @@ import logging from django.db import models +from django.core.urlresolvers import reverse +from django.utils.translation import ugettext_lazy as _ + from awx.main.models.base import * # noqa from awx.main.notifications.email_backend import CustomEmailBackend from awx.main.notifications.slack_backend import SlackBackend @@ -14,6 +17,8 @@ from jsonfield import JSONField logger = logging.getLogger('awx.main.models.notifications') +__all__ = ['NotificationTemplate'] + class NotificationTemplate(CommonModel): NOTIFICATION_TYPES = [('email', _('Email'), CustomEmailBackend), @@ -32,6 +37,9 @@ class NotificationTemplate(CommonModel): notification_configuration = JSONField(blank=False) + def get_absolute_url(self): + return reverse('api:notification_template_detail', args=(self.pk,)) + @property def notification_class(self): return CLASS_FOR_NOTIFICATION_TYPE[self.notification_type] diff --git a/awx/main/notifications/email_backend.py b/awx/main/notifications/email_backend.py index 47c6c6dd00..db0a8b3c2f 100644 --- a/awx/main/notifications/email_backend.py +++ b/awx/main/notifications/email_backend.py @@ -7,5 +7,10 @@ from django.core.mail.backends.smtp import EmailBackend class CustomEmailBackend(EmailBackend): - init_parameters = ("host", "port", "username", "password", - "use_tls", "use_ssl") + init_parameters = {"host": {"label": "Host", "type": "string"}, + "port": {"label": "Port", "type": "int"}, + "username": {"label": "Username", "type": "string"}, + "password": {"label": "Password", "type": "password"}, + "use_tls": {"label": "Use TLS", "type": "bool"}, + "use_ssl": {"label": "Use SSL", "type": "bool"}} + diff --git a/awx/main/notifications/slack_backend.py b/awx/main/notifications/slack_backend.py index 07c0f2c4fc..84ae60c3cb 100644 --- a/awx/main/notifications/slack_backend.py +++ b/awx/main/notifications/slack_backend.py @@ -10,7 +10,7 @@ logger = logging.getLogger('awx.main.notifications.slack_backend') class SlackBackend(BaseEmailBackend): - init_parameters = ('token',) + init_parameters = {"token": {"label": "Token", "type": "password"}} def __init__(self, token, fail_silently=False, **kwargs): super(SlackBackend, self).__init__(fail_silently=fail_silently) diff --git a/awx/main/notifications/twilio_backend.py b/awx/main/notifications/twilio_backend.py index 86d6829c09..d0d2fbfe76 100644 --- a/awx/main/notifications/twilio_backend.py +++ b/awx/main/notifications/twilio_backend.py @@ -11,7 +11,9 @@ logger = logging.getLogger('awx.main.notifications.twilio_backend') class TwilioBackend(BaseEmailBackend): - init_parameters = ('account_sid', 'account_token', 'from_phone',) + init_parameters = {"account_sid": {"label": "Account SID", "type": "string"}, + "account_token": {"label": "Account Token", "type": "password"}, + "from_phone": {"label": "Source Phone Number", "type": "string"}} def __init__(self, account_sid, account_token, from_phone, fail_silently=False, **kwargs): super(TwilioBackend, self).__init__(fail_silently=fail_silently)