From 7fd35f5d6fbbcbd33e5017e4740938c140480cd7 Mon Sep 17 00:00:00 2001 From: thatmattlove Date: Mon, 26 Dec 2022 16:23:56 -0500 Subject: [PATCH] fix `device_type` selection for linux platforms --- hyperglass/constants.py | 5 +++++ hyperglass/execution/drivers/ssh_netmiko.py | 2 +- hyperglass/models/config/devices.py | 20 +++++++++++++++++++- 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/hyperglass/constants.py b/hyperglass/constants.py index 75db52b..b04ea11 100644 --- a/hyperglass/constants.py +++ b/hyperglass/constants.py @@ -80,3 +80,8 @@ DRIVER_MAP = { "frr": "netmiko", "http": "hyperglass_http_client", } + +LINUX_PLATFORMS = ( + "frr", + "bird", +) diff --git a/hyperglass/execution/drivers/ssh_netmiko.py b/hyperglass/execution/drivers/ssh_netmiko.py index 8c601e1..16ed067 100644 --- a/hyperglass/execution/drivers/ssh_netmiko.py +++ b/hyperglass/execution/drivers/ssh_netmiko.py @@ -64,7 +64,7 @@ class NetmikoConnection(SSHConnection): driver_kwargs = { "host": host or self.device._target, "port": port or self.device.port, - "device_type": self.device.platform, + "device_type": self.device.get_device_type(), "username": self.device.credential.username, "global_delay_factor": 0.1, "timeout": math.floor(params.request_timeout * 1.25), diff --git a/hyperglass/models/config/devices.py b/hyperglass/models/config/devices.py index 15d89f3..fc191e0 100644 --- a/hyperglass/models/config/devices.py +++ b/hyperglass/models/config/devices.py @@ -15,7 +15,12 @@ from hyperglass.log import log from hyperglass.util import get_driver, get_fmt_keys, resolve_hostname from hyperglass.state import use_state from hyperglass.settings import Settings -from hyperglass.constants import DRIVER_MAP, SCRAPE_HELPERS, SUPPORTED_STRUCTURED_OUTPUT +from hyperglass.constants import ( + DRIVER_MAP, + SCRAPE_HELPERS, + LINUX_PLATFORMS, + SUPPORTED_STRUCTURED_OUTPUT, +) from hyperglass.exceptions.private import ConfigError, UnsupportedDevice # Local @@ -124,6 +129,19 @@ class Device(HyperglassModelWithId, extra="allow"): return True return False + def get_device_type(self) -> str: + """Get the `device_type` field for use by Netmiko. + + In some cases, the platform might be different than the + device_type. For example, any linux-based platform like FRR, + BIRD, or OpenBGPD will have directives associated with those + platforms, but the `device_type` sent to Netmiko needs to be + `linux_ssh`. + """ + if self.platform in LINUX_PLATFORMS: + return "linux_ssh" + return self.platform + def _validate_directive_attrs(self) -> None: # Set of all keys except for built-in key `target`.