|
| 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 |
0 commit comments