diff --git a/inventory_provider/routes/classifier.py b/inventory_provider/routes/classifier.py index 51d41131ee8d843dbab42ba6aebd44a9993ff5f7..0a9602b8a92c737005567918e1bb9983f09b166b 100644 --- a/inventory_provider/routes/classifier.py +++ b/inventory_provider/routes/classifier.py @@ -1,6 +1,6 @@ import json -from flask import Blueprint, Response, current_app, jsonify +from flask import Blueprint, Response from inventory_provider.routes import common diff --git a/inventory_provider/routes/opsdb.py b/inventory_provider/routes/opsdb.py index 4b7fba38a3ea89d7b03933ded6f5b145dab978c9..6a33c1743fdbcce229ba981d6888959a2f44fd01 100644 --- a/inventory_provider/routes/opsdb.py +++ b/inventory_provider/routes/opsdb.py @@ -18,71 +18,3 @@ interface_status_key = "interface_statuses" # return {k.decode('utf8'): json.loads(v) for k, v in d.items()} # # -@routes.route("/interfaces") -def get_all_interface_details(): - r = common.get_redis() - result = collections.defaultdict(list) - for k in r.keys('opsdb:interface_services:*'): - m = re.match( - r'^opsdb:interface_services:([^:]+):(.*)$', - k.decode('utf-8')) - assert m - result[m.group(1)].append(m.group(2)) - return jsonify(result) - - -@routes.route("/interfaces/<equipment_name>") -def get_interface_details_for_equipment(equipment_name): - r = common.get_redis() - result = [] - for k in r.keys('opsdb:interface_services:%s:*' % equipment_name): - m = re.match( - r'^opsdb:interface_services:%s:(.*)$' % equipment_name, - k.decode('utf-8')) - assert m - result.append(m.group(1)) - return jsonify(result) - - -@routes.route("/interfaces/<equipment_name>/<path:interface>") -def get_interface_details(equipment_name, interface): - r = common.get_redis() - key = 'opsdb:interface_services:%s:%s' % (equipment_name, interface) - # TODO: handle None (return 404) - return jsonify(json.loads(r.get(key).decode('utf-8'))) - - -@routes.route("/equipment-location") -def get_all_equipment_locations(): - r = common.get_redis() - result = {} - for k in r.keys('opsdb:location:*'): - k = k.decode('utf-8') - m = re.match(r'^opsdb:location:(.*)$', k) - assert m - result[m.group(1)] = json.loads(r.get(k).decode('utf-8')) - return jsonify(result) - - -@routes.route("/equipment-location/<path:equipment_name>") -def get_equipment_location(equipment_name): - r = common.get_redis() - result = r.get('opsdb:location:' + equipment_name) - # TODO: handle None (return 404) - return jsonify(json.loads(result.decode('utf-8'))) - - -@routes.route("/circuit-hierarchy/children/<int:parent_id>") -def get_children(parent_id): - r = common.get_redis() - result = r.get('opsdb:services:children:%d' % parent_id) - # TODO: handle None (return 404) - return jsonify(json.loads(result.decode('utf-8'))) - - -@routes.route("/circuit-hierarchy/parents/<int:child_id>") -def get_parents(child_id): - r = common.get_redis() - result = r.get('opsdb:services:parents:%d' % child_id) - # TODO: handle None (return 404) - return jsonify(json.loads(result.decode('utf-8'))) diff --git a/inventory_provider/routes/testing.py b/inventory_provider/routes/testing.py index 1770be9db9cc80e6a543010105935d0788101e43..8d66aa5016c124ed9238a957116ac2a58c79cc4f 100644 --- a/inventory_provider/routes/testing.py +++ b/inventory_provider/routes/testing.py @@ -1,4 +1,6 @@ +import collections import json +import re from flask import Blueprint, Response, current_app, jsonify from inventory_provider.routes import common @@ -6,13 +8,13 @@ from inventory_provider.routes import common routes = Blueprint("inventory-data-testing-support-routes", __name__) -@routes.route("/flushdb", methods=['GET', 'POST']) +@routes.route("flushdb", methods=['GET', 'POST']) def flushdb(): common.get_redis().flushdb() return Response('OK') -@routes.route("/infinera-dna-addresses", methods=['GET', 'POST']) +@routes.route("infinera-dna-addresses", methods=['GET', 'POST']) @common.require_accepts_json def infinera_addresses(): infinera_config = current_app.config[ @@ -20,7 +22,7 @@ def infinera_addresses(): return jsonify([dna['address'] for dna in infinera_config]) -@routes.route("/coriant-tnms-addresses", methods=['GET', 'POST']) +@routes.route("coriant-tnms-addresses", methods=['GET', 'POST']) @common.require_accepts_json def coriant_addresses(): coriant_config = current_app.config[ @@ -28,7 +30,7 @@ def coriant_addresses(): return jsonify([tnms['address'] for tnms in coriant_config]) -@routes.route("/juniper-server-addresses", methods=['GET', 'POST']) +@routes.route("juniper-server-addresses", methods=['GET', 'POST']) @common.require_accepts_json def juniper_addresses(): # TODO: this route (and corant, infinera routes) can be removed @@ -40,3 +42,73 @@ def juniper_addresses(): info = json.loads(info.decode('utf-8')) routers.append(info['address']) return jsonify(routers) + + +@routes.route("opsdb/interfaces") +def get_all_interface_details(): + r = common.get_redis() + result = collections.defaultdict(list) + for k in r.keys('opsdb:interface_services:*'): + m = re.match( + r'^opsdb:interface_services:([^:]+):(.*)$', + k.decode('utf-8')) + assert m + result[m.group(1)].append(m.group(2)) + return jsonify(result) + + +@routes.route("opsdb/interfaces/<equipment_name>") +def get_interface_details_for_equipment(equipment_name): + r = common.get_redis() + result = [] + for k in r.keys('opsdb:interface_services:%s:*' % equipment_name): + m = re.match( + r'^opsdb:interface_services:%s:(.*)$' % equipment_name, + k.decode('utf-8')) + assert m + result.append(m.group(1)) + return jsonify(result) + + +@routes.route("opsdb/interfaces/<equipment_name>/<path:interface>") +def get_interface_details(equipment_name, interface): + r = common.get_redis() + key = 'opsdb:interface_services:%s:%s' % (equipment_name, interface) + # TODO: handle None (return 404) + return jsonify(json.loads(r.get(key).decode('utf-8'))) + + +@routes.route("opsdb/equipment-location") +def get_all_equipment_locations(): + r = common.get_redis() + result = {} + for k in r.keys('opsdb:location:*'): + k = k.decode('utf-8') + m = re.match(r'^opsdb:location:(.*)$', k) + assert m + result[m.group(1)] = json.loads(r.get(k).decode('utf-8')) + return jsonify(result) + + +@routes.route("opsdb/equipment-location/<path:equipment_name>") +def get_equipment_location(equipment_name): + r = common.get_redis() + result = r.get('opsdb:location:' + equipment_name) + # TODO: handle None (return 404) + return jsonify(json.loads(result.decode('utf-8'))) + + +@routes.route("opsdb/circuit-hierarchy/children/<int:parent_id>") +def get_children(parent_id): + r = common.get_redis() + result = r.get('opsdb:services:children:%d' % parent_id) + # TODO: handle None (return 404) + return jsonify(json.loads(result.decode('utf-8'))) + + +@routes.route("opsdb/circuit-hierarchy/parents/<int:child_id>") +def get_parents(child_id): + r = common.get_redis() + result = r.get('opsdb:services:parents:%d' % child_id) + # TODO: handle None (return 404) + return jsonify(json.loads(result.decode('utf-8'))) diff --git a/test/test_external_inventory_routes.py b/test/test_external_inventory_routes.py index 94687251b8fc35075d8a8763a47d9cd69bc4dc87..3b07012d30b24ee73e19c4389f08f13958e59ed1 100644 --- a/test/test_external_inventory_routes.py +++ b/test/test_external_inventory_routes.py @@ -4,57 +4,3 @@ DEFAULT_REQUEST_HEADERS = { "Accept": ["application/json"] } - -def test_get_equipment_location(client_with_mocked_data): - rv = client_with_mocked_data.get( - '/opsdb/equipment-location', - headers=DEFAULT_REQUEST_HEADERS) - assert rv.status_code == 200 - assert rv.is_json - # TODO: validate against schema - - -def test_get_interface_info(client_with_mocked_data): - rv = client_with_mocked_data.get( - '/opsdb/interfaces', - headers=DEFAULT_REQUEST_HEADERS) - assert rv.status_code == 200 - assert rv.is_json - # TODO: validate against schema - - -def test_get_interface_info_for_equipment(client_with_mocked_data): - rv = client_with_mocked_data.get( - '/opsdb/interfaces/mx1.ams.nl.geant.net', - headers=DEFAULT_REQUEST_HEADERS) - assert rv.status_code == 200 - assert rv.is_json - # TODO: validate against schema - - -def test_get_interface_info_for_equipment_and_interface( - client_with_mocked_data): - rv = client_with_mocked_data.get( - '/opsdb/interfaces/mx1.ams.nl.geant.net/ae0.0', - headers=DEFAULT_REQUEST_HEADERS) - assert rv.status_code == 200 - assert rv.is_json - # TODO: validate against schema - - -def test_get_children(client_with_mocked_data): - rv = client_with_mocked_data.get( - '/opsdb/circuit-hierarchy/children/12363', - headers=DEFAULT_REQUEST_HEADERS) - assert rv.status_code == 200 - assert rv.is_json - # TODO: validate against schema - - -def test_get_parents(client_with_mocked_data): - rv = client_with_mocked_data.get( - '/opsdb/circuit-hierarchy/parents/11725', - headers=DEFAULT_REQUEST_HEADERS) - assert rv.status_code == 200 - assert rv.is_json - # TODO: validate against schema diff --git a/test/test_testing_routes.py b/test/test_testing_routes.py index c3fa0e53881b64e136813d31ca74fb6e60e25019..fd7dd96f05798e9e6af32033fd14073396f7dae4 100644 --- a/test/test_testing_routes.py +++ b/test/test_testing_routes.py @@ -38,7 +38,7 @@ def test_coriant_addresses(client): ROUTER_LIST_SCHEMA) -def test_juniper_addresses(mocker, client): +def test_juniper_addresses(client): rv = client.post( "/testing/juniper-server-addresses", headers=DEFAULT_REQUEST_HEADERS) @@ -46,3 +46,59 @@ def test_juniper_addresses(mocker, client): response_data = json.loads(rv.data.decode('utf-8')) jsonschema.validate(response_data, ROUTER_LIST_SCHEMA) assert len(response_data) > 0 # test data is not empty + + + +def test_get_equipment_location(client_with_mocked_data): + rv = client_with_mocked_data.get( + '/testing/opsdb/equipment-location', + headers=DEFAULT_REQUEST_HEADERS) + assert rv.status_code == 200 + assert rv.is_json + # TODO: validate against schema + + +def test_get_interface_info(client_with_mocked_data): + rv = client_with_mocked_data.get( + '/testing/opsdb/interfaces', + headers=DEFAULT_REQUEST_HEADERS) + assert rv.status_code == 200 + assert rv.is_json + # TODO: validate against schema + + +def test_get_interface_info_for_equipment(client_with_mocked_data): + rv = client_with_mocked_data.get( + '/testing/opsdb/interfaces/mx1.ams.nl.geant.net', + headers=DEFAULT_REQUEST_HEADERS) + assert rv.status_code == 200 + assert rv.is_json + # TODO: validate against schema + + +def test_get_interface_info_for_equipment_and_interface( + client_with_mocked_data): + rv = client_with_mocked_data.get( + '/testing/opsdb/interfaces/mx1.ams.nl.geant.net/ae0.0', + headers=DEFAULT_REQUEST_HEADERS) + assert rv.status_code == 200 + assert rv.is_json + # TODO: validate against schema + + +def test_get_children(client_with_mocked_data): + rv = client_with_mocked_data.get( + '/testing/opsdb/circuit-hierarchy/children/12363', + headers=DEFAULT_REQUEST_HEADERS) + assert rv.status_code == 200 + assert rv.is_json + # TODO: validate against schema + + +def test_get_parents(client_with_mocked_data): + rv = client_with_mocked_data.get( + '/testing/opsdb/circuit-hierarchy/parents/11725', + headers=DEFAULT_REQUEST_HEADERS) + assert rv.status_code == 200 + assert rv.is_json + # TODO: validate against schema