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

added test route for alarmsdb connection

parent 022d1a7d
No related branches found
No related tags found
No related merge requests found
......@@ -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"
......
......@@ -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
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")
......@@ -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),
......
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