From 9a3410a34cb046de0b0c7823438feae4a5d530f4 Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Fri, 20 Jan 2017 22:36:42 -0500 Subject: [PATCH] Starting the work to add indention back in. --- keepalived/templates/config.jinja | 12 ++++++++---- test/test_keepalived_config.py | 2 +- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/keepalived/templates/config.jinja b/keepalived/templates/config.jinja index 2749f36..30afbb4 100644 --- a/keepalived/templates/config.jinja +++ b/keepalived/templates/config.jinja @@ -2,15 +2,16 @@ {%- set value = 1 -%} {%- set carryovers = ['real_server', 'virtual_server', 'virtual_server_group', 'vrrp_instance', 'vrrp_script', 'vrrp_sync_group'] -%} -{%- macro keepalived_config(data, carryover='', recurse=-1) -%} +{%- macro keepalived_config(data, carryover='', recurse=-1, indent=0) -%} {%- set recurse = recurse + 1 -%} {%- if data is none -%} {{- '\n' -}} {%- elif data is string or data is number -%} - {{- data }}{{ '\n' -}} + {{- data|string|indent(indent, True) }}{{ '\n' -}} {%- else -%} {%- if recurse > 0 -%} {{- '{\n' -}} + {%- set indent = indent + 2 -%} {%- endif -%} {%- if data is mapping -%} {%- for item in data|dictsort -%} @@ -20,12 +21,15 @@ {%- if carryover -%} {{- carryover }}{{ ' ' -}} {%- endif -%} - {{- item[key] }} {{ keepalived_config(item[value], recurse=recurse) -}} + {%- if item[value] is string or item[value] is not iterable -%} + {%- set indent = 0 -%} + {%- endif -%} + {{- item[key] }} {{ keepalived_config(item[value], recurse=recurse, indent=indent) -}} {%- endif -%} {%- endfor -%} {%- else -%} {%- for item in data -%} - {{- keepalived_config(item) -}} + {{- keepalived_config(item, indent=indent) -}} {%- endfor -%} {%- endif -%} {%- if recurse > 0 -%} diff --git a/test/test_keepalived_config.py b/test/test_keepalived_config.py index f579236..a2149c8 100755 --- a/test/test_keepalived_config.py +++ b/test/test_keepalived_config.py @@ -41,7 +41,7 @@ class TestKeepalivedConfiguration(unittest.TestCase): def test_key_array_pair(self): testdata = {'flintstone': ['fred', 'wilma', 'pebbles']} - result = 'flintstone {\nfred\nwilma\npebbles\n}\n' + result = 'flintstone {\n fred\n wilma\n pebbles\n}\n' self.renderTest(testdata, result) def test_key_hash_pair(self):