Skip to content
Snippets Groups Projects
Commit 10e0c052 authored by Erik Reid's avatar Erik Reid
Browse files

added ix public peer groups

parent d6d79ce3
No related branches found
No related tags found
No related merge requests found
import ipaddress
import json import json
from flask import Blueprint, Response, jsonify from flask import Blueprint, Response, jsonify
...@@ -49,6 +50,35 @@ def get_trap_metadata(source_equipment, interface): ...@@ -49,6 +50,35 @@ def get_trap_metadata(source_equipment, interface):
@common.require_accepts_json @common.require_accepts_json
def peer_info(address): def peer_info(address):
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:
"""
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()
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
if peer['name'] == address:
continue # skip self
peer_address = ipaddress.ip_address(peer['name'])
if protocol == type(peer_address).__name__:
yield peer['name']
r = common.get_redis() r = common.get_redis()
result = {} result = {}
...@@ -57,6 +87,8 @@ def peer_info(address): ...@@ -57,6 +87,8 @@ def peer_info(address):
if info: if info:
info = info.decode('utf-8') info = info.decode('utf-8')
result['ix-public-peer-info'] = json.loads(info) result['ix-public-peer-info'] = json.loads(info)
result['ix-public-peer-group'] = list(
_related_ix_peers(result['ix-public-peer-info']))
info = r.get('vpn_rr_peer:%s' % address) info = r.get('vpn_rr_peer:%s' % address)
if info: if info:
......
...@@ -22,14 +22,19 @@ def test_trap_metadata(client_with_mocked_data): ...@@ -22,14 +22,19 @@ def test_trap_metadata(client_with_mocked_data):
jsonschema.validate(response_data, response_schema) jsonschema.validate(response_data, response_schema)
@pytest.mark.parametrize("peer_address,peer_type", [ VPN_RR_PEER_INFO_KEYS = {'vpn-rr-peer-info'}
('109.105.110.54', 'vpn-rr-peer-info'), IX_PUBLIC_PEER_INFO_KEYS = {'ix-public-peer-info', 'ix-public-peer-group'}
('2001:07f8:001c:024a:0000:0000:316e:0001', 'ix-public-peer-info'),
('2001:07f8:000b:0100:01d1:a5d1:0310:0029', 'ix-public-peer-info'),
('195.66.224.238', 'ix-public-peer-info'), @pytest.mark.parametrize("peer_address,expected_response_keys", [
('109.105.110.54', VPN_RR_PEER_INFO_KEYS),
('2001:07f8:001c:024a:0000:0000:316e:0001', IX_PUBLIC_PEER_INFO_KEYS),
('2001:07f8:000b:0100:01d1:a5d1:0310:0029', IX_PUBLIC_PEER_INFO_KEYS),
('195.66.224.238', IX_PUBLIC_PEER_INFO_KEYS),
] ]
) )
def test_peer_info(client_with_mocked_data, peer_address, peer_type): def test_peer_info(
client_with_mocked_data, peer_address, expected_response_keys):
response_schema = { response_schema = {
"$schema": "http://json-schema.org/draft-07/schema#", "$schema": "http://json-schema.org/draft-07/schema#",
"type": "object", "type": "object",
...@@ -53,7 +58,6 @@ def test_peer_info(client_with_mocked_data, peer_address, peer_type): ...@@ -53,7 +58,6 @@ def test_peer_info(client_with_mocked_data, peer_address, peer_type):
"required": ["name", "description"], "required": ["name", "description"],
"additionalProperties": False "additionalProperties": False
}, },
"ix-public-peer": { "ix-public-peer": {
"type": "object", "type": "object",
"properties": { "properties": {
...@@ -72,12 +76,18 @@ def test_peer_info(client_with_mocked_data, peer_address, peer_type): ...@@ -72,12 +76,18 @@ def test_peer_info(client_with_mocked_data, peer_address, peer_type):
}, },
"required": ["name", "description", "as"], "required": ["name", "description", "as"],
"additionalProperties": False "additionalProperties": False
} },
"ix-public-peer-group": {
"type": "array",
"items": {"$ref": "#/definitions/ip-address"}
},
}, },
"type": "object", "type": "object",
"properties": { "properties": {
"ix-public-peer-info": {"$ref": "#/definitions/ix-public-peer"}, "ix-public-peer-info": {"$ref": "#/definitions/ix-public-peer"},
"ix-public-peer-group": {"$ref": "#/definitions/ix-public-peer-group"},
"vpn-rr-peer-info": {"$ref": "#/definitions/vpn-rr-peer"} "vpn-rr-peer-info": {"$ref": "#/definitions/vpn-rr-peer"}
}, },
"additionalProperties": False "additionalProperties": False
...@@ -91,10 +101,7 @@ def test_peer_info(client_with_mocked_data, peer_address, peer_type): ...@@ -91,10 +101,7 @@ def test_peer_info(client_with_mocked_data, peer_address, peer_type):
response_data = json.loads(rv.data.decode('utf-8')) response_data = json.loads(rv.data.decode('utf-8'))
jsonschema.validate(response_data, response_schema) jsonschema.validate(response_data, response_schema)
assert len(response_data) == 1, \ assert set(response_data.keys()) == expected_response_keys
"peer should be only vpn-rr or ix-public, not both"
assert peer_type in response_data
def test_peer_not_found(client_with_mocked_data): def test_peer_not_found(client_with_mocked_data):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment