Skip to content

Commit f8648d9

Browse files
author
Josh VanDeraa
committed
Adds exceptions; reorders check_ip
1 parent 6fc4933 commit f8648d9

File tree

3 files changed

+37
-21
lines changed

3 files changed

+37
-21
lines changed

netbox_onboarding/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class OnboardingConfig(PluginConfig):
3838
"default_management_interface": "PLACEHOLDER",
3939
"default_management_prefix_length": 0,
4040
"default_device_status": "active",
41+
"create_management_interface_if_missing": True,
4142
}
4243
caching_config = {}
4344

netbox_onboarding/onboard.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -212,27 +212,28 @@ def check_ip(self):
212212
False if unable to find a device IP (error)
213213
214214
"""
215-
# Assign checked_ip to None for error handling
216215
try:
216+
# Assign checked_ip to None for error handling
217217
# If successful, this is an IP address and can pass
218-
checked_ip = netaddr.IPNetwork(self.ot.ip_address)
218+
checked_ip = netaddr.IPAddress(self.ot.ip_address)
219219
return True
220+
# Catch when someone has put in a prefix address, raise an exception
221+
except ValueError:
222+
raise OnboardException(
223+
reason="fail-prefix", message=f"ERROR appears a prefix was entered: {self.ot.ip_address}"
224+
)
220225
# An AddrFormatError exception means that there is not an IP address in the field, and should continue on
221226
except AddrFormatError:
222-
pass
223-
# Catch when someone has put in a prefix address
224-
except ValueError:
225-
pass
226-
227-
# An IP address was not detected in the IP address field, attempt to find DNS
228-
try:
229-
# Do a lookup of name
230-
checked_ip = socket.gethostbyname(self.ot.ip_address)
231-
self.ot.ip_address = checked_ip
232-
return True
233-
except socket.gaierror:
234-
# DNS Lookup has failed, not a DNS, do not do anything
235-
return False
227+
try:
228+
# Do a lookup of name to get the IP address to connect to
229+
checked_ip = socket.gethostbyname(self.ot.ip_address)
230+
self.ot.ip_address = checked_ip
231+
return True
232+
except socket.gaierror:
233+
# DNS Lookup has failed, Raise an exception for unable to complete DNS lookup
234+
raise OnboardException(
235+
reason="fail-dns", message=f"ERROR failed to complete DNS lookup: {self.ot.ip_address}"
236+
)
236237

237238
def get_required_info(
238239
self,
@@ -512,5 +513,6 @@ def ensure_device(self):
512513
self.ensure_device_type()
513514
self.ensure_device_role()
514515
self.ensure_device_instance()
515-
self.ensure_interface()
516-
self.ensure_primary_ip()
516+
if PLUGIN_SETTINGS["create_management_interface_if_missing"]:
517+
self.ensure_interface()
518+
self.ensure_primary_ip()

netbox_onboarding/tests/test_onboard.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ def setUp(self):
4949
self.onboarding_task5 = OnboardingTask.objects.create(
5050
ip_address="bad.local", site=self.site1, role=self.device_role1, platform=self.platform1
5151
)
52+
self.onboarding_task7 = OnboardingTask.objects.create(
53+
ip_address="192.0.2.1/32", site=self.site1, role=self.device_role1, platform=self.platform1
54+
)
5255

5356
self.ndk1 = NetdevKeeper(self.onboarding_task1)
5457
self.ndk1.hostname = "device1"
@@ -181,7 +184,7 @@ def test_ensure_interface_exist(self):
181184
self.assertEqual(nbk.interface, intf)
182185

183186
def test_ensure_primary_ip_not_exist(self):
184-
"""Verify ensure_primary_ip function when the Ip address do not already exist."""
187+
"""Verify ensure_primary_ip function when the IP address do not already exist."""
185188
nbk = NetboxKeeper(self.ndk2)
186189
nbk.device_type = self.device_type1
187190
nbk.netdev.ot = self.onboarding_task3
@@ -213,6 +216,16 @@ def test_failed_check_ip(self, mock_get_hostbyname):
213216
# Look up a failed response
214217
mock_get_hostbyname.side_effect = gaierror(8)
215218
ndk5 = NetdevKeeper(self.onboarding_task5)
219+
ndk7 = NetdevKeeper(self.onboarding_task7)
220+
221+
# Check for bad.local raising an exception
222+
with self.assertRaises(OnboardException) as exc_info:
223+
ndk5.check_ip()
224+
self.assertEqual(exc_info.exception.message, "ERROR failed to complete DNS lookup: bad.local")
225+
self.assertEqual(exc_info.exception.reason, "fail-dns")
216226

217-
self.assertFalse(ndk5.check_ip())
218-
self.assertEqual(ndk5.ot.ip_address, "bad.local")
227+
# Check for exception with prefix address entered
228+
with self.assertRaises(OnboardException) as exc_info:
229+
ndk7.check_ip()
230+
self.assertEqual(exc_info.exception.reason, "fail-prefix")
231+
self.assertEqual(exc_info.exception.message, "ERROR appears a prefix was entered: 192.0.2.1/32")

0 commit comments

Comments
 (0)