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
This commit is contained in:
Shinya Kawabata 2016-11-07 17:02:09 +09:00
parent 0cd115d1e1
commit dee770ec47
7 changed files with 40 additions and 31 deletions

View File

@ -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

View File

@ -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(

View File

@ -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):

View File

@ -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')

View File

@ -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<vm.list.length;i+=1){
for (var i = 0; i<vm.list.length; i+=1) {
if(vm.list[i].id === id){
vm.list.splice(i, 1);
vm.select.options.push(allOptions[id]);
@ -237,7 +237,7 @@ function NotificationField($rootScope) {
$rootScope.$on('mon_deterministic_changed', onDeterministicChange);
function prepareNotify(item){
var selected = item[7]
var selected = item[7];
var notify = {
id: item[0],
label: item[1] +' ('+ item[2] +')',
@ -258,7 +258,7 @@ function NotificationField($rootScope) {
function removeFromSelect(){
var opts = vm.select.options;
for(var i = 0;i<opts.length;i+=1){
for (var i = 0; i<opts.length; i+=1) {
if(opts[i].id === vm.select.model){
opts.splice(i, 1);
break;

View File

@ -267,7 +267,7 @@ function monAlarmSubExpressionDirective(staticPath) {
delete vm.tags;
delete vm.matchingMetrics;
delete vm.model;
}
};
}

View File

@ -275,7 +275,7 @@ tagsInput.directive('tagsInput', ["$timeout","$document","tagsInputConfig", func
ngModelCtrl.$setValidity('leftoverText', options.allowLeftoverText ? true : !scope.newTag.text);
}
else {
scope.newTag.text = '' // added by Rob to clear leftover text
scope.newTag.text = ''; // added by Rob to clear leftover text
}
});
@ -776,4 +776,4 @@ tagsInput.run(["$templateCache", function($templateCache) {
);
}]);
}());
}());