From dee770ec47e8787d6d1b074db60a3807aa2a7f4e Mon Sep 17 00:00:00 2001 From: Shinya Kawabata Date: Mon, 7 Nov 2016 17:02:09 +0900 Subject: [PATCH] Add support for notification plugin This patch enable to select new notification types. But translation notification types will be unsupported. Note this translation constraints as bug to below launchpad, to develop in future. https://bugs.launchpad.net/monasca/+bug/1657673 Change-Id: I47dd187d2efc71a53d64307821b0420a00935bc0 --- monitoring/api/monitor.py | 5 ++++ monitoring/notifications/constants.py | 11 -------- monitoring/notifications/forms.py | 10 ++++++- monitoring/notifications/tests.py | 13 +++++++--- .../static/monitoring/js/controllers.js | 26 +++++++++---------- monitoring/static/monitoring/js/directives.js | 2 +- .../static/monitoring/js/ng-tags-input.js | 4 +-- 7 files changed, 40 insertions(+), 31 deletions(-) diff --git a/monitoring/api/monitor.py b/monitoring/api/monitor.py index 1207ca67..cbe18158 100644 --- a/monitoring/api/monitor.py +++ b/monitoring/api/monitor.py @@ -173,6 +173,11 @@ def notification_update(request, notification_id, **kwargs): update(notification_id=notification_id, **kwargs) +def notification_type_list(request, **kwargs): + result = monascaclient(request).notificationtypes.list(**kwargs) + return result['elements'] if type(result) is dict else result + + def metrics_list(request, **kwargs): result = monascaclient(request).metrics.list(**kwargs) return result['elements'] if type(result) is dict else result diff --git a/monitoring/notifications/constants.py b/monitoring/notifications/constants.py index f4d8835b..e21ee06e 100644 --- a/monitoring/notifications/constants.py +++ b/monitoring/notifications/constants.py @@ -21,17 +21,6 @@ class NotificationType(object): WEBHOOK = "WEBHOOK" PAGERDUTY = "PAGERDUTY" - CHOICES = [(EMAIL, _("Email")), - (WEBHOOK, _("Webhook")), - (PAGERDUTY, _("PagerDuty")), ] - - @staticmethod - def get_label(key): - for choice in NotificationType.CHOICES: - if choice[0] == key: - return choice[1] - return key - EMAIL_VALIDATOR = validators.EmailValidator( message=_("Address must contain a valid email address.")) WEBHOOK_VALIDATOR = validators.URLValidator( diff --git a/monitoring/notifications/forms.py b/monitoring/notifications/forms.py index d0cecdbe..edeeb042 100644 --- a/monitoring/notifications/forms.py +++ b/monitoring/notifications/forms.py @@ -12,6 +12,7 @@ # License for the specific language governing permissions and limitations # under the License. +from django.utils.functional import cached_property # noqa from django.utils.translation import ugettext_lazy as _ # noqa from horizon import exceptions @@ -41,6 +42,9 @@ class BaseNotificationMethodForm(forms.SelfHandlingForm): textWidget = readOnlyTextInput selectWidget = readOnlySelectInput + choices = [(n['type'], n['type'].capitalize()) for n in self.notification_types] + choices = sorted(choices, key=lambda c: c[0]) + self.fields['name'] = forms.CharField(label=_("Name"), required=required, max_length="250", @@ -51,7 +55,7 @@ class BaseNotificationMethodForm(forms.SelfHandlingForm): label=_("Type"), required=required, widget=selectWidget, - choices=constants.NotificationType.CHOICES, + choices=choices, initial=constants.NotificationType.EMAIL, help_text=_("The type of notification method (i.e. email).")) self.fields['address'] = forms.CharField(label=_("Address"), @@ -74,6 +78,10 @@ class BaseNotificationMethodForm(forms.SelfHandlingForm): return data['period'] + @cached_property + def notification_types(self): + return api.monitor.notification_type_list(self.request) + class CreateMethodForm(BaseNotificationMethodForm): def __init__(self, request, *args, **kwargs): diff --git a/monitoring/notifications/tests.py b/monitoring/notifications/tests.py index 0db5c366..5741195b 100644 --- a/monitoring/notifications/tests.py +++ b/monitoring/notifications/tests.py @@ -26,20 +26,27 @@ class AlarmsTest(helpers.TestCase): res, 'monitoring/notifications/index.html') def test_notifications_create(self): - res = self.client.get(CREATE_URL) + with patch('monitoring.api.monitor', **{ + 'spec_set': ['notification_type_list'], + 'notification_type_list.return_value': [], + }) as mock: + res = self.client.get(CREATE_URL) + self.assertEqual(mock. notification_type_list.call_count, 1) self.assertTemplateUsed( res, 'monitoring/notifications/_create.html') def test_notifications_edit(self): with patch('monitoring.api.monitor', **{ - 'spec_set': ['notification_get'], + 'spec_set': ['notification_get', 'notification_type_list'], 'notification_get.return_value': { 'alarm_actions': [] - } + }, + 'notification_type_list.return_value': [], }) as mock: res = self.client.get(EDIT_URL) self.assertEqual(mock.notification_get.call_count, 1) + self.assertEqual(mock.notification_type_list.call_count, 1) self.assertTemplateUsed( res, 'monitoring/notifications/_edit.html') diff --git a/monitoring/static/monitoring/js/controllers.js b/monitoring/static/monitoring/js/controllers.js index 19b52971..8769566f 100644 --- a/monitoring/static/monitoring/js/controllers.js +++ b/monitoring/static/monitoring/js/controllers.js @@ -11,20 +11,20 @@ angular.module('monitoring.controllers', []) "$scope", "$window", "$location", function($scope, $window, $location){ var offset = getTimezoneOffset(), - queryParams = urlParams() + queryParams = urlParams(); - $scope.currentFormat = undefined - $scope.currentOffset = undefined + $scope.currentFormat = undefined; + $scope.currentOffset = undefined; $scope.setUp = setUp; function setUp(currentFormat){ if(currentFormat){ - $scope.currentFormat = currentFormat + $scope.currentFormat = currentFormat; } - $scope.$watch('currentFormat', onFormatChange) + $scope.$watch('currentFormat', onFormatChange); if(queryParams['ts_mode'] === 'bl'){ - $scope.currentOffset = queryParams['ts_offset'] + $scope.currentOffset = queryParams['ts_offset']; } } @@ -36,7 +36,7 @@ angular.module('monitoring.controllers', []) // overwrite to new values queryParams['ts_mode'] = nval; - if(nval === 'utc'){ + if (nval === 'utc') { queryParams['ts_offset'] = 0; } else { queryParams['ts_offset'] = offset; @@ -158,9 +158,9 @@ function MatchByController($q, $rootScope) { } function saveDimKey() { - var matchByTags = [] + var matchByTags = []; for (var i = 0; i < vm.matchByTags.length; i++) { - matchByTags.push(vm.matchByTags[i]['text']) + matchByTags.push(vm.matchByTags[i]['text']); } $('#id_match_by').val(matchByTags.join(',')); } @@ -172,7 +172,7 @@ function MatchByController($q, $rootScope) { return function destroyer() { watcher(); - } + }; function onMatchByChange(event, matchBy) { // remove from tags those match by that do not match @@ -221,7 +221,7 @@ function NotificationField($rootScope) { } }; vm.remove = function(id){ - for(var i = 0;i