feat: add status page declarative setup

This commit is contained in:
sinavir 2024-04-21 11:55:34 +02:00
parent 456f923905
commit 1c73542d29
3 changed files with 66 additions and 6 deletions

View file

@ -40,6 +40,10 @@ in
type = with lib.types; attrsOf probesFormat.type; type = with lib.types; attrsOf probesFormat.type;
default = { }; default = { };
}; };
status_pages = lib.mkOption {
type = with lib.types; attrsOf probesFormat.type;
default = { };
};
}; };
}; };
config.statelessUptimeKuma = { config.statelessUptimeKuma = {

View file

@ -5,7 +5,7 @@ Classes to generate the item tree from json spec
import logging import logging
import sys import sys
from .uptime_kuma import Monitor, Notification, Tag from .uptime_kuma import Monitor, Notification, StatusPage, Tag # type: ignore
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -44,8 +44,25 @@ def from_dict(api, tree, autocreate_tags=True):
] ]
monitor_kwargs["notifications"] = associated_notifications monitor_kwargs["notifications"] = associated_notifications
indexed_monitors[monitor_name] = Monitor(api, monitor_name, **monitor_kwargs) indexed_monitors[monitor_name] = Monitor(api, monitor_name, **monitor_kwargs)
status_pages = tree.get("status_pages", [])
indexed_status_pages = {}
for slug, kwargs in status_pages.items():
for group in kwargs.get("publicGroupList", []):
if "monitorList" in group:
monitorList = []
for monitor in group["monitorList"]:
if monitor not in indexed_monitors:
logger.error(
"Fatal: status page is referencing a monitor that doesn't exist"
)
sys.exit(1)
monitorList.append(indexed_monitors[monitor])
group["monitorList"] = monitorList
indexed_status_pages[slug] = StatusPage(api, slug, **kwargs)
return { return {
"monitors": indexed_monitors.values(), "monitors": indexed_monitors.values(),
"tags": indexed_tags.values(), "tags": indexed_tags.values(),
"notifications": indexed_notifications.values(), "notifications": indexed_notifications.values(),
"status_pages": indexed_status_pages.values(),
} }

View file

@ -18,6 +18,7 @@ class Manager:
self.sync_tags() self.sync_tags()
self.sync_notifications() self.sync_notifications()
self.sync_monitors() self.sync_monitors()
self.sync_status_pages()
self.save() self.save()
def save(self): def save(self):
@ -28,20 +29,25 @@ class Manager:
def sync_monitors(self): def sync_monitors(self):
old = self.api.get_monitors() old = self.api.get_monitors()
new = self.target_tree.get("monitors", []) new = self.target_tree.get("monitors", [])
self.sync(new, old) self.sync(new, old, "name")
def sync_notifications(self): def sync_notifications(self):
old = self.api.get_notifications() old = self.api.get_notifications()
new = self.target_tree.get("notifications", []) new = self.target_tree.get("notifications", [])
self.sync(new, old) self.sync(new, old, "name")
def sync_tags(self): def sync_tags(self):
old = self.api.get_tags() old = self.api.get_tags()
new = self.target_tree.get("tags", []) new = self.target_tree.get("tags", [])
self.sync(new, old) self.sync(new, old, "name")
def sync(self, new, old): def sync_status_pages(self):
indexed_old = {elem["name"]: elem for elem in old} old = self.api.get_status_pages()
new = self.target_tree.get("status_pages", [])
self.sync(new, old, "slug")
def sync(self, new, old, pk):
indexed_old = {elem[pk]: elem for elem in old}
for k in new: for k in new:
if k.name in indexed_old: if k.name in indexed_old:
k.id = indexed_old[k.name]["id"] k.id = indexed_old[k.name]["id"]
@ -194,3 +200,36 @@ class Notification(Item):
def __repr__(self): def __repr__(self):
return f"Notification({str(self)})" return f"Notification({str(self)})"
class StatusPage(Item):
def __init__(self, api, name, id=None, old_name=None, **kwargs):
super().__init__(api, name, id, old_name)
self.kwargs = kwargs
def save(self):
if self.saved:
return
logger.debug(f"Saving {repr(self)}")
if self.id is None:
self.api.add_status_page(
slug=self.name,
title=self.kwargs["title"],
)
rslt = self.api.get_status_page(self.name)
self.id = rslt["id"]
kwargs = self.kwargs
for group in kwargs.get("publicGroupList", []):
if "monitorList" in group:
monitorList = []
for monitor in group["monitorList"]:
monitorList.append({"id": monitor.id})
group["monitorList"] = monitorList
self.api.save_status_page(
slug=self.name,
**kwargs,
)
self.saved = True
def __repr__(self):
return f"StatusPage({str(self)})"