diff --git a/inventory_provider/routes/poller.py b/inventory_provider/routes/poller.py index bbc3b5d5720246d9639f48dbf05609940c5e9f4e..675b2def56391baaa80b21f6936050483d5dd5de 100644 --- a/inventory_provider/routes/poller.py +++ b/inventory_provider/routes/poller.py @@ -1,6 +1,6 @@ import json -from flask import Blueprint, Response, jsonify +from flask import Blueprint, Response, jsonify, current_app from lxml import etree from inventory_provider import juniper from inventory_provider.routes import common @@ -88,10 +88,12 @@ def poller_interface_oids(hostname): @common.require_accepts_json def service_category_interfaces(category): - def _interfaces(rc, cat): - for k in rc.scan_iter(f'interface-services:{cat}:*'): - cached_ifc = rc.get(k.decode('utf-8')).decode('utf-8') - cached_ifc = json.loads(cached_ifc) + def _interfaces(cat): + config_params = current_app.config['INVENTORY_PROVIDER_CONFIG'] + for doc in common.load_json_docs( + config_params=config_params, + key_pattern= f'interface-services:{cat}:*'): + cached_ifc = doc['value'] basic_ifc_info = dict() for k in ['description', 'interface', 'router']: basic_ifc_info[k] = cached_ifc[k] @@ -110,15 +112,17 @@ def service_category_interfaces(category): result = r.get(cache_key) if result: - result = json.loads(result.decode('utf-8')) + result = result.decode('utf-8') else: - result = list(_interfaces(r, category)) - if result: - r.set(cache_key, json.dumps(result).encode('utf-8')) - else: + result = list(_interfaces(category)) + if not result: return Response( response=f'no info available for service category {category}', status=404, mimetype="text/html") - return jsonify(result) + result = json.dumps(result) + # cache this data for the next call + r.set(cache_key, result.encode('utf-8')) + + return Response(result, mimetype="application/json")