Skip to content

Commit

Permalink
fix plain text output on Junos
Browse files Browse the repository at this point in the history
  • Loading branch information
thatmattlove committed Jul 17, 2020
1 parent 24cb5ab commit b686174
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
13 changes: 11 additions & 2 deletions hyperglass/api/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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}")
Expand Down
17 changes: 15 additions & 2 deletions hyperglass/execution/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ async def parsed_response(self, output):
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:
Expand All @@ -92,8 +95,18 @@ async def parsed_response(self, output):
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)
Expand Down

0 comments on commit b686174

Please sign in to comment.