Skip to content

Commit 159f309

Browse files
authored
Handle netbox api cache more gracefully (#1111)
There are instances where we fail to read the cached netbox_api_dump.json file, and the code was setting openapi to an empty dictionary, and netbox_api_version to 0. This is not correct, because we can still fetch the netbox_api_version by querying the live server. In addition, in cases where the cached API was failing to read, it would end up triggering the if/else statement to try and access the older API doc endpoint, even if the server was not < 3.5.0 Overall the code was very convoluted and could break if the cached file was not readable or writable. This patch cleans the logic up and makes it more clear what is going on.
1 parent dbaf1b1 commit 159f309

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

plugins/inventory/nb_inventory.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1491,33 +1491,33 @@ def wrapper():
14911491

14921492
def fetch_api_docs(self):
14931493
try:
1494-
status = self._fetch_information(self.api_endpoint + "/api/status")
1495-
netbox_api_version = ".".join(status["netbox-version"].split(".")[:2])
1496-
except Exception:
1497-
netbox_api_version = 0
1498-
1499-
tmp_dir = os.path.split(DEFAULT_LOCAL_TMP)[0]
1500-
tmp_file = os.path.join(tmp_dir, "netbox_api_dump.json")
1501-
1502-
try:
1494+
tmp_dir = os.path.split(DEFAULT_LOCAL_TMP)[0]
1495+
tmp_file = os.path.join(tmp_dir, "netbox_api_dump.json")
15031496
with open(tmp_file) as file:
1504-
openapi = json.load(file)
1497+
cache = json.load(file)
1498+
cached_api_version = ".".join(cache["info"]["version"].split(".")[:2])
15051499
except Exception:
1506-
openapi = {}
1500+
cached_api_version = None
1501+
cache = None
15071502

1508-
cached_api_version = openapi.get("info", {}).get("version")
1509-
if cached_api_version:
1510-
cached_api_version = ".".join(cached_api_version.split(".")[:2])
1503+
status = self._fetch_information(self.api_endpoint + "/api/status")
1504+
netbox_api_version = ".".join(status["netbox-version"].split(".")[:2])
15111505

1512-
if netbox_api_version != cached_api_version:
1513-
if version.parse(netbox_api_version) >= version.parse("3.5.0"):
1514-
endpoint_url = self.api_endpoint + "/api/schema/?format=json"
1515-
else:
1516-
endpoint_url = self.api_endpoint + "/api/docs/?format=openapi"
1506+
if version.parse(netbox_api_version) >= version.parse("3.5.0"):
1507+
endpoint_url = self.api_endpoint + "/api/schema/?format=json"
1508+
else:
1509+
endpoint_url = self.api_endpoint + "/api/docs/?format=openapi"
15171510

1511+
if cache and cached_api_version == netbox_api_version:
1512+
openapi = cache
1513+
else:
15181514
openapi = self._fetch_information(endpoint_url)
1519-
with open(tmp_file, "w") as file:
1520-
json.dump(openapi, file)
1515+
1516+
try:
1517+
with open(tmp_file, "w") as file:
1518+
json.dump(openapi, file)
1519+
except Exception:
1520+
pass
15211521

15221522
self.api_version = version.parse(netbox_api_version)
15231523

0 commit comments

Comments
 (0)