Make it possible to include files in templates 81/17881/1
authorPeter Barabas <peter.barabas@ericsson.com>
Mon, 1 Aug 2016 12:41:29 +0000 (14:41 +0200)
committerPeter Barabas <peter.barabas@ericsson.com>
Mon, 1 Aug 2016 12:41:29 +0000 (14:41 +0200)
Change-Id: I45c42ac65cfbe6562f0035df3375a2231148e22e
Signed-off-by: Peter Barabas <peter.barabas@ericsson.com>
deploy/README.templater
deploy/templater.py

index 964872f..b5d52ab 100644 (file)
@@ -49,6 +49,12 @@ syntax is described below:
 
    Specify a network type and a role as arguments to interface().
 
+5. File inclusion
+
+   %{include(templates/interfaces.yaml)}
+
+   Filename with absolute or relative path.
+
 
 ======== EXAMPLES ========
 
@@ -275,3 +281,91 @@ compute_private_if: ens5
 mongo_mgmt_if: ens3
 controller_private_if: ens4
 
+
+--- Example 4 ---
+
+Template file:
+
+version: 1.1
+created: Mon Jun 13 19:39:35 2016
+comment: None
+%{include(environment.yaml)}
+
+
+environment.yaml:
+
+environment:
+  name: F9-NOSDN-NOFEATURE-VXLAN-BAREMETAL
+  net_segment_type: tun
+
+
+Result:
+
+version: 1.1
+created: Mon Jun 13 19:39:35 2016
+comment: None
+environment:
+  name: F9-NOSDN-NOFEATURE-VXLAN-BAREMETAL
+  net_segment_type: tun
+
+
+--- Example 5 ---
+
+Template file (except):
+
+settings:
+  editable:
+    access:
+      email:
+        description: Email address for Administrator
+        label: Email
+        regex:
+          error: Invalid email
+          source: ^\S+@\S+$
+        type: text
+        value: admin@localhost
+        weight: 40
+# ...
+# lines omitted for brevity
+    %{include(templates/cgroups.yaml)}
+
+
+cgroups.yaml:
+
+    cgroups:
+      metadata:
+        always_editable: true
+        group: general
+        label: Cgroups conguration for services
+        restrictions:
+        - action: hide
+          condition: 'true'
+        weight: 90
+
+
+Result:
+
+settings:
+  editable:
+    access:
+      email:
+        description: Email address for Administrator
+        label: Email
+        regex:
+          error: Invalid email
+          source: ^\S+@\S+$
+        type: text
+        value: admin@localhost
+        weight: 40
+# ...
+# again, lines omitted for brevity
+    cgroups:
+      metadata:
+        always_editable: true
+        group: general
+        label: Cgroups conguration for services
+        restrictions:
+        - action: hide
+          condition: 'true'
+        weight: 90
+
index 2ad6e05..6b41e1f 100755 (executable)
@@ -107,12 +107,28 @@ class Templater(object):
 
         return self.get_interface_from_network(interfaces, args[0])
 
+    def parse_include_tag(self, tag):
+        # Remove 'include(' prefix and trailing ')'
+        filename = tag[len('include('):].rstrip(')')
+
+        if not filename:
+            err('No argument for include().')
+
+        return filename
+
+    def include_file(self, filename):
+        fragment = self.load_yaml(filename)
+        return yaml.dump(fragment, default_flow_style=False)
+
     def parse_tag(self, tag, indent):
         fragment = ''
 
         if 'interface(' in tag:
             args = self.parse_interface_tag(tag)
             fragment = self.lookup_interface(args)
+        elif 'include(' in tag:
+            filename = self.parse_include_tag(tag)
+            fragment = self.include_file(filename)
         else:
             path = tag.split(DELIMITER)
             fragment = self.base