Added get functions to Hosts controller

Change-Id: I4968435b73b5f05a0bf8a2947306201e48c833cb
This commit is contained in:
aviau 2014-08-18 15:24:58 -04:00
parent 12906fd952
commit 566f89cece
11 changed files with 184 additions and 13 deletions

View File

@ -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"]

View File

@ -1 +1,2 @@
pecan>=0.5.0
pymongo>=2.7.2

View File

@ -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

View File

@ -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 = {

View File

@ -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

24
surveil/api/hooks.py Normal file
View File

@ -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

View File

@ -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]
)

View File

@ -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
}
})

33
surveil/tests/base.py Normal file
View File

@ -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

View File

@ -2,6 +2,7 @@
hacking>=0.9.2,<0.10
sphinxcontrib-pecanwsme>=0.8
sphinxcontrib-httpdomain
wsme
oslosphinx
testrepository>=0.0.18
mongomock
wsme

View File

@ -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