fix plain text output on Junos

This commit is contained in:
checktheroads 2020-07-17 01:43:17 -07:00
parent 24cb5ab9a7
commit b686174952
2 changed files with 26 additions and 4 deletions

View file

@ -89,6 +89,15 @@ async def query(query_data: Query, request: Request, background_tasks: Backgroun
cache_response = await cache.get_dict(cache_key, "output")
json_output = False
if query_data.device.structured_output and query_data.query_type in (
"bgp_route",
"bgp_community",
"bgp_aspath",
):
json_output = True
cached = False
if cache_response:
log.debug("Query {q} exists in cache", q=cache_key)
@ -118,7 +127,7 @@ async def query(query_data: Query, request: Request, background_tasks: Backgroun
raise HyperglassError(message=params.messages.general, alert="danger")
# Create a cache entry
if query_data.device.structured_output:
if json_output:
raw_output = json.dumps(cache_output)
else:
raw_output = str(cache_output)
@ -134,7 +143,7 @@ async def query(query_data: Query, request: Request, background_tasks: Backgroun
cache_response = await cache.get_dict(cache_key, "output")
response_format = "text/plain"
if query_data.device.structured_output:
if json_output:
response_format = "application/json"
log.debug(f"Cache match for {cache_key}:\n {cache_response}")

View file

@ -84,6 +84,9 @@ class Connect:
parsed = ()
response = None
nos_to_parse = nos_parsers.keys()
query_type_to_parse = nos_parsers[self.device.nos].keys()
if not self.device.structured_output:
for coro in parsers:
for response in output:
@ -92,8 +95,18 @@ class Connect:
response = "\n\n".join(parsed)
elif (
self.device.structured_output
and self.device.nos in nos_parsers.keys()
and self.query_type in nos_parsers[self.device.nos].keys()
and self.device.nos in nos_to_parse
and self.query_type not in query_type_to_parse
):
for coro in parsers:
for response in output:
_output = await coro(commands=self.query, output=response)
parsed += (_output,)
response = "\n\n".join(parsed)
elif (
self.device.structured_output
and self.device.nos in nos_to_parse
and self.query_type in query_type_to_parse
):
func = nos_parsers[self.device.nos][self.query_type]
response = func(output)