fix: refactor tags handling

This commit is contained in:
sinavir 2024-04-18 20:26:18 +02:00
parent c4dfae1833
commit ca878138a1
2 changed files with 4 additions and 4 deletions

View file

@ -12,7 +12,7 @@ logger = logging.getLogger(__name__)
def die_tag_format_error(): def die_tag_format_error():
logger.error( logger.error(
"Fatal: You must provide tags in monitors in the format [[name, value]]" "Fatal: You must provide tags in monitors in the format [{name:str, value:str}] (value is optional)"
) )
sys.exit(1) sys.exit(1)
@ -27,10 +27,10 @@ def from_dict(api, tree):
for m in monitors: for m in monitors:
associated_tags = [] associated_tags = []
for tag in m.get("tags", []): for tag in m.get("tags", []):
if not isinstance(tag, list): if not isinstance(tag, dict) or "name" not in tag:
die_tag_format_error() die_tag_format_error()
try: try:
associated_tags.append((indexed_tags[tag[0]], tag[1])) associated_tags.append((indexed_tags[tag["name"]], tag.get("value")))
except IndexError: except IndexError:
die_tag_format_error() die_tag_format_error()
m["tags"] = associated_tags m["tags"] = associated_tags

View file

@ -11,7 +11,7 @@
"name":"test" "name":"test"
}], }],
"monitors": [{ "monitors": [{
"tags": [["test", "value"]], "tags": [{ "name":"test", "value":"value"}],
"notifications": [ "dgn" ], "notifications": [ "dgn" ],
"old_name": "test_monitor", "old_name": "test_monitor",
"name": "test_monitor2", "name": "test_monitor2",