diff --git a/inventory_provider/routes/neteng.py b/inventory_provider/routes/neteng.py index 867f7715d4ce722eeb0eadc414889599a37456fb..c09d44600a8168330bb615f461eab79954ea1545 100644 --- a/inventory_provider/routes/neteng.py +++ b/inventory_provider/routes/neteng.py @@ -42,9 +42,9 @@ def after_request(resp): @common.require_accepts_json def get_location(equipment): """ - Handler for `/neteng/location/equipment-name` + Handler for `/neteng/location/<equipment-name>` - This method will pop location information for the IMS node + This method will return pop location information for the IMS node with name = `equipment-name`. 404 is returned if the IMS node name is not known. @@ -100,3 +100,39 @@ def get_pops(): return jsonify(sorted(list(_pops()))) + +@routes.route('/pop/<name>', methods=['GET', 'POST']) +@common.require_accepts_json +def get_pop_location(name): + """ + Handler for `/neteng/pop/<name>` + + This method will return location information for the POP + with name = `name` in IMS. + + 404 is returned if the POP name is not known. + Otherwise the return value will be formatted as: + + .. asjson:: + inventory_provider.db.ims_data.POP_LOCATION_SCHEMA + + :return: as above + """ + + r = common.get_current_redis() + + value = r.get(f'ims:pop:{name}') + if not value: + return Response( + response='no location information available for "{pop}"', + status=404, + mimetype='text/html') + + value = json.loads(value.decode('utf-8')) + if not value: + return Response( + response='unexpected empty cached data for "{name}"', + status=500, + mimetype='text/html') + + return jsonify(value) diff --git a/test/data/router-info.json b/test/data/router-info.json index 11cf50f26a517892c3177aaebce341a8dd8207d0..2f118d9e38d0562b5521cf06e4673610dd114d00 100644 Binary files a/test/data/router-info.json and b/test/data/router-info.json differ diff --git a/test/test_neteng_routes.py b/test/test_neteng_routes.py index 23a7bba78bbb960a69fe63a240e7302e1cc588b2..2154dcd2e674b5034adf5891b4ee9602dad38108 100644 --- a/test/test_neteng_routes.py +++ b/test/test_neteng_routes.py @@ -37,3 +37,25 @@ def test_get_pops(client, mocked_redis): jsonschema.validate( json.loads(rv.data.decode('utf-8')), STRING_LIST_SCHEMA) + + +@pytest.mark.parametrize('pop_name', [ + 'AMSTERDAM', 'VIENNA', 'LONDON', 'LONDON 2' +]) +def test_pop_location(client, mocked_redis, pop_name): + rv = client.post( + f'/neteng/pop/{pop_name}', + headers={'Accept': ['application/json']}) + assert rv.status_code == 200 + jsonschema.validate( + json.loads(rv.data.decode('utf-8')), + POP_LOCATION_SCHEMA) + s = json.loads(rv.data.decode('utf-8')) + print(s) + +def test_pop_not_found(client, mocked_redis): + rv = client.post( + '/neteng/pop/BOGUS.POP.NAME', + headers={'Accept': ['application/json']}) + assert rv.status_code == 404 +