From 5e5ccd9e92155b372e84c623ff4467c1dd7c67fd Mon Sep 17 00:00:00 2001 From: Brandon Squizzato Date: Thu, 16 Dec 2021 11:14:46 -0500 Subject: [PATCH] Fetch reservation/clowdapp data only if it is 'None' --- bonfire/bonfire.py | 1 + bonfire/namespaces.py | 27 ++++++++++++++++----------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/bonfire/bonfire.py b/bonfire/bonfire.py index d48007f4..eeff4b90 100755 --- a/bonfire/bonfire.py +++ b/bonfire/bonfire.py @@ -561,6 +561,7 @@ def _list_namespaces(available, mine, output): _error(NO_RESERVATION_SYS) namespaces = get_namespaces(available=available, mine=mine) + if not namespaces: if output == "json": click.echo("{}") diff --git a/bonfire/namespaces.py b/bonfire/namespaces.py index ee1fe924..4c5948a9 100644 --- a/bonfire/namespaces.py +++ b/bonfire/namespaces.py @@ -107,9 +107,9 @@ def available(self): return not self.reserved and self.ready def refresh(self, namespace_data=None, reservation_data=None, clowdapps_data=None): - self._data = copy.deepcopy(namespace_data) or {} - self._reservation = copy.deepcopy(reservation_data) or {} - self._clowdapps = copy.deepcopy(clowdapps_data) or [] + self._data = copy.deepcopy(namespace_data) + self._reservation = copy.deepcopy(reservation_data) + self._clowdapps = copy.deepcopy(clowdapps_data) self._data = namespace_data or get_json("namespace", self.name) self.name = self._data.get("metadata", {}).get("name") @@ -127,10 +127,10 @@ def refresh(self, namespace_data=None, reservation_data=None, clowdapps_data=Non self.expires = None def __init__(self, name=None, namespace_data=None, reservation_data=None, clowdapps_data=None): - self._data = {} - self._reservation = {} - self._clowdapps = [] self.name = name + self._data = namespace_data # if empty/None, we will fetch data + self._reservation = reservation_data # if None, we will fetch data + self._clowdapps = clowdapps_data # if None, we will fetch data self.requester = None self.expires = None @@ -150,12 +150,13 @@ def __str__(self): @property def reservation(self): - if not self._reservation: + if self._reservation is None: + log.debug("fetching reservation for ns '%s'", self.name) self._reservation = get_reservation(namespace=self.name) if not self._reservation or not self._reservation.get("status"): self._reservation = None - log.error("Could not retrieve reservation details for ns: %s", self.name) + log.warning("could not retrieve reservation details for ns: %s", self.name) return self._reservation @@ -163,7 +164,8 @@ def reservation(self): def clowdapps(self): if not self.reserved or not self.ready: return "none" - if not self._clowdapps: + if self._clowdapps is None: + log.debug("fetching clowdapps for ns %s", self.name) self._clowdapps = get_json("app", namespace=self.name).get("items", []) managed = len(self._clowdapps) @@ -192,6 +194,7 @@ def get_namespaces(available=False, mine=False): # build a list containing the ns data, reservation data, and clowdapp # data pertaining to each ns all_ns_kwargs = [] + for ns in all_namespaces: ns_name = ns["metadata"]["name"] clowdapps_data = [ @@ -200,10 +203,12 @@ def get_namespaces(available=False, mine=False): reservation_data = [ res for res in all_res if res.get("status", {}).get("namespace") == ns_name ] + # ensure a non-None value is passed in for these kwargs since we have already + # pre-fetched the data kwargs = { "namespace_data": ns, - "clowdapps_data": clowdapps_data or None, - "reservation_data": reservation_data[0] if reservation_data else None, + "clowdapps_data": clowdapps_data, + "reservation_data": reservation_data[0] if reservation_data else {}, } all_ns_kwargs.append(kwargs)