diff --git a/inventory_provider/routes/classifier.py b/inventory_provider/routes/classifier.py index 2d082836fe228796fe2cd7d1d19e02687b79b13b..a9e5e21173e2ff2d3351d8efcfb317626189c46c 100644 --- a/inventory_provider/routes/classifier.py +++ b/inventory_provider/routes/classifier.py @@ -33,35 +33,24 @@ def _remove_duplicates_from_list(all): return list(tmp_dict.values()) -def _location_from_router(router_name): +def _locations_from_router(router_name): r = common.get_current_redis() result = r.get(f'opsdb:location:{router_name}') if not result: logger.error(f'error looking up location for {router_name}') - return { - 'a': _LOCATION( - equipment=router_name, - name='?', - abbreviation='?') - } + return [] result = json.loads(result.decode('utf-8')) if not result: logger.error(f'sanity failure: empty list for location {router_name}') - return None - return { - 'a': _LOCATION( - equipment=router_name, - name='?', - abbreviation='?') - } + return [] - return { + return [{ 'a': _LOCATION( equipment=result[0]['equipment-name'], name=result[0]['pop']['name'], abbreviation=result[0]['pop']['abbreviation']) - } + }] def _location_from_service_dict(s): @@ -217,7 +206,7 @@ def get_juniper_link_info(source_equipment, interface): result['related-services'] = top_level_services if not result['locations']: - result['locations'] = [_location_from_router(source_equipment)] + result['locations'] = _locations_from_router(source_equipment) result['locations'] = _remove_duplicates_from_list(result['locations']) result = json.dumps(result) @@ -351,16 +340,14 @@ def peer_info(address): info = info.decode('utf-8') info = json.loads(info) result['ix-public-peer-info'] = ix_peering_info(info) - router_location = _location_from_router(info['router']) - result['locations'] += [router_location] + result['locations'] += _locations_from_router(info['router']) info = r.get('vpn_rr_peer:%s' % address) if info: info = info.decode('utf-8') info = json.loads(info) result['vpn-rr-peer-info'] = info - router_location = _location_from_router(info['router']) - result['locations'] += [router_location] + result['locations'] += _locations_from_router(info['router']) interfaces = list(find_interfaces_and_services(address)) if interfaces: diff --git a/test/test_classifier_routes.py b/test/test_classifier_routes.py index a255809613f447d655485f02c1efaca3cb8f0d25..5de175c93a72f52874f8403048dc1cf5707dedad 100644 --- a/test/test_classifier_routes.py +++ b/test/test_classifier_routes.py @@ -30,8 +30,7 @@ LOCATIONS_DEFINITIONS = { }, "locations-list": { "type": "array", - "items": {"$ref": "#/definitions/location"}, - "minitems": 1 + "items": {"$ref": "#/definitions/location"} } } @@ -235,12 +234,7 @@ def test_juniper_link_unknown_router(client): 'bundle': [], 'bundle_members': [] }, - 'locations': [{ - 'a': { - 'equipment': 'unknown-router', - 'name': '?', - 'abbreviation': '?'} - }] + 'locations': [] }