diff --git a/inventory_provider/routes/classifier.py b/inventory_provider/routes/classifier.py index a4e46342066311abaa1f8cfaad4d4ca55b1e4e6c..c98f7068e6c4c7b2e1c8f47f75bfe9c74f94b484 100644 --- a/inventory_provider/routes/classifier.py +++ b/inventory_provider/routes/classifier.py @@ -46,36 +46,39 @@ def get_trap_metadata(source_equipment, interface): return Response(result, mimetype="application/json") -@routes.route("/peer-info/<address>", methods=['GET', 'POST']) -@common.require_accepts_json -def peer_info(address): +def ix_peering_group(ix_public_peer_info): + """ + TODO: this is probably the least efficient way of doing this + (if it's a problem, pre-compute these lists) - def _related_ix_peers(ix_public_peer_info): - """ - TODO: this is probably the least efficient way of doing this - (if it's a problem, pre-compute these lists) + :param ix_public_peer_info: ix public peer info loaded for address + :return: + """ - :param ix_public_peer_info: ix public peer info loaded for address - :return: - """ + address = ix_public_peer_info['name'] + protocol = type(ipaddress.ip_address(address)).__name__ - protocol = type(ipaddress.ip_address(address)).__name__ + description = ix_public_peer_info['description'] + assert description is not None # sanity: at least empty string + keyword = description.split(' ')[0] # regex needed??? (e.g. tabs???) + + r = common.get_redis() - description = ix_public_peer_info['description'] - assert description is not None # sanity: at least empty string - keyword = description.split(' ')[0] # regex needed??? (e.g. tabs???) + for k in r.keys('ix_public_peer:*'): + peer = r.get(k.decode('utf-8')).decode('utf-8') + peer = json.loads(peer) + assert peer['description'] is not None # sanity: as above... + if not peer['description'].startswith(keyword): + continue + peer_address = ipaddress.ip_address(peer['name']) + if protocol == type(peer_address).__name__: + yield peer['name'] - r = common.get_redis() - for k in r.keys('ix_public_peer:*'): - peer = r.get(k.decode('utf-8')).decode('utf-8') - peer = json.loads(peer) - assert peer['description'] is not None # sanity: as above... - if not peer['description'].startswith(keyword): - continue - peer_address = ipaddress.ip_address(peer['name']) - if protocol == type(peer_address).__name__: - yield peer['name'] +@routes.route("/peer-info/<address>", methods=['GET', 'POST']) +@common.require_accepts_json +def peer_info(address): + r = common.get_redis() @@ -86,7 +89,7 @@ def peer_info(address): info = info.decode('utf-8') result['ix-public-peer-info'] = json.loads(info) result['ix-public-peer-group'] = list( - _related_ix_peers(result['ix-public-peer-info'])) + ix_peering_group(result['ix-public-peer-info'])) info = r.get('vpn_rr_peer:%s' % address) if info: