Catch missing 404 exceptions in client

Whenever an entity wasn't found, the ceilometerclient would issue a
NotFound exception. Which is fine, except that it's not the exception
that was being expected in the client at all. This exception was defined
in the apiclient in the openstack/common directory. So, instead this
exception is now being caught in the manager, and now the appropriate
exception is being raised.

There were some functions where, if the entity was not to be found, a
404 exception would be raised, which was never caught by the client
since it was expecting the function to return None, instead of raising.
This CR fixes that.

Closes-Bug: #1451833
Change-Id: I47b34af2df9c85f86ba1f7f4d7b2951f8d27f96c
This commit is contained in:
Juan Antonio Osorio Robles
2015-05-05 15:38:29 +03:00
parent ba02fdc1df
commit ec49113154
3 changed files with 16 additions and 6 deletions

View File

@@ -21,6 +21,7 @@ import copy
from ceilometerclient import exc from ceilometerclient import exc
from ceilometerclient.openstack.common.apiclient import base from ceilometerclient.openstack.common.apiclient import base
from ceilometerclient.openstack.common.apiclient import exceptions
# Python 2.4 compat # Python 2.4 compat
try: try:
@@ -65,7 +66,10 @@ class Manager(object):
def _list(self, url, response_key=None, obj_class=None, body=None, def _list(self, url, response_key=None, obj_class=None, body=None,
expect_single=False): expect_single=False):
try:
resp = self.api.get(url) resp = self.api.get(url)
except exceptions.NotFound:
raise exc.HTTPNotFound
if not resp.content: if not resp.content:
raise exc.HTTPNotFound raise exc.HTTPNotFound
body = resp.json() body = resp.json()

View File

@@ -511,7 +511,7 @@ class ShellSampleShowCommandTest(utils.BaseTestCase):
@mock.patch('sys.stdout', new=six.StringIO()) @mock.patch('sys.stdout', new=six.StringIO())
def test_sample_show_raises_command_err(self): def test_sample_show_raises_command_err(self):
self.cc.new_samples.get.return_value = None self.cc.new_samples.get.side_effect = exc.HTTPNotFound
self.assertRaises(exc.CommandError, ceilometer_shell.do_sample_show, self.assertRaises(exc.CommandError, ceilometer_shell.do_sample_show,
self.cc, self.args) self.cc, self.args)

View File

@@ -170,9 +170,9 @@ def _do_sample_list(cc, args):
help='ID (aka message ID) of the sample to show.') help='ID (aka message ID) of the sample to show.')
def do_sample_show(cc, args): def do_sample_show(cc, args):
'''Show an sample.''' '''Show an sample.'''
try:
sample = cc.new_samples.get(args.sample_id) sample = cc.new_samples.get(args.sample_id)
except exc.HTTPNotFound:
if sample is None:
raise exc.CommandError('Sample not found: %s' % args.sample_id) raise exc.CommandError('Sample not found: %s' % args.sample_id)
fields = ['id', 'meter', 'volume', 'type', 'unit', 'source', fields = ['id', 'meter', 'volume', 'type', 'unit', 'source',
@@ -405,6 +405,8 @@ def _display_alarm(alarm):
def do_alarm_show(cc, args={}): def do_alarm_show(cc, args={}):
"""Show an alarm.""" """Show an alarm."""
alarm = cc.alarms.get(args.alarm_id) alarm = cc.alarms.get(args.alarm_id)
# alarm.get actually catches the HTTPNotFound exception and turns the
# result into None if the alarm wasn't found.
if alarm is None: if alarm is None:
raise exc.CommandError('Alarm not found: %s' % args.alarm_id) raise exc.CommandError('Alarm not found: %s' % args.alarm_id)
else: else:
@@ -1046,7 +1048,11 @@ def do_event_list(cc, args={}):
help='The ID of the event. Should be a UUID.') help='The ID of the event. Should be a UUID.')
def do_event_show(cc, args={}): def do_event_show(cc, args={}):
"""Show a particular event.""" """Show a particular event."""
try:
event = cc.events.get(args.message_id) event = cc.events.get(args.message_id)
except exc.HTTPNotFound:
raise exc.CommandError('Event not found: %s' % args.message_id)
fields = ['event_type', 'generated', 'traits', 'raw'] fields = ['event_type', 'generated', 'traits', 'raw']
data = dict([(f, getattr(event, f, '')) for f in fields]) data = dict([(f, getattr(event, f, '')) for f in fields])
utils.print_dict(data, wrap=72) utils.print_dict(data, wrap=72)