Merge "Add support for new rest api notification-type-list" into stable/newton

This commit is contained in:
Jenkins 2017-02-14 08:18:46 +00:00 committed by Gerrit Code Review
commit 1a59a043b5
6 changed files with 89 additions and 40 deletions

View File

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

View File

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

View File

@ -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()
@ -379,3 +359,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")

View File

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

View 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

View File

@ -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 or args.period == 0:
@ -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)