diff --git a/stateless_uptime_kuma/uptime_kuma.py b/stateless_uptime_kuma/uptime_kuma.py index 7208ba3..866e9dd 100644 --- a/stateless_uptime_kuma/uptime_kuma.py +++ b/stateless_uptime_kuma/uptime_kuma.py @@ -5,6 +5,8 @@ Classes to make the needed operations to reach the specified state. import logging import sys +from requests.api import delete + logger = logging.getLogger(__name__) @@ -13,6 +15,7 @@ class Manager: self.api = api self.prune_unused = prune_unused self.target_tree = target_tree + self.to_prune = [] def process(self): self.sync_tags() @@ -20,6 +23,7 @@ class Manager: self.sync_monitors() self.sync_status_pages() self.sync_settings() + self.prune() self.save() def save(self): @@ -27,25 +31,34 @@ class Manager: for i in v: i.save() # this method should be safe to be called in whatever order + def prune(self): + for e in self.to_prune: + e.prune() + + def sync_monitors(self): + logger.debug(f"Syncing monitors...") old = self.api.get_monitors() new = self.target_tree.get("monitors", []) - self.sync(new, old, "name") + self.sync(new, old, "name", Monitor) def sync_notifications(self): + logger.debug(f"Syncing notifications...") old = self.api.get_notifications() new = self.target_tree.get("notifications", []) - self.sync(new, old, "name") + self.sync(new, old, "name", Notification) def sync_tags(self): + logger.debug(f"Syncing tags...") old = self.api.get_tags() new = self.target_tree.get("tags", []) - self.sync(new, old, "name") + self.sync(new, old, "name", Tag) def sync_status_pages(self): + logger.debug(f"Syncing status pages...") old = self.api.get_status_pages() new = self.target_tree.get("status_pages", []) - self.sync(new, old, "slug") + self.sync(new, old, "slug", StatusPage) def sync_settings(self): if "settings" not in self.target_tree: @@ -58,8 +71,9 @@ class Manager: if k not in new.kwargs: new.kwargs[k] = v - def sync(self, new, old, pk): + def sync(self, new, old, pk, item_t): indexed_old = {elem[pk]: elem for elem in old} + indexed_new = {elem.name: elem for elem in new} for k in new: if k.name in indexed_old: k.id = indexed_old[k.name]["id"] @@ -72,6 +86,14 @@ class Manager: else: k.id = None # Useless logger.debug(f"Creating key {k}") + for k in indexed_old: + if k not in indexed_new: + if item_t == StatusPage: + self.to_prune.append(item_t(api=None, name=indexed_old[k]["slug"], id=indexed_old[k]["id"])) + else: + self.to_prune.append(item_t(api=self.api, name=indexed_old[k]["name"], id=indexed_old[k]["id"])) + + class Item: @@ -85,6 +107,10 @@ class Item: def save(self): raise NotImplementedError() + def prune(self): + print(f"Would prune: {self}") + + def __setattr__(self, name, value): if name != "saved": self.saved = False @@ -157,6 +183,12 @@ class Monitor(Item): self.api.add_monitor_tag(tag_id=t.id, monitor_id=self.id, value=v) self.saved = True + + def prune(self): + logger.debug(f"Deleting monitor {self.name}") + self.api.delete_monitor(self.id) + + def __repr__(self): return f"Monitor({str(self)})" @@ -185,6 +217,9 @@ class Tag(Item): ) self.saved = True + def prune(self): + pass + def __repr__(self): return f"Tag({str(self)})"