Add a method to freeze a ResourceDefinition

Change-Id: Idcbdd0efbc9c3b38c39b8ac1e64e9057fad15899
This commit is contained in:
Zane Bitter 2014-08-26 18:37:09 -04:00
parent b45bd873fd
commit 01187b30f9
2 changed files with 44 additions and 0 deletions

View File

@ -92,6 +92,33 @@ class ResourceDefinitionCore(object):
function.Function)) function.Function))
self._hash ^= _hash_data(update_policy) 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): def reparse(self, stack, template):
""" """
Reinterpret the resource definition in the context of a new stack. 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 This returns a new resource definition, with all of the functions
parsed in the context of the specified stack and template. 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): def reparse_snippet(snippet):
return template.parse(stack, copy.deepcopy(snippet)) return template.parse(stack, copy.deepcopy(snippet))

View File

@ -112,6 +112,20 @@ class ResourceDefinitionTest(HeatTestCase):
up = rd.update_policy(schema) up = rd.update_policy(schema)
self.assertEqual('bar', up['SomePolicy']['Foo']) 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): def test_render_hot(self):
rd = self.make_me_one_with_everything() rd = self.make_me_one_with_everything()