From 63f12b20c4016476e53997d9e7be97c252025606 Mon Sep 17 00:00:00 2001 From: cindy oneill Date: Wed, 26 Mar 2014 15:50:45 -0600 Subject: [PATCH] Notification create working --- README.rst | 2 +- monclient/shell.py | 2 +- monclient/tests/test_shell.py | 48 +++++++++++++++++++++++++++++++-- monclient/v2_0/client.py | 3 +++ monclient/v2_0/metrics.py | 3 +-- monclient/v2_0/notifications.py | 25 +++++++++++++++++ monclient/v2_0/shell.py | 38 ++++++++++++++++++++++++++ test-requirements.txt | 1 + 8 files changed, 116 insertions(+), 6 deletions(-) create mode 100644 monclient/v2_0/notifications.py diff --git a/README.rst b/README.rst index 9570c67..6493b01 100644 --- a/README.rst +++ b/README.rst @@ -1,6 +1,6 @@ Python bindings to the Monitoring API ============================================= -This is a client library for Monitoring built on the Heat orchestration API. It +This is a client library for Monitoring built to interface to the Monitoring API. It provides a Python API (the ``monclient`` module) and a command-line tool (``mon``). diff --git a/monclient/shell.py b/monclient/shell.py index 1cd47b7..13fc55c 100644 --- a/monclient/shell.py +++ b/monclient/shell.py @@ -51,7 +51,7 @@ class MonShell(object): parser.add_argument('--runlocal', action='store_true', - help=argparse.SUPPRESS) + help='test with a local mon-api (no keystone)') parser.add_argument('--version', action='version', diff --git a/monclient/tests/test_shell.py b/monclient/tests/test_shell.py index 8f4eb45..f1485fc 100644 --- a/monclient/tests/test_shell.py +++ b/monclient/tests/test_shell.py @@ -164,12 +164,12 @@ class ShellTestMonCommands(ShellBase): 'metrics-create --value 123', 'metrics-create', ] + _shell = monclient.shell.MonShell() for argstr in argstrings: - self.assertRaises(Exception, monclient.shell, argstr.split()) + self.assertRaises(SystemExit, _shell.main, argstr.split()) def test_good_metrics_create_subcommand(self): self._script_keystone_client() - # fakes.script_metrics_create() resp = fakes.FakeHTTPResponse( 204, @@ -194,3 +194,47 @@ class ShellTestMonCommands(ShellBase): for argstr in argstrings: retvalue = self.shell(argstr) self.assertRegexpMatches(retvalue, "^Success") + + def test_bad_notifications_create_missing_args_subcommand(self): + argstrings = [ + 'notifications-create --name email1 --address cindy.o-neill@hp.com', + ] + _shell = monclient.shell.MonShell() + for argstr in argstrings: + self.assertRaises(SystemExit, _shell.main, argstr.split()) + + def test_bad_notifications_create_type_subcommand(self): + self._script_keystone_client() + argstrings = [ + 'notifications-create --name email1 --type DOG --address cindy.o-neill@hp.com', + ] + 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() + + resp = fakes.FakeHTTPResponse( + 201, + 'Created', + {'location': 'http://no.where/v2.0/notification-methods'}, + None) + http.HTTPClient.json_request( + 'POST', + '/notification-methods', + data={'name': 'email1', + 'type': 'EMAIL', + 'address': 'john.doe@hp.com'}, + headers={'X-Auth-Key': 'password', + 'X-Auth-User': 'username'}).AndReturn((resp, 'id')) + + self.m.ReplayAll() + + argstrings = [ + 'notifications-create --name email1 --type EMAIL --address john.doe@hp.com', + ] + for argstr in argstrings: + retvalue = self.shell(argstr) + self.assertRegexpMatches(retvalue, "id") diff --git a/monclient/v2_0/client.py b/monclient/v2_0/client.py index 97e4fb8..4acd2bd 100644 --- a/monclient/v2_0/client.py +++ b/monclient/v2_0/client.py @@ -15,6 +15,7 @@ from monclient.common import http from monclient.v2_0 import metrics +from monclient.v2_0 import notifications class Client(object): @@ -32,3 +33,5 @@ class Client(object): """Initialize a new http client for the mon API.""" self.http_client = http.HTTPClient(*args, **kwargs) self.metrics = metrics.MetricsManager(self.http_client) + self.notifications = notifications.NotificationsManager( + self.http_client) diff --git a/monclient/v2_0/metrics.py b/monclient/v2_0/metrics.py index 8f84797..c334350 100644 --- a/monclient/v2_0/metrics.py +++ b/monclient/v2_0/metrics.py @@ -32,5 +32,4 @@ class MetricsManager(base.BaseManager): headers['X-Tenant-Id'] = '1' resp, body = self.client.json_request('POST', '/metrics', data=kwargs, headers=headers) - # return body - return resp + return body diff --git a/monclient/v2_0/notifications.py b/monclient/v2_0/notifications.py new file mode 100644 index 0000000..d42e6d2 --- /dev/null +++ b/monclient/v2_0/notifications.py @@ -0,0 +1,25 @@ +''' +Created on Mar 25, 2014 + +''' +from monclient.openstack.common.apiclient import base + + +class Notifications(base.Resource): + + def __repr__(self): + return "" % self._info + + +class NotificationsManager(base.BaseManager): + resource_class = Notifications + + def create(self, runlocal=False, **kwargs): + """Create a notification.""" + headers = self.client.credentials_headers() + if runlocal: + # temp header, used when running locally. + headers['X-Tenant-Id'] = '1' + resp, body = self.client.json_request('POST', '/notification-methods', + data=kwargs, headers=headers) + return body diff --git a/monclient/v2_0/shell.py b/monclient/v2_0/shell.py index c9c76a7..a326287 100644 --- a/monclient/v2_0/shell.py +++ b/monclient/v2_0/shell.py @@ -15,6 +15,7 @@ from monclient.common import utils import monclient.exc as exc +from monclient.openstack.common import jsonutils import time @@ -55,3 +56,40 @@ def do_metrics_create(mc, args): raise else: print('Successfully created metric') + + +@utils.arg('--name', metavar='', + help='Name of the notification to create.', required=True) +@utils.arg('--type', metavar='', + help='The notification type. Types is one of [EMAIL, SMS].', + required=True) +@utils.arg('--address', metavar='
', + help='Depending on the type, a valid EMAIL or SMS Address', + required=True) +def do_notifications_create(mc, args): + '''Create notification.''' + notification_types = ['EMAIL', 'SMS'] + if args.type 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 + fields['address'] = args.address + try: + notification = mc.notifications.create(args.runlocal, **fields) + except exc.HTTPInternalServerError as e1: + raise exc.CommandError('HTTPInternalServerError %s' % e1.code) + except exc.BadRequest as e2: + raise exc.CommandError('BadRequest %s' % e2.code) + except exc.Unauthorized as e3: + raise exc.CommandError('Unauthorized %s' % e3.code) + except exc.HTTPConflict as e4: + raise exc.CommandError('Conflict %s' % e4.code) + except Exception: + print('Command Failed. Please use the -d option for more details.') + raise + else: + print(jsonutils.dumps(notification, indent=2)) diff --git a/test-requirements.txt b/test-requirements.txt index 8202de3..4f713bd 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -1,4 +1,5 @@ # Hacking already pins down pep8, pyflakes and flake8 +tox>=1.6,<1.7 hacking>=0.8.0,<0.9 coverage>=3.6 discover