Skip to content

Commit

Permalink
Fetch reservation/clowdapp data only if it is 'None'
Browse files Browse the repository at this point in the history
  • Loading branch information
bsquizz committed Dec 17, 2021
1 parent 9b991bf commit 5e5ccd9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
1 change: 1 addition & 0 deletions bonfire/bonfire.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("{}")
Expand Down
27 changes: 16 additions & 11 deletions bonfire/namespaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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

Expand All @@ -150,20 +150,22 @@ 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

@property
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)
Expand Down Expand Up @@ -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 = [
Expand All @@ -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)

Expand Down

0 comments on commit 5e5ccd9

Please sign in to comment.