Skip to content

Commit

Permalink
fixes issue with handling of pruning objects from unknonw sources bb-…
Browse files Browse the repository at this point in the history
  • Loading branch information
bb-Ricardo authored and kuznetsov andrei committed Oct 9, 2024
1 parent 153532a commit e697e4c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 35 deletions.
53 changes: 21 additions & 32 deletions module/netbox/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ class NetBoxInventory:

base_structure = dict()

source_tags_of_disabled_sources = list()
source_tags_of_enabled_sources = list()
source_list = list()

def __init__(self):

Expand All @@ -33,31 +32,18 @@ def __init__(self):

self.base_structure[object_type.name] = list()

def add_enabled_source_tag(self, source_tag=None):
"""
adds $source_tag to list of enabled sources
Parameters
----------
source_tag: str
source tag of disabled source
"""
if source_tag is not None:
self.source_tags_of_enabled_sources.append(source_tag)

def add_disabled_source_tag(self, source_tag=None):
def add_source(self, source_handler=None):
"""
adds $source_tag to list of disabled sources
Parameters
----------
source_tag: str
source tag of disabled source
source_handler: object
source handler object
"""
if source_tag is not None:
self.source_tags_of_disabled_sources.append(source_tag)
if source_handler is not None:
self.source_list.append(source_handler)

def get_by_id(self, object_type, nb_id=None):
"""
Expand Down Expand Up @@ -299,42 +285,45 @@ def tag_all_the_things(self, netbox_handler):
the object instance of a NetBox handler to get the tag names from
"""

all_sources_tags = [x.source_tag for x in self.source_list]
disabled_sources_tags = [x.source_tag for x in self.source_list if getattr(x, "enabled") is False]

for object_type in NetBoxObject.__subclasses__():

for this_object in self.get_all_items(object_type):

this_object_tags = this_object.get_tags()

# if object was found in source
if this_object.source is not None:
this_object.add_tags([netbox_handler.primary_tag, this_object.source.source_tag])

# if object was orphaned remove tag again
if netbox_handler.orphaned_tag in this_object.get_tags():
if netbox_handler.orphaned_tag in this_object_tags:
this_object.remove_tags(netbox_handler.orphaned_tag)

# if object was tagged by this program in previous runs but is not present
# anymore then add the orphaned tag except it originated from a disabled source
else:
if bool(set(this_object.get_tags()).intersection(self.source_tags_of_disabled_sources)) is True:

if bool(set(this_object_tags).intersection(disabled_sources_tags)) is True:
log.debug2(f"Object '{this_object.get_display_name()}' was added "
f"from a currently disabled source. Skipping orphaned tagging.")
continue

if getattr(this_object, "prune", False) is False:

# or just remove primary tag if pruning is disabled
this_object.remove_tags(netbox_handler.primary_tag)
this_object.remove_tags(netbox_handler.orphaned_tag)

continue

# test for different conditions.
if netbox_handler.primary_tag not in this_object.get_tags():
if netbox_handler.primary_tag not in this_object_tags:
continue

if bool(set(this_object.get_tags()).intersection(self.source_tags_of_enabled_sources)) is False \
if bool(set(this_object_tags).intersection(all_sources_tags)) is False \
and netbox_handler.ignore_unknown_source_object_pruning is True:
continue

if getattr(this_object, "prune", False) is False:
# or just remove primary tag if pruning is disabled
this_object.remove_tags(netbox_handler.orphaned_tag)
continue

# don't mark IPs as orphaned if vm/device is only switched off
if isinstance(this_object, NBIPAddress):
device_vm_object = this_object.get_device_vm()
Expand Down
5 changes: 2 additions & 3 deletions module/sources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,8 @@ def instantiate_sources(config_handler=None, inventory=None):
# add to list of source handlers
if source_handler.init_successful is True:
sources.append(source_handler)
inventory.add_enabled_source_tag(source_handler.source_tag)
elif getattr(source_handler, "enabled") is False:
inventory.add_disabled_source_tag(source_handler.source_tag)

inventory.add_source(source_handler)

return sources

Expand Down

0 comments on commit e697e4c

Please sign in to comment.