mirror of
https://github.com/checktheroads/hyperglass
synced 2024-05-11 05:55:08 +00:00
set juniper_junos to juniper if used, improve device validation. closes #104
This commit is contained in:
@@ -63,9 +63,10 @@ FUNC_COLOR_MAP = {
|
|||||||
TRANSPORT_REST = ("frr", "bird")
|
TRANSPORT_REST = ("frr", "bird")
|
||||||
|
|
||||||
SCRAPE_HELPERS = {
|
SCRAPE_HELPERS = {
|
||||||
|
"mikrotik": "mikrotik_routeros",
|
||||||
|
"juniper_junos": "juniper",
|
||||||
"junos": "juniper",
|
"junos": "juniper",
|
||||||
"ios": "cisco_ios",
|
"ios": "cisco_ios",
|
||||||
"mikrotik": "mikrotik_routeros",
|
|
||||||
"tsnr": "tnsr",
|
"tsnr": "tnsr",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -8,7 +8,7 @@ from pathlib import Path
|
|||||||
from ipaddress import IPv4Address, IPv6Address
|
from ipaddress import IPv4Address, IPv6Address
|
||||||
|
|
||||||
# Third Party
|
# Third Party
|
||||||
from pydantic import StrictInt, StrictStr, StrictBool, validator
|
from pydantic import StrictInt, StrictStr, StrictBool, validator, root_validator
|
||||||
|
|
||||||
# Project
|
# Project
|
||||||
from hyperglass.log import log
|
from hyperglass.log import log
|
||||||
@@ -113,26 +113,6 @@ class Device(HyperglassModel):
|
|||||||
|
|
||||||
return value
|
return value
|
||||||
|
|
||||||
@validator("nos")
|
|
||||||
def supported_nos(cls, value):
|
|
||||||
"""Validate that nos is supported by hyperglass.
|
|
||||||
|
|
||||||
Raises:
|
|
||||||
UnsupportedDevice: Raised if nos is unsupported.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
{str} -- Valid NOS
|
|
||||||
"""
|
|
||||||
if value in SCRAPE_HELPERS.keys():
|
|
||||||
value = SCRAPE_HELPERS[value]
|
|
||||||
|
|
||||||
supported, _ = validate_nos(value)
|
|
||||||
|
|
||||||
if not supported:
|
|
||||||
raise UnsupportedDevice('"{nos}" is not supported.', nos=value)
|
|
||||||
|
|
||||||
return value
|
|
||||||
|
|
||||||
@validator("ssl")
|
@validator("ssl")
|
||||||
def validate_ssl(cls, value, values):
|
def validate_ssl(cls, value, values):
|
||||||
"""Set default cert file location if undefined.
|
"""Set default cert file location if undefined.
|
||||||
@@ -154,18 +134,43 @@ class Device(HyperglassModel):
|
|||||||
value.cert = cert_file
|
value.cert = cert_file
|
||||||
return value
|
return value
|
||||||
|
|
||||||
@validator("commands", always=True)
|
@root_validator(pre=True)
|
||||||
def validate_commands(cls, value: str, values: "Device") -> str:
|
def validate_nos_commands(cls, values: "Device") -> "Device":
|
||||||
"""If a named command profile is not defined, use the NOS name."""
|
"""Validate & rewrite NOS, set default commands."""
|
||||||
if value is None:
|
|
||||||
value = values["nos"]
|
nos = values.get("nos", "")
|
||||||
|
if not nos:
|
||||||
|
# Ensure nos is defined.
|
||||||
|
raise ValueError(
|
||||||
|
f'Device {values["name"]} is missing a `nos` (Network Operating System).'
|
||||||
|
)
|
||||||
|
|
||||||
|
if nos in SCRAPE_HELPERS.keys():
|
||||||
|
# Rewrite NOS to helper value if needed.
|
||||||
|
nos = SCRAPE_HELPERS[nos]
|
||||||
|
|
||||||
|
# Verify NOS is supported by hyperglass.
|
||||||
|
supported, _ = validate_nos(nos)
|
||||||
|
if not supported:
|
||||||
|
raise UnsupportedDevice('"{nos}" is not supported.', nos=nos)
|
||||||
|
|
||||||
|
values["nos"] = nos
|
||||||
|
|
||||||
|
commands = values.get("commands")
|
||||||
|
|
||||||
|
if commands is None:
|
||||||
|
# If no commands are defined, set commands to the NOS.
|
||||||
|
inferred = values["nos"]
|
||||||
|
|
||||||
# If the _telnet prefix is added, remove it from the command
|
# If the _telnet prefix is added, remove it from the command
|
||||||
# profile so the commands are the same regardless of
|
# profile so the commands are the same regardless of
|
||||||
# protocol.
|
# protocol.
|
||||||
if "_telnet" in value:
|
if "_telnet" in inferred:
|
||||||
value = value.replace("_telnet", "")
|
inferred = inferred.replace("_telnet", "")
|
||||||
return value
|
|
||||||
|
values["commands"] = inferred
|
||||||
|
|
||||||
|
return values
|
||||||
|
|
||||||
@validator("vrfs", pre=True)
|
@validator("vrfs", pre=True)
|
||||||
def validate_vrfs(cls, value, values):
|
def validate_vrfs(cls, value, values):
|
||||||
|
Reference in New Issue
Block a user