Skip to content

Commit 2589077

Browse files
committedDec 18, 2020
Modified functions ensure_interface and ensure_primary_ip to check for class attributes
1 parent f34817d commit 2589077

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed
 

‎netbox_onboarding/netbox_keeper.py

+13-11
Original file line numberDiff line numberDiff line change
@@ -401,23 +401,25 @@ def ensure_device_instance(self, default_status=PLUGIN_SETTINGS["default_device_
401401

402402
def ensure_interface(self):
403403
"""Ensures that the interface associated with the mgmt_ipaddr exists and is assigned to the device."""
404-
self.nb_mgmt_ifname, _ = Interface.objects.get_or_create(name=self.netdev_mgmt_ifname, device=self.device)
404+
if self.netdev_mgmt_ifname:
405+
self.nb_mgmt_ifname, _ = Interface.objects.get_or_create(name=self.netdev_mgmt_ifname, device=self.device)
405406

406407
def ensure_primary_ip(self):
407408
"""Ensure mgmt_ipaddr exists in IPAM, has the device interface, and is assigned as the primary IP address."""
408409
# see if the primary IP address exists in IPAM
409-
self.nb_primary_ip, created = IPAddress.objects.get_or_create(
410-
address=f"{self.netdev_mgmt_ip_address}/{self.netdev_mgmt_pflen}"
411-
)
410+
if self.netdev_mgmt_ip_address and self.netdev_mgmt_pflen:
411+
self.nb_primary_ip, created = IPAddress.objects.get_or_create(
412+
address=f"{self.netdev_mgmt_ip_address}/{self.netdev_mgmt_pflen}"
413+
)
412414

413-
if created or not self.nb_primary_ip in self.nb_mgmt_ifname.ip_addresses.all():
414-
logger.info("ASSIGN: IP address %s to %s", self.nb_primary_ip.address, self.nb_mgmt_ifname.name)
415-
self.nb_mgmt_ifname.ip_addresses.add(self.nb_primary_ip)
416-
self.nb_mgmt_ifname.save()
415+
if created or not self.nb_primary_ip in self.nb_mgmt_ifname.ip_addresses.all():
416+
logger.info("ASSIGN: IP address %s to %s", self.nb_primary_ip.address, self.nb_mgmt_ifname.name)
417+
self.nb_mgmt_ifname.ip_addresses.add(self.nb_primary_ip)
418+
self.nb_mgmt_ifname.save()
417419

418-
# Ensure the primary IP is assigned to the device
419-
self.device.primary_ip4 = self.nb_primary_ip
420-
self.device.save()
420+
# Ensure the primary IP is assigned to the device
421+
self.device.primary_ip4 = self.nb_primary_ip
422+
self.device.save()
421423

422424
def ensure_device(self):
423425
"""Ensure that the device represented by the DevNetKeeper exists in the NetBox system."""

0 commit comments

Comments
 (0)
Please sign in to comment.