Skip to content
This repository has been archived by the owner on Dec 30, 2021. It is now read-only.

Commit

Permalink
add support for ansible jinja2 filters and fixes for handling newline…
Browse files Browse the repository at this point in the history
… and double qoute characters
  • Loading branch information
Johan Bakker committed Dec 4, 2019
1 parent 84fd30b commit 4232607
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 14 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
Changelog
=========

0.4.8 (2019-12-04)
------------------

Added
-----
- support jinja2 filters provided by the jinja2-ansible-filters library

Fixed
-----
- fix handling consul keys containing newlines and double quote characters

0.4.7 (2019-11-25)
------------------

Expand Down
2 changes: 1 addition & 1 deletion e2j2/helpers/constants.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION = '0.4.7'
VERSION = '0.4.8'
ERROR = '** ERROR'
BRIGHT_RED = '\033[1;31m'
RESET_ALL = '\033[00m'
Expand Down
3 changes: 2 additions & 1 deletion e2j2/helpers/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ def render(**kwargs):
variable_start_string=kwargs['variable_start'],
variable_end_string=kwargs['variable_end'],
comment_start_string=kwargs['comment_start'],
comment_end_string=kwargs['comment_end'])
comment_end_string=kwargs['comment_end'],
extensions=['jinja2_ansible_filters.AnsibleCoreFiltersExtension'])

try:
first_pass = j2.get_template(filename).render(kwargs['j2vars'])
Expand Down
29 changes: 18 additions & 11 deletions e2j2/tags/consul_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from functools import reduce
from deepmerge import Merger
from six.moves.urllib.parse import urlparse
from e2j2.helpers.exceptions import E2j2Exception
from e2j2.helpers.exceptions import E2j2Exception, JSONDecodeError


class ConsulKV:
Expand Down Expand Up @@ -41,18 +41,25 @@ def parse(tag_config, value):
kv_entries = consul_kv.get(recurse=True, key=consul_key)
except ACLPermissionDenied:
raise E2j2Exception('access denied connecting to: {}://{}:{} **'.format(consul_kv.scheme, consul_kv.host, consul_kv.port))
except AssertionError as err:
raise E2j2Exception(err)

if not kv_entries:
# Mark as failed if we can't find the consul key
raise E2j2Exception('key not found')
consul_dict = {}
for entry in kv_entries:
subkeys = entry['Key'].split('/')
value = entry['Value'].decode('utf-8') if hasattr(entry['Value'], 'decode') else entry['Value']
value = '' if value is None else value
if '/' in entry['Key']:
key = '{"' + entry['Key'].replace('/', '":{"') + '": "' + value + '"}'.ljust(len(subkeys)+1, '}')
consul_dict = consul_merger.merge(consul_dict, json.loads(key))
else:
consul_dict[entry['Key']] = value
return reduce(operator.getitem, consul_key.split('/'), consul_dict)
try:
for entry in kv_entries:
subkeys = entry['Key'].split('/')
value = entry['Value'].decode('utf-8') if hasattr(entry['Value'], 'decode') else entry['Value']
value = '' if value is None else value
value = value.replace('"', '\\"') # escape double quotes
value = value.replace('\n', '\\n') # escape newlines
if '/' in entry['Key']:
key = '{"' + entry['Key'].replace('/', '":{"') + '": "' + value + '"}'.ljust(len(subkeys)+1, '}')
consul_dict = consul_merger.merge(consul_dict, json.loads(key))
else:
consul_dict[entry['Key']] = value
return reduce(operator.getitem, consul_key.split('/'), consul_dict)
except JSONDecodeError as err:
raise E2j2Exception(err)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# pyinit: python3
jinja2>=2.10.1
jinja2-ansible-filters
python-consul>=0.7.0
deepmerge>=0.0.4
jsonschema
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
'from shell environment variables'

setup(name='e2j2',
version='0.4.7',
version='0.4.8',
description=description,
long_description=open('README.rst').read() + '\n\n' + open('CHANGELOG.rst').read(),
install_requires=[
Expand Down

0 comments on commit 4232607

Please sign in to comment.