Skip to content

Commit

Permalink
First pass at 2.0 migration (for nautobot) and nornir-nautobot 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
itdependsnetworks committed Sep 10, 2023
1 parent bff0ac4 commit 1964295
Show file tree
Hide file tree
Showing 23 changed files with 1,439 additions and 1,286 deletions.
22 changes: 11 additions & 11 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- name: "Set up Python"
uses: "actions/setup-python@v2"
with:
python-version: "3.9.7"
python-version: "3.11"
- name: "Install Python Packages for Linting"
run: "pip install --upgrade pip wheel invoke toml black"
- name: "Linting: Black"
Expand All @@ -29,7 +29,7 @@ jobs:
- name: "Set up Python"
uses: "actions/setup-python@v2"
with:
python-version: "3.9"
python-version: "3.11"
- name: "Install Python Packages for Linting"
run: "pip install --upgrade pip wheel invoke toml bandit"
- name: "Linting: Bandit"
Expand All @@ -46,7 +46,7 @@ jobs:
- name: "Set up Python"
uses: "actions/setup-python@v2"
with:
python-version: "3.9"
python-version: "3.11"
- name: "Install Python Packages for Linting"
run: "pip install --upgrade pip wheel invoke toml pydocstyle"
- name: "Linting: pydocstyle"
Expand All @@ -63,7 +63,7 @@ jobs:
- name: "Set up Python"
uses: "actions/setup-python@v2"
with:
python-version: "3.9"
python-version: "3.11"
- name: "Install Python Packages for Linting"
run: "pip install --upgrade pip wheel invoke toml flake8"
- name: "Linting: flake8"
Expand All @@ -80,7 +80,7 @@ jobs:
- name: "Set up Python"
uses: "actions/setup-python@v2"
with:
python-version: "3.9"
python-version: "3.11"
- name: "Install Python Packages for Linting"
run: "pip install --upgrade pip wheel invoke toml yamllint"
- name: "Linting: yamllint"
Expand All @@ -95,7 +95,7 @@ jobs:
- name: "Set up Python"
uses: "actions/setup-python@v2"
with:
python-version: "3.9"
python-version: "3.11"
- name: "Install Python Packages for Setup"
run: "pip install invoke poetry toml"
- name: "Build Container"
Expand All @@ -113,7 +113,7 @@ jobs:
- name: "Set up Python"
uses: "actions/setup-python@v2"
with:
python-version: "3.9"
python-version: "3.11"
- name: "Install Python Packages for Linting"
run: "pip install invoke poetry toml"
- name: "Build Container"
Expand All @@ -126,7 +126,7 @@ jobs:
strategy:
fail-fast: true
matrix:
python-version: ["3.7", "3.8", "3.9", "3.10"]
python-version: ["3.8", "3.9", "3.10", "3.11"]
runs-on: "ubuntu-20.04"
env:
PYTHON_VER: "${{ matrix.python-version }}"
Expand All @@ -136,7 +136,7 @@ jobs:
- name: "Set up Python"
uses: "actions/setup-python@v2"
with:
python-version: "3.9"
python-version: "3.11"
- name: "Install Python Packages for Linting"
run: "pip install invoke poetry toml"
- name: "Build Container"
Expand All @@ -155,7 +155,7 @@ jobs:
- name: "Set up Python"
uses: "actions/setup-python@v2"
with:
python-version: "3.9"
python-version: "3.11"
- name: "Install Python Packages for Linting"
run: "pip install invoke poetry toml"
- name: "Set env"
Expand Down Expand Up @@ -184,7 +184,7 @@ jobs:
- name: "Set up Python"
uses: "actions/setup-python@v2"
with:
python-version: "3.9"
python-version: "3.11"
- name: "Install Python Packages"
run: "pip install poetry"
- name: "Set env"
Expand Down
61 changes: 33 additions & 28 deletions docs/dev/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
# Changelog

Remove legacy dispatcher method
error codes



## v2.6.0

- (#96) Changes backup_file to be conditional for get_config
- (#98) Adds merge_config method
- [#96](https://github.com/nautobot/nornir-nautobot/pull/96) Changes backup_file to be conditional for get_config
- [#98](https://github.com/nautobot/nornir-nautobot/pull/98) Adds merge_config method

## v2.5.0

- (#93) Updates Nornir-Netmiko to 1.0.0 release
- (#97) Adds Jinja Environment option to generate_config
- [#93](https://github.com/nautobot/nornir-nautobot/pull/93) Updates Nornir-Netmiko to 1.0.0 release
- [#97](https://github.com/nautobot/nornir-nautobot/pull/97) Adds Jinja Environment option to generate_config

## v2.4.0

- (#61) Be more clear on error messages by @itdependsnetworks
- (#66) Add basic typing to the methods in logger by @KalleDK
- (#75) Documentation refactor by @susanhooks
- (#77) added ICX/Fastiron Nornir Driver by @pato23arg
- (#78) Fix RTD docs build by @cmsirbu
- (#83) Adds provision_config method by @joewesch
- (#76) Mikrotik RouterOS CLI Support by @pato23arg
- (#79) Mikrotik RouterOS API support by @pato23arg
- (#85) Ruckus Smartzone WLC and Access Point Driver by @pato23arg
- [#61](https://github.com/nautobot/nornir-nautobot/pull/61) Be more clear on error messages by @itdependsnetworks
- [#66](https://github.com/nautobot/nornir-nautobot/pull/66) Add basic typing to the methods in logger by @KalleDK
- [#75](https://github.com/nautobot/nornir-nautobot/pull/75) Documentation refactor by @susanhooks
- [#77](https://github.com/nautobot/nornir-nautobot/pull/77) added ICX/Fastiron Nornir Driver by @pato23arg
- [#78](https://github.com/nautobot/nornir-nautobot/pull/78) Fix RTD docs build by @cmsirbu
- [#83](https://github.com/nautobot/nornir-nautobot/pull/83) Adds provision_config method by @joewesch
- [#76](https://github.com/nautobot/nornir-nautobot/pull/76) Mikrotik RouterOS CLI Support by @pato23arg
- [#79](https://github.com/nautobot/nornir-nautobot/pull/79) Mikrotik RouterOS API support by @pato23arg
- [#85](https://github.com/nautobot/nornir-nautobot/pull/85) Ruckus Smartzone WLC and Access Point Driver by @pato23arg

## New Contributors
* @KalleDK made their first contribution in https://github.com/nautobot/nornir-nautobot/pull/66
Expand All @@ -31,41 +36,41 @@

## v2.3.0

- (#67) fix pylint, tests, and drop py36 support #67
- [#67](https://github.com/nautobot/nornir-nautobot/pull/67) fix pylint, tests, and drop py36 support #67

## v2.2.0

- (#41) Added jinja2 filter pass to generate config
- (#42) Added Cisco ASA mapping to default mapping
- [#41](https://github.com/nautobot/nornir-nautobot/pull/41) Added jinja2 filter pass to generate config
- [#42](https://github.com/nautobot/nornir-nautobot/pull/42) Added Cisco ASA mapping to default mapping

## v2.1.2

- (#40) Fix nornir inventory defaults
- [#40](https://github.com/nautobot/nornir-nautobot/pull/40) Fix nornir inventory defaults

## v2.1.1

- (#31) Migrate from Travis-CI to GitHub Actions for CI
- (#32) Fix data population when generating configurations from the dispatcher
- [#31](https://github.com/nautobot/nornir-nautobot/pull/31) Migrate from Travis-CI to GitHub Actions for CI
- [#32](https://github.com/nautobot/nornir-nautobot/pull/32) Fix data population when generating configurations from the dispatcher

## v2.1.0

- (#26) Updates in poetry packaging for pre 1.0.0 dependencies in Nornir to allow all new up to 1.0.0 releases
- [#26](https://github.com/nautobot/nornir-nautobot/pull/26) Updates in poetry packaging for pre 1.0.0 dependencies in Nornir to allow all new up to 1.0.0 releases

## v2.0.3

- (#24) Change import mechanism / changes deprecated function
- [#24](https://github.com/nautobot/nornir-nautobot/pull/24) Change import mechanism / changes deprecated function

## v2.0.1

- (#20) Fixes inventory failure when platform is not defined for a device
- [#20](https://github.com/nautobot/nornir-nautobot/pull/20) Fixes inventory failure when platform is not defined for a device
## v2.0.0

- (#18) Migrates functions to new NTC netutils library, which is removing methods previously available:
- compliance
- make_folder
- hostname_resolves
- test_tcp_port
- is_ip
- [#18](https://github.com/nautobot/nornir-nautobot/pull/18) Migrates functions to new NTC netutils library, which is removing methods previously available:
- compliance
- make_folder
- hostname_resolves
- test_tcp_port
- is_ip

## v0.1.0 - 2020-12-27

Expand Down
6 changes: 3 additions & 3 deletions docs/dev/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ This section describes how to install *nornir_nautobot* for development, how to

## Python Versions

This leverages Python3.6 or later. All features will be tested against 3.6 - 3.9 versions of Python.
This leverages Python3.8 or later. All features will be tested against 3.8 - 3.11 versions of Python.

## Versioning

Expand All @@ -27,7 +27,7 @@ Follow these steps to set up your local development environment:
```bash
# Double check your version
$ python --version
Python 3.7.7
Python 3.11.1
# Activate the Poetry environment, which will auto create the virtual environment related to the project
$ poetry shell
# Install project dependencies as well as development dependencies
Expand Down Expand Up @@ -61,7 +61,7 @@ All tests should be located within the `tests\` directory with `tests\unit` for
The following linting tasks are required:

* [Bandit](https://bandit.readthedocs.io/en/latest/)
* Basic security tests, should be run on Python3.6 or Python3.7
* Basic security tests, should be run on Python3.11
* [Black code style](https://github.com/psf/black)
* Code formatting with version 20.8b1. There are some differences in the format between versions 19 and 20.
* [Flake8](https://flake8.pycqa.org/en/latest/)
Expand Down
8 changes: 4 additions & 4 deletions docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mkdocs==1.3.1
mkdocs-material==8.3.9
mkdocstrings==0.19
mkdocstrings-python==0.7.1
mkdocs==1.5.2
mkdocs-material==9.2.4
mkdocstrings==0.22.0
mkdocstrings-python==1.5.2
mkdocs-version-annotations==1.0.0
6 changes: 3 additions & 3 deletions docs/task/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ The only task plugin currently is the "dispatcher" plugin. This plugin dispatche
try:
driver_task = getattr(driver_class, method)
except AttributeError:
logger.log_failure(obj, f"Unable to locate the method {method} for {driver}")
logger.log_error(obj, f"Unable to locate the method {method} for {driver}")
raise NornirNautobotException()

result = task.run(task=driver_task, *args, **kwargs)
Expand All @@ -24,8 +24,8 @@ The only task plugin currently is the "dispatcher" plugin. This plugin dispatche
class NautobotNornirDriver:
"""Default collection of Nornir Tasks based on Napalm."""

@staticmethod
def get_config(task: Task, backup_file: str, *args, **kwargs) -> Result:
@classmethod
def get_config(cls, task: Task, backup_file: str, *args, **kwargs) -> Result:
```

## Calling Dispatcher
Expand Down
79 changes: 32 additions & 47 deletions nornir_nautobot/plugins/tasks/dispatcher/__init__.py
Original file line number Diff line number Diff line change
@@ -1,73 +1,58 @@
"""Used to intialize the dispatcher."""
"""Used to initialize the dispatcher."""
# pylint: disable=raise-missing-from

import importlib
import logging

from nornir.core.task import Result, Task
from nornir_nautobot.exceptions import NornirNautobotException
from nornir_nautobot.utils.helpers import snake_to_title_case, import_string

LOGGER = logging.getLogger(__name__)
PATH_ROOT = "nornir_nautobot.plugins.tasks.dispatcher.default"

_DEFAULT_DRIVERS_MAPPING = {
"default": "nornir_nautobot.plugins.tasks.dispatcher.default.NautobotNornirDriver",
"default_netmiko": "nornir_nautobot.plugins.tasks.dispatcher.default.NetmikoNautobotNornirDriver",
"cisco_asa": "nornir_nautobot.plugins.tasks.dispatcher.cisco_asa.NautobotNornirDriver",
"cisco_nxos": "nornir_nautobot.plugins.tasks.dispatcher.cisco_nxos.NautobotNornirDriver",
"cisco_ios": "nornir_nautobot.plugins.tasks.dispatcher.cisco_ios.NautobotNornirDriver",
"cisco_xr": "nornir_nautobot.plugins.tasks.dispatcher.cisco_ios_xr.NautobotNornirDriver",
"juniper_junos": "nornir_nautobot.plugins.tasks.dispatcher.juniper_junos.NautobotNornirDriver",
"arista_eos": "nornir_nautobot.plugins.tasks.dispatcher.arista_eos.NautobotNornirDriver",
"mikrotik_routeros_api": "nornir_nautobot.plugins.tasks.dispatcher.mikrotik_routeros_api.NautobotNornirDriver",
"ruckus_fastiron": "nornir_nautobot.plugins.tasks.dispatcher.ruckus_fastiron.NautobotNornirDriver",
"mikrotik_routeros": "nornir_nautobot.plugins.tasks.dispatcher.mikrotik_routeros.NautobotNornirDriver",
"ruckus_smartzone_api": "nornir_nautobot.plugins.tasks.dispatcher.ruckus_smartzone_api.NautobotNornirDriver",
"ruckus_access_point": "nornir_nautobot.plugins.tasks.dispatcher.ruckus_smartzone_api.NautobotNornirDriver",
}


def dispatcher(task: Task, method: str, logger, obj, *args, **kwargs) -> Result:
def dispatcher( # pylint: disable=too-many-arguments,too-many-locals
task: Task, method: str, logger, obj, framework, *args, **kwargs
) -> Result:
"""Helper Task to retrieve a given Nornir task for a given platform.
Args:
task (Nornir Task): Nornir Task object.
method (str): The string value of the method to dynamically find.
task: Nornir Task object.
method: The string value of the method to dynamically find.
Returns:
Result: Nornir Task result.
Result: Nornir Task result object.
"""
if kwargs.get("default_drivers_mapping"):
default_drivers_mapping = kwargs["default_drivers_mapping"]
del kwargs["default_drivers_mapping"]
if not kwargs.get("custom_dispatcher"):
custom_dispatcher = {}
logger.log_debug(f"Dispatcher process started for {task.host.name} ({task.host.platform.network_driver})")

network_driver = task.host.platform.network_driver
network_driver_title = snake_to_title_case(network_driver)
custom_dispatcher_path = [custom_dispatcher.get(network_driver)]
framework_path = f"nornir_nautobot.plugins.tasks.dispatcher.{network_driver}.{framework}{network_driver_title}"
framework_default_path = f"nornir_nautobot.plugins.tasks.dispatcher.default.{framework}Default"

if custom_dispatcher.get(network_driver):
driver_class = import_string(custom_dispatcher_path)
checked_path = [custom_dispatcher_path]
elif import_string(framework_path):
driver_class = import_string(framework_path)
else:
default_drivers_mapping = _DEFAULT_DRIVERS_MAPPING

logger.log_debug(f"Executing dispatcher for {task.host.name} ({task.host.platform})")

# Get the platform specific driver, if not available, get the default driver
driver = default_drivers_mapping.get(task.host.platform, default_drivers_mapping.get("default"))
logger.log_debug(f"Found driver {driver}")

if not driver:
logger.log_failure(
obj, f"Unable to find the driver for {method} for platform: {task.host.platform}, preemptively failed."
)
raise NornirNautobotException(
f"Unable to find the driver for {method} for platform: {task.host.platform}, preemptively failed."
)

module_name, class_name = driver.rsplit(".", 1)
driver_class = getattr(importlib.import_module(module_name), class_name)
driver_class = import_string(framework_default_path)
checked_path = [framework_path, framework_default_path]

if not driver_class:
logger.log_failure(obj, f"Unable to locate the class {driver}, preemptively failed.")
raise NornirNautobotException(f"Unable to locate the class {driver}, preemptively failed.")
error_msg = f"E1001: Did not find a valid dispatcher in {checked_path}, preemptively failed."
logger.log_error(error_msg, extra={"object": obj})
raise NornirNautobotException(error_msg)

try:
driver_task = getattr(driver_class, method)
except AttributeError:
logger.log_failure(obj, f"Unable to locate the method {method} for {driver}, preemptively failed.")
raise NornirNautobotException(f"Unable to locate the method {method} for {driver}, preemptively failed.")
error_msg = f"E1002: Unable to locate the method {method} for {driver_class}, preemptively failed."
logger.log_error(error_msg, extra={"object": obj})
raise NornirNautobotException(error_msg)

result = task.run(task=driver_task, logger=logger, obj=obj, *args, **kwargs)

Expand Down
12 changes: 8 additions & 4 deletions nornir_nautobot/plugins/tasks/dispatcher/arista_eos.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
"""network_importer driver for arista_eos."""
"""nornir dispatcher for arista_eos."""

from .default import NautobotNornirDriver as DefaultNautobotNornirDriver
from nornir_nautobot.plugins.tasks.dispatcher.default import NapalmDefault, NetmikoDefault


class NautobotNornirDriver(DefaultNautobotNornirDriver):
"""Collection of Nornir Tasks specific to Arista EOS devices."""
class NapalmAristaEos(NapalmDefault):
"""Collection of Napalm Nornir Tasks specific to Arista EOS devices."""


class NetmikoAristaEos(NetmikoDefault):
"""Collection of Netmiko Nornir Tasks specific to Arista EOS devices."""

config_command = "show run"
Loading

0 comments on commit 1964295

Please sign in to comment.