diff --git a/inventory_provider/routes/classifier.py b/inventory_provider/routes/classifier.py index 6a604491242dd778ef6a72c456d1654c1cf016ed..c98bb5f67f1a5d12119fcada3692b609268408f2 100644 --- a/inventory_provider/routes/classifier.py +++ b/inventory_provider/routes/classifier.py @@ -2,9 +2,10 @@ import ipaddress import json import re -from flask import Blueprint, Response +from flask import Blueprint, Response, current_app from inventory_provider.routes import common +from inventory_provider.db import opsdb, db routes = Blueprint("inventory-data-classifier-support-routes", __name__) @@ -277,3 +278,36 @@ def get_trap_metadata(source_equipment, interface, circuit_id): r.set(cache_key, result.encode('utf-8')) return Response(result, mimetype="application/json") + + +@routes.route("/coriant-info/" + "<equipment_name>/<card_id>/<port_number>", + methods=['GET', 'POST']) +@common.require_accepts_json +def get_coriant_info(equipment_name, card_id, port_number): + r = common.get_redis() + + cache_key = 'classifier-cache:coriant:%s:%s:%s' % ( + equipment_name, card_id, port_number) + result = r.get(cache_key) + + if result: + result = result.decode('utf-8') + else: + config = current_app.config['INVENTORY_PROVIDER_CONFIG'] + with db.connection(config['ops-db']) as cx: + result = opsdb.get_coriant_path( + cx, equipment_name, card_id, port_number) + + if not result: + return Response( + response="no available info for {} {} {}".format( + equipment_name, card_id, port_number), + status=404, + mimetype="text/html") + + result = json.dumps(result) + # cache this data for the next call + r.set(cache_key, result.encode('utf-8')) + + return Response(result, mimetype="application/json")