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
This commit is contained in:
Fei Long Wang 2013-07-01 16:12:03 +08:00
parent 298e2f7e7b
commit fbb1eb9285
2 changed files with 61 additions and 3 deletions

View File

@ -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"))

View File

@ -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',