diff --git a/inventory_provider/__init__.py b/inventory_provider/__init__.py index 40a08173aece9c87baafeeb74ed880fd22987606..a8e55e8edb0d75ffbbbcae07669b9d63450a064f 100644 --- a/inventory_provider/__init__.py +++ b/inventory_provider/__init__.py @@ -26,6 +26,9 @@ def create_app(): from inventory_provider.routes import opsdb app.register_blueprint(opsdb.routes, url_prefix='/opsdb') + from inventory_provider.routes import alarmsdb + app.register_blueprint(alarmsdb.routes, url_prefix='/alarmsdb') + if "SETTINGS_FILENAME" not in os.environ: assert False, \ "environment variable SETTINGS_FILENAME' must be defined" diff --git a/inventory_provider/alarmsdb.py b/inventory_provider/alarmsdb.py index 01a4390b2ba53bf112185a508da8b760365ef42b..ee16e0dd2c0a9738b23f63cc941dd4d4f06960ad 100644 --- a/inventory_provider/alarmsdb.py +++ b/inventory_provider/alarmsdb.py @@ -40,3 +40,4 @@ def _db_test(db, router): crs.execute(query, (router['hostname'],)) for (absid,) in crs: database_logger.debug("absid: %r" % absid) + yield absid \ No newline at end of file diff --git a/inventory_provider/routes/alarmsdb.py b/inventory_provider/routes/alarmsdb.py new file mode 100644 index 0000000000000000000000000000000000000000..ce06752987a037d8958f227a4b32268805c4b5c9 --- /dev/null +++ b/inventory_provider/routes/alarmsdb.py @@ -0,0 +1,41 @@ +import functools +import json + +from flask import Blueprint, request, Response, current_app +from inventory_provider import alarmsdb + +routes = Blueprint("inventory-alarmsdb-query-routes", __name__) + + +def require_accepts_json(f): + """ + used as a route handler decorator to return an error + unless the request allows responses with type "application/json" + :param f: the function to be decorated + :return: the decorated function + """ + @functools.wraps(f) + def decorated_function(*args, **kwargs): + # TODO: use best_match to disallow */* ...? + if not request.accept_mimetypes.accept_json: + return Response( + response="response will be json", + status=406, + mimetype="text/html") + return f(*args, **kwargs) + return decorated_function + + +@routes.route("/test", methods=['GET', 'POST']) +@require_accepts_json +def alarmsdb_test(): + config = current_app.config['INVENTORY_PROVIDER_CONFIG'] + + result = {} + with alarmsdb.connection(config['alarms-db']) as db: + for r in config['routers']: + result[r['hostname']] = list(alarmsdb._db_test(db, r)) + + return Response( + json.dumps(result), + mimetype="application/json") diff --git a/inventory_provider/routes/opsdb.py b/inventory_provider/routes/opsdb.py index 92cea909ccc0d8d98ef775292b119b65d8885fab..1b3195ecec04c04d87598918bc03d4862e07739a 100644 --- a/inventory_provider/routes/opsdb.py +++ b/inventory_provider/routes/opsdb.py @@ -6,11 +6,6 @@ from inventory_provider import opsdb routes = Blueprint("inventory-opsdb-query-routes", __name__) -VERSION = { - "api": "0.1", - "module": "0.1" -} - def require_accepts_json(f): """ @@ -35,14 +30,11 @@ def require_accepts_json(f): @require_accepts_json def opsdb_test(): config = current_app.config['INVENTORY_PROVIDER_CONFIG'] - result = [] - with opsdb.connection(config['ops-db']) as db: + result = {} + with opsdb.connection(config['ops-db']) as db: for r in config['routers']: - result.append({ - 'router': r['hostname'], - 'info': list(opsdb._db_test(db, r)) - }) + result[r['hostname']] = list(opsdb._db_test(db, r)) return Response( json.dumps(result),