From 19664389a86ff038cd46c2ef11a186f486f42bb4 Mon Sep 17 00:00:00 2001 From: "ricardo.bartels@telekom.de" Date: Sat, 28 Sep 2024 00:19:06 +0200 Subject: [PATCH] fix issue with cached data from previous NetBox versions #415 --- module/netbox/connection.py | 30 ++++++++++++++++++++++++++++-- module/netbox/object_classes.py | 3 +++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/module/netbox/connection.py b/module/netbox/connection.py index 37eb8a0..e1b943a 100644 --- a/module/netbox/connection.py +++ b/module/netbox/connection.py @@ -102,7 +102,7 @@ def __init__(self): def setup_caching(self): """ Validate if all requirements are met to cache NetBox data. - If a condition fails, caching is switched of. + If a condition fails, caching is switched off. """ if self.settings.use_caching is False: @@ -386,6 +386,25 @@ def query_current_data(self, netbox_objects_to_query=None): if netbox_objects_to_query is None: raise AttributeError(f"Attribute netbox_objects_to_query is: '{netbox_objects_to_query}'") + # try to read cached file witch stores the NetBox version the cache was build upon + cache_tainted = True + cached_version_file_name = f"{self.cache_directory}{os.sep}cached_version" + cached_version = None + # noinspection PyBroadException + try: + with open(cached_version_file_name) as file: + cached_version = version.parse(file.read()) + except Exception: + pass + + if cached_version is not None and cached_version == version.parse(self.inventory.netbox_api_version): + cache_tainted = False + else: + if cached_version is not None: + log.info(f"Cache was build for NetBox version {cached_version} " + f"which does not match discovered NetBox version {self.inventory.netbox_api_version}. " + f"Rebuilding cache.") + # query all dependencies for nb_object_class in netbox_objects_to_query: @@ -420,7 +439,7 @@ def query_current_data(self, netbox_objects_to_query=None): cache_this_class = False # read data from cache file - if cache_this_class is True: + if cache_this_class is True and cache_tainted is False: # noinspection PyBroadException try: cached_nb_data = pickle.load(open(cache_file, "rb")) @@ -523,6 +542,13 @@ def query_current_data(self, netbox_objects_to_query=None): # mark this object class as retrieved self.resolved_dependencies.add(nb_object_class) + # noinspection PyBroadException + try: + with open(cached_version_file_name, "w") as file: + file.write(self.inventory.netbox_api_version) + except Exception: + pass + def initialize_basic_data(self): """ Adds the two basic tags to keep track of objects and see which diff --git a/module/netbox/object_classes.py b/module/netbox/object_classes.py index 7f4fda1..2c65906 100644 --- a/module/netbox/object_classes.py +++ b/module/netbox/object_classes.py @@ -1196,6 +1196,9 @@ def update(self, data=None, read_from_netbox=False, source=None): if isinstance(data.get("object_types"), str): data["object_types"] = [data.get("object_types")] + elif data.get("object_types") is None: + data["object_types"] = [] + for object_type in data.get("object_types"): if object_type not in self.valid_object_types and read_from_netbox is False: log.error(f"Invalid content type '{object_type}' for {self.name}")