Skip to content

Commit

Permalink
fixes IP addresses marked as orphan when VM is turned off bb-Ricardo#192
Browse files Browse the repository at this point in the history
  • Loading branch information
bb-Ricardo authored and kuznetsov andrei committed Feb 27, 2023
1 parent b33def8 commit 5072cbb
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
25 changes: 22 additions & 3 deletions module/netbox/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,10 +302,29 @@ def tag_all_the_things(self, netbox_handler):
continue

if getattr(this_object, "prune", False) is True:
if netbox_handler.primary_tag in this_object.get_tags() and \
netbox_handler.ignore_unknown_source_object_pruning is False:

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

if netbox_handler.ignore_unknown_source_object_pruning is True:
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()

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

this_object.add_tags(netbox_handler.orphaned_tag)

# or just remove primary tag if pruning is disabled
else:
Expand Down
28 changes: 28 additions & 0 deletions module/netbox/object_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1580,6 +1580,33 @@ def update(self, data=None, read_from_netbox=False, source=None):
if "assigned_object_id" in self.updated_items:
self.updated_items.append("assigned_object_type")

def get_interface(self):
o_id = self.data.get("assigned_object_id")
o_type = self.data.get("assigned_object_type")

if isinstance(o_id, (NBInterface, NBVMInterface)):
return o_id

if o_type is None or not isinstance(o_id, int):
return

if o_type not in self.data_model.get("assigned_object_type"):
return

return self.inventory.get_by_id(self.data_model_relation.get(o_type), nb_id=o_id)

def get_device_vm(self):

o_interface = self.get_interface()

if o_interface is None:
return

if isinstance(o_interface, NBInterface):
return o_interface.data.get("device")
elif isinstance(o_interface, NBVMInterface):
return o_interface.data.get("virtual_machine")


class NBFHRPGroupItem(NetBoxObject):
"""
Expand All @@ -1590,6 +1617,7 @@ class NBFHRPGroupItem(NetBoxObject):
api_path = "/ipam/fhrp-groups"
primary_key = "group_id"
prune = False

def __init__(self, *args, **kwargs):
self.data_model = {
"group_id": int,
Expand Down

0 comments on commit 5072cbb

Please sign in to comment.