In InventoryList.find() raise NotFound if invalid resource class
If InventoryList.find() was provided with a string of a resource class that does not exist it would cause an uncaught ValueError. This would eventually raise up to the Placement API as a 500 in response, for example, to 'GET /resource_providers/{uuid}/inventories/HOUSE' NotFound makes sense at both the object and HTTP levels. A unit test for the object level has been added and a gabbi test for the HTTP level. Note: this was discovered while developing some test client code for the placement API. A simple, but wrong, argument was passed through the stack and caused a 500. Change-Id: I0d4066a3f213f726a92d673986b702e08bda4459 Partially-Implements: blueprint generic-resource-pools
This commit is contained in:
@@ -544,7 +544,11 @@ class InventoryList(base.ObjectListBase, base.NovaObject):
|
|||||||
string.
|
string.
|
||||||
"""
|
"""
|
||||||
if isinstance(res_class, six.string_types):
|
if isinstance(res_class, six.string_types):
|
||||||
res_class = fields.ResourceClass.index(res_class)
|
try:
|
||||||
|
res_class = fields.ResourceClass.index(res_class)
|
||||||
|
except ValueError:
|
||||||
|
raise exception.NotFound("No such resource class '%s'" %
|
||||||
|
res_class)
|
||||||
|
|
||||||
for inv_rec in self.objects:
|
for inv_rec in self.objects:
|
||||||
if fields.ResourceClass.index(inv_rec.resource_class) == res_class:
|
if fields.ResourceClass.index(inv_rec.resource_class) == res_class:
|
||||||
|
@@ -239,6 +239,10 @@ tests:
|
|||||||
GET: /resource_providers/$ENVIRON['RP_UUID']/inventories/IPV4_ADDRESS
|
GET: /resource_providers/$ENVIRON['RP_UUID']/inventories/IPV4_ADDRESS
|
||||||
status: 404
|
status: 404
|
||||||
|
|
||||||
|
- name: get invalid inventory class
|
||||||
|
GET: /resource_providers/$ENVIRON['RP_UUID']/inventories/HOUSE
|
||||||
|
status: 404
|
||||||
|
|
||||||
- name: create another resource provider
|
- name: create another resource provider
|
||||||
POST: /resource_providers
|
POST: /resource_providers
|
||||||
request_headers:
|
request_headers:
|
||||||
|
@@ -435,6 +435,12 @@ class TestInventory(test_objects._LocalTest):
|
|||||||
self.assertIsNotNone(found)
|
self.assertIsNotNone(found)
|
||||||
self.assertEqual(24, found.total)
|
self.assertEqual(24, found.total)
|
||||||
|
|
||||||
|
# Use an invalid string...
|
||||||
|
error = self.assertRaises(exception.NotFound,
|
||||||
|
inv_list.find,
|
||||||
|
'HOUSE')
|
||||||
|
self.assertIn('No such resource class', str(error))
|
||||||
|
|
||||||
|
|
||||||
class _TestAllocationNoDB(object):
|
class _TestAllocationNoDB(object):
|
||||||
@mock.patch('nova.objects.Allocation._create_in_db',
|
@mock.patch('nova.objects.Allocation._create_in_db',
|
||||||
|
Reference in New Issue
Block a user