From 483c505d61a1161ecdab77e56f014ff34f1304d3 Mon Sep 17 00:00:00 2001 From: Steve Baker Date: Fri, 15 Jul 2016 11:53:31 +1200 Subject: [PATCH] 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 --- heat/db/sqlalchemy/api.py | 7 ------- heat/engine/stack.py | 17 +++++++---------- heat/tests/db/test_sqlalchemy_api.py | 9 ++++----- 3 files changed, 11 insertions(+), 22 deletions(-) diff --git a/heat/db/sqlalchemy/api.py b/heat/db/sqlalchemy/api.py index c2a5ebd86a..0bffc087e0 100644 --- a/heat/db/sqlalchemy/api.py +++ b/heat/db/sqlalchemy/api.py @@ -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) diff --git a/heat/engine/stack.py b/heat/engine/stack.py index 002a2bb9dd..749c549d84 100644 --- a/heat/engine/stack.py +++ b/heat/engine/stack.py @@ -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 diff --git a/heat/tests/db/test_sqlalchemy_api.py b/heat/tests/db/test_sqlalchemy_api.py index 0fa863dbd2..945ac4ca30 100644 --- a/heat/tests/db/test_sqlalchemy_api.py +++ b/heat/tests/db/test_sqlalchemy_api.py @@ -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 = [