From f96a5b9b4b5dd30fb7835a32b764500fab2f6e2f Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Wed, 18 Jan 2017 20:40:19 -0500 Subject: [PATCH] Added key hash pairs. --- keepalived/templates/config.jinja | 31 +++++++++++++++++++------------ test/test_keepalived_config.py | 5 +++++ 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/keepalived/templates/config.jinja b/keepalived/templates/config.jinja index f46e018..832678d 100644 --- a/keepalived/templates/config.jinja +++ b/keepalived/templates/config.jinja @@ -1,17 +1,24 @@ -{%- macro keepalived_config(data) -%} +{%- macro keepalived_config(data, recurse=-1) -%} + {%- set recurse = recurse + 1 -%} {%- if data is none -%} -{{ '\n' }} - {%- elif data is mapping -%} - {%- for key, value in data.iteritems() -%} -{{ key }} {{ keepalived_config(value) }} - {%- endfor -%} + {{- '\n' -}} {%- elif data is string or data is number -%} -{{ data }}{{ '\n' }} - {%- else -%} -{{ '{\n' }} - {%- for value in data -%} -{{ keepalived_config(value) }} + {{- data }}{{ '\n' -}} + {%- elif data is mapping -%} + {%- if recurse > 0 -%} + {{- '{\n' -}} + {%- endif -%} + {%- for key, value in data.iteritems() -%} + {{- key }} {{ keepalived_config(value, recurse) -}} {%- endfor -%} -{{ '}\n' }} + {%- if recurse > 0 -%} + {{- '}\n' -}} + {%- endif -%} + {%- else -%} + {{- '{\n' -}} + {%- for value in data -%} + {{- keepalived_config(value) -}} + {%- endfor -%} + {{- '}\n' -}} {%- endif -%} {%- endmacro -%} diff --git a/test/test_keepalived_config.py b/test/test_keepalived_config.py index d8add52..55b46a1 100755 --- a/test/test_keepalived_config.py +++ b/test/test_keepalived_config.py @@ -44,5 +44,10 @@ class TestKeepalivedConfiguration(unittest.TestCase): result = 'flintstone {\nfred\nwilma\npebbles\n}\n' self.renderTest(testdata, result) + def test_key_hash_pair(self): + testdata = {'friends': {'rubble': 'barney'}} + result = 'friends {\nrubble barney\n}\n' + self.renderTest(testdata, result) + if __name__ == '__main__': unittest.main()