aboutsummaryrefslogtreecommitdiffstats
path: root/templating/templating.py
diff options
context:
space:
mode:
Diffstat (limited to 'templating/templating.py')
-rwxr-xr-xtemplating/templating.py31
1 files changed, 18 insertions, 13 deletions
diff --git a/templating/templating.py b/templating/templating.py
index 603b370..55bb3f5 100755
--- a/templating/templating.py
+++ b/templating/templating.py
@@ -10,21 +10,24 @@ import requests
from flask import Flask, request
from jinja2 import Environment, FileSystemLoader, TemplateNotFound, TemplateError
-endpoints = "read/networks read/oplog read/snmp read/switches-management public/distro-tree public/config public/dhcp public/dhcp-summary public/ping public/switches public/switch-state".split()
+endpoints = ["read/networks", "read/oplog", "read/snmp", "read/switches-management", "public/distro-tree",
+ "public/config", "public/dhcp", "public/dhcp-summary", "public/ping", "public/switches",
+ "public/switch-state"]
objects = {}
-def getEndpoint(endpoint):
- uri = "{}/api/{}".format(args.server, endpoint)
- try:
- r = requests.get(uri, timeout=args.timeout)
- except (requests.exceptions.ConnectTimeout, requests.exceptions.ConnectionError):
- raise Exception("Couldn't connect to server {}".format(uri))
+
+
+def getEndpoint(endpoint: str) -> dict:
+ """
+ Fetches an endpoint and returns the data as a dict.
+ """
+ uri = f"{args.server}/api/{endpoint}"
+ r = requests.get(uri, timeout=args.timeout)
r.raise_for_status()
return r.json()
-
def updateData():
for a in endpoints:
objects[a] = getEndpoint(a)
@@ -59,14 +62,16 @@ def root_get(path):
updateData()
template = env.get_template(path)
body = template.render(objects=objects, options=request.args)
+ except (requests.exceptions.ConnectTimeout, requests.exceptions.ConnectionError) as error:
+ return f'Timeout or connection error from gondul: {err}', 500
except TemplateNotFound:
- return 'Template "{}" not found\n'.format(path), 404
+ return f'Template "{path}" not found\n', 404
except TemplateError as err:
- return 'Templating of "{}" failed to render. Most likely due to an error in the template. Error transcript:\n\n{}\n----\n\n{}\n'.format(path, err, traceback.format_exc()), 400
+ return f'Templating of "{path}" failed to render. Most likely due to an error in the template. Error transcript:\n\n{err}\n----\n\n{traceback.format_exc()}\n', 400
except requests.exceptions.HTTPError as err:
- return 'HTTP error from gondul: {}'.format(err), 500
+ return f'HTTP error from gondul: {err}', 500
except Exception as err:
- return 'Connection issues: {}'.format(err), 500
+ return f'Uncaight error: {err}', 500
return body, 200
@@ -78,7 +83,7 @@ def root_post(path):
template = env.from_string(content.decode("utf-8"))
body = template.render(objects=objects, options=request.args)
except Exception as err:
- return 'Templating of "{}" failed to render. Most likely due to an error in the template. Error transcript:\n\n{}\n----\n\n{}\n'.format(path, err, traceback.format_exc()), 400
+ return 'Templating of "{path}" failed to render. Most likely due to an error in the template. Error transcript:\n\n{err}\n----\n\n{traceback.format_exc()}\n', 400
return body, 200