diff --git a/inventory_provider/routes/data.py b/inventory_provider/routes/data.py index ba4742ed8df174a2ed2ff8ef5bdcfcfa8ea32dd8..43663aa8e21b53e65282a0e4e7f00ad2cdbf3f32 100644 --- a/inventory_provider/routes/data.py +++ b/inventory_provider/routes/data.py @@ -104,3 +104,32 @@ def debug_dump_router_info(hostname): return Response( json.dumps(info), mimetype="application/json") + + +@routes.route("/bgp/<hostname>", methods=['GET', 'POST']) +@require_accepts_json +def bgp_configs(hostname): + redis_config = current_app.config["INVENTORY_PROVIDER_CONFIG"]["redis"] + r = redis.StrictRedis( + host=redis_config["hostname"], + port=redis_config["port"]) + bgp_data_string = r.hget(hostname, 'bgp') + if not bgp_data_string: + return Response( + response="no available bgp info for '%s'" % hostname, + status=404, + mimetype="text/html") + + def _interfaces(s): + for ifc in json.loads(s): + yield { + "description": ifc["description"][0], + "as": { + "peer": ifc["peer-as"][0], + "local": ifc["local-as"][0]["as-number"][0] + } + } + interfaces = list(_interfaces(bgp_data_string.decode('utf-8'))) + return Response( + json.dumps(interfaces), + mimetype="application/json")