From 566f89ceced2cbadebd9aa9fc8f6b71427aecb5f Mon Sep 17 00:00:00 2001 From: aviau Date: Mon, 18 Aug 2014 15:24:58 -0400 Subject: [PATCH] Added get functions to Hosts controller Change-Id: I4968435b73b5f05a0bf8a2947306201e48c833cb --- Dockerfile | 6 +- requirements.txt | 1 + surveil/api/app.py | 6 +- surveil/api/config.py | 13 ++++- surveil/api/controllers/v1/hosts.py | 18 ++++-- surveil/api/hooks.py | 24 ++++++++ .../tests/api/controllers/v1/test_hosts.py | 58 +++++++++++++++++++ surveil/tests/api/functionalTest.py | 19 +++++- surveil/tests/base.py | 33 +++++++++++ test-requirements.txt | 3 +- tools/docker/etc/mongodb.conf | 16 +++++ 11 files changed, 184 insertions(+), 13 deletions(-) create mode 100644 surveil/api/hooks.py create mode 100644 surveil/tests/api/controllers/v1/test_hosts.py create mode 100644 surveil/tests/base.py create mode 100644 tools/docker/etc/mongodb.conf diff --git a/Dockerfile b/Dockerfile index 0d610fa..9c9d32e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -58,8 +58,9 @@ RUN apt-get install -y libapache2-mod-proxy-html RUN a2enmod proxy_http ADD tools/docker/etc/apache2/conf-enabled/influxdb.conf /etc/apache2/conf-enabled/influxdb.conf -### Mongo +### Mongodb RUN apt-get install -y mongodb +ADD tools/docker/etc/mongodb.conf /etc/mongodb.conf ### Surveil ## Copy files @@ -94,4 +95,7 @@ EXPOSE 80 # Riemann EXPOSE 5555 +# Mongodb +EXPOSE 27017 + CMD ["/usr/bin/supervisord"] diff --git a/requirements.txt b/requirements.txt index ede54b0..09faf82 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ pecan>=0.5.0 +pymongo>=2.7.2 diff --git a/surveil/api/app.py b/surveil/api/app.py index c936082..d1dfbde 100644 --- a/surveil/api/app.py +++ b/surveil/api/app.py @@ -13,16 +13,16 @@ # under the License. import pecan -# from pecanrest import model def setup_app(config): - # model.init_model() app_conf = dict(config.app) - return pecan.make_app( + app = pecan.make_app( app_conf.pop('root'), logging=getattr(config, 'logging', {}), **app_conf ) + + return app diff --git a/surveil/api/config.py b/surveil/api/config.py index 2ae7c4f..31cf795 100644 --- a/surveil/api/config.py +++ b/surveil/api/config.py @@ -12,12 +12,22 @@ # License for the specific language governing permissions and limitations # under the License. +import pymongo + +from surveil.api import hooks + # Server Specific Configurations server = { 'port': '8080', 'host': '0.0.0.0' } +app_hooks = [ + hooks.DBHook( + pymongo.MongoClient('172.17.0.2', 27017) + ) +] + # Pecan Application Configurations app = { 'root': 'surveil.api.controllers.root.RootController', @@ -27,7 +37,8 @@ app = { 'errors': { 404: '/error/404', '__force_dict__': True - } + }, + 'hooks': app_hooks, } logging = { diff --git a/surveil/api/controllers/v1/hosts.py b/surveil/api/controllers/v1/hosts.py index be70ad2..9d3fa32 100644 --- a/surveil/api/controllers/v1/hosts.py +++ b/surveil/api/controllers/v1/hosts.py @@ -22,10 +22,15 @@ class HostController(rest.RestController): pecan.request.context['host_id'] = host_id self._id = host_id - @pecan.expose() + @pecan.expose("json") def get(self): """Returns a specific host.""" - return "Returns a specific host: " + self._id + host = pecan.request.mongo_connection.shinken.hosts.find_one( + {"host_name": self._id} + ) + if host: + del host['_id'] + return host class HostsController(rest.RestController): @@ -34,7 +39,12 @@ class HostsController(rest.RestController): def _lookup(self, host_id, *remainder): return HostController(host_id), remainder - @pecan.expose() + @pecan.expose("json") def get_all(self): """Returns all host.""" - return "Returns all hosts" + hosts = [host for host in + pecan.request.mongo_connection.shinken.hosts.find()] + for host in hosts: + del host['_id'] + + return hosts \ No newline at end of file diff --git a/surveil/api/hooks.py b/surveil/api/hooks.py new file mode 100644 index 0000000..1aad2ac --- /dev/null +++ b/surveil/api/hooks.py @@ -0,0 +1,24 @@ +# Copyright 2014 - Savoir-Faire Linux inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from pecan import hooks + + +class DBHook(hooks.PecanHook): + + def __init__(self, mongo_connection): + self.mongo_connection = mongo_connection + + def before(self, state): + state.request.mongo_connection = self.mongo_connection diff --git a/surveil/tests/api/controllers/v1/test_hosts.py b/surveil/tests/api/controllers/v1/test_hosts.py new file mode 100644 index 0000000..2e476c6 --- /dev/null +++ b/surveil/tests/api/controllers/v1/test_hosts.py @@ -0,0 +1,58 @@ +# Copyright 2014 - Savoir-Faire Linux inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import copy +import json + +from surveil.tests.api import functionalTest + + +class TestRootController(functionalTest.FunctionalTest): + + def test_get_all_hosts(self): + hosts = [ + {u"use": u"generic-host", u"contact_groups": u"admins", + u"host_name": u"testhost1", u"address": u"www.google.ca"}, + {u"use": u"generic-host", u"contact_groups": u"admins", + u"host_name": u"testhost2", u"address": u"www.google.ca"}, + {u"use": u"generic-host", u"contact_groups": u"admins", + u"host_name": u"testhost3", u"address": u"www.google.ca"} + ] + self.mongoconnection.shinken.hosts.insert(copy.deepcopy(hosts)) + + response = self.app.get('/v1/hosts') + + self.assert_count_equal_backport( + json.loads(response.body.decode()), + hosts + ) + self.assertEqual(response.status_int, 200) + + def test_get_specific_host(self): + hosts = [ + {u"use": u"generic-host", u"contact_groups": u"admins", + u"host_name": u"testhost1", u"address": u"www.google.ca"}, + {u"use": u"generic-host", u"contact_groups": u"admins", + u"host_name": u"testhost2", u"address": u"www.google.ca"}, + {u"use": u"generic-host", u"contact_groups": u"admins", + u"host_name": u"testhost3", u"address": u"www.google.ca"} + ] + self.mongoconnection.shinken.hosts.insert(copy.deepcopy(hosts)) + + response = self.app.get('/v1/hosts/testhost2') + + self.assert_count_equal_backport( + json.loads(response.body.decode()), + hosts[1] + ) diff --git a/surveil/tests/api/functionalTest.py b/surveil/tests/api/functionalTest.py index 500e3d5..0910f32 100644 --- a/surveil/tests/api/functionalTest.py +++ b/surveil/tests/api/functionalTest.py @@ -12,15 +12,18 @@ # License for the specific language governing permissions and limitations # under the License. +import mongomock import pecan import pecan.testing -import unittest +from surveil.api import hooks +from surveil.tests import base + __all__ = ['FunctionalTest'] -class FunctionalTest(unittest.TestCase): +class FunctionalTest(base.BaseTestCase): """Used for functional tests. Used where you need to test your literal @@ -28,11 +31,21 @@ class FunctionalTest(unittest.TestCase): """ def setUp(self): + + self.mongoconnection = mongomock.Connection() + + app_hooks = [ + hooks.DBHook( + self.mongoconnection + ) + ] + self.app = pecan.testing.load_test_app({ 'app': { 'root': 'surveil.api.controllers.root.RootController', 'modules': ['surveil.api'], - 'debug': False + 'debug': False, + 'hooks': app_hooks } }) diff --git a/surveil/tests/base.py b/surveil/tests/base.py new file mode 100644 index 0000000..625e5d0 --- /dev/null +++ b/surveil/tests/base.py @@ -0,0 +1,33 @@ +# Copyright 2014 - Savoir-Faire Linux inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import sys +import unittest + + +class BaseTestCase(unittest.TestCase): + + def assert_count_equal_backport(self, item1, item2): + if sys.version_info[0] >= 3: + result = self.assertCountEqual( + item1, + item2 + ) + else: + result = self.assertItemsEqual( + sorted(item1), + sorted(item2) + ) + + return result diff --git a/test-requirements.txt b/test-requirements.txt index 1c681a9..10bc845 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -2,6 +2,7 @@ hacking>=0.9.2,<0.10 sphinxcontrib-pecanwsme>=0.8 sphinxcontrib-httpdomain -wsme oslosphinx testrepository>=0.0.18 +mongomock +wsme \ No newline at end of file diff --git a/tools/docker/etc/mongodb.conf b/tools/docker/etc/mongodb.conf new file mode 100644 index 0000000..34d6272 --- /dev/null +++ b/tools/docker/etc/mongodb.conf @@ -0,0 +1,16 @@ +# mongodb.conf + +# Where to store the data. +dbpath=/var/lib/mongodb + +#where to log +logpath=/var/log/mongodb/mongodb.log + +logappend=true + +bind_ip = 0.0.0.0 +#port = 27017 + +# Enable journaling, http://www.mongodb.org/display/DOCS/Journaling +journal=true +