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 053e9d9f62
)
This commit is contained in:
parent
1aecce6e1d
commit
21234f40ab
@ -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.
|
||||
|
@ -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
|
||||
|
@ -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")
|
||||
|
@ -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)
|
||||
|
40
monascaclient/v2_0/notificationtypes.py
Normal file
40
monascaclient/v2_0/notificationtypes.py
Normal file
@ -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 "<NotificationTypes %s>" % 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
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user