3232from .constants import NETMIKO_TO_NAPALM_STATIC
3333from .exceptions import OnboardException
3434
35+ logger = logging .getLogger ("rq.worker" )
36+
3537PLUGIN_SETTINGS = settings .PLUGINS_CONFIG ["netbox_onboarding" ]
3638
3739
@@ -144,7 +146,7 @@ def check_reachability(self):
144146 OnboardException('fail-connect'):
145147 When device unreachable
146148 """
147- logging .info ("CHECK: IP %s:%s" , self .hostname , self .port )
149+ logger .info ("CHECK: IP %s:%s" , self .hostname , self .port )
148150
149151 try :
150152 sock = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
@@ -169,32 +171,32 @@ def guess_netmiko_device_type(self):
169171 }
170172
171173 try :
172- logging .info ("INFO guessing device type: %s" , self .hostname )
174+ logger .info ("INFO guessing device type: %s" , self .hostname )
173175 guesser = SSHDetect (** remote_device )
174176 guessed_device_type = guesser .autodetect ()
175- logging .info ("INFO guessed device type: %s" , guessed_device_type )
177+ logger .info ("INFO guessed device type: %s" , guessed_device_type )
176178
177179 except NetMikoAuthenticationException as err :
178- logging .error ("ERROR %s" , err )
180+ logger .error ("ERROR %s" , err )
179181 raise OnboardException (reason = "fail-login" , message = f"ERROR: { str (err )} " )
180182
181183 except (NetMikoTimeoutException , SSHException ) as err :
182- logging .error ("ERROR: %s" , str (err ))
184+ logger .error ("ERROR: %s" , str (err ))
183185 raise OnboardException (reason = "fail-connect" , message = f"ERROR: { str (err )} " )
184186
185187 except Exception as err :
186- logging .error ("ERROR: %s" , str (err ))
188+ logger .error ("ERROR: %s" , str (err ))
187189 raise OnboardException (reason = "fail-general" , message = f"ERROR: { str (err )} " )
188190
189- logging .info ("INFO device type is: %s" , guessed_device_type )
191+ logger .info ("INFO device type is: %s" , guessed_device_type )
190192
191193 return guessed_device_type
192194
193195 def set_napalm_driver_name (self ):
194196 """Sets napalm driver name."""
195197 if not self .napalm_driver :
196198 netmiko_device_type = self .guess_netmiko_device_type ()
197- logging .info ("Guessed Netmiko Device Type: %s" , netmiko_device_type )
199+ logger .info ("Guessed Netmiko Device Type: %s" , netmiko_device_type )
198200
199201 self .netmiko_device_type = netmiko_device_type
200202
@@ -234,7 +236,7 @@ def get_onboarding_facts(self):
234236
235237 self .check_reachability ()
236238
237- logging .info ("COLLECT: device information %s" , self .hostname )
239+ logger .info ("COLLECT: device information %s" , self .hostname )
238240
239241 try :
240242 # Get Napalm Driver with Netmiko if needed
@@ -257,20 +259,32 @@ def get_onboarding_facts(self):
257259
258260 napalm_device .open ()
259261
260- logging .info ("COLLECT: device facts" )
262+ logger .info ("COLLECT: device facts" )
261263 self .facts = napalm_device .get_facts ()
262264
263- logging .info ("COLLECT: device interface IPs" )
265+ logger .info ("COLLECT: device interface IPs" )
264266 self .ip_ifs = napalm_device .get_interfaces_ip ()
265267
266- try :
267- module_name = PLUGIN_SETTINGS ["onboarding_extensions_map" ].get (self .napalm_driver )
268- module = importlib .import_module (module_name )
269- driver_addon_class = module .OnboardingDriverExtensions (napalm_device = napalm_device )
270- self .onboarding_class = driver_addon_class .onboarding_class
271- self .driver_addon_result = driver_addon_class .ext_result
272- except ImportError as exc :
273- logging .info ("No onboarding extension found for driver %s" , self .napalm_driver )
268+ module_name = PLUGIN_SETTINGS ["onboarding_extensions_map" ].get (self .napalm_driver )
269+
270+ if module_name :
271+ try :
272+ module = importlib .import_module (module_name )
273+ driver_addon_class = module .OnboardingDriverExtensions (napalm_device = napalm_device )
274+ self .onboarding_class = driver_addon_class .onboarding_class
275+ self .driver_addon_result = driver_addon_class .ext_result
276+ except ModuleNotFoundError as exc :
277+ raise OnboardException (
278+ reason = "fail-general" ,
279+ message = f"ERROR: ModuleNotFoundError: Onboarding extension for napalm driver { self .napalm_driver } configured but can not be imported per configuration" ,
280+ )
281+ except ImportError as exc :
282+ raise OnboardException (reason = "fail-general" , message = "ERROR: ImportError: %s" % exc .args [0 ])
283+ else :
284+ logger .info (
285+ "INFO: No onboarding extension defined for napalm driver %s, using default napalm driver" ,
286+ self .napalm_driver ,
287+ )
274288
275289 except ConnectionException as exc :
276290 raise OnboardException (reason = "fail-login" , message = exc .args [0 ])
0 commit comments