Skip to content

Commit

Permalink
Use community drivers on discovery and also give hint for user
Browse files Browse the repository at this point in the history
  • Loading branch information
leoparente committed Jun 28, 2024
1 parent 154750c commit 2cd1a35
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
8 changes: 7 additions & 1 deletion diode-napalm-agent/diode_napalm/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from napalm import get_network_driver

from diode_napalm.client import Client
from diode_napalm.discovery import discover_device_driver
from diode_napalm.discovery import discover_device_driver, supported_drivers
from diode_napalm.parser import (
Diode,
DiscoveryConfig,
Expand Down Expand Up @@ -45,6 +45,12 @@ def run_driver(info: Napalm, config: DiscoveryConfig):
raise Exception(
f"Hostname {info.hostname}: Not able to discover device driver"
)
elif not info.driver in supported_drivers:
raise Exception(
f"Hostname {info.hostname}: specified driver '{info.driver}' was not found in the current supported drivers list: "
f"{supported_drivers}.\nHINT: If '{info.driver}' is a community napalm driver, try to perform the following command:"
f"\n\n\tpip install napalm-{info.driver.replace('_', '-')}\n"
)

logger.info(f"Hostname {info.hostname}: Get driver '{info.driver}'")
np_driver = get_network_driver(info.driver)
Expand Down
27 changes: 26 additions & 1 deletion diode-napalm-agent/diode_napalm/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Copyright 2024 NetBox Labs Inc
"""Discover the correct NAPALM Driver."""

import importlib_metadata
import logging

from napalm import get_network_driver
Expand All @@ -10,7 +11,31 @@
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

supported_drivers = ["ios", "eos", "junos", "nxos"]

def napalm_driver_list():
"""
List the available NAPALM drivers.
This function scans the installed Python packages to identify NAPALM drivers,
appending their names (with the 'napalm-' prefix removed and hyphens replaced
with underscores) to a list of known drivers.
Returns:
-------
List[str]: A list of strings representing the names of available NAPALM drivers.
The list includes some predefined driver names and dynamically
discovered driver names from the installed packages.
"""
napalm_packages = ["ios", "eos", "junos", "nxos"]
prefix = "napalm-"
for dist in importlib_metadata.distributions():
if dist.metadata["Name"].startswith(prefix):
package = dist.metadata["Name"][len(prefix) :].replace("-", "_")
napalm_packages.append(package)
return napalm_packages


supported_drivers = napalm_driver_list()


def set_napalm_logs_level(level: int):
Expand Down

0 comments on commit 2cd1a35

Please sign in to comment.