mirror of
https://github.com/checktheroads/hyperglass
synced 2024-05-11 05:55:08 +00:00
Fix unused imports, syntax cleanups
This commit is contained in:
@@ -1,18 +1,17 @@
|
|||||||
# https://github.com/checktheroads/hyperglass
|
|
||||||
"""
|
"""
|
||||||
Accepts input from front end application, validates the input and returns errors if input is \
|
Accepts input from front end application, validates the input and
|
||||||
invalid. Passes validated parameters to construct.py, which is used to build & run the Netmiko \
|
returns errors if input is invalid. Passes validated parameters to
|
||||||
connectoins or hyperglass-frr API calls, returns the output back to the front end.
|
construct.py, which is used to build & run the Netmiko connectoins or
|
||||||
|
hyperglass-frr API calls, returns the output back to the front end.
|
||||||
"""
|
"""
|
||||||
# Standard Imports
|
# Standard Lib Imports
|
||||||
import json
|
import json
|
||||||
import time
|
import time
|
||||||
|
|
||||||
# Module Imports
|
# Third Party Imports
|
||||||
import requests
|
import requests
|
||||||
import requests.exceptions
|
import requests.exceptions
|
||||||
from logzero import logger
|
from logzero import logger
|
||||||
import logzero
|
|
||||||
from netmiko import (
|
from netmiko import (
|
||||||
ConnectHandler,
|
ConnectHandler,
|
||||||
redispatch,
|
redispatch,
|
||||||
@@ -31,7 +30,7 @@ from hyperglass.configuration import (
|
|||||||
devices,
|
devices,
|
||||||
credentials,
|
credentials,
|
||||||
proxies,
|
proxies,
|
||||||
logzero_config,
|
logzero_config, # pylint: disable=unused-import
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -53,10 +52,9 @@ class Rest:
|
|||||||
|
|
||||||
def frr(self):
|
def frr(self):
|
||||||
"""Sends HTTP POST to router running the hyperglass-frr API"""
|
"""Sends HTTP POST to router running the hyperglass-frr API"""
|
||||||
# Debug
|
|
||||||
logger.debug(f"FRR host params:\n{self.device}")
|
logger.debug(f"FRR host params:\n{self.device}")
|
||||||
logger.debug(f"Raw query parameters: {self.query}")
|
logger.debug(f"Raw query parameters: {self.query}")
|
||||||
# End Debug
|
|
||||||
try:
|
try:
|
||||||
headers = {
|
headers = {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
@@ -66,18 +64,19 @@ class Rest:
|
|||||||
frr_endpoint = (
|
frr_endpoint = (
|
||||||
f"http://{self.device.address.exploded}:{self.device.port}/frr"
|
f"http://{self.device.address.exploded}:{self.device.port}/frr"
|
||||||
)
|
)
|
||||||
|
|
||||||
logger.debug(f"HTTP Headers: {headers}")
|
logger.debug(f"HTTP Headers: {headers}")
|
||||||
logger.debug(f"JSON query: {json_query}")
|
logger.debug(f"JSON query: {json_query}")
|
||||||
logger.debug(f"FRR endpoint: {frr_endpoint}")
|
logger.debug(f"FRR endpoint: {frr_endpoint}")
|
||||||
|
|
||||||
frr_response = requests.post(
|
frr_response = requests.post(
|
||||||
frr_endpoint, headers=headers, data=json_query, timeout=7
|
frr_endpoint, headers=headers, data=json_query, timeout=7
|
||||||
)
|
)
|
||||||
response = frr_response.text
|
response = frr_response.text
|
||||||
status = frr_response.status_code
|
status = frr_response.status_code
|
||||||
# Debug
|
|
||||||
logger.debug(f"FRR status code: {status}")
|
logger.debug(f"FRR status code: {status}")
|
||||||
logger.debug(f"FRR response text:\n{response}")
|
logger.debug(f"FRR response text:\n{response}")
|
||||||
# End Debug
|
|
||||||
except requests.exceptions.RequestException as rest_error:
|
except requests.exceptions.RequestException as rest_error:
|
||||||
logger.error(
|
logger.error(
|
||||||
f"Error connecting to device {self.device.location}: {rest_error}"
|
f"Error connecting to device {self.device.location}: {rest_error}"
|
||||||
@@ -88,10 +87,9 @@ class Rest:
|
|||||||
|
|
||||||
def bird(self):
|
def bird(self):
|
||||||
"""Sends HTTP POST to router running the hyperglass-bird API"""
|
"""Sends HTTP POST to router running the hyperglass-bird API"""
|
||||||
# Debug
|
|
||||||
logger.debug(f"BIRD host params:\n{self.device}")
|
logger.debug(f"BIRD host params:\n{self.device}")
|
||||||
logger.debug(f"Raw query parameters: {self.query}")
|
logger.debug(f"Raw query parameters: {self.query}")
|
||||||
# End Debug
|
|
||||||
try:
|
try:
|
||||||
headers = {
|
headers = {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
@@ -101,18 +99,19 @@ class Rest:
|
|||||||
bird_endpoint = (
|
bird_endpoint = (
|
||||||
f"http://{self.device.address.exploded}:{self.device.port}/bird"
|
f"http://{self.device.address.exploded}:{self.device.port}/bird"
|
||||||
)
|
)
|
||||||
|
|
||||||
logger.debug(f"HTTP Headers: {headers}")
|
logger.debug(f"HTTP Headers: {headers}")
|
||||||
logger.debug(f"JSON query: {json_query}")
|
logger.debug(f"JSON query: {json_query}")
|
||||||
logger.debug(f"BIRD endpoint: {bird_endpoint}")
|
logger.debug(f"BIRD endpoint: {bird_endpoint}")
|
||||||
|
|
||||||
bird_response = requests.post(
|
bird_response = requests.post(
|
||||||
bird_endpoint, headers=headers, data=json_query, timeout=7
|
bird_endpoint, headers=headers, data=json_query, timeout=7
|
||||||
)
|
)
|
||||||
response = bird_response.text
|
response = bird_response.text
|
||||||
status = bird_response.status_code
|
status = bird_response.status_code
|
||||||
# Debug
|
|
||||||
logger.debug(f"BIRD status code: {status}")
|
logger.debug(f"BIRD status code: {status}")
|
||||||
logger.debug(f"BIRD response text:\n{response}")
|
logger.debug(f"BIRD response text:\n{response}")
|
||||||
# End Debug
|
|
||||||
except requests.exceptions.RequestException as requests_exception:
|
except requests.exceptions.RequestException as requests_exception:
|
||||||
logger.error(
|
logger.error(
|
||||||
f"Error connecting to device {self.device}: {requests_exception}"
|
f"Error connecting to device {self.device}: {requests_exception}"
|
||||||
@@ -149,6 +148,7 @@ class Netmiko:
|
|||||||
output.
|
output.
|
||||||
"""
|
"""
|
||||||
logger.debug(f"Connecting to {self.device.location} via Netmiko library...")
|
logger.debug(f"Connecting to {self.device.location} via Netmiko library...")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
nm_connect_direct = ConnectHandler(**self.nm_host)
|
nm_connect_direct = ConnectHandler(**self.nm_host)
|
||||||
response = nm_connect_direct.send_command(self.command)
|
response = nm_connect_direct.send_command(self.command)
|
||||||
@@ -180,14 +180,16 @@ class Netmiko:
|
|||||||
}
|
}
|
||||||
nm_connect_proxied = ConnectHandler(**nm_proxy)
|
nm_connect_proxied = ConnectHandler(**nm_proxy)
|
||||||
nm_ssh_command = device_proxy.ssh_command.format(**self.nm_host) + "\n"
|
nm_ssh_command = device_proxy.ssh_command.format(**self.nm_host) + "\n"
|
||||||
# Debug
|
|
||||||
logger.debug(f"Netmiko proxy {self.device.proxy}")
|
logger.debug(f"Netmiko proxy {self.device.proxy}")
|
||||||
logger.debug(f"Proxy SSH command: {nm_ssh_command}")
|
logger.debug(f"Proxy SSH command: {nm_ssh_command}")
|
||||||
# End Debug
|
|
||||||
nm_connect_proxied.write_channel(nm_ssh_command)
|
nm_connect_proxied.write_channel(nm_ssh_command)
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
proxy_output = nm_connect_proxied.read_channel()
|
proxy_output = nm_connect_proxied.read_channel()
|
||||||
|
|
||||||
logger.debug(f"Proxy output:\n{proxy_output}")
|
logger.debug(f"Proxy output:\n{proxy_output}")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Accept SSH key warnings
|
# Accept SSH key warnings
|
||||||
if "Are you sure you want to continue connecting" in proxy_output:
|
if "Are you sure you want to continue connecting" in proxy_output:
|
||||||
@@ -206,6 +208,7 @@ class Netmiko:
|
|||||||
redispatch(nm_connect_proxied, self.nm_host["device_type"])
|
redispatch(nm_connect_proxied, self.nm_host["device_type"])
|
||||||
response = nm_connect_proxied.send_command(self.command)
|
response = nm_connect_proxied.send_command(self.command)
|
||||||
status = code.valid
|
status = code.valid
|
||||||
|
|
||||||
logger.debug(f"Netmiko proxied response:\n{response}")
|
logger.debug(f"Netmiko proxied response:\n{response}")
|
||||||
except (
|
except (
|
||||||
NetMikoAuthenticationException,
|
NetMikoAuthenticationException,
|
||||||
@@ -215,6 +218,7 @@ class Netmiko:
|
|||||||
) as netmiko_exception:
|
) as netmiko_exception:
|
||||||
response = params.messages.general
|
response = params.messages.general
|
||||||
status = code.invalid
|
status = code.invalid
|
||||||
|
|
||||||
logger.error(f"{netmiko_exception}, {status},Proxy: {self.device.proxy}")
|
logger.error(f"{netmiko_exception}, {status},Proxy: {self.device.proxy}")
|
||||||
return response, status
|
return response, status
|
||||||
|
|
||||||
@@ -238,16 +242,19 @@ class Execute:
|
|||||||
protocol-agnostic commands (Community & AS_PATH Lookups).
|
protocol-agnostic commands (Community & AS_PATH Lookups).
|
||||||
"""
|
"""
|
||||||
logger.debug("Parsing output...")
|
logger.debug("Parsing output...")
|
||||||
|
|
||||||
parsed = output
|
parsed = output
|
||||||
if self.input_type in ("bgp_community", "bgp_aspath"):
|
if self.input_type in ("bgp_community", "bgp_aspath"):
|
||||||
if nos in ("cisco_ios",):
|
if nos in ("cisco_ios",):
|
||||||
logger.debug(f"Parsing output for device type {nos}")
|
logger.debug(f"Parsing output for device type {nos}")
|
||||||
|
|
||||||
delimiter = "For address family: "
|
delimiter = "For address family: "
|
||||||
parsed_ipv4 = output.split(delimiter)[1]
|
parsed_ipv4 = output.split(delimiter)[1]
|
||||||
parsed_ipv6 = output.split(delimiter)[2]
|
parsed_ipv6 = output.split(delimiter)[2]
|
||||||
parsed = delimiter + parsed_ipv4 + delimiter + parsed_ipv6
|
parsed = delimiter + parsed_ipv4 + delimiter + parsed_ipv6
|
||||||
elif nos in ("cisco_xr",):
|
elif nos in ("cisco_xr",):
|
||||||
logger.debug(f"Parsing output for device type {nos}")
|
logger.debug(f"Parsing output for device type {nos}")
|
||||||
|
|
||||||
delimiter = "Address Family: "
|
delimiter = "Address Family: "
|
||||||
parsed_ipv4 = output.split(delimiter)[1]
|
parsed_ipv4 = output.split(delimiter)[1]
|
||||||
parsed_ipv6 = output.split(delimiter)[2]
|
parsed_ipv6 = output.split(delimiter)[2]
|
||||||
@@ -260,8 +267,10 @@ class Execute:
|
|||||||
returns errors to front end. Otherwise, executes queries.
|
returns errors to front end. Otherwise, executes queries.
|
||||||
"""
|
"""
|
||||||
device_config = getattr(devices, self.input_location)
|
device_config = getattr(devices, self.input_location)
|
||||||
|
|
||||||
logger.debug(f"Received query for {self.input_data}")
|
logger.debug(f"Received query for {self.input_data}")
|
||||||
logger.debug(f"Matched device config:\n{device_config}")
|
logger.debug(f"Matched device config:\n{device_config}")
|
||||||
|
|
||||||
# Run query parameters through validity checks
|
# Run query parameters through validity checks
|
||||||
validity, msg, status = getattr(Validate(device_config), self.input_type)(
|
validity, msg, status = getattr(Validate(device_config), self.input_type)(
|
||||||
self.input_target
|
self.input_target
|
||||||
@@ -271,15 +280,16 @@ class Execute:
|
|||||||
return {"output": msg, "status": status}
|
return {"output": msg, "status": status}
|
||||||
connection = None
|
connection = None
|
||||||
output = params.messages.general
|
output = params.messages.general
|
||||||
info = self.input_data
|
|
||||||
logger.debug(f"Validity: {validity}, Message: {msg}, Status: {status}")
|
logger.debug(f"Validity: {validity}, Message: {msg}, Status: {status}")
|
||||||
|
|
||||||
if Supported.is_rest(device_config.nos):
|
if Supported.is_rest(device_config.nos):
|
||||||
connection = Rest("rest", device_config, self.input_type, self.input_target)
|
connection = Rest("rest", device_config, self.input_type, self.input_target)
|
||||||
raw_output, status = getattr(connection, device_config.nos)()
|
raw_output, status = getattr(connection, device_config.nos)()
|
||||||
output = self.parse(raw_output, device_config.nos)
|
output = self.parse(raw_output, device_config.nos)
|
||||||
# return {"output": output, "status": status}
|
|
||||||
elif Supported.is_scrape(device_config.nos):
|
elif Supported.is_scrape(device_config.nos):
|
||||||
logger.debug(f"Initializing Netmiko...")
|
logger.debug(f"Initializing Netmiko...")
|
||||||
|
|
||||||
connection = Netmiko(
|
connection = Netmiko(
|
||||||
"scrape", device_config, self.input_type, self.input_target
|
"scrape", device_config, self.input_type, self.input_target
|
||||||
)
|
)
|
||||||
@@ -288,8 +298,8 @@ class Execute:
|
|||||||
elif not device_config.proxy:
|
elif not device_config.proxy:
|
||||||
raw_output, status = connection.direct()
|
raw_output, status = connection.direct()
|
||||||
output = self.parse(raw_output, device_config.nos)
|
output = self.parse(raw_output, device_config.nos)
|
||||||
|
|
||||||
logger.debug(
|
logger.debug(
|
||||||
f"Parsed output for device type {device_config.nos}:\n{output}"
|
f"Parsed output for device type {device_config.nos}:\n{output}"
|
||||||
)
|
)
|
||||||
# return {"output": output, "status": status}
|
|
||||||
return (output, status)
|
return (output, status)
|
||||||
|
Reference in New Issue
Block a user