Skip to content

Commit

Permalink
fix issue with cached data from previous NetBox versions #415
Browse files Browse the repository at this point in the history
  • Loading branch information
bb-Ricardo committed Sep 27, 2024
1 parent b3fe545 commit 1966438
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
30 changes: 28 additions & 2 deletions module/netbox/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:

Expand Down Expand Up @@ -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"))
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions module/netbox/object_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down

0 comments on commit 1966438

Please sign in to comment.