2024-04-16 22:48:52 +02:00
|
|
|
"""
|
|
|
|
Classes to generate the item tree from json spec
|
|
|
|
"""
|
|
|
|
|
|
|
|
import logging
|
|
|
|
import sys
|
|
|
|
|
|
|
|
from .uptime_kuma import Monitor, Notification, Tag
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
def die_tag_format_error():
|
|
|
|
logger.error(
|
2024-04-18 20:26:18 +02:00
|
|
|
"Fatal: You must provide tags in monitors in the format [{name:str, value:str}] (value is optional)"
|
2024-04-16 22:48:52 +02:00
|
|
|
)
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
2024-04-20 13:37:45 +02:00
|
|
|
def from_dict(api, tree, autocreate_tags=True):
|
2024-04-16 22:48:52 +02:00
|
|
|
notif = tree.get("notifications", [])
|
2024-04-20 13:37:45 +02:00
|
|
|
indexed_notifications = {
|
|
|
|
name: Notification(api, name, **kwargs) for name, kwargs in notif.items()
|
|
|
|
}
|
2024-04-16 22:48:52 +02:00
|
|
|
tags = tree.get("tags", [])
|
2024-04-20 13:37:45 +02:00
|
|
|
indexed_tags = {name: Tag(api, name, **kwargs) for name, kwargs in tags.items()}
|
2024-04-16 22:48:52 +02:00
|
|
|
monitors = tree.get("monitors", [])
|
|
|
|
indexed_monitors = {}
|
2024-04-20 13:37:45 +02:00
|
|
|
for monitor_name, monitor_kwargs in monitors.items():
|
2024-04-16 22:48:52 +02:00
|
|
|
associated_tags = []
|
2024-04-20 13:37:45 +02:00
|
|
|
for tag in monitor_kwargs.get("tags", []):
|
2024-04-18 20:26:18 +02:00
|
|
|
if not isinstance(tag, dict) or "name" not in tag:
|
2024-04-16 22:48:52 +02:00
|
|
|
die_tag_format_error()
|
|
|
|
try:
|
2024-04-20 13:37:45 +02:00
|
|
|
if autocreate_tags and tag["name"] not in indexed_tags:
|
|
|
|
indexed_tags[tag["name"]] = Tag(api, name=tag["name"])
|
2024-04-18 20:26:18 +02:00
|
|
|
associated_tags.append((indexed_tags[tag["name"]], tag.get("value")))
|
2024-04-16 22:48:52 +02:00
|
|
|
except IndexError:
|
|
|
|
die_tag_format_error()
|
2024-04-20 13:37:45 +02:00
|
|
|
monitor_kwargs["tags"] = associated_tags
|
2024-04-16 22:48:52 +02:00
|
|
|
associated_notifications = [
|
2024-04-20 13:37:45 +02:00
|
|
|
indexed_notifications[notif]
|
|
|
|
for notif in monitor_kwargs.get("notifications", [])
|
2024-04-16 22:48:52 +02:00
|
|
|
]
|
2024-04-20 13:37:45 +02:00
|
|
|
monitor_kwargs["notifications"] = associated_notifications
|
|
|
|
indexed_monitors[monitor_name] = Monitor(api, monitor_name, **monitor_kwargs)
|
2024-04-16 22:48:52 +02:00
|
|
|
return {
|
|
|
|
"monitors": indexed_monitors.values(),
|
|
|
|
"tags": indexed_tags.values(),
|
|
|
|
"notifications": indexed_notifications.values(),
|
|
|
|
}
|