plugin - defines TowerSummary as object to improve readability of the code

This commit is contained in:
Sergi Delgado Segura
2020-05-05 15:11:04 +02:00
parent cb4920b566
commit 8704dcf2b9
3 changed files with 28 additions and 23 deletions

View File

@@ -59,7 +59,7 @@ def add_appointment(plugin, tower_id, tower, appointment_dict, signature):
def send_appointment(tower_id, tower, appointment_dict, signature): def send_appointment(tower_id, tower, appointment_dict, signature):
data = {"appointment": appointment_dict, "signature": 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)) response = process_post_response(post_request(data, add_appointment_endpoint, tower_id))
tower_signature = response.get("signature") tower_signature = response.get("signature")

View File

@@ -49,10 +49,16 @@ class TowerInfo:
return self.__dict__ return self.__dict__
def get_summary(self): def get_summary(self):
return { return TowerSummary(self)
"netaddr": self.netaddr,
"status": self.status,
"available_slots": self.available_slots, class TowerSummary:
"pending_appointments": self.pending_appointments, def __init__(self, tower_info):
"invalid_appointments": self.invalid_appointments, 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__

View File

@@ -163,7 +163,6 @@ def get_appointment(plugin, tower_id, locator):
""" """
# FIXME: All responses from the tower should be signed. # FIXME: All responses from the tower should be signed.
try: try:
tower_id, locator = arg_parser.parse_get_appointment_arguments(tower_id, locator) 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} data = {"locator": locator, "signature": signature}
# Send request to the server. # 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" get_appointment_endpoint = f"{tower_netaddr}/get_appointment"
plugin.log(f"Requesting appointment from {tower_id}") 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.") @plugin.method("listtowers", desc="List all towers registered towers.")
def list_towers(plugin): def list_towers(plugin):
towers_info = {"towers": []} towers_info = {"towers": []}
for tower_id, info in plugin.wt_client.towers.items(): for tower_id, tower in plugin.wt_client.towers.items():
values = {k: v for k, v in info.items() if k != "pending_appointments"} 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 info["pending_appointments"]] pending_appointments = [appointment.get("locator") for appointment, signature in tower.pending_appointments]
invalid_appointments = [appointment.get("locator") for appointment, signature in info["invalid_appointments"]] invalid_appointments = [appointment.get("locator") for appointment, signature in tower.invalid_appointments]
values["pending_appointments"] = pending_appointments values["pending_appointments"] = pending_appointments
values["invalid_appointments"] = invalid_appointments values["invalid_appointments"] = invalid_appointments
towers_info["towers"].append({"id": tower_id, **values}) towers_info["towers"].append({"id": tower_id, **values})
@@ -225,15 +224,15 @@ def retry_tower(plugin, tower_id):
if not tower: if not tower:
response = {"error": f"{tower_id} is not a registered tower"} response = {"error": f"{tower_id} is not a registered tower"}
if tower.get("status") not in ["unreachable", "subscription error"]: if tower.status not in ["unreachable", "subscription error"]:
response = {"error": f"{tower_id} is not unreachable. {tower.get('status')}"} response = {"error": f"{tower_id} is not unreachable. {tower.status}"}
if not tower.get("pending_appointments"): if not tower.pending_appointments:
response = {"error": f"{tower_id} does not have pending appointments"} response = {"error": f"{tower_id} does not have pending appointments"}
if not response: if not response:
response = f"Retrying tower {tower_id}" response = f"Retrying tower {tower_id}"
plugin.log(response) 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.retrier.temp_unreachable_towers.put(tower_id)
plugin.wt_client.lock.release() 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(): for tower_id, tower in plugin.wt_client.towers.items():
tower_update = {} tower_update = {}
if tower.get("status") == "misbehaving": if tower.status == "misbehaving":
return {"result": "continue"} return {"result": "continue"}
try: try:
if tower.get("status") == "reachable": if tower.status == "reachable":
tower_signature, available_slots = add_appointment( tower_signature, available_slots = add_appointment(
plugin, tower_id, tower, appointment.to_dict(), signature plugin, tower_id, tower, appointment.to_dict(), signature
) )
@@ -272,9 +271,9 @@ def on_commitment_revocation(plugin, **kwargs):
tower_update["available_slots"] = available_slots tower_update["available_slots"] = available_slots
else: else:
if tower.get("status") in ["temporarily unreachable", "unreachable"]: if tower.status in ["temporarily unreachable", "unreachable"]:
plugin.log(f"{tower_id} is {tower.get('status')}. Adding {appointment.locator} to pending") plugin.log(f"{tower_id} is {tower.status}. Adding {appointment.locator} to pending")
elif tower.get("status") == "subscription error": elif tower.status == "subscription error":
plugin.log(f"There is a subscription issue with {tower_id}. Adding appointment to pending") plugin.log(f"There is a subscription issue with {tower_id}. Adding appointment to pending")
tower_update["pending_appointment"] = (appointment.to_dict(), signature), "add" tower_update["pending_appointment"] = (appointment.to_dict(), signature), "add"