Skip to content

Commit 33848bc

Browse files
committed
[auto-discovery] tags could be a dict of KV pairs (#3442)
* [auto-discovery] tpl_tags can also be dicts apparently - parse them. * [test][auto-discovery] add test to templates.
1 parent d9fcf04 commit 33848bc

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

tests/core/test_service_discovery.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,13 @@ def test_fill_tpl(self, *args):
434434
['host', 'port_1'], ['foo', 'bar:baz']),
435435
({'host': '%%host%%', 'port': '%%port_1%%', 'tags': ['env:test', 'foo', 'bar:baz']},
436436
{'host': '127.0.0.1', 'port_1': '42'})
437+
),
438+
(
439+
({'NetworkSettings': {'IPAddress': '127.0.0.1', 'Ports': {'42/tcp': None, '22/tcp': None}}},
440+
{'host': '%%host%%', 'port': '%%port_1%%', 'tags': {'env': 'test'}},
441+
['host', 'port_1'], ['foo', 'bar:baz']),
442+
({'host': '%%host%%', 'port': '%%port_1%%', 'tags': ['env:test', 'foo', 'bar:baz']},
443+
{'host': '127.0.0.1', 'port_1': '42'})
437444
)
438445
]
439446

@@ -529,6 +536,7 @@ def test_fill_tpl(self, *args):
529536
for key in instance_tpl.keys():
530537
if isinstance(instance_tpl[key], list):
531538
self.assertEquals(len(instance_tpl[key]), len(co[1][0].get(key)))
539+
532540
for elem in instance_tpl[key]:
533541
self.assertTrue(elem in co[1][0].get(key))
534542
else:

utils/service_discovery/sd_docker_backend.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,11 @@ def _fill_tpl(self, state, c_id, instance_tpl, variables, tags=None):
491491
# add default tags to the instance
492492
if tags:
493493
tpl_tags = instance_tpl.get('tags', [])
494-
tags += tpl_tags if isinstance(tpl_tags, list) else [tpl_tags]
494+
if isinstance(tpl_tags, dict):
495+
for key, val in tpl_tags.iteritems():
496+
tags.append("{}:{}".format(key, val))
497+
else:
498+
tags += tpl_tags if isinstance(tpl_tags, list) else [tpl_tags]
495499
instance_tpl['tags'] = list(set(tags))
496500

497501
for var in variables:

0 commit comments

Comments
 (0)