From 425a8a8caf22e006cb0a90584ff674209c571b9f Mon Sep 17 00:00:00 2001 From: Erik Reid <erik.reid@geant.org> Date: Mon, 10 Dec 2018 21:51:51 +0100 Subject: [PATCH] added interface descriptions to interface/hostname response --- inventory_provider/routes/data.py | 20 +++++++++++--------- test/test_data_routes.py | 10 +++++++++- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/inventory_provider/routes/data.py b/inventory_provider/routes/data.py index 13d6a9f2..c364e4d1 100644 --- a/inventory_provider/routes/data.py +++ b/inventory_provider/routes/data.py @@ -59,22 +59,24 @@ def router_interfaces(hostname): r = redis.StrictRedis( host=redis_config["hostname"], port=redis_config["port"]) - ifc_data_string = r.hget(hostname, 'snmp-interfaces') + ifc_data_string = r.hget(hostname, 'interfaces') if not ifc_data_string: return Response( response="no available info for '%s'" % hostname, status=404, mimetype="text/html") + def _interfaces(d): + for ii in d['interface-information']: + for ifc_list in ii['physical-interface'] + ii['logical-interface']: + for ifc in ifc_list: + yield { + 'name': ifc['name'][0]['data'], + 'description': ifc['description'][0]['data'] + } - def _interfaces(s): - for ifc in json.loads(s): - if 'v4InterfaceName' in ifc: - yield ifc['v4InterfaceName'] - if 'v6InterfaceName' in ifc: - yield ifc['v6InterfaceName'] - - interfaces = list(_interfaces(ifc_data_string.decode('utf-8'))) + ifc_data = json.loads(ifc_data_string.decode('utf-8')) + interfaces = list(_interfaces(ifc_data)) if not interfaces: return Response( response="no interfaces found for '%s'" % hostname, diff --git a/test/test_data_routes.py b/test/test_data_routes.py index a40b7b22..9c7e52a0 100644 --- a/test/test_data_routes.py +++ b/test/test_data_routes.py @@ -241,7 +241,15 @@ def test_router_interfaces(client_with_mocked_data): interfaces_list_schema = { "$schema": "http://json-schema.org/draft-07/schema#", "type": "array", - "items": {"type": "string"} + "items": { + "type": "object", + "properties": { + "name": {"type": "string"}, + "description": {"type": "string"} + }, + "required": ["name", "description"], + "additionalProperties": False + } } for router in _routers(client_with_mocked_data): -- GitLab