From fbb1eb9285c7787f8508d3f69790522ff6847fc8 Mon Sep 17 00:00:00 2001 From: Fei Long Wang Date: Mon, 1 Jul 2013 16:12:03 +0800 Subject: [PATCH] Fix return error when resource can't be found By current implement, the HTTP request for /v2/resources/ will return 500 if there is no coresponding resource or the resource id is invalid. And the response is as below: {"error_message": "{"debuginfo": null, "faultcode": "Server", "faultstring": "list index out of range"}"} After fixed, the response will be like this: {"error_message": "{"debuginfo": null, "faultcode": "Client", "faultstring": "Invalid input for field/attribute resource_id. Value: '9'. Unknown resource"}"} Fixes bug: 1195925 Change-Id: I73c73fb3ba57bc6cbbf421f9ac5ec3cdb68ce784 --- ceilometer/api/controllers/v2.py | 14 +++++++-- tests/api/v2/list_resources.py | 50 ++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 3 deletions(-) diff --git a/ceilometer/api/controllers/v2.py b/ceilometer/api/controllers/v2.py index 0511396ba..677f3eeec 100644 --- a/ceilometer/api/controllers/v2.py +++ b/ceilometer/api/controllers/v2.py @@ -692,9 +692,15 @@ class ResourcesController(rest.RestController): :param resource_id: The UUID of the resource. """ authorized_project = acl.get_limited_to_project(pecan.request.headers) - r = list(pecan.request.storage_conn.get_resources( - resource=resource_id, project=authorized_project))[0] - return Resource.from_db_and_links(r, + resources = list(pecan.request.storage_conn.get_resources( + resource=resource_id, project=authorized_project)) + # FIXME (flwang): Need to change this to return a 404 error code when + # we get a release of WSME that supports it. + if not resources: + raise wsme.exc.InvalidInput("resource_id", + resource_id, + _("Unknown resource")) + return Resource.from_db_and_links(resources[0], self._resource_links(resource_id)) @wsme_pecan.wsexpose([Resource], [Query]) @@ -876,6 +882,8 @@ class AlarmsController(rest.RestController): auth_project = acl.get_limited_to_project(pecan.request.headers) alarms = list(conn.get_alarms(alarm_id=alarm_id, project=auth_project)) + # FIXME (flwang): Need to change this to return a 404 error code when + # we get a release of WSME that supports it. if len(alarms) < 1: raise wsme.exc.ClientSideError(_("Unknown alarm")) diff --git a/tests/api/v2/list_resources.py b/tests/api/v2/list_resources.py index a2112b953..f10145f17 100644 --- a/tests/api/v2/list_resources.py +++ b/tests/api/v2/list_resources.py @@ -196,6 +196,56 @@ class TestListResources(FunctionalTest): ids = [r['resource_id'] for r in data] self.assertEquals(['resource-id'], ids) + def test_with_invalid_resource_id(self): + counter1 = counter.Counter( + 'instance', + 'cumulative', + '', + 1, + 'user-id', + 'project-id', + 'resource-id-1', + timestamp=datetime.datetime(2012, 7, 2, 10, 40), + resource_metadata={'display_name': 'test-server', + 'tag': 'self.counter', + } + ) + msg = rpc.meter_message_from_counter( + counter1, + cfg.CONF.publisher_rpc.metering_secret, + 'test_list_resources', + ) + self.conn.record_metering_data(msg) + + counter2 = counter.Counter( + 'instance', + 'cumulative', + '', + 1, + 'user-id2', + 'project-id', + 'resource-id-2', + timestamp=datetime.datetime(2012, 7, 2, 10, 41), + resource_metadata={'display_name': 'test-server', + 'tag': 'self.counter2', + } + ) + msg2 = rpc.meter_message_from_counter( + counter2, + cfg.CONF.publisher_rpc.metering_secret, + 'test_list_resources', + ) + self.conn.record_metering_data(msg2) + + resp1 = self.get_json('/resources/resource-id-1') + self.assertEquals(resp1["resource_id"], "resource-id-1") + + resp2 = self.get_json('/resources/resource-id-2') + self.assertEquals(resp2["resource_id"], "resource-id-2") + + resp3 = self.get_json('/resources/resource-id-3', expect_errors=True) + self.assertEquals(resp3.status_code, 400) + def test_with_user(self): counter1 = counter.Counter( 'instance',