mirror of
https://github.com/bb-Ricardo/netbox-sync.git
synced 2026-04-25 19:28:53 -05:00
fixes IP addresses marked as orphan when VM is turned off #192
This commit is contained in:
@@ -302,10 +302,29 @@ class NetBoxInventory:
|
||||
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:
|
||||
|
||||
@@ -1580,6 +1580,33 @@ class NBIPAddress(NetBoxObject):
|
||||
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):
|
||||
"""
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user