From 01187b30f9925a8083553417b8fac7e6cfb46dcb Mon Sep 17 00:00:00 2001 From: Zane Bitter Date: Tue, 26 Aug 2014 18:37:09 -0400 Subject: [PATCH] Add a method to freeze a ResourceDefinition Change-Id: Idcbdd0efbc9c3b38c39b8ac1e64e9057fad15899 --- heat/engine/rsrc_defn.py | 30 ++++++++++++++++++++++++++++++ heat/tests/test_rsrc_defn.py | 14 ++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/heat/engine/rsrc_defn.py b/heat/engine/rsrc_defn.py index 08fd75625..df4a1ae39 100644 --- a/heat/engine/rsrc_defn.py +++ b/heat/engine/rsrc_defn.py @@ -92,6 +92,33 @@ class ResourceDefinitionCore(object): function.Function)) self._hash ^= _hash_data(update_policy) + def freeze(self, **overrides): + """ + Return a frozen resource definition, with all functions resolved. + + This return a new resource definition with fixed data (containing no + intrinsic functions). Named arguments passed to this method override + the values passed as arguments to the constructor. + """ + def arg_item(attr_name): + name = attr_name.lstrip('_') + if name in overrides: + value = overrides[name] + if not value and getattr(self, attr_name) is None: + value = None + else: + value = function.resolve(getattr(self, attr_name)) + + return name, value + + args = ('name', 'resource_type', '_properties', '_metadata', + '_depends', '_deletion_policy', '_update_policy', + 'description') + + defn = type(self)(**dict(arg_item(a) for a in args)) + defn._frozen = True + return defn + def reparse(self, stack, template): """ Reinterpret the resource definition in the context of a new stack. @@ -99,6 +126,9 @@ class ResourceDefinitionCore(object): This returns a new resource definition, with all of the functions parsed in the context of the specified stack and template. """ + assert not getattr(self, '_frozen', False), \ + "Cannot re-parse a frozen definition" + def reparse_snippet(snippet): return template.parse(stack, copy.deepcopy(snippet)) diff --git a/heat/tests/test_rsrc_defn.py b/heat/tests/test_rsrc_defn.py index bced961bc..e94dba6fc 100644 --- a/heat/tests/test_rsrc_defn.py +++ b/heat/tests/test_rsrc_defn.py @@ -112,6 +112,20 @@ class ResourceDefinitionTest(HeatTestCase): up = rd.update_policy(schema) self.assertEqual('bar', up['SomePolicy']['Foo']) + def test_freeze(self): + rd = self.make_me_one_with_everything() + + frozen = rd.freeze() + self.assertEqual('bar', frozen._properties['Foo']) + self.assertEqual('quux', frozen._metadata['Baz']) + + def test_freeze_override(self): + rd = self.make_me_one_with_everything() + + frozen = rd.freeze(metadata={'Baz': 'wibble'}) + self.assertEqual('bar', frozen._properties['Foo']) + self.assertEqual('wibble', frozen._metadata['Baz']) + def test_render_hot(self): rd = self.make_me_one_with_everything()