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

Finished feature DBOARD3-445-fix-broken-lg-api.

parents d97b4a22 634b9792
No related branches found
No related tags found
No related merge requests found
......@@ -12,8 +12,9 @@ These endpoints are intended for use by LG.
"""
import json
import logging
import re
from flask import Blueprint, jsonify, Response, request
from flask import Blueprint, Response, request
from inventory_provider.routes import common
from inventory_provider.routes.common import _ignore_cache_or_retrieve
......@@ -93,6 +94,10 @@ def routers(access):
.. asjson::
inventory_provider.routes.lg.LG_ROUTERS_SCHEMA
"equipment name" will be the IMS node name, but if this
doesn't end in ".geant.org" or ".geant.net", will be appended
with "geant.net"
:param access: one of `public` or `all`
:return:
"""
......@@ -111,22 +116,30 @@ def routers(access):
return router['type'] == 'CORE'
def _routers():
i = 0
for k in redis.scan_iter('ims:lg:*'):
for k in redis.scan_iter('ims:lg:*', count=1000):
rtr = redis.get(k.decode('utf-8')).decode('utf-8')
rtr = json.loads(rtr)
i += 1
if _visible(rtr):
yield rtr
hostname = rtr['equipment name'].lower()
if ' ' in hostname:
logger.warning(
'skipping LG router with ws in hostname: {hostname}')
continue
if not re.match(r'.*\.geant\.(net|org)$', hostname):
hostname = f'{hostname}.geant.net'
rtr['equipment name'] = hostname
yield rtr
cache_key = f'classifier-cache:ims-lg:{access}'
result = _ignore_cache_or_retrieve(request, cache_key, redis)
if not result:
result = list(_routers())
# cache this data for the next call
redis.set(cache_key, json.dumps(result).encode('utf-8'))
result = list(filter(_visible, _routers()))
if result:
result = json.dumps(result)
# cache this data for the next call
redis.set(cache_key, result)
if not result:
return Response(
......@@ -134,4 +147,4 @@ def routers(access):
status=404,
mimetype="text/html")
return jsonify(result)
return Response(result, mimetype="application/json")
import json
import re
import jsonschema
import pytest
from inventory_provider.routes.lg import LG_ROUTERS_SCHEMA
......@@ -21,23 +23,31 @@ def test_public_routers(client):
assert response_data # test data is non-empty
# no internal routers should be present
assert all(r['type'] == 'CORE' for r in response_data)
assert all(
re.match(r'^\S+\.geant\.(net|org)$', r['equipment name'])
for r in response_data)
def test_with_cache(client):
test_public_routers(client)
test_public_routers(client)
@pytest.mark.skip(reason='IMS INTERNAL vs CORE calcualtion is not clear')
def test_internal_routers(client):
rv = client.get(
'/lg/routers/all',
headers=DEFAULT_REQUEST_HEADERS)
assert rv.status_code == 200
assert rv.is_json
response_data = json.loads(rv.data.decode('utf-8'))
jsonschema.validate(response_data, LG_ROUTERS_SCHEMA)
assert response_data # test data is non-empty
# Temporarily removed until I check IMS - RL
# def test_internal_routers(client):
# rv = client.get(
# '/lg/routers/all',
# headers=DEFAULT_REQUEST_HEADERS)
# assert rv.status_code == 200
# assert rv.is_json
# response_data = json.loads(rv.data.decode('utf-8'))
# jsonschema.validate(response_data, LG_ROUTERS_SCHEMA)
#
# assert response_data # test data is non-empty
#
# # response should contain both public & internal routers
# assert any(r['type'] == 'INTERNAL' for r in response_data)
# assert any(r['type'] == 'CORE' for r in response_data)
# response should contain both public & internal routers
assert any(r['type'] == 'INTERNAL' for r in response_data)
assert any(r['type'] == 'CORE' for r in response_data)
@pytest.mark.parametrize('bad_endpoint', [
......
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