Notification create working

This commit is contained in:
cindy oneill 2014-03-26 15:50:45 -06:00
parent 155129d30a
commit 63f12b20c4
8 changed files with 116 additions and 6 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -0,0 +1,25 @@
'''
Created on Mar 25, 2014
'''
from monclient.openstack.common.apiclient import base
class Notifications(base.Resource):
def __repr__(self):
return "<Notifications %s>" % 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

View File

@ -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='<NOTIFICATION_NAME>',
help='Name of the notification to create.', required=True)
@utils.arg('--type', metavar='<EMAIL | SMS>',
help='The notification type. Types is one of [EMAIL, SMS].',
required=True)
@utils.arg('--address', metavar='<ADDRESS>',
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))

View File

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