Skip to content

Commit 6463b25

Browse files
Jenkinsopenstack-gerrit
authored andcommitted
Merge "Add support for notification plugin"
2 parents 536e0c2 + dee770e commit 6463b25

7 files changed

Lines changed: 40 additions & 31 deletions

File tree

monitoring/api/monitor.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,11 @@ def notification_update(request, notification_id, **kwargs):
173173
update(notification_id=notification_id, **kwargs)
174174

175175

176+
def notification_type_list(request, **kwargs):
177+
result = monascaclient(request).notificationtypes.list(**kwargs)
178+
return result['elements'] if type(result) is dict else result
179+
180+
176181
def metrics_list(request, **kwargs):
177182
result = monascaclient(request).metrics.list(**kwargs)
178183
return result['elements'] if type(result) is dict else result

monitoring/notifications/constants.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,6 @@ class NotificationType(object):
2121
WEBHOOK = "WEBHOOK"
2222
PAGERDUTY = "PAGERDUTY"
2323

24-
CHOICES = [(EMAIL, _("Email")),
25-
(WEBHOOK, _("Webhook")),
26-
(PAGERDUTY, _("PagerDuty")), ]
27-
28-
@staticmethod
29-
def get_label(key):
30-
for choice in NotificationType.CHOICES:
31-
if choice[0] == key:
32-
return choice[1]
33-
return key
34-
3524
EMAIL_VALIDATOR = validators.EmailValidator(
3625
message=_("Address must contain a valid email address."))
3726
WEBHOOK_VALIDATOR = validators.URLValidator(

monitoring/notifications/forms.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# License for the specific language governing permissions and limitations
1313
# under the License.
1414

15+
from django.utils.functional import cached_property # noqa
1516
from django.utils.translation import ugettext_lazy as _ # noqa
1617

1718
from horizon import exceptions
@@ -41,6 +42,9 @@ def _init_fields(self, readOnly=False, create=False):
4142
textWidget = readOnlyTextInput
4243
selectWidget = readOnlySelectInput
4344

45+
choices = [(n['type'], n['type'].capitalize()) for n in self.notification_types]
46+
choices = sorted(choices, key=lambda c: c[0])
47+
4448
self.fields['name'] = forms.CharField(label=_("Name"),
4549
required=required,
4650
max_length="250",
@@ -51,7 +55,7 @@ def _init_fields(self, readOnly=False, create=False):
5155
label=_("Type"),
5256
required=required,
5357
widget=selectWidget,
54-
choices=constants.NotificationType.CHOICES,
58+
choices=choices,
5559
initial=constants.NotificationType.EMAIL,
5660
help_text=_("The type of notification method (i.e. email)."))
5761
self.fields['address'] = forms.CharField(label=_("Address"),
@@ -74,6 +78,10 @@ def clean_period(self):
7478

7579
return data['period']
7680

81+
@cached_property
82+
def notification_types(self):
83+
return api.monitor.notification_type_list(self.request)
84+
7785

7886
class CreateMethodForm(BaseNotificationMethodForm):
7987
def __init__(self, request, *args, **kwargs):

monitoring/notifications/tests.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,27 @@ def test_index(self):
3838
res, 'monitoring/notifications/index.html')
3939

4040
def test_notifications_create(self):
41-
res = self.client.get(CREATE_URL)
41+
with patch('monitoring.api.monitor', **{
42+
'spec_set': ['notification_type_list'],
43+
'notification_type_list.return_value': [],
44+
}) as mock:
45+
res = self.client.get(CREATE_URL)
46+
self.assertEqual(mock. notification_type_list.call_count, 1)
4247

4348
self.assertTemplateUsed(
4449
res, 'monitoring/notifications/_create.html')
4550

4651
def test_notifications_edit(self):
4752
with patch('monitoring.api.monitor', **{
48-
'spec_set': ['notification_get'],
53+
'spec_set': ['notification_get', 'notification_type_list'],
4954
'notification_get.return_value': {
5055
'alarm_actions': []
51-
}
56+
},
57+
'notification_type_list.return_value': [],
5258
}) as mock:
5359
res = self.client.get(EDIT_URL)
5460
self.assertEqual(mock.notification_get.call_count, 1)
61+
self.assertEqual(mock.notification_type_list.call_count, 1)
5562

5663
self.assertTemplateUsed(
5764
res, 'monitoring/notifications/_edit.html')

monitoring/static/monitoring/js/controllers.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,20 @@ angular.module('monitoring.controllers', [])
1111
"$scope", "$window", "$location",
1212
function($scope, $window, $location){
1313
var offset = getTimezoneOffset(),
14-
queryParams = urlParams()
14+
queryParams = urlParams();
1515

16-
$scope.currentFormat = undefined
17-
$scope.currentOffset = undefined
16+
$scope.currentFormat = undefined;
17+
$scope.currentOffset = undefined;
1818

1919
$scope.setUp = setUp;
2020

2121
function setUp(currentFormat){
2222
if(currentFormat){
23-
$scope.currentFormat = currentFormat
23+
$scope.currentFormat = currentFormat;
2424
}
25-
$scope.$watch('currentFormat', onFormatChange)
25+
$scope.$watch('currentFormat', onFormatChange);
2626
if(queryParams['ts_mode'] === 'bl'){
27-
$scope.currentOffset = queryParams['ts_offset']
27+
$scope.currentOffset = queryParams['ts_offset'];
2828
}
2929
}
3030

@@ -36,7 +36,7 @@ angular.module('monitoring.controllers', [])
3636

3737
// overwrite to new values
3838
queryParams['ts_mode'] = nval;
39-
if(nval === 'utc'){
39+
if (nval === 'utc') {
4040
queryParams['ts_offset'] = 0;
4141
} else {
4242
queryParams['ts_offset'] = offset;
@@ -158,9 +158,9 @@ function MatchByController($q, $rootScope) {
158158
}
159159

160160
function saveDimKey() {
161-
var matchByTags = []
161+
var matchByTags = [];
162162
for (var i = 0; i < vm.matchByTags.length; i++) {
163-
matchByTags.push(vm.matchByTags[i]['text'])
163+
matchByTags.push(vm.matchByTags[i]['text']);
164164
}
165165
$('#id_match_by').val(matchByTags.join(','));
166166
}
@@ -172,7 +172,7 @@ function MatchByController($q, $rootScope) {
172172

173173
return function destroyer() {
174174
watcher();
175-
}
175+
};
176176

177177
function onMatchByChange(event, matchBy) {
178178
// remove from tags those match by that do not match
@@ -221,7 +221,7 @@ function NotificationField($rootScope) {
221221
}
222222
};
223223
vm.remove = function(id){
224-
for(var i = 0;i<vm.list.length;i+=1){
224+
for (var i = 0; i<vm.list.length; i+=1) {
225225
if(vm.list[i].id === id){
226226
vm.list.splice(i, 1);
227227
vm.select.options.push(allOptions[id]);
@@ -237,7 +237,7 @@ function NotificationField($rootScope) {
237237
$rootScope.$on('mon_deterministic_changed', onDeterministicChange);
238238

239239
function prepareNotify(item){
240-
var selected = item[7]
240+
var selected = item[7];
241241
var notify = {
242242
id: item[0],
243243
label: item[1] +' ('+ item[2] +')',
@@ -258,7 +258,7 @@ function NotificationField($rootScope) {
258258

259259
function removeFromSelect(){
260260
var opts = vm.select.options;
261-
for(var i = 0;i<opts.length;i+=1){
261+
for (var i = 0; i<opts.length; i+=1) {
262262
if(opts[i].id === vm.select.model){
263263
opts.splice(i, 1);
264264
break;

monitoring/static/monitoring/js/directives.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ function monAlarmSubExpressionDirective(staticPath) {
267267
delete vm.tags;
268268
delete vm.matchingMetrics;
269269
delete vm.model;
270-
}
270+
};
271271

272272
}
273273

monitoring/static/monitoring/js/ng-tags-input.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ tagsInput.directive('tagsInput', ["$timeout","$document","tagsInputConfig", func
275275
ngModelCtrl.$setValidity('leftoverText', options.allowLeftoverText ? true : !scope.newTag.text);
276276
}
277277
else {
278-
scope.newTag.text = '' // added by Rob to clear leftover text
278+
scope.newTag.text = ''; // added by Rob to clear leftover text
279279
}
280280
});
281281

@@ -776,4 +776,4 @@ tagsInput.run(["$templateCache", function($templateCache) {
776776
);
777777
}]);
778778

779-
}());
779+
}());

0 commit comments

Comments
 (0)