From df07258c26f4bbbb8f4151ec876289b5bc397390 Mon Sep 17 00:00:00 2001
From: Erik Reid <erik.reid@geant.org>
Date: Mon, 10 Dec 2018 21:03:50 +0100
Subject: [PATCH] add snmp id/ifcname map route

---
 inventory_provider/routes/data.py | 31 +++++++++++++++++++++++++++++++
 test/test_data_routes.py          | 27 +++++++++++++++++++++++++++
 2 files changed, 58 insertions(+)

diff --git a/inventory_provider/routes/data.py b/inventory_provider/routes/data.py
index 8b13439c..13d6a9f2 100644
--- a/inventory_provider/routes/data.py
+++ b/inventory_provider/routes/data.py
@@ -66,6 +66,7 @@ def router_interfaces(hostname):
             status=404,
             mimetype="text/html")
 
+
     def _interfaces(s):
         for ifc in json.loads(s):
             if 'v4InterfaceName' in ifc:
@@ -85,6 +86,36 @@ def router_interfaces(hostname):
         mimetype="application/json")
 
 
+@routes.route("/snmp/<hostname>", methods=['GET', 'POST'])
+@require_accepts_json
+def snmp_ids(hostname):
+    redis_config = current_app.config["INVENTORY_PROVIDER_CONFIG"]["redis"]
+    r = redis.StrictRedis(
+        host=redis_config["hostname"],
+        port=redis_config["port"])
+    ifc_data_string = r.hget(hostname, 'snmp-interfaces')
+    if not ifc_data_string:
+        return Response(
+            response="no available info for '%s'" % hostname,
+            status=404,
+            mimetype="text/html")
+
+    def _ifc_name(ifc):
+        if 'v4InterfaceName' in ifc:
+            return ifc['v4InterfaceName']
+        if 'v6InterfaceName' in ifc:
+            return ifc['v6InterfaceName']
+        assert False, "sanity failure: no interface name found"
+
+    ifc_data = json.loads(ifc_data_string.decode('utf-8'))
+    result = [
+        { 'index': i['index'], 'name': _ifc_name(i)}
+        for i in ifc_data]
+    return Response(
+        json.dumps(result),
+        mimetype="application/json")
+
+
 @routes.route("/debug-dump/<hostname>", methods=['GET', 'POST'])
 @require_accepts_json
 def debug_dump_router_info(hostname):
diff --git a/test/test_data_routes.py b/test/test_data_routes.py
index 0f9cc36a..a40b7b22 100644
--- a/test/test_data_routes.py
+++ b/test/test_data_routes.py
@@ -254,6 +254,33 @@ def test_router_interfaces(client_with_mocked_data):
         assert response  # at least shouldn't be empty
 
 
+def test_snmp_ids(client_with_mocked_data):
+
+    snmp_id_list_schema = {
+        "$schema": "http://json-schema.org/draft-07/schema#",
+        "type": "array",
+        "items": {
+            "type": "object",
+            "properties": {
+                "index": {"type": "string"},
+                "name": {"type": "string"}
+            },
+            "required": ["index", "name"],
+            "additionalProperties": False
+        }
+    }
+
+
+    for hostname in _routers(client_with_mocked_data):
+        rv = client_with_mocked_data.post(
+            "/data/snmp/" + hostname,
+            headers=DEFAULT_REQUEST_HEADERS)
+
+        response = json.loads(rv.data.decode("utf-8"))
+        jsonschema.validate(response, snmp_id_list_schema)
+        assert response  # at least shouldn't be empty
+
+
 def test_router_bgp_route(client_with_mocked_data):
 
     bgp_list_schema = {
-- 
GitLab