Skip to content

Commit 66b120e

Browse files
committed
Onboarding process refactor
1 parent d38343d commit 66b120e

File tree

14 files changed

+1416
-768
lines changed

14 files changed

+1416
-768
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
"""Example of custom onboarding class.
2+
3+
(c) 2020 Network To Code
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
Unless required by applicable law or agreed to in writing, software
9+
distributed under the License is distributed on an "AS IS" BASIS,
10+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
See the License for the specific language governing permissions and
12+
limitations under the License.
13+
"""
14+
15+
from netbox_onboarding.netbox_keeper import NetboxKeeper
16+
from netbox_onboarding.onboarding.onboarding import Onboarding
17+
18+
19+
class MyOnboardingClass(Onboarding):
20+
"""Custom onboarding class example.
21+
22+
Main purpose of this class is to access and modify the onboarding_kwargs.
23+
By accessing the onboarding kwargs, user gains ability to modify
24+
onboarding parameters before the objects are created in NetBox.
25+
26+
This class adds the get_device_role method that does the static
27+
string comparison and returns the device role.
28+
"""
29+
30+
def run(self, onboarding_kwargs):
31+
"""Ensures network device."""
32+
# Access hostname from onboarding_kwargs and get device role automatically
33+
device_new_role = self.get_device_role(hostname=onboarding_kwargs["netdev_hostname"])
34+
35+
# Update the device role in onboarding kwargs dictionary
36+
onboarding_kwargs["netdev_nb_role_slug"] = device_new_role
37+
38+
nb_k = NetboxKeeper(**onboarding_kwargs)
39+
nb_k.ensure_device()
40+
41+
self.created_device = nb_k.device
42+
43+
@staticmethod
44+
def get_device_role(hostname):
45+
"""Returns the device role based on hostname data.
46+
47+
This is a static analysis of hostname string content only
48+
"""
49+
hostname_lower = hostname.lower()
50+
if ("rtr" in hostname_lower) or ("router" in hostname_lower):
51+
role = "router"
52+
elif ("sw" in hostname_lower) or ("switch" in hostname_lower):
53+
role = "switch"
54+
elif ("fw" in hostname_lower) or ("firewall" in hostname_lower):
55+
role = "firewall"
56+
elif "dc" in hostname_lower:
57+
role = "datacenter"
58+
else:
59+
role = "generic"
60+
61+
return role
62+
63+
64+
class OnboardingDriverExtensions:
65+
"""This is an example of a custom onboarding driver extension.
66+
67+
This extension sets the onboarding_class to MyOnboardingClass,
68+
which is an example class of how to access and modify the device
69+
role automatically through the onboarding process.
70+
"""
71+
72+
def __init__(self, napalm_device):
73+
"""Inits the class."""
74+
self.napalm_device = napalm_device
75+
self.onboarding_class = MyOnboardingClass
76+
self.ext_result = None
77+
78+
def get_onboarding_class(self):
79+
"""Return onboarding class for IOS driver.
80+
81+
Currently supported is Standalone Onboarding Process
82+
83+
Result of this method is used by the OnboardingManager to
84+
initiate the instance of the onboarding class.
85+
"""
86+
return self.onboarding_class
87+
88+
def get_ext_result(self):
89+
"""This method is used to store any object as a return value.
90+
91+
Result of this method is passed to the onboarding class as
92+
driver_addon_result argument.
93+
94+
:return: Any()
95+
"""
96+
return self.ext_result

netbox_onboarding/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class OnboardingConfig(PluginConfig):
4040
"default_device_status": "active",
4141
"create_management_interface_if_missing": True,
4242
"platform_map": {},
43+
"onboarding_extensions_map": {"ios": "netbox_onboarding.onboarding_extensions.ios",},
4344
}
4445
caching_config = {}
4546

netbox_onboarding/constants.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Constants for netbox_onboarding plugin."""
2-
NETMIKO_TO_NAPALM = {
2+
3+
NETMIKO_TO_NAPALM_STATIC = {
34
"cisco_ios": "ios",
45
"cisco_nxos": "nxos_ssh",
56
"arista_eos": "eos",

netbox_onboarding/exceptions.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""Exceptions.
2+
3+
(c) 2020 Network To Code
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
Unless required by applicable law or agreed to in writing, software
9+
distributed under the License is distributed on an "AS IS" BASIS,
10+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
See the License for the specific language governing permissions and
12+
limitations under the License.
13+
"""
14+
15+
16+
class OnboardException(Exception):
17+
"""A failure occurred during the onboarding process.
18+
19+
The exception includes a reason "slug" as defined below as well as a humanized message.
20+
"""
21+
22+
REASONS = (
23+
"fail-config", # config provided is not valid
24+
"fail-connect", # device is unreachable at IP:PORT
25+
"fail-execute", # unable to execute device/API command
26+
"fail-login", # bad username/password
27+
"fail-dns", # failed to get IP address from name resolution
28+
"fail-general", # other error
29+
)
30+
31+
def __init__(self, reason, message, **kwargs):
32+
"""Exception Init."""
33+
super(OnboardException, self).__init__(kwargs)
34+
self.reason = reason
35+
self.message = message
36+
37+
def __str__(self):
38+
"""Exception __str__."""
39+
return f"{self.__class__.__name__}: {self.reason}: {self.message}"

0 commit comments

Comments
 (0)