Skips IP within 0.0.0.0/0 within network.py

Fixed an issue with IPMI interfaces with a default IP of 0.0.0.0/0, which can not be assigned within netbox. 

Fixed a small typo:

(assigned_object and assigned_object_id != interface.id):
vs.
(assigned_object and assigned_object.id != interface.id):
This commit is contained in:
Robert Romero 2024-03-20 13:09:06 -07:00 committed by GitHub
parent bbe30d230a
commit 51687dd9f0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -326,6 +326,9 @@ class Network(object):
* If IP exists and isn't assigned, take it * If IP exists and isn't assigned, take it
* If IP exists and interface is wrong, change interface * If IP exists and interface is wrong, change interface
''' '''
netbox_ip = None
# Check if the IP is not 0.0.0.0/0
if ip != '0.0.0.0/0':
netbox_ips = nb.ipam.ip_addresses.filter( netbox_ips = nb.ipam.ip_addresses.filter(
address=ip, address=ip,
) )
@ -343,9 +346,18 @@ class Network(object):
**query_params **query_params
) )
return netbox_ip return netbox_ip
else:
# IP already exists, log a warning and return None
logging.warning('IP {ip} already exists in NetBox'.format(ip=ip))
return None
else:
# Log that this IP is not being added due to being 0.0.0.0/0
logging.info('Skipping IP {ip} on {interface} as it is 0.0.0.0/0'.format(
ip=ip, interface=interface))
netbox_ip = list(netbox_ips)[0] # Check if netbox_ip is not None before accessing its properties
# If IP exists in anycast if netbox_ip is not None:
# Proceed with further processing only if netbox_ip is created
if netbox_ip.role and netbox_ip.role.label == 'Anycast': if netbox_ip.role and netbox_ip.role.label == 'Anycast':
logging.debug('IP {} is Anycast..'.format(ip)) logging.debug('IP {} is Anycast..'.format(ip))
unassigned_anycast_ip = [x for x in netbox_ips if x.interface is None] unassigned_anycast_ip = [x for x in netbox_ips if x.interface is None]
@ -377,7 +389,7 @@ class Network(object):
logging.info('Assigning existing IP {ip} to {interface}'.format( logging.info('Assigning existing IP {ip} to {interface}'.format(
ip=ip, interface=interface)) ip=ip, interface=interface))
elif (ip_interface and ip_interface.id != interface.id) or \ elif (ip_interface and ip_interface.id != interface.id) or \
(assigned_object and assigned_object_id != interface.id): (assigned_object and assigned_object.id != interface.id):
old_interface = getattr(netbox_ip, "assigned_object", "n/a") old_interface = getattr(netbox_ip, "assigned_object", "n/a")
logging.info( logging.info(