Skip to content
Snippets Groups Projects
Commit 2294e90e authored by Erik Reid's avatar Erik Reid
Browse files

route handler for lg endpoint

parent ccee6010
No related branches found
No related tags found
No related merge requests found
import json
import re
from flask import Blueprint, jsonify, Response
from inventory_provider.routes import common
routes = Blueprint("lg-query-routes", __name__)
ACCESS_PUBLIC = 'public'
ACCESS_INTERNAL = 'all'
@routes.after_request
def after_request(resp):
return common.after_request(resp)
@routes.route("/routers/<string:access>", methods=['GET', 'POST'])
@common.require_accepts_json
def routers(access):
if access not in {ACCESS_INTERNAL, ACCESS_PUBLIC}:
return Response(
response='unknown access level',
status=404,
mimetype='text/html')
redis = common.get_current_redis()
def _visible(router):
if access == ACCESS_INTERNAL:
return True
return router['type'] == 'CORE'
def _routers():
i = 0
for k in redis.scan_iter(f'opsdb:lg:*'):
rtr = redis.get(k.decode('utf-8')).decode('utf-8')
rtr = json.loads(rtr)
i += 1
if _visible(rtr):
yield rtr
cache_key = f'classifier-cache:lg:{access}'
result = redis.get(cache_key)
if result:
result = json.loads(result.decode('utf-8'))
else:
result = list(_routers())
# cache this data for the next call
redis.set(cache_key, json.dumps(result).encode('utf-8'))
if not result:
return Response(
response=f'no routers found for access level {access}',
status=404,
mimetype="text/html")
return jsonify(result)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment