From ec40c02aed09ed150cbeced4a1c78da797d34e28 Mon Sep 17 00:00:00 2001
From: Erik Reid <erik.reid@geant.org>
Date: Wed, 28 Nov 2018 14:43:39 +0100
Subject: [PATCH] added sample bgp info route

---
 inventory_provider/routes/data.py | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)

diff --git a/inventory_provider/routes/data.py b/inventory_provider/routes/data.py
index ba4742ed..43663aa8 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")
-- 
GitLab