diff --git a/surveil/api/config.py b/surveil/api/config.py index 6f4ce9d..0747092 100644 --- a/surveil/api/config.py +++ b/surveil/api/config.py @@ -12,7 +12,6 @@ # License for the specific language governing permissions and limitations # under the License. -import pymongo from surveil.api import hooks @@ -22,10 +21,18 @@ server = { 'host': '0.0.0.0' } + +# In the future, surveil_api_config could be loaded from somewhere else, +# as long as is it in the same format. +surveil_api_config = { + "mongodb_uri": "mongodb://mongo:27017", + "ws_arbiter_url": "http://shinken:7760" +} + app_hooks = [ hooks.DBHook( - pymongo.MongoClient('mongo', 27017), - "http://shinken:7760" + surveil_api_config['mongodb_uri'], + surveil_api_config['ws_arbiter_url'] ) ] diff --git a/surveil/api/hooks.py b/surveil/api/hooks.py index ed42c99..a79eb98 100644 --- a/surveil/api/hooks.py +++ b/surveil/api/hooks.py @@ -13,14 +13,20 @@ # under the License. from pecan import hooks +import pymongo class DBHook(hooks.PecanHook): - def __init__(self, mongo_connection, ws_arbiter_url): - self.mongo_connection = mongo_connection + def __init__(self, mongo_url, ws_arbiter_url): + self.mongo_url = mongo_url self.ws_arbiter_url = ws_arbiter_url def before(self, state): - state.request.mongo_connection = self.mongo_connection + self.mongoclient = pymongo.MongoClient(self.mongo_url) + + state.request.mongo_connection = self.mongoclient state.request.ws_arbiter_url = self.ws_arbiter_url + + def after(self, state): + self.mongoclient.close() diff --git a/surveil/cmd/init.py b/surveil/cmd/init.py index 7682ed4..f15963d 100644 --- a/surveil/cmd/init.py +++ b/surveil/cmd/init.py @@ -16,6 +16,7 @@ import subprocess +import pymongo import surveilclient.client as sc from surveil.api import config @@ -23,7 +24,7 @@ from surveil.api import config def main(): # Create a basic config in mongodb - mongo = config.app_hooks[0].mongo_connection + mongo = pymongo.MongoClient(config.surveil_api_config['mongodb_uri']) # Drop the current shinken config mongo.drop_database('shinken') diff --git a/surveil/tests/api/functionalTest.py b/surveil/tests/api/functionalTest.py index af626a1..2f98f97 100644 --- a/surveil/tests/api/functionalTest.py +++ b/surveil/tests/api/functionalTest.py @@ -17,9 +17,9 @@ import os import mongomock from oslo_config import cfg import pecan +from pecan import hooks import pecan.testing -from surveil.api import hooks from surveil.tests import base @@ -38,8 +38,17 @@ class FunctionalTest(base.BaseTestCase): self.mongoconnection = mongomock.Connection() self.ws_arbiter_url = "http://localhost:7760" + class TestHook(hooks.PecanHook): + def __init__(self, mongoclient, wsarbiterurl): + self.mongoclient = mongoclient + self.ws_arbiter_url = wsarbiterurl + + def before(self, state): + state.request.mongo_connection = self.mongoclient + state.request.ws_arbiter_url = self.ws_arbiter_url + app_hooks = [ - hooks.DBHook( + TestHook( self.mongoconnection, self.ws_arbiter_url )