From 937af5f49feb3876e226134bac40a3750b362ae2 Mon Sep 17 00:00:00 2001
From: Erik Reid <erik.reid@geant.org>
Date: Fri, 15 Jan 2021 17:10:28 +0100
Subject: [PATCH] added snmp to peer-info response

---
 inventory_provider/routes/classifier.py | 23 ++++++++++++++++-------
 inventory_provider/tasks/worker.py      |  2 ++
 2 files changed, 18 insertions(+), 7 deletions(-)

diff --git a/inventory_provider/routes/classifier.py b/inventory_provider/routes/classifier.py
index c78f35f7..ad3b5db0 100644
--- a/inventory_provider/routes/classifier.py
+++ b/inventory_provider/routes/classifier.py
@@ -240,7 +240,7 @@ def get_juniper_link_info(source_equipment, interface):
         # cache this data for the next call
         r.set(cache_key, result.encode('utf-8'))
 
-    return Response(result, mimetype="application/json")
+    return Response(result, mimetype='application/json')
 
 
 def ix_peering_info(peer_info):
@@ -362,14 +362,14 @@ def peer_info(address):
             'locations': []
         }
 
-        info = r.get('ix_public_peer:%s' % address)
+        info = r.get(f'ix_public_peer:{address}')
         if info:
             info = info.decode('utf-8')
             info = json.loads(info)
             result['ix-public-peer-info'] = ix_peering_info(info)
             result['locations'] += _locations_from_router(info['router'])
 
-        info = r.get('vpn_rr_peer:%s' % address)
+        info = r.get(f'vpn_rr_peer:{address}')
         if info:
             info = info.decode('utf-8')
             info = json.loads(info)
@@ -383,12 +383,21 @@ def peer_info(address):
                 result['locations'] += [
                     _location_from_service_dict(s) for s in i['services']]
 
+        snmp_info = r.get(
+            f'snmp-peerings:{address}')
+        if snmp_info:
+            snmp_info = json.loads(snmp_info.decode('utf-8'))
+            result['snmp'] = {
+                'community': snmp_info['community'],
+                'oid': snmp_info['oid']
+            }
+
         result['locations'] = _remove_duplicates_from_list(result['locations'])
         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")
+    return Response(result, mimetype='application/json')
 
 
 @routes.route("/infinera-lambda-info/"
@@ -442,7 +451,7 @@ def get_trap_metadata(source_equipment, interface, circuit_id):
         # cache this data for the next call
         r.set(cache_key, result.encode('utf-8'))
 
-    return Response(result, mimetype="application/json")
+    return Response(result, mimetype='application/json')
 
 
 @routes.route("/infinera-fiberlink-info/<ne_name_str>/<object_name_str>",
@@ -512,7 +521,7 @@ def get_fiberlink_trap_metadata(ne_name_str, object_name_str):
                 status=404,
                 mimetype="text/html")
 
-    return Response(result, mimetype="application/json")
+    return Response(result, mimetype='application/json')
 
 
 @routes.route('/coriant-info/<equipment_name>/<path:entity_string>',
@@ -575,4 +584,4 @@ def get_coriant_info(equipment_name, entity_string):
         result = json.dumps(result).encode('utf-8')
         r.set(cache_key, result)
 
-    return Response(result, mimetype="application/json")
+    return Response(result, mimetype='application/json')
diff --git a/inventory_provider/tasks/worker.py b/inventory_provider/tasks/worker.py
index c263c416..2f6edb33 100644
--- a/inventory_provider/tasks/worker.py
+++ b/inventory_provider/tasks/worker.py
@@ -90,6 +90,8 @@ class InventoryTask(Task):
 def snmp_refresh_peerings(self, hostname, community):
     try:
         peerings = list(snmp.get_peer_state_info(hostname, community))
+        for p in peerings:
+            p['community'] = community
     except ConnectionError:
         msg = f'error loading snmp peering data from {hostname}'
         logger.exception(msg)
-- 
GitLab