diff options
author | Fei Long Wang <flwang@cn.ibm.com> | 2013-07-01 16:12:03 +0800 |
---|---|---|
committer | Eoghan Glynn <eglynn@redhat.com> | 2013-07-31 16:22:15 +0100 |
commit | c51bfc29752ef1d4080149f0ed8cce5f3cb22129 (patch) | |
tree | 0f09a693b6c39457d98271648b7e8c28d65ac838 | |
parent | 72edce2bec7100baf87d25023cd870793eaa4bd4 (diff) | |
download | ceilometer-c51bfc29752ef1d4080149f0ed8cce5f3cb22129.tar.gz |
Fix return error when resource can't be found
By current implement, the HTTP request for /v2/resources/<resource_id>
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
(cherry picked from commit fbb1eb9285c7787f8508d3f69790522ff6847fc8)
-rw-r--r-- | ceilometer/api/controllers/v2.py | 10 | ||||
-rw-r--r-- | tests/api/v2/test_list_resources.py | 50 |
2 files changed, 57 insertions, 3 deletions
diff --git a/ceilometer/api/controllers/v2.py b/ceilometer/api/controllers/v2.py index 0129ea63..c58241ee 100644 --- a/ceilometer/api/controllers/v2.py +++ b/ceilometer/api/controllers/v2.py @@ -501,9 +501,13 @@ class ResourcesController(rest.RestController): :param resource_id: The UUID of the resource. """ - r = list(pecan.request.storage_conn.get_resources( - resource=resource_id))[0] - return Resource(**r) + resources = list(pecan.request.storage_conn.get_resources( + resource=resource_id)) + if not resources: + raise wsme.exc.InvalidInput("resource_id", + resource_id, + _("Unknown resource")) + return Resource(**resources[0]) @wsme_pecan.wsexpose([Resource], [Query]) def get_all(self, q=[]): diff --git a/tests/api/v2/test_list_resources.py b/tests/api/v2/test_list_resources.py index ab9c1359..88337baf 100644 --- a/tests/api/v2/test_list_resources.py +++ b/tests/api/v2/test_list_resources.py @@ -189,6 +189,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', |