From: Zane Bitter Date: Fri, 24 May 2013 13:50:12 +0000 (+0200) Subject: Add a separate Template class for the HOT format X-Git-Tag: 2014.1~535^2 X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=d87f68c43d4c1556fb0f54d6968c77ba9d2440dd;p=openstack-build%2Fheat-build.git Add a separate Template class for the HOT format An alternative to https://review.openstack.org/30405 Change-Id: Iea3422b1219a1ddf1b12ac2d156279165eb28c07 --- diff --git a/heat/engine/template.py b/heat/engine/template.py index b0714c1f..3bd77f31 100644 --- a/heat/engine/template.py +++ b/heat/engine/template.py @@ -28,6 +28,15 @@ SECTIONS = (VERSION, DESCRIPTION, MAPPINGS, class Template(collections.Mapping): '''A stack template.''' + def __new__(cls, template, *args, **kwargs): + '''Create a new Template of the appropriate class.''' + + if cls == Template: + if 'heat_template_version' in template: + return HOTemplate(template, *args, **kwargs) + + return super(Template, cls).__new__(cls) + def __init__(self, template, template_id=None): ''' Initialise the template with a JSON object and a set of Parameters @@ -225,6 +234,27 @@ class Template(collections.Mapping): return _resolve(lambda k, v: k == 'Fn::Base64', handle_base64, s) +class HOTemplate(Template): + ''' + A Heat Orchestration Template format stack template. + ''' + + def __getitem__(self, section): + '''Get the relevant section in the template.''' + if section not in SECTIONS: + raise KeyError('"%s" is not a valid template section' % section) + + if section == VERSION: + return self.t['heat_template_version'] + + if section == DESCRIPTION: + default = 'No description' + else: + default = {} + + return self.t.get(section.lower(), default) + + def _resolve(match, handle, snippet): ''' Resolve constructs in a snippet of a template. The supplied match function