mirror of
https://github.com/checktheroads/hyperglass
synced 2024-05-11 05:55:08 +00:00
add VRF to AFI mapping model
This commit is contained in:
@@ -20,6 +20,44 @@ from pydantic import validator
|
||||
from hyperglass.configuration.models._utils import clean_name
|
||||
from hyperglass.constants import Supported
|
||||
from hyperglass.exceptions import UnsupportedDevice
|
||||
from hyperglass.constants import afi_nos_map
|
||||
|
||||
|
||||
class AfiMap(BaseSettings):
|
||||
"""Model for AFI map"""
|
||||
|
||||
ipv4: Union[str, None] = None
|
||||
ipv6: Union[str, None] = None
|
||||
ipv4_vpn: Union[str, None] = None
|
||||
ipv6_vpn: Union[str, None] = None
|
||||
|
||||
@validator("ipv4", always=True)
|
||||
def validate_ipv4(cls, v): # noqa: N805
|
||||
"""If a map field is undefined, get a default value"""
|
||||
if v is None:
|
||||
v = afi_nos_map.get("default").get("ipv4")
|
||||
return v
|
||||
|
||||
@validator("ipv6", always=True)
|
||||
def validate_ipv6(cls, v): # noqa: N805
|
||||
"""If a map field is undefined, get a default value"""
|
||||
if v is None:
|
||||
v = afi_nos_map.get("default").get("ipv6")
|
||||
return v
|
||||
|
||||
@validator("ipv4_vpn", always=True)
|
||||
def validate_ipv4_vpn(cls, v): # noqa: N805
|
||||
"""If a map field is undefined, get a default value"""
|
||||
if v is None:
|
||||
v = afi_nos_map.get("default").get("ipv4_vpn")
|
||||
return v
|
||||
|
||||
@validator("ipv6_vpn", always=True)
|
||||
def validate_ipv6_vpn(cls, v): # noqa: N805
|
||||
"""If a map field is undefined, get a default value"""
|
||||
if v is None:
|
||||
v = afi_nos_map.get("default").get("ipv6_vpn")
|
||||
return v
|
||||
|
||||
|
||||
class Router(BaseSettings):
|
||||
@@ -37,10 +75,13 @@ class Router(BaseSettings):
|
||||
nos: str
|
||||
commands: Union[str, None] = None
|
||||
vrfs: List[str] = ["default"]
|
||||
afi_map: Union[AfiMap, None] = None
|
||||
|
||||
@validator("nos")
|
||||
def supported_nos(cls, v): # noqa: N805
|
||||
"""Validates that passed nos string is supported by hyperglass"""
|
||||
"""
|
||||
Validates that passed nos string is supported by hyperglass.
|
||||
"""
|
||||
if not Supported.is_supported(v):
|
||||
raise UnsupportedDevice(f'"{v}" device type is not supported.')
|
||||
return v
|
||||
@@ -52,10 +93,26 @@ class Router(BaseSettings):
|
||||
|
||||
@validator("commands", always=True)
|
||||
def validate_commands(cls, v, values): # noqa: N805
|
||||
"""
|
||||
If a named command profile is not defined, use theNOS name.
|
||||
"""
|
||||
if v is None:
|
||||
v = values["nos"]
|
||||
return v
|
||||
|
||||
@validator("afi_map", always=True)
|
||||
def validate_afis(cls, v, values): # noqa: N805
|
||||
"""
|
||||
If an AFI map is not defined, try to get one based on the
|
||||
NOS name. If that doesn't exist, use a default.
|
||||
"""
|
||||
if v is None:
|
||||
v = AfiMap(**afi_nos_map.get(values["nos"], afi_nos_map.get("default")))
|
||||
return v
|
||||
|
||||
|
||||
Router.update_forward_refs()
|
||||
|
||||
|
||||
class Routers(BaseSettings):
|
||||
"""Base model for devices class."""
|
||||
|
@@ -19,7 +19,7 @@ class Vrf(BaseSettings):
|
||||
"""Model for per VRF/afi config in devices.yaml"""
|
||||
|
||||
display_name: str
|
||||
name: str
|
||||
label: str
|
||||
afis: List[str]
|
||||
|
||||
|
||||
@@ -36,26 +36,26 @@ class Vrfs(BaseSettings):
|
||||
vrfs: Vrf = {
|
||||
"default": {
|
||||
"display_name": "Default",
|
||||
"name": "default",
|
||||
"label": "default",
|
||||
"afis": ["ipv4, ipv6"],
|
||||
}
|
||||
}
|
||||
names: List[str] = ["default"]
|
||||
labels: List[str] = ["default"]
|
||||
_all: List[str] = ["default"]
|
||||
|
||||
for (vrf_key, params) in input_params.items():
|
||||
vrf = clean_name(vrf_key)
|
||||
vrf_params = Vrf(**params)
|
||||
vrfs.update({vrf: vrf_params.dict()})
|
||||
names.append(params.get("name"))
|
||||
labels.append(params.get("label"))
|
||||
_all.append(vrf_key)
|
||||
for (vrf_key, params) in vrfs.items():
|
||||
setattr(Vrfs, vrf_key, params)
|
||||
|
||||
names: List[str] = list(set(names))
|
||||
labels: List[str] = list(set(labels))
|
||||
_all: List[str] = list(set(_all))
|
||||
Vrfs.vrfs = vrfs
|
||||
Vrfs.names = names
|
||||
Vrfs.labels = labels
|
||||
Vrfs._all = _all
|
||||
return Vrfs()
|
||||
|
||||
|
@@ -4,6 +4,15 @@ Global Constants for hyperglass
|
||||
|
||||
protocol_map = {80: "http", 8080: "http", 443: "https", 8443: "https"}
|
||||
|
||||
afi_nos_map = {
|
||||
"default": {
|
||||
"ipv4": "ipv4",
|
||||
"ipv6": "ipv6",
|
||||
"ipv4_vpn": "vpnv4",
|
||||
"ipv6_vpn": "vpnv6",
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class Supported:
|
||||
"""
|
||||
|
Reference in New Issue
Block a user