Skip to content

Commit

Permalink
[auto-discovery] tags could be a dict of KV pairs (#3442)
Browse files Browse the repository at this point in the history
* [auto-discovery] tpl_tags can also be dicts apparently - parse them.

* [test][auto-discovery] add test to templates.
  • Loading branch information
truthbk committed Jul 17, 2017
1 parent d9fcf04 commit 33848bc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
8 changes: 8 additions & 0 deletions tests/core/test_service_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,13 @@ def test_fill_tpl(self, *args):
['host', 'port_1'], ['foo', 'bar:baz']),
({'host': '%%host%%', 'port': '%%port_1%%', 'tags': ['env:test', 'foo', 'bar:baz']},
{'host': '127.0.0.1', 'port_1': '42'})
),
(
({'NetworkSettings': {'IPAddress': '127.0.0.1', 'Ports': {'42/tcp': None, '22/tcp': None}}},
{'host': '%%host%%', 'port': '%%port_1%%', 'tags': {'env': 'test'}},
['host', 'port_1'], ['foo', 'bar:baz']),
({'host': '%%host%%', 'port': '%%port_1%%', 'tags': ['env:test', 'foo', 'bar:baz']},
{'host': '127.0.0.1', 'port_1': '42'})
)
]

Expand Down Expand Up @@ -529,6 +536,7 @@ def test_fill_tpl(self, *args):
for key in instance_tpl.keys():
if isinstance(instance_tpl[key], list):
self.assertEquals(len(instance_tpl[key]), len(co[1][0].get(key)))

for elem in instance_tpl[key]:
self.assertTrue(elem in co[1][0].get(key))
else:
Expand Down
6 changes: 5 additions & 1 deletion utils/service_discovery/sd_docker_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,11 @@ def _fill_tpl(self, state, c_id, instance_tpl, variables, tags=None):
# add default tags to the instance
if tags:
tpl_tags = instance_tpl.get('tags', [])
tags += tpl_tags if isinstance(tpl_tags, list) else [tpl_tags]
if isinstance(tpl_tags, dict):
for key, val in tpl_tags.iteritems():
tags.append("{}:{}".format(key, val))
else:
tags += tpl_tags if isinstance(tpl_tags, list) else [tpl_tags]
instance_tpl['tags'] = list(set(tags))

for var in variables:
Expand Down

0 comments on commit 33848bc

Please sign in to comment.