Do not raise NotFound for resource_get_all_* queries

A stack with zero resources is not an exception worthy condition (for
example, a stack being deleted, a stack which really does have zero
resources).

Also its unusual that a collection query function would raise a
NotFound when the result count is zero.

This change fixes that for resource_get_all_by_stack and
resource_get_all_active_by_stack

Change-Id: I3420d5099830f5626095b17c01e9d886b477915c
Related-Bug: #1301320
Related-Bug: #1523748
This commit is contained in:
Steve Baker 2016-07-15 11:53:31 +12:00
parent 10088def6c
commit 483c505d61
3 changed files with 11 additions and 22 deletions

View File

@ -349,10 +349,6 @@ def resource_get_all_by_stack(context, stack_id, filters=None):
query = db_filters.exact_filter(query, models.Resource, filters)
results = query.all()
if not results:
raise exception.NotFound(_("no resources for stack_id %s were found")
% stack_id)
return dict((res.name, res) for res in results)
@ -365,9 +361,6 @@ def resource_get_all_active_by_stack(context, stack_id):
models.Resource.id.notin_(subquery.as_scalar())
).options(orm.joinedload("data")).all()
if not results:
raise exception.NotFound(_("no active resources for stack_id %s were"
" found") % stack_id)
return dict((res.id, res) for res in results)

View File

@ -344,22 +344,19 @@ class Stack(collections.Mapping):
yield nested_res
def db_active_resources_get(self):
try:
return resource_objects.Resource.get_all_active_by_stack(
self.context, self.id)
except exception.NotFound:
return None
resources = resource_objects.Resource.get_all_active_by_stack(
self.context, self.id)
return resources or None
def db_resource_get(self, name):
if not self.id:
return None
if self._db_resources is None:
try:
_db_resources = resource_objects.Resource.get_all_by_stack(
self.context, self.id)
self._db_resources = _db_resources
except exception.NotFound:
_db_resources = resource_objects.Resource.get_all_by_stack(
self.context, self.id)
if not _db_resources:
return None
self._db_resources = _db_resources
return self._db_resources.get(name)
@property

View File

@ -2055,9 +2055,8 @@ class DBAPIStackTest(common.HeatTestCase):
rt_id = stacks[s].raw_template_id
self.assertRaises(exception.NotFound,
db_api.raw_template_get, ctx, rt_id)
self.assertRaises(exception.NotFound,
db_api.resource_get_all_by_stack,
ctx, stacks[s].id)
self.assertEqual({}, db_api.resource_get_all_by_stack(
ctx, stacks[s].id))
self.assertRaises(exception.NotFound,
db_api.raw_template_files_get,
ctx, tmpl_files[tmpl_idx].files_id)
@ -2252,8 +2251,8 @@ class DBAPIResourceTest(common.HeatTestCase):
self.assertEqual('res1', resources.get('res1').name)
self.assertEqual('res2', resources.get('res2').name)
self.assertRaises(exception.NotFound, db_api.resource_get_all_by_stack,
self.ctx, self.stack2.id)
self.assertEqual({}, db_api.resource_get_all_by_stack(
self.ctx, self.stack2.id))
def test_resource_get_all_active_by_stack(self):
values = [