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

View File

@@ -511,7 +511,7 @@ class ShellSampleShowCommandTest(utils.BaseTestCase):
@mock.patch('sys.stdout', new=six.StringIO())
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.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.')
def do_sample_show(cc, args):
'''Show an sample.'''
sample = cc.new_samples.get(args.sample_id)
if sample is None:
try:
sample = cc.new_samples.get(args.sample_id)
except exc.HTTPNotFound:
raise exc.CommandError('Sample not found: %s' % args.sample_id)
fields = ['id', 'meter', 'volume', 'type', 'unit', 'source',
@@ -405,6 +405,8 @@ def _display_alarm(alarm):
def do_alarm_show(cc, args={}):
"""Show an alarm."""
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:
raise exc.CommandError('Alarm not found: %s' % args.alarm_id)
else:
@@ -1046,7 +1048,11 @@ def do_event_list(cc, args={}):
help='The ID of the event. Should be a UUID.')
def do_event_show(cc, args={}):
"""Show a particular event."""
event = cc.events.get(args.message_id)
try:
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']
data = dict([(f, getattr(event, f, '')) for f in fields])
utils.print_dict(data, wrap=72)