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:
Chris Dent 2016-08-25 13:56:30 +00:00
parent 3609487867
commit 2bb527b06d
3 changed files with 15 additions and 1 deletions

View File

@ -544,7 +544,11 @@ class InventoryList(base.ObjectListBase, base.NovaObject):
string.
"""
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:
if fields.ResourceClass.index(inv_rec.resource_class) == res_class:

View File

@ -239,6 +239,10 @@ tests:
GET: /resource_providers/$ENVIRON['RP_UUID']/inventories/IPV4_ADDRESS
status: 404
- name: get invalid inventory class
GET: /resource_providers/$ENVIRON['RP_UUID']/inventories/HOUSE
status: 404
- name: create another resource provider
POST: /resource_providers
request_headers:

View File

@ -435,6 +435,12 @@ class TestInventory(test_objects._LocalTest):
self.assertIsNotNone(found)
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):
@mock.patch('nova.objects.Allocation._create_in_db',