From 8704dcf2b9af362e12e2e0133ef56c6da3863518 Mon Sep 17 00:00:00 2001 From: Sergi Delgado Segura Date: Tue, 5 May 2020 15:11:04 +0200 Subject: [PATCH] plugin - defines TowerSummary as object to improve readability of the code --- watchtower-plugin/net/http.py | 2 +- watchtower-plugin/tower_info.py | 20 +++++++++++++------- watchtower-plugin/watchtower.py | 29 ++++++++++++++--------------- 3 files changed, 28 insertions(+), 23 deletions(-) diff --git a/watchtower-plugin/net/http.py b/watchtower-plugin/net/http.py index c456855..97e47d3 100644 --- a/watchtower-plugin/net/http.py +++ b/watchtower-plugin/net/http.py @@ -59,7 +59,7 @@ def add_appointment(plugin, tower_id, tower, appointment_dict, signature): def send_appointment(tower_id, tower, appointment_dict, signature): data = {"appointment": appointment_dict, "signature": signature} - add_appointment_endpoint = f"{tower.get('netaddr')}/add_appointment" + add_appointment_endpoint = f"{tower.netaddr}/add_appointment" response = process_post_response(post_request(data, add_appointment_endpoint, tower_id)) tower_signature = response.get("signature") diff --git a/watchtower-plugin/tower_info.py b/watchtower-plugin/tower_info.py index 7156dce..e8b8597 100644 --- a/watchtower-plugin/tower_info.py +++ b/watchtower-plugin/tower_info.py @@ -49,10 +49,16 @@ class TowerInfo: return self.__dict__ def get_summary(self): - return { - "netaddr": self.netaddr, - "status": self.status, - "available_slots": self.available_slots, - "pending_appointments": self.pending_appointments, - "invalid_appointments": self.invalid_appointments, - } + return TowerSummary(self) + + +class TowerSummary: + def __init__(self, tower_info): + self.netaddr = tower_info.netaddr + self.status = tower_info.status + self.available_slots = tower_info.available_slots + self.pending_appointments = tower_info.pending_appointments + self.invalid_appointments = tower_info.invalid_appointments + + def to_dict(self): + return self.__dict__ diff --git a/watchtower-plugin/watchtower.py b/watchtower-plugin/watchtower.py index 3a5c85e..b46837b 100755 --- a/watchtower-plugin/watchtower.py +++ b/watchtower-plugin/watchtower.py @@ -163,7 +163,6 @@ def get_appointment(plugin, tower_id, locator): """ # FIXME: All responses from the tower should be signed. - try: tower_id, locator = arg_parser.parse_get_appointment_arguments(tower_id, locator) @@ -175,7 +174,7 @@ def get_appointment(plugin, tower_id, locator): data = {"locator": locator, "signature": signature} # Send request to the server. - tower_netaddr = plugin.wt_client.towers[tower_id].get("netaddr") + tower_netaddr = plugin.wt_client.towers[tower_id].netaddr get_appointment_endpoint = f"{tower_netaddr}/get_appointment" plugin.log(f"Requesting appointment from {tower_id}") @@ -190,10 +189,10 @@ def get_appointment(plugin, tower_id, locator): @plugin.method("listtowers", desc="List all towers registered towers.") def list_towers(plugin): towers_info = {"towers": []} - for tower_id, info in plugin.wt_client.towers.items(): - values = {k: v for k, v in info.items() if k != "pending_appointments"} - pending_appointments = [appointment.get("locator") for appointment, signature in info["pending_appointments"]] - invalid_appointments = [appointment.get("locator") for appointment, signature in info["invalid_appointments"]] + for tower_id, tower in plugin.wt_client.towers.items(): + values = {k: v for k, v in tower.to_dict().items() if k not in ["pending_appointments", "invalid_appointments"]} + pending_appointments = [appointment.get("locator") for appointment, signature in tower.pending_appointments] + invalid_appointments = [appointment.get("locator") for appointment, signature in tower.invalid_appointments] values["pending_appointments"] = pending_appointments values["invalid_appointments"] = invalid_appointments towers_info["towers"].append({"id": tower_id, **values}) @@ -225,15 +224,15 @@ def retry_tower(plugin, tower_id): if not tower: response = {"error": f"{tower_id} is not a registered tower"} - if tower.get("status") not in ["unreachable", "subscription error"]: - response = {"error": f"{tower_id} is not unreachable. {tower.get('status')}"} - if not tower.get("pending_appointments"): + if tower.status not in ["unreachable", "subscription error"]: + response = {"error": f"{tower_id} is not unreachable. {tower.status}"} + if not tower.pending_appointments: response = {"error": f"{tower_id} does not have pending appointments"} if not response: response = f"Retrying tower {tower_id}" plugin.log(response) - plugin.wt_client.towers[tower_id]["status"] = "temporarily unreachable" + plugin.wt_client.towers[tower_id].status = "temporarily unreachable" plugin.wt_client.retrier.temp_unreachable_towers.put(tower_id) plugin.wt_client.lock.release() @@ -260,11 +259,11 @@ def on_commitment_revocation(plugin, **kwargs): for tower_id, tower in plugin.wt_client.towers.items(): tower_update = {} - if tower.get("status") == "misbehaving": + if tower.status == "misbehaving": return {"result": "continue"} try: - if tower.get("status") == "reachable": + if tower.status == "reachable": tower_signature, available_slots = add_appointment( plugin, tower_id, tower, appointment.to_dict(), signature ) @@ -272,9 +271,9 @@ def on_commitment_revocation(plugin, **kwargs): tower_update["available_slots"] = available_slots else: - if tower.get("status") in ["temporarily unreachable", "unreachable"]: - plugin.log(f"{tower_id} is {tower.get('status')}. Adding {appointment.locator} to pending") - elif tower.get("status") == "subscription error": + if tower.status in ["temporarily unreachable", "unreachable"]: + plugin.log(f"{tower_id} is {tower.status}. Adding {appointment.locator} to pending") + elif tower.status == "subscription error": plugin.log(f"There is a subscription issue with {tower_id}. Adding appointment to pending") tower_update["pending_appointment"] = (appointment.to_dict(), signature), "add"