Skip to content
Snippets Groups Projects
Commit d258e42e authored by Sam Roberts's avatar Sam Roberts
Browse files

make corrections from MR

parent 7b4cff8a
No related branches found
No related tags found
1 merge request!1Feature/reporting 297 msr mdvpn endpoint
...@@ -982,12 +982,6 @@ def mdvpn(): ...@@ -982,12 +982,6 @@ def mdvpn():
return description.replace(prefix, '') return description.replace(prefix, '')
return description return description
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# REVIEW COMMENT ONLY, DO NOT MERGE:
# _make_group_index is extremely, extremely generic,
# but is it worth extracting, will anything else use it?
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
def _make_group_index(group, index_key): def _make_group_index(group, index_key):
""" """
Utility function to take a list and make it a dict based off a given Utility function to take a list and make it a dict based off a given
...@@ -1002,64 +996,63 @@ def mdvpn(): ...@@ -1002,64 +996,63 @@ def mdvpn():
index = {} index = {}
for peering in group: for peering in group:
key = peering.get(index_key) key = peering.get(index_key)
if key in index: index.setdefault(key, []).append(peering)
peering_list = index[key]
peering_list.append(peering)
else:
index[key] = [peering]
return index return index
def _bgplu_peerings(asn, bgplu_index): def _bgplu_peerings(asn, bgplu_index):
peerings = [] if asn not in bgplu_index:
if asn in bgplu_index: yield False
for peering in bgplu_index[asn]: for peering in bgplu_index[asn]:
formatted_peering = { formatted_peering = {
"name": peering['description'], "name": peering['description'],
"v4": peering['address'], "v4": peering['address'],
"v6": '', "v6": '',
"hostname": peering['hostname'] "hostname": peering['hostname']
} }
peerings.append(formatted_peering) yield formatted_peering
return peerings
def _vpnrr_peerings(asn, vpnrr_index): def _vpnrr_peerings(asn, vpnrr_index):
peerings = [] if asn not in vpnrr_index:
if asn in vpnrr_index: yield False
vrr_peering_group = vpnrr_index[asn] vrr_peering_group = vpnrr_index[asn]
# rearrange into index using ipv4 as key # rearrange into index using ipv4 as key
# this will collect related entries under the same ipv4 # this will collect related entries under the same ipv4
ip_index = _make_group_index(vrr_peering_group, 'address') ip_index = _make_group_index(vrr_peering_group, 'address')
for ip in ip_index: for ip in ip_index:
ip_details = ip_index[ip] # a list of all info for given ipv4 ip_details = ip_index[ip] # a list of all info for given ipv4
hostnames = [item['hostname'] for item in ip_details] hostnames = [item['hostname'] for item in ip_details]
description = ip_details[0]['description'] description = ip_details[0]['description']
formatted_peering = { formatted_peering = {
"description": _get_consistent_description(description), "description": _get_consistent_description(description),
"v4": ip, "v4": ip,
"hostname": hostnames "hostname": hostnames
} }
peerings.append(formatted_peering) yield formatted_peering
return peerings
def _peerings_for_nren(asn, bgplu_index, vpnrr_index): def _peerings_for_nren(asn, bgplu_index, vpnrr_index):
return { return {
"asn": asn, "asn": asn,
"AP": _bgplu_peerings(asn, bgplu_index), "AP": list(filter(None, _bgplu_peerings(asn, bgplu_index))),
"VRR": _vpnrr_peerings(asn, vpnrr_index) "VRR": list(filter(None, _vpnrr_peerings(asn, vpnrr_index)))
} }
r = common.get_current_redis() r = common.get_current_redis()
bgplu = json.loads(r.get('juniper-peerings:group:BGPLU').decode('utf-8')) cache_key = 'classifier-cache:msr:mdvpn'
vpnrr = json.loads(r.get('juniper-peerings:group:VPN-RR').decode('utf-8')) response = _ignore_cache_or_retrieve(request, cache_key, r)
bgplu_index = _make_group_index(bgplu, 'remote-asn') if not response:
vpnrr_index = _make_group_index(vpnrr, 'remote-asn') bgplu = json.loads(
config = current_app.config['INVENTORY_PROVIDER_CONFIG'] r.get('juniper-peerings:group:BGPLU').decode('utf-8'))
nren_asn_map = config['nren-asn-map'] vpnrr = json.loads(
nren_details = [ r.get('juniper-peerings:group:VPN-RR').decode('utf-8'))
_peerings_for_nren(pair['asn'], bgplu_index = _make_group_index(bgplu, 'remote-asn')
bgplu_index, vpnrr_index = _make_group_index(vpnrr, 'remote-asn')
vpnrr_index) config = current_app.config['INVENTORY_PROVIDER_CONFIG']
for pair in nren_asn_map] nren_asn_map = config['nren-asn-map']
response = json.dumps(nren_details) nren_details = [
_peerings_for_nren(pair['asn'],
bgplu_index,
vpnrr_index)
for pair in nren_asn_map]
response = json.dumps(nren_details)
return Response(response, mimetype='application/json') return Response(response, mimetype='application/json')
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment