diff --git a/surveil/api/controllers/v2/config/__init__.py b/surveil/api/controllers/v2/config/__init__.py index 3c415cf..c0afb9d 100644 --- a/surveil/api/controllers/v2/config/__init__.py +++ b/surveil/api/controllers/v2/config/__init__.py @@ -15,6 +15,7 @@ from surveil.api.controllers.v2.config import commands from surveil.api.controllers.v2.config import contacts from surveil.api.controllers.v2.config import hosts +from surveil.api.controllers.v2.config import realms from surveil.api.controllers.v2.config import reload_config from surveil.api.controllers.v2.config import services from surveil.api.controllers.v2.config import timeperiods @@ -30,9 +31,9 @@ class ConfigController(rest.RestController): reload_config = reload_config.ReloadConfigController() contacts = contacts.ContactsController() timeperiods = timeperiods.TimePeriodsController() + realms = realms.RealmsController() # hostgroups = HostGroupsController() # contactgroups = ContactGroupsController() # servicegroups = ServiceGroupsController() - # realms = RealmsController() # notificationways = NotificationWayController() # engine = EngineController() diff --git a/surveil/api/controllers/v2/config/realms.py b/surveil/api/controllers/v2/config/realms.py new file mode 100644 index 0000000..ecd71f5 --- /dev/null +++ b/surveil/api/controllers/v2/config/realms.py @@ -0,0 +1,62 @@ +# Copyright 2015 - 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 pecan +from pecan import rest +import wsmeext.pecan as wsme_pecan + +from surveil.api.datamodel.config import realm +from surveil.api.handlers.config import realm_handler + + +class RealmsController(rest.RestController): + + @wsme_pecan.wsexpose([realm.Realm]) + def get_all(self): + """Returns all realms.""" + handler = realm_handler.RealmHandler(pecan.request) + realms = handler.get_all() + return realms + + @wsme_pecan.wsexpose(realm.Realm, unicode) + def get_one(self, realm_name): + """Returns a specific realm.""" + handler = realm_handler.RealmHandler(pecan.request) + realm = handler.get(realm_name) + return realm + + @wsme_pecan.wsexpose(body=realm.Realm, status_code=201) + def post(self, data): + """Create a new realm. + + :param data: a realm within the request body. + """ + handler = realm_handler.RealmHandler(pecan.request) + handler.create(data) + + @wsme_pecan.wsexpose(realm.Realm, unicode, status_code=204) + def delete(self, realm_name): + """Deletes a specific realm.""" + handler = realm_handler.RealmHandler(pecan.request) + handler.delete(realm_name) + + @wsme_pecan.wsexpose(realm.Realm, + unicode, + body=realm.Realm, + status_code=204) + def put(self, realm_name, realm): + """Updates a specific realm.""" + handler = realm_handler.RealmHandler(pecan.request) + handler.update(realm_name, realm) diff --git a/surveil/api/datamodel/config/realm.py b/surveil/api/datamodel/config/realm.py new file mode 100644 index 0000000..b29ed44 --- /dev/null +++ b/surveil/api/datamodel/config/realm.py @@ -0,0 +1,32 @@ +# Copyright 2015 - 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 wsme +import wsme.types as wtypes + +from surveil.api.datamodel import types + + +class Realm(types.Base): + realm_name = wsme.wsattr(wtypes.text, mandatory=True) + realm_members = wsme.wsattr(wtypes.text, mandatory=True) + default = wsme.wsattr(int, mandatory=True) + + @classmethod + def sample(cls): + return cls( + realm_name='World', + realm_members='Europe,America,Asia', + default=0 + ) diff --git a/surveil/api/handlers/config/realm_handler.py b/surveil/api/handlers/config/realm_handler.py new file mode 100644 index 0000000..7481bd1 --- /dev/null +++ b/surveil/api/handlers/config/realm_handler.py @@ -0,0 +1,62 @@ +# 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 surveil.api.datamodel.config import realm +from surveil.api.handlers import handler + + +class RealmHandler(handler.Handler): + """Fulfills a request on the realm resource.""" + + def get(self, realm_name): + """Return a realm.""" + + r = self.request.mongo_connection.shinken.realms.find_one( + {"realm_name": realm_name}, {'_id': 0} + ) + return realm.Realm(**r) + + def update(self, realm_name, realm): + """Modify an existing realm.""" + realm_dict = realm.as_dict() + if "realm_name" not in realm_dict.keys(): + realm_dict['realm_name'] = realm_name + + self.request.mongo_connection.shinken.realms.update( + {"realm_name": realm_name}, + realm_dict + ) + + def delete(self, realm_name): + """Delete existing realm.""" + self.request.mongo_connection.shinken.realms.remove( + {"realm_name": realm_name} + ) + + def create(self, realm): + """Create a new realm.""" + self.request.mongo_connection.shinken.realms.insert( + realm.as_dict() + ) + + def get_all(self): + """Return all realms.""" + realms = [c for c + in self.request.mongo_connection. + shinken.realms.find( + {"register": {"$ne": "0"}}, # Don't return templates + {'_id': 0} + )] + realms = [realm.Realm(**r) for r in realms] + return realms diff --git a/surveil/tests/api/controllers/v2/config/test_realms.py b/surveil/tests/api/controllers/v2/config/test_realms.py new file mode 100644 index 0000000..9bb49b1 --- /dev/null +++ b/surveil/tests/api/controllers/v2/config/test_realms.py @@ -0,0 +1,104 @@ +# 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.api.datamodel.config import realm +from surveil.tests.api import functionalTest + + +class TestRealmsController(functionalTest.FunctionalTest): + + def setUp(self): + super(TestRealmsController, self).setUp() + self.realms = [ + { + 'realm_name': 'World', + 'realm_members': 'Europe,America,Asia', + 'default': 0 + }, + { + 'realm_name': 'Anti-world', + 'realm_members': 'void,black-hole', + 'default': 1 + }, + ] + self.mongoconnection.shinken.realms.insert( + copy.deepcopy(self.realms) + ) + + def test_get_all_realms(self): + response = self.app.get('/v2/config/realms') + + self.assert_count_equal_backport( + json.loads(response.body.decode()), + self.realms + ) + self.assertEqual(response.status_int, 200) + + def test_get_one_realm(self): + response = self.app.get('/v2/config/realms/World') + + self.assertEqual( + json.loads(response.body.decode()), + self.realms[0] + ) + + def test_create_realm(self): + r = realm.Realm( + realm_name='John', + realm_members="marie,bob,joe", + default=1 + ) + + self.app.post_json('/v2/config/realms', r.as_dict()) + + self.assertIsNotNone( + self.mongoconnection.shinken.realms.find_one(r.as_dict()) + ) + + def test_delete_realm(self): + self.assertIsNotNone( + self.mongoconnection.shinken.realms.find_one(self.realms[0]) + ) + + self.app.delete('/v2/config/realms/World') + + self.assertIsNone( + self.mongoconnection.shinken.realms.find_one(self.realms[0]) + ) + + def test_put_realm(self): + self.assertEqual( + self.mongoconnection.shinken.realms.find_one( + {'realm_name': 'World'} + )['realm_members'], + 'Europe,America,Asia' + ) + + self.app.put_json( + '/v2/config/realms/World', + {"realm_name": "World", + "realm_members": "updated", + "default": 0} + ) + + self.assertEqual( + self.mongoconnection.shinken.realms.find_one( + {'realm_name': 'World'} + )['realm_members'], + 'updated' + )