From ec491131546f8a9d50c912d3f8f81587937e50e6 Mon Sep 17 00:00:00 2001 From: Juan Antonio Osorio Robles Date: Tue, 5 May 2015 15:38:29 +0300 Subject: 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 --- ceilometerclient/common/base.py | 6 +++++- ceilometerclient/tests/unit/v2/test_shell.py | 2 +- ceilometerclient/v2/shell.py | 14 ++++++++++---- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/ceilometerclient/common/base.py b/ceilometerclient/common/base.py index 7e8f80f..19d4e8a 100644 --- a/ceilometerclient/common/base.py +++ b/ceilometerclient/common/base.py @@ -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() diff --git a/ceilometerclient/tests/unit/v2/test_shell.py b/ceilometerclient/tests/unit/v2/test_shell.py index 109cb56..f568ebd 100644 --- a/ceilometerclient/tests/unit/v2/test_shell.py +++ b/ceilometerclient/tests/unit/v2/test_shell.py @@ -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) diff --git a/ceilometerclient/v2/shell.py b/ceilometerclient/v2/shell.py index 5e900a8..81e2ae0 100644 --- a/ceilometerclient/v2/shell.py +++ b/ceilometerclient/v2/shell.py @@ -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) -- cgit v1.2.1