mirror of
https://github.com/checktheroads/hyperglass
synced 2024-05-11 05:55:08 +00:00
model restructure, front-end improvements
This commit is contained in:
@ -4,20 +4,19 @@ command for Netmiko library or API call parameters for supported
|
||||
hyperglass API modules.
|
||||
"""
|
||||
# Standard Library Imports
|
||||
import re
|
||||
import ipaddress
|
||||
import json
|
||||
import operator
|
||||
import re
|
||||
|
||||
# Third Party Imports
|
||||
from logzero import logger as log
|
||||
|
||||
# Project Imports
|
||||
from hyperglass.configuration import vrfs
|
||||
from hyperglass.configuration import commands
|
||||
from hyperglass.configuration import logzero_config # NOQA: F401
|
||||
from hyperglass.configuration import stack # NOQA: F401
|
||||
from hyperglass.constants import target_format_space
|
||||
from hyperglass.exceptions import HyperglassError
|
||||
|
||||
|
||||
class Construct:
|
||||
@ -26,12 +25,26 @@ class Construct:
|
||||
input parameters.
|
||||
"""
|
||||
|
||||
def get_device_vrf(self):
|
||||
_device_vrf = None
|
||||
for vrf in self.device.vrfs:
|
||||
if vrf.name == self.query_vrf:
|
||||
_device_vrf = vrf
|
||||
if not _device_vrf:
|
||||
raise HyperglassError(
|
||||
message="Unable to match query VRF to any configured VRFs",
|
||||
alert="danger",
|
||||
keywords=[self.query_vrf],
|
||||
)
|
||||
return _device_vrf
|
||||
|
||||
def __init__(self, device, query_data, transport):
|
||||
self.device = device
|
||||
self.query_data = query_data
|
||||
self.transport = transport
|
||||
self.query_target = self.query_data["query_target"]
|
||||
self.query_vrf = self.query_data["query_vrf"]
|
||||
self.device_vrf = self.get_device_vrf()
|
||||
|
||||
def format_target(self, target):
|
||||
"""Formats query target based on NOS requirement"""
|
||||
@ -60,7 +73,7 @@ class Construct:
|
||||
"vpnv", if not, AFI prefix is "ipv"
|
||||
"""
|
||||
if query_vrf and query_vrf != "default":
|
||||
cmd_type = f"{query_protocol}_vrf"
|
||||
cmd_type = f"{query_protocol}_vpn"
|
||||
else:
|
||||
cmd_type = f"{query_protocol}_default"
|
||||
return cmd_type
|
||||
@ -74,8 +87,7 @@ class Construct:
|
||||
|
||||
query = []
|
||||
query_protocol = f"ipv{ipaddress.ip_network(self.query_target).version}"
|
||||
vrf = getattr(self.device.vrfs, self.query_vrf)
|
||||
afi = getattr(vrf, query_protocol)
|
||||
afi = getattr(self.device_vrf, query_protocol)
|
||||
|
||||
if self.transport == "rest":
|
||||
query.append(
|
||||
@ -90,7 +102,7 @@ class Construct:
|
||||
)
|
||||
)
|
||||
elif self.transport == "scrape":
|
||||
cmd_type = self.get_cmd_type(afi.afi_name, self.query_vrf)
|
||||
cmd_type = self.get_cmd_type(query_protocol, self.query_vrf)
|
||||
cmd = self.device_commands(self.device.commands, cmd_type, "ping")
|
||||
query.append(
|
||||
cmd.format(
|
||||
@ -117,8 +129,7 @@ class Construct:
|
||||
|
||||
query = []
|
||||
query_protocol = f"ipv{ipaddress.ip_network(self.query_target).version}"
|
||||
vrf = getattr(self.device.vrfs, self.query_vrf)
|
||||
afi = getattr(vrf, query_protocol)
|
||||
afi = getattr(self.device_vrf, query_protocol)
|
||||
|
||||
if self.transport == "rest":
|
||||
query.append(
|
||||
@ -133,7 +144,7 @@ class Construct:
|
||||
)
|
||||
)
|
||||
elif self.transport == "scrape":
|
||||
cmd_type = self.get_cmd_type(afi.afi_name, self.query_vrf)
|
||||
cmd_type = self.get_cmd_type(query_protocol, self.query_vrf)
|
||||
cmd = self.device_commands(self.device.commands, cmd_type, "traceroute")
|
||||
query.append(
|
||||
cmd.format(
|
||||
@ -157,8 +168,7 @@ class Construct:
|
||||
|
||||
query = []
|
||||
query_protocol = f"ipv{ipaddress.ip_network(self.query_target).version}"
|
||||
vrf = getattr(self.device.vrfs, self.query_vrf)
|
||||
afi = getattr(vrf, query_protocol)
|
||||
afi = getattr(self.device_vrf, query_protocol)
|
||||
|
||||
if self.transport == "rest":
|
||||
query.append(
|
||||
@ -173,7 +183,7 @@ class Construct:
|
||||
)
|
||||
)
|
||||
elif self.transport == "scrape":
|
||||
cmd_type = self.get_cmd_type(afi.afi_name, self.query_vrf)
|
||||
cmd_type = self.get_cmd_type(query_protocol, self.query_vrf)
|
||||
cmd = self.device_commands(self.device.commands, cmd_type, "bgp_route")
|
||||
query.append(
|
||||
cmd.format(
|
||||
@ -200,19 +210,16 @@ class Construct:
|
||||
)
|
||||
|
||||
query = []
|
||||
|
||||
vrf = getattr(self.device.vrfs, self.query_vrf)
|
||||
afis = []
|
||||
|
||||
vrf_dict = getattr(vrfs, self.query_vrf).dict()
|
||||
for vrf_key, vrf_value in {
|
||||
p: e for p, e in vrf_dict.items() if p in ("ipv4", "ipv6")
|
||||
p: e for p, e in self.device_vrf.dict().items() if p in ("ipv4", "ipv6")
|
||||
}.items():
|
||||
if vrf_value:
|
||||
afis.append(vrf_key)
|
||||
|
||||
for afi in afis:
|
||||
afi_attr = getattr(vrf, afi)
|
||||
afi_attr = getattr(self.device_vrf, afi)
|
||||
if self.transport == "rest":
|
||||
query.append(
|
||||
json.dumps(
|
||||
@ -226,7 +233,7 @@ class Construct:
|
||||
)
|
||||
)
|
||||
elif self.transport == "scrape":
|
||||
cmd_type = self.get_cmd_type(afi.afi_name, self.query_vrf)
|
||||
cmd_type = self.get_cmd_type(afi, self.query_vrf)
|
||||
cmd = self.device_commands(
|
||||
self.device.commands, cmd_type, "bgp_community"
|
||||
)
|
||||
@ -254,19 +261,16 @@ class Construct:
|
||||
)
|
||||
|
||||
query = []
|
||||
|
||||
vrf = getattr(self.device.vrfs, self.query_vrf)
|
||||
afis = []
|
||||
|
||||
vrf_dict = getattr(vrfs, self.query_vrf).dict()
|
||||
for vrf_key, vrf_value in {
|
||||
p: e for p, e in vrf_dict.items() if p in ("ipv4", "ipv6")
|
||||
p: e for p, e in self.device_vrf.dict().items() if p in ("ipv4", "ipv6")
|
||||
}.items():
|
||||
if vrf_value:
|
||||
afis.append(vrf_key)
|
||||
|
||||
for afi in afis:
|
||||
afi_attr = getattr(vrf, afi)
|
||||
afi_attr = getattr(self.device_vrf, afi)
|
||||
if self.transport == "rest":
|
||||
query.append(
|
||||
json.dumps(
|
||||
@ -280,7 +284,7 @@ class Construct:
|
||||
)
|
||||
)
|
||||
elif self.transport == "scrape":
|
||||
cmd_type = self.get_cmd_type(afi.afi_name, self.query_vrf)
|
||||
cmd_type = self.get_cmd_type(afi, self.query_vrf)
|
||||
cmd = self.device_commands(self.device.commands, cmd_type, "bgp_aspath")
|
||||
query.append(
|
||||
cmd.format(
|
||||
|
Reference in New Issue
Block a user