Skip to content

Commit 234ad32

Browse files
author
Josh VanDeraa
committed
Brings in changes for name lookup
1 parent d3c6bbf commit 234ad32

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

netbox_onboarding/onboard.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
from napalm import get_network_driver
2020
from napalm.base.exceptions import ConnectionException, CommandErrorException
21+
import netaddr
22+
from netaddr.core import AddrFormatError
2123

2224
from django.conf import settings
2325
from django.utils.text import slugify
@@ -236,6 +238,46 @@ def get_platform_object_from_netbox(
236238

237239
return platform
238240

241+
def check_ip(self):
242+
"""Method to check if the IP address form field was an IP address.
243+
244+
If it is a DNS name, attempt to resolve the DNS address and assign the IP address to the
245+
name.
246+
247+
Returns:
248+
(bool): True if the IP address is an IP address, or a DNS entry was found and
249+
reassignment of the ot.ip_address was done.
250+
False if unable to find a device IP (error)
251+
252+
Raises:
253+
OnboardException("fail-general"):
254+
When a prefix was entered for an IP address
255+
OnboardException("fail-dns"):
256+
When a Name lookup via DNS fails to resolve an IP address
257+
"""
258+
try:
259+
# Assign checked_ip to None for error handling
260+
# If successful, this is an IP address and can pass
261+
checked_ip = netaddr.IPAddress(self.ot.ip_address)
262+
return True
263+
# Catch when someone has put in a prefix address, raise an exception
264+
except ValueError:
265+
raise OnboardException(
266+
reason="fail-general", message=f"ERROR appears a prefix was entered: {self.ot.ip_address}"
267+
)
268+
# An AddrFormatError exception means that there is not an IP address in the field, and should continue on
269+
except AddrFormatError:
270+
try:
271+
# Do a lookup of name to get the IP address to connect to
272+
checked_ip = socket.gethostbyname(self.ot.ip_address)
273+
self.ot.ip_address = checked_ip
274+
return True
275+
except socket.gaierror:
276+
# DNS Lookup has failed, Raise an exception for unable to complete DNS lookup
277+
raise OnboardException(
278+
reason="fail-dns", message=f"ERROR failed to complete DNS lookup: {self.ot.ip_address}"
279+
)
280+
239281
def get_required_info(
240282
self,
241283
default_mgmt_if=PLUGIN_SETTINGS["default_management_interface"],

netbox_onboarding/tests/test_netbox_keeper.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
See the License for the specific language governing permissions and
1212
limitations under the License.
1313
"""
14+
from socket import gaierror
15+
from unittest import mock
1416
from django.test import TestCase
1517
from django.utils.text import slugify
1618

0 commit comments

Comments
 (0)