fixes issue with moving ip addresses from device a to b if ip address is primary ip of device a #224

This commit is contained in:
ricardo.bartels@telekom.de
2022-12-28 00:30:05 +01:00
parent 6a052334c4
commit 3202391198
3 changed files with 34 additions and 8 deletions

View File

@@ -617,12 +617,6 @@ class NetBoxHandler:
for this_object in self.inventory.get_all_items(nb_object_sub_class):
# resolve dependencies
for dependency in this_object.get_dependencies():
if dependency not in self.resolved_dependencies:
log.debug2("Resolving dependency: %s" % dependency.name)
self.update_object(dependency)
# unset data if requested
if unset is True:
@@ -655,6 +649,12 @@ class NetBoxHandler:
continue
# resolve dependencies
for dependency in this_object.get_dependencies():
if dependency not in self.resolved_dependencies:
log.debug2("Resolving dependency: %s" % dependency.name)
self.update_object(dependency)
data_to_patch = dict()
unresolved_dependency_data = dict()

View File

@@ -1059,6 +1059,9 @@ class NetBoxObject:
if current_value is None:
return
if attribute_name in self.unset_items:
return
# mark attribute to unset, this way it will be deleted in NetBox before any other updates are performed
log.info(f"Setting attribute '{attribute_name}' for '{self.get_display_name()}' to None")
self.unset_items.append(attribute_name)
@@ -1648,9 +1651,21 @@ class NBIPAddress(NetBoxObject):
object_type = data.get("assigned_object_type")
assigned_object = data.get("assigned_object_id")
# used to track changes in object primary IP assignments
previous_ip_device_vm = None
is_primary_ipv4_of_previous_device = False
is_primary_ipv6_of_previous_device = False
# we got an object data structure where we have to find the object
if read_from_netbox is False and assigned_object is not None:
# get current device to make sure to unset primary ip before moving IP address
previous_ip_device_vm = self.get_device_vm()
if grab(previous_ip_device_vm, "data.primary_ip4") is self:
is_primary_ipv4_of_previous_device = True
if grab(previous_ip_device_vm, "data.primary_ip6") is self:
is_primary_ipv6_of_previous_device = True
if not isinstance(assigned_object, NetBoxObject):
data["assigned_object_id"] = \
@@ -1666,6 +1681,17 @@ class NBIPAddress(NetBoxObject):
if "assigned_object_id" in self.updated_items:
self.updated_items.append("assigned_object_type")
if assigned_object is None or previous_ip_device_vm is None:
return
if previous_ip_device_vm is self.get_device_vm():
return
if is_primary_ipv4_of_previous_device is True:
previous_ip_device_vm.unset_attribute("primary_ip4")
if is_primary_ipv6_of_previous_device is True:
previous_ip_device_vm.unset_attribute("primary_ip6")
def get_interface(self):
o_id = self.data.get("assigned_object_id")
o_type = self.data.get("assigned_object_type")

View File

@@ -1329,11 +1329,11 @@ class VMWareHandler(SourceBase):
# add all interface IPs
for ip_object in ip_address_objects:
ip_interface_object = ip_interface(grab(ip_object, "data.address"))
if ip_object is None:
continue
ip_interface_object = ip_interface(grab(ip_object, "data.address"))
# continue if address is not a primary IP
if ip_interface_object not in [primary_ipv4_object, primary_ipv6_object]:
continue