From 29040846ae3093a04cc7e06dcddb17127b7f0afd Mon Sep 17 00:00:00 2001 From: Thomas Herve Date: Tue, 24 Dec 2013 13:22:26 +0100 Subject: [PATCH] Don't query for resource during stack creation In Resource.__init__, we unconditionally make a query on the resource table even if the stack hasn't been stored yet and stack.id is None. The patch adds a simple conditon to skip it. Change-Id: Ic8f915d6bb82c95db0a65b029b09b864c1348f3d --- heat/engine/resource.py | 8 ++++++-- heat/tests/test_resource.py | 12 ++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/heat/engine/resource.py b/heat/engine/resource.py index 557ab6fa7..fbd4d47ee 100644 --- a/heat/engine/resource.py +++ b/heat/engine/resource.py @@ -180,8 +180,12 @@ class Resource(object): self.attributes_schema, self._resolve_attribute) - resource = db_api.resource_get_by_name_and_stack(self.context, - name, stack.id) + if stack.id: + resource = db_api.resource_get_by_name_and_stack(self.context, + name, stack.id) + else: + resource = None + if resource: self.resource_id = resource.nova_instance self.action = resource.action diff --git a/heat/tests/test_resource.py b/heat/tests/test_resource.py index 8b335dd03..395f9ab17 100644 --- a/heat/tests/test_resource.py +++ b/heat/tests/test_resource.py @@ -15,6 +15,8 @@ import itertools import uuid +import mock + from heat.common import exception from heat.engine import dependencies from heat.engine import parser @@ -58,6 +60,16 @@ class ResourceTest(HeatTestCase): snippet = {'Type': 'GenericResourceType'} res = resource.Resource('aresource', snippet, self.stack) self.assertIsInstance(res, generic_rsrc.GenericResource) + self.assertEqual("INIT", res.action) + + def test_resource_new_stack_not_stored(self): + snippet = {'Type': 'GenericResourceType'} + self.stack.id = None + db_method = 'resource_get_by_name_and_stack' + with mock.patch.object(db_api, db_method) as resource_get: + res = resource.Resource('aresource', snippet, self.stack) + self.assertEqual("INIT", res.action) + self.assertIs(False, resource_get.called) def test_resource_new_err(self): snippet = {'Type': 'NoExistResourceType'}