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

Finished feature opsdb-connection.

parents 8eb02466 07cdddde
No related branches found
No related tags found
No related merge requests found
......@@ -17,11 +17,17 @@ def create_app():
app = Flask(__name__)
app.secret_key = "super secret session key"
from inventory_provider import data_routes
app.register_blueprint(data_routes.routes, url_prefix='/data')
from inventory_provider.routes import data
app.register_blueprint(data.routes, url_prefix='/data')
from inventory_provider import job_routes
app.register_blueprint(job_routes.routes, url_prefix='/jobs')
from inventory_provider.routes import jobs
app.register_blueprint(jobs.routes, url_prefix='/jobs')
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, \
......
......@@ -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
......@@ -6,9 +6,9 @@ import jsonschema
CONFIG_SCHEMA = {
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"alarms-db": {
"definitions": {
"database_credentials": {
"type": "object",
"properties": {
"hostname": {"type": "string"},
......@@ -18,7 +18,14 @@ CONFIG_SCHEMA = {
},
"required": ["hostname", "dbname", "username", "password"],
"additionalProperties": False
},
}
},
"type": "object",
"properties": {
"alarms-db": {"$ref": "#/definitions/database_credentials"},
"ops-db": {"$ref": "#/definitions/database_credentials"},
"oid_list.conf": {"type": "string"},
"routers_community.conf": {"type": "string"},
"ssh": {
......@@ -42,6 +49,7 @@ CONFIG_SCHEMA = {
},
"required": [
"alarms-db",
"ops-db",
"oid_list.conf",
"routers_community.conf",
"ssh",
......
import contextlib
import logging
import mysql.connector
from inventory_provider.constants import DATABASE_LOGGER_NAME
@contextlib.contextmanager
def connection(opsdb):
cx = None
try:
cx = mysql.connector.connect(
host=opsdb["hostname"],
user=opsdb["username"],
passwd=opsdb["password"],
db=opsdb["dbname"])
yield cx
finally:
if cx:
cx.close()
@contextlib.contextmanager
def cursor(cnx):
csr = None
try:
csr = cnx.cursor()
yield csr
finally:
if csr:
csr.close()
def _db_test(db, router):
database_logger = logging.getLogger(DATABASE_LOGGER_NAME)
with cursor(db) as crs:
query = "select model, manufacturer from equipment where name = %s"
crs.execute(query, (router['hostname'],))
for (model, manufacturer) in crs:
database_logger.debug("%s: %s %s" % (
router['hostname'], model, manufacturer))
yield {"model": model, "manufacturer": manufacturer}
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")
import functools
import json
from flask import Blueprint, request, Response, current_app
from inventory_provider import opsdb
routes = Blueprint("inventory-opsdb-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 opsdb_test():
config = current_app.config['INVENTORY_PROVIDER_CONFIG']
result = {}
with opsdb.connection(config['ops-db']) as db:
for r in config['routers']:
result[r['hostname']] = list(opsdb._db_test(db, r))
return Response(
json.dumps(result),
mimetype="application/json")
......@@ -90,6 +90,12 @@ def data_config_filename(tmp_dir_name):
"username": "xxxxxx",
"password": "xxxxxxxx"
},
"ops-db": {
"hostname": "xxxxxxx.yyyyy.zzz",
"dbname": "xxxxxx",
"username": "xxxxxx",
"password": "xxxxxxxx"
},
"oid_list.conf": os.path.join(
tmp_dir_name,
"oid_list.conf"),
......@@ -199,7 +205,7 @@ def test_routers_list(mocker, client):
'inventory_provider.router_details.redis.StrictRedis',
MockedRedis)
mocker.patch(
'inventory_provider.data_routes.redis.StrictRedis',
'inventory_provider.routes.data.redis.StrictRedis',
MockedRedis)
rv = client.post(
"data/routers",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment