Skip to content

Commit

Permalink
fixes orphan tagging if ignore_unknown_source_object_puning is set to…
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 b6237a3 commit a07ba46
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 34 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ cache/*

# settings file
settings.ini

*.toml
66 changes: 40 additions & 26 deletions module/netbox/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class NetBoxInventory:
base_structure = dict()

source_tags_of_disabled_sources = list()
source_tags_of_enabled_sources = list()

def __init__(self):

Expand All @@ -32,6 +33,19 @@ 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):
"""
adds $source_tag to list of disabled sources
Expand Down Expand Up @@ -305,40 +319,40 @@ def tag_all_the_things(self, netbox_handler):
f"from a currently disabled source. Skipping orphaned tagging.")
continue

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

# test for different conditions.
if netbox_handler.primary_tag not in this_object.get_tags():
continue
# 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)

if netbox_handler.ignore_unknown_source_object_pruning is True:
continue
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()
# test for different conditions.
if netbox_handler.primary_tag not in this_object.get_tags():
continue

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

if device_vm_object is not None and \
grab(device_vm_object, "data.status") is not None and \
"active" not in str(grab(device_vm_object, "data.status")):
# 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()

if netbox_handler.orphaned_tag in this_object.get_tags():
this_object.remove_tags(netbox_handler.orphaned_tag)
if device_vm_object is not None and \
grab(device_vm_object, "data.status") is not None and \
"active" not in str(grab(device_vm_object, "data.status")):

log.debug2(f"{device_vm_object.name} '{device_vm_object.get_display_name()}' has IP "
f"'{this_object.get_display_name()}' assigned but is in status "
f"{grab(device_vm_object, 'data.status')}. "
f"IP address will not marked as orphaned.")
continue
if netbox_handler.orphaned_tag in this_object.get_tags():
this_object.remove_tags(netbox_handler.orphaned_tag)

this_object.add_tags(netbox_handler.orphaned_tag)
log.debug2(f"{device_vm_object.name} '{device_vm_object.get_display_name()}' has IP "
f"'{this_object.get_display_name()}' assigned but is in status "
f"{grab(device_vm_object, 'data.status')}. "
f"IP address will not marked as orphaned.")
continue

# or just remove primary tag if pruning is disabled
else:
if netbox_handler.primary_tag in this_object.get_tags():
this_object.remove_tags(netbox_handler.primary_tag)
if netbox_handler.orphaned_tag in this_object.get_tags():
this_object.remove_tags(netbox_handler.orphaned_tag)
this_object.add_tags(netbox_handler.orphaned_tag)

def query_ptr_records_for_all_ips(self):
"""
Expand Down
12 changes: 5 additions & 7 deletions module/sources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
# repository or visit: <https://opensource.org/licenses/MIT>.

# define all available sources here
from .vmware.connection import VMWareHandler
from .check_redfish.import_inventory import CheckRedfish
from module.sources.vmware.connection import VMWareHandler
from module.sources.check_redfish.import_inventory import CheckRedfish
from .vclouddirector.load_civm import CheckCloudDirector

from module.common.configuration import get_config
Expand Down Expand Up @@ -58,7 +58,7 @@ def validate_source(source_class_object=None, state="pre"):
# post initialization validation
for attr, value_type in necessary_attributes.items():

value = getattr(source_class_object, attr)
value = getattr(source_class_object, attr, None)

if not isinstance(value, value_type):
raise ValueError(f"Value for attribute '{attr}' needs to be {value_type}")
Expand Down Expand Up @@ -114,11 +114,8 @@ def instantiate_sources(config_handler=None, inventory=None):
source_class = None
for possible_source_class in valid_sources:
validate_source(possible_source_class)
source_class_type = getattr(possible_source_class, "source_type", None)
if source_class_type is None:
raise AttributeError("'%s' class attribute 'source_type' not defined." % source_class_type.__name__)

if source_class_type == source_type:
if possible_source_class.implements(source_type):
source_class = possible_source_class
break

Expand All @@ -142,6 +139,7 @@ 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)

Expand Down
10 changes: 9 additions & 1 deletion module/sources/common/source_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,15 @@ class SourceBase:
inventory = None
source_tag = None

# dummy function to implement a finish call for each source
@classmethod
def implements(cls, source_type):

if getattr(cls, "source_type", None) == source_type:
return True

return False

# stub function to implement a finish call for each source
def finish(self):
pass

Expand Down

0 comments on commit a07ba46

Please sign in to comment.