forked from DGNum/stateless-uptime-kuma
Merge pull request 'feat: allow configuring settings' (#6) from lukegb/stateless-uptime-kuma:settings into master
Reviewed-on: DGNum/stateless-uptime-kuma#6 Reviewed-by: thubrecht <tom.hubrecht@dgnum.eu> Reviewed-by: mdebray <mdebray@dgnum.eu>
This commit is contained in:
commit
880f444ff7
3 changed files with 46 additions and 1 deletions
|
@ -52,6 +52,10 @@ in
|
||||||
type = with lib.types; attrsOf probesFormat.type;
|
type = with lib.types; attrsOf probesFormat.type;
|
||||||
default = { };
|
default = { };
|
||||||
};
|
};
|
||||||
|
settings = lib.mkOption {
|
||||||
|
type = with lib.types; probesFormat.type;
|
||||||
|
default = { };
|
||||||
|
};
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
config = {
|
config = {
|
||||||
|
|
|
@ -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, StatusPage, Tag # type: ignore
|
from .uptime_kuma import Monitor, Notification, Settings, StatusPage, Tag # type: ignore
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -59,10 +59,22 @@ def from_dict(api, tree, autocreate_tags=True):
|
||||||
monitorList.append(indexed_monitors[monitor])
|
monitorList.append(indexed_monitors[monitor])
|
||||||
group["monitorList"] = monitorList
|
group["monitorList"] = monitorList
|
||||||
indexed_status_pages[slug] = StatusPage(api, slug, **kwargs)
|
indexed_status_pages[slug] = StatusPage(api, slug, **kwargs)
|
||||||
|
settings = []
|
||||||
|
if "settings" in tree:
|
||||||
|
parsed_settings = {}
|
||||||
|
for k, v in tree["settings"].items():
|
||||||
|
if k.endswith("__FILE"):
|
||||||
|
with open(v, "rt") as f:
|
||||||
|
parsed_settings[k[:-len("__FILE")]] = f.read().strip()
|
||||||
|
else:
|
||||||
|
parsed_settings[k] = v
|
||||||
|
settings = [Settings(api, **parsed_settings)]
|
||||||
|
|
||||||
|
|
||||||
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(),
|
"status_pages": indexed_status_pages.values(),
|
||||||
|
"settings": settings,
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,7 @@ class Manager:
|
||||||
self.sync_notifications()
|
self.sync_notifications()
|
||||||
self.sync_monitors()
|
self.sync_monitors()
|
||||||
self.sync_status_pages()
|
self.sync_status_pages()
|
||||||
|
self.sync_settings()
|
||||||
self.save()
|
self.save()
|
||||||
|
|
||||||
def save(self):
|
def save(self):
|
||||||
|
@ -46,6 +47,17 @@ class Manager:
|
||||||
new = self.target_tree.get("status_pages", [])
|
new = self.target_tree.get("status_pages", [])
|
||||||
self.sync(new, old, "slug")
|
self.sync(new, old, "slug")
|
||||||
|
|
||||||
|
def sync_settings(self):
|
||||||
|
if "settings" not in self.target_tree:
|
||||||
|
return
|
||||||
|
old = self.api.get_settings()
|
||||||
|
new = self.target_tree["settings"][0]
|
||||||
|
# We need all of the arguments; settings are saved all at once.
|
||||||
|
# We take any settings that are not explicitly set back from the server.
|
||||||
|
for k, v in old.items():
|
||||||
|
if k not in new.kwargs:
|
||||||
|
new.kwargs[k] = v
|
||||||
|
|
||||||
def sync(self, new, old, pk):
|
def sync(self, new, old, pk):
|
||||||
indexed_old = {elem[pk]: elem for elem in old}
|
indexed_old = {elem[pk]: elem for elem in old}
|
||||||
for k in new:
|
for k in new:
|
||||||
|
@ -235,3 +247,20 @@ class StatusPage(Item):
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return f"StatusPage({str(self)})"
|
return f"StatusPage({str(self)})"
|
||||||
|
|
||||||
|
|
||||||
|
class Settings(Item):
|
||||||
|
def __init__(self, api, **kwargs):
|
||||||
|
super().__init__(api, "settings", None, None)
|
||||||
|
self.kwargs = kwargs
|
||||||
|
|
||||||
|
def save(self):
|
||||||
|
if self.saved:
|
||||||
|
return
|
||||||
|
logger.debug(f"Saving {repr(self)}")
|
||||||
|
self.api.set_settings(**self.kwargs)
|
||||||
|
self.saved = True
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f"Settings({str(self.kwargs)})"
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue