Skip to content

Commit d375845

Browse files
dgarroscarbonarok
authored andcommitted
Fix for isses #80, #102, #103.
Fix pydocstyle Increase version to 1.3.0 Adds doc to restart NB svcs, Adds doc for install in venv Onboarding process refactor fix async task race condition NetBox 2.9 support expose onboarding details in device view skip onboarding update version to 2.0.0-beta1 Improve Logging for netdev_keeper.py Fixes onboarding_extensions_map issue The onboarding_extensions_map setting (specified in netbox_onboarding/__init__.py or overridden in configuration.py) is used to map napalm driver names to a custom class which extends the driver, allowing extensibility. Currently, when a mapping doesn't exist for a napalm driver, the NetdevKeepr class's get_onboarding_facts() method fails. This causes the rq-worker to be unable to run the onbaord_device() function to onboard a device. The changes in this commit fix the issue. Render right_page onboarding template correctly Currently, the template_content.py DeviceContent class returns `None` if onboarding is not enabled for an OnboardingDevice object. Likewise, if no OnboardingDevice object exists, the template continues trying to access attributes for an OnboardingDevice object. In the first case, template rendering will fail as an empty string is needed in order to insert nothing into the rendered HTML template presented to the user. In the second case, an AttributeError is raised as you can not access attributes of a NoneType object. Enhance logging and error handling Change version to 2.0.0-beta.2 Add NetBox's changelog model to OnboardingTask documentation update support to skip device type updates Expose onboarding metrics on worker level Changes for multiple criteria searching Update version to 2.0.0 Optional arguments for NAPALM and Netmiko Rebase Rebase fix Fixes for issues #80, #102, #103 Black reformatting Platform option args removed Rebase Rebase fix Fixes for issues #80, #102, #103 Fixes for issues #80, #102, #103
1 parent 986fe8b commit d375845

File tree

3 files changed

+50
-10
lines changed

3 files changed

+50
-10
lines changed

netbox_onboarding/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class OnboardingConfig(PluginConfig):
4444
"platform_map": {},
4545
"onboarding_extensions_map": {"ios": "netbox_onboarding.onboarding_extensions.ios",},
4646
"object_match_strategy": "loose",
47+
"netmiko_optional_args": {"timeout": 10, "session_timeout": 10, "auth_timeout": 10, "banner_timeout": 10},
4748
}
4849
caching_config = {}
4950

netbox_onboarding/netdev_keeper.py

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from django.conf import settings
2121
from napalm import get_network_driver
2222
from napalm.base.exceptions import ConnectionException, CommandErrorException
23+
from napalm.base.netmiko_helpers import netmiko_args
2324
from netaddr.core import AddrFormatError
2425
from netmiko.ssh_autodetect import SSHDetect
2526
from netmiko.ssh_exception import NetMikoAuthenticationException
@@ -64,7 +65,15 @@ class NetdevKeeper:
6465
"""Used to maintain information about the network device during the onboarding process."""
6566

6667
def __init__( # pylint: disable=R0913
67-
self, hostname, port=None, timeout=None, username=None, password=None, secret=None, napalm_driver=None
68+
self,
69+
hostname,
70+
port=None,
71+
timeout=None,
72+
username=None,
73+
password=None,
74+
secret=None,
75+
napalm_driver=None,
76+
optional_args=None,
6877
):
6978
"""Initialize the network device keeper instance and ensure the required configuration parameters are provided.
7079
@@ -85,10 +94,11 @@ def __init__( # pylint: disable=R0913
8594
self.hostname = hostname
8695
self.port = port
8796
self.timeout = timeout
88-
self.username = username or settings.NAPALM_USERNAME
89-
self.password = password or settings.NAPALM_PASSWORD
90-
self.secret = secret or settings.NAPALM_ARGS.get("secret", None)
97+
self.username = username
98+
self.password = password
99+
self.secret = secret
91100
self.napalm_driver = napalm_driver
101+
self.optional_args = optional_args
92102

93103
self.facts = None
94104
self.ip_ifs = None
@@ -162,14 +172,25 @@ def guess_netmiko_device_type(self):
162172
"""Guess the device type of host, based on Netmiko."""
163173
guessed_device_type = None
164174

175+
netmiko_optional_args = netmiko_args(self.optional_args)
176+
165177
remote_device = {
166178
"device_type": "autodetect",
167179
"host": self.hostname,
168180
"username": self.username,
169181
"password": self.password,
170-
"secret": self.secret,
182+
**netmiko_optional_args,
171183
}
172184

185+
if self.secret:
186+
remote_device["secret"] = self.secret
187+
188+
if self.port:
189+
remote_device["port"] = self.port
190+
191+
if self.timeout:
192+
remote_device["timeout"] = self.timeout
193+
173194
try:
174195
logger.info("INFO guessing device type: %s", self.hostname)
175196
guesser = SSHDetect(**remote_device)
@@ -246,8 +267,13 @@ def get_onboarding_facts(self):
246267
self.check_napalm_driver_name()
247268

248269
driver = get_network_driver(self.napalm_driver)
249-
optional_args = settings.NAPALM_ARGS.copy()
250-
optional_args["secret"] = self.secret
270+
271+
optional_args = self.optional_args
272+
if self.port:
273+
optional_args["port"] = self.port
274+
275+
if self.secret:
276+
optional_args["secret"] = self.secret
251277

252278
napalm_device = driver(
253279
hostname=self.hostname,

netbox_onboarding/onboard.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
limitations under the License.
1313
"""
1414

15+
import json
16+
1517
from django.conf import settings
1618

1719
from .netdev_keeper import NetdevKeeper
@@ -34,6 +36,16 @@ def napalm_driver(self):
3436

3537
return None
3638

39+
@property
40+
def optional_args(self):
41+
"""Return platform optional args."""
42+
if self.ot.platform and self.ot.platform.napalm_args:
43+
napalm_args = json.loads(self.ot.platform.napalm_args)
44+
45+
return napalm_args
46+
47+
return {}
48+
3749
@property
3850
def ip_address(self):
3951
"""Return ot's ip address."""
@@ -86,10 +98,11 @@ def __init__(self, ot, username, password, secret):
8698
hostname=otm.ip_address,
8799
port=otm.port,
88100
timeout=otm.timeout,
89-
username=self.username,
90-
password=self.password,
91-
secret=self.secret,
101+
username=self.username or settings.NAPALM_USERNAME,
102+
password=self.password or settings.NAPALM_PASSWORD,
103+
secret=self.secret or otm.optional_args.get("secret", None) or settings.NAPALM_ARGS.get("secret", None),
92104
napalm_driver=otm.napalm_driver,
105+
optional_args=otm.optional_args or settings.NAPALM_ARGS,
93106
)
94107

95108
netdev.get_onboarding_facts()

0 commit comments

Comments
 (0)