From 21234f40ab6762122d764dc92faa014db827bf33 Mon Sep 17 00:00:00 2001 From: haali1 Date: Mon, 11 Jul 2016 15:48:49 -0700 Subject: [PATCH] Add support for new rest api notification-type-list As part of this blueprint we have added a new rest api GET /v2.0/notification-methods/types. This patch adds support for that api in the client. Change-Id: I322d1277c1859c990e150f22eceff122ffd77cea Partially-implements: blueprint notification-engine-plugin (cherry picked from commit 053e9d9f6202d5fb9f9843cb77ebc6ad2d0f722f) --- monascaclient/client.py | 2 +- monascaclient/tests/fakes.py | 4 +-- monascaclient/tests/test_shell.py | 42 ++++++++++++------------- monascaclient/v2_0/client.py | 3 ++ monascaclient/v2_0/notificationtypes.py | 40 +++++++++++++++++++++++ monascaclient/v2_0/shell.py | 42 ++++++++++++++----------- 6 files changed, 91 insertions(+), 42 deletions(-) create mode 100644 monascaclient/v2_0/notificationtypes.py diff --git a/monascaclient/client.py b/monascaclient/client.py index cccd323..a86ea60 100644 --- a/monascaclient/client.py +++ b/monascaclient/client.py @@ -1,4 +1,4 @@ -# (C) Copyright 2014-2015 Hewlett Packard Enterprise Development Company LP +# (C) Copyright 2014-2016 Hewlett Packard Enterprise Development LP # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/monascaclient/tests/fakes.py b/monascaclient/tests/fakes.py index 25574c4..a2921f8 100644 --- a/monascaclient/tests/fakes.py +++ b/monascaclient/tests/fakes.py @@ -1,4 +1,4 @@ -# Copyright (c) 2014 Hewlett-Packard Development Company, L.P. +# (C) Copyright 2014,2016 Hewlett Packard Enterprise Development LP # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -62,7 +62,7 @@ class FakeHTTPResponse(object): version = 1.1 - def __init__(self, status_code, reason, headers, content): + def __init__(self, status_code=None, reason=None, headers=None, content=None): self.headers = headers self.content = content self.status_code = status_code diff --git a/monascaclient/tests/test_shell.py b/monascaclient/tests/test_shell.py index 76a9f93..1a7a027 100644 --- a/monascaclient/tests/test_shell.py +++ b/monascaclient/tests/test_shell.py @@ -1,4 +1,4 @@ -# (C) Copyright 2014-2016 Hewlett Packard Enterprise Development Company LP +# (C) Copyright 2014-2016 Hewlett Packard Enterprise Development LP # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -207,26 +207,6 @@ class ShellTestMonascaCommands(ShellBase): for argstr in argstrings: self.assertRaises(SystemExit, _shell.main, argstr.split()) - def test_bad_notifications_create_type_subcommand(self): - self._script_keystone_client() - argstrings = [ - 'notification-create email1 DOG metric1@hp.com', - ] - self.m.ReplayAll() - for argstr in argstrings: - retvalue = self.shell(argstr) - self.assertRegexpMatches(retvalue, "^Invalid type") - - def test_notifications_create_type_sms(self): - self._script_keystone_client() - argstrings = [ - 'notification-create sms1 SMS myphonenumber', - ] - self.m.ReplayAll() - for argstr in argstrings: - retvalue = self.shell(argstr) - self.assertRegexpMatches(retvalue, "^Invalid type") - def test_good_notifications_create_subcommand(self): self._script_keystone_client() @@ -319,3 +299,23 @@ class ShellTestMonascaCommands(ShellBase): argstring = " ".join(args) retvalue = self.shell(argstring) self.assertRegexpMatches(retvalue, "id") + + def test_notifications_types_list(self): + self._script_keystone_client() + + resp_body = [{"type": "WEBHOOK"}, {"type": "EMAIL"}, {"type": "PAGERDUTY"}] + resp = fakes.FakeHTTPResponse( + status_code=200, + content=resp_body) + http.HTTPClient.json_request( + 'GET', + '/notification-methods/types', + headers={'X-Auth-Key': 'password', + 'X-Auth-User': 'username'}).AndReturn(((resp, resp_body))) + + self.m.ReplayAll() + + argstrings = ["notification-type-list"] + + retvalue = self.shell("".join(argstrings)) + self.assertRegexpMatches(retvalue, "types") diff --git a/monascaclient/v2_0/client.py b/monascaclient/v2_0/client.py index e267333..3031073 100644 --- a/monascaclient/v2_0/client.py +++ b/monascaclient/v2_0/client.py @@ -19,6 +19,7 @@ from monascaclient.v2_0 import alarm_definitions from monascaclient.v2_0 import alarms from monascaclient.v2_0 import metrics from monascaclient.v2_0 import notifications +from monascaclient.v2_0 import notificationtypes class Client(object): @@ -44,6 +45,8 @@ class Client(object): self.alarms = alarms.AlarmsManager(self.http_client) self.alarm_definitions = alarm_definitions.AlarmDefinitionsManager( self.http_client) + self.notificationtypes = notificationtypes.NotificationTypesManager( + self.http_client) def replace_token(self, token): self.http_client.replace_token(token) diff --git a/monascaclient/v2_0/notificationtypes.py b/monascaclient/v2_0/notificationtypes.py new file mode 100644 index 0000000..8b21a28 --- /dev/null +++ b/monascaclient/v2_0/notificationtypes.py @@ -0,0 +1,40 @@ +# (C) Copyright 2016 Hewlett Packard Enterprise Development LP +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from monascaclient.common import monasca_manager +from monascaclient.openstack.common.apiclient import base +from monascaclient.openstack.common.py3kcompat import urlutils + + +class NotificationTypes(base.Resource): + + def __repr__(self): + return "" % self._info + + +class NotificationTypesManager(monasca_manager.MonascaManager): + resource_class = NotificationTypes + base_url = '/notification-methods/types' + + def list(self, **kwargs): + """Get a list of notifications.""" + newheaders = self.get_headers() + url_str = self.base_url + if kwargs: + url_str = url_str + '?%s' % urlutils.urlencode(kwargs, True) + + resp, body = self.client.json_request('GET', url_str, + headers=newheaders) + return body['elements'] if type(body) is dict else body diff --git a/monascaclient/v2_0/shell.py b/monascaclient/v2_0/shell.py index a8f3333..589c0f5 100644 --- a/monascaclient/v2_0/shell.py +++ b/monascaclient/v2_0/shell.py @@ -1,4 +1,4 @@ -# (C) Copyright 2014-2016 Hewlett Packard Enterprise Development Company LP +# (C) Copyright 2014-2016 Hewlett Packard Enterprise Development LP # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -507,11 +507,7 @@ def _validate_notification_period(period, notification_type): help='A period for the notification method. Can only be non zero with webhooks') def do_notification_create(mc, args): '''Create notification.''' - if args.type.upper() not in notification_types: - errmsg = ('Invalid type, not one of [' + - ', '.join(notification_types) + ']') - print(errmsg) - return + fields = {} fields['name'] = args.name fields['type'] = args.type @@ -648,11 +644,7 @@ def do_notification_update(mc, args): fields = {} fields['notification_id'] = args.id fields['name'] = args.name - if args.type.upper() not in notification_types: - errmsg = ('Invalid type, not one of [' + - ', '.join(state_types) + ']') - print(errmsg) - return + fields['type'] = args.type fields['address'] = args.address if not _validate_notification_period(args.period, args.type.upper()): @@ -684,13 +676,8 @@ def do_notification_patch(mc, args): fields['notification_id'] = args.id if args.name: fields['name'] = args.name - if args.type: - if args.type.upper() not in notification_types: - errmsg = ('Invalid type, not one of [' + - ', '.join(notification_types) + ']') - print(errmsg) - return - fields['type'] = args.type + + fields['type'] = args.type if args.address: fields['address'] = args.address if args.period: @@ -1400,6 +1387,25 @@ def do_alarm_history_list(mc, args): output_alarm_history(args, alarm) +def do_notification_type_list(mc, args): + '''List notification types supported by monasca.''' + + try: + notification_types = mc.notificationtypes.list() + except exc.HTTPException as he: + raise exc.CommandError( + 'HTTPException code=%s message=%s' % + (he.code, he.message)) + else: + if args.json: + print(utils.json_formatter(notification_types)) + return + else: + formatters = {'types': lambda x: x["type"]} + # utils.print_list(notification_types['types'], ["types"], formatters=formatters) + utils.print_list(notification_types, ["types"], formatters=formatters) + + def _translate_starttime(args): if args.starttime[0] == '-': deltaT = time.time() + (int(args.starttime) * 60)