|
18 | 18 |
|
19 | 19 | from napalm import get_network_driver
|
20 | 20 | from napalm.base.exceptions import ConnectionException, CommandErrorException
|
| 21 | +import netaddr |
| 22 | +from netaddr.core import AddrFormatError |
21 | 23 |
|
22 | 24 | from django.conf import settings
|
23 | 25 | from django.utils.text import slugify
|
@@ -236,6 +238,46 @@ def get_platform_object_from_netbox(
|
236 | 238 |
|
237 | 239 | return platform
|
238 | 240 |
|
| 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 | + |
239 | 281 | def get_required_info(
|
240 | 282 | self,
|
241 | 283 | default_mgmt_if=PLUGIN_SETTINGS["default_management_interface"],
|
|
0 commit comments