From 0ae67f44e612dc19a3a907fac0ad4cac5f70dfbe Mon Sep 17 00:00:00 2001 From: Robert Romero <1013414+NessieCanCode@users.noreply.github.com> Date: Fri, 22 Mar 2024 09:20:00 -0700 Subject: [PATCH] handle the case where nb_mgmt_ip is None gracefully and prevent the AttributeError from occurring. --- netbox_agent/network.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/netbox_agent/network.py b/netbox_agent/network.py index a9e2e8a..6f94c22 100644 --- a/netbox_agent/network.py +++ b/netbox_agent/network.py @@ -540,10 +540,8 @@ class ServerNetwork(Network): nb_mgmt_ip = nb.ipam.ip_addresses.get( address=switch_ip, ) - if not nb_mgmt_ip: - logging.error('Switch IP {} cannot be found in Netbox'.format(switch_ip)) - return nb_server_interface - + # Add the check here + if nb_mgmt_ip is not None: try: nb_switch = nb_mgmt_ip.assigned_object.device logging.info('Found a switch in Netbox based on LLDP infos: {} (id: {})'.format( @@ -557,6 +555,9 @@ class ServerNetwork(Network): ) ) return nb_server_interface + else: + logging.error('No NetBox IP found for switch IP {}'.format(switch_ip)) + return nb_server_interface switch_interface = self.lldp.get_switch_port(nb_server_interface.name) nb_switch_interface = nb.dcim.interfaces.get(