]> review.fuel-infra Code Review - openstack-build/heat-build.git/commitdiff
Add a separate Template class for the HOT format
authorZane Bitter <zbitter@redhat.com>
Fri, 24 May 2013 13:50:12 +0000 (15:50 +0200)
committerZane Bitter <zbitter@redhat.com>
Fri, 24 May 2013 13:50:12 +0000 (15:50 +0200)
An alternative to https://review.openstack.org/30405

Change-Id: Iea3422b1219a1ddf1b12ac2d156279165eb28c07

heat/engine/template.py

index b0714c1fb9ab807619f1916f6d85ffa0ffe7a833..3bd77f314d6c6b6ab43f1360db3d2d362a4d39c1 100644 (file)
@@ -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