diff --git a/inventory_provider/routes/lg.py b/inventory_provider/routes/lg.py index e8b72f6a0f312b4fb445a34f438a7d9bb168fab0..bcb528ccc8252f806c3ce5b8b6366dea7e8da0f8 100644 --- a/inventory_provider/routes/lg.py +++ b/inventory_provider/routes/lg.py @@ -1,6 +1,6 @@ import json -from flask import Blueprint, jsonify, Response +from flask import Blueprint, jsonify, Response, current_app from inventory_provider.routes import common @@ -25,29 +25,25 @@ def routers(access): 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('opsdb:lg:*'): - rtr = redis.get(k.decode('utf-8')).decode('utf-8') - rtr = json.loads(rtr) - i += 1 - if _visible(rtr): - yield rtr + for doc in common.load_json_docs( + config_params=current_app.config['INVENTORY_PROVIDER_CONFIG'], + key_pattern='opsdb:lg:*'): + yield doc['value'] + redis = common.get_current_redis() 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()) + result = list(filter(_visible, _routers())) # cache this data for the next call redis.set(cache_key, json.dumps(result).encode('utf-8'))