diff --git a/surveil/api/controllers/v2/config/__init__.py b/surveil/api/controllers/v2/config/__init__.py index b1fe1bd..9525e29 100644 --- a/surveil/api/controllers/v2/config/__init__.py +++ b/surveil/api/controllers/v2/config/__init__.py @@ -13,6 +13,7 @@ # under the License. from surveil.api.controllers.v2.config import commands +from surveil.api.controllers.v2.config import contactgroups from surveil.api.controllers.v2.config import contacts from surveil.api.controllers.v2.config import hostgroups from surveil.api.controllers.v2.config import hosts @@ -36,6 +37,6 @@ class ConfigController(rest.RestController): realms = realms.RealmsController() servicegroups = servicegroup.ServiceGroupsController() hostgroups = hostgroups.HostGroupsController() - # contactgroups = ContactGroupsController() + contactgroups = contactgroups.ContactGroupsController() # notificationways = NotificationWayController() # engine = EngineController() diff --git a/surveil/api/controllers/v2/config/contactgroups.py b/surveil/api/controllers/v2/config/contactgroups.py new file mode 100644 index 0000000..196a45e --- /dev/null +++ b/surveil/api/controllers/v2/config/contactgroups.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 contactgroup +from surveil.api.handlers.config import contactgroup_handler + + +class ContactGroupsController(rest.RestController): + + @wsme_pecan.wsexpose([contactgroup.ContactGroup]) + def get_all(self): + """Returns all contact groups.""" + handler = contactgroup_handler.ContactGroupHandler(pecan.request) + contact_groups = handler.get_all() + return contact_groups + + @wsme_pecan.wsexpose(contactgroup.ContactGroup, unicode) + def get_one(self, group_name): + """Returns a contact group.""" + handler = contactgroup_handler.ContactGroupHandler(pecan.request) + contactgroup = handler.get(group_name) + return contactgroup + + @wsme_pecan.wsexpose(body=contactgroup.ContactGroup, status_code=201) + def post(self, data): + """Create a new contact group. + + :param data: a contact group within the request body. + """ + handler = contactgroup_handler.ContactGroupHandler(pecan.request) + handler.create(data) + + @wsme_pecan.wsexpose(contactgroup.ContactGroup, unicode, status_code=204) + def delete(self, group_name): + """Delete a specific contact group.""" + handler = contactgroup_handler.ContactGroupHandler(pecan.request) + handler.delete(group_name) + + @wsme_pecan.wsexpose(contactgroup.ContactGroup, + unicode, + body=contactgroup.ContactGroup, + status_code=204) + def put(self, group_name, contactgroup): + """Update a specific contact group.""" + handler = contactgroup_handler.ContactGroupHandler(pecan.request) + handler.update(group_name, contactgroup) diff --git a/surveil/api/datamodel/config/contactgroup.py b/surveil/api/datamodel/config/contactgroup.py new file mode 100644 index 0000000..abc5b2e --- /dev/null +++ b/surveil/api/datamodel/config/contactgroup.py @@ -0,0 +1,33 @@ +# 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 ContactGroup(types.Base): + contactgroup_name = wsme.wsattr(wtypes.text, mandatory=True) + members = wsme.wsattr(wtypes.text, mandatory=False) + alias = wsme.wsattr(wtypes.text, mandatory=False) + contactgroup_members = wsme.wsattr(wtypes.text, mandatory=False) + + @classmethod + def sample(cls): + return cls( + contactgroup_name='novell-admins', + alias='Novell Administrators', + members='jdoe,rtobert,tzach' + ) diff --git a/surveil/api/handlers/config/contactgroup_handler.py b/surveil/api/handlers/config/contactgroup_handler.py new file mode 100644 index 0000000..e3353cf --- /dev/null +++ b/surveil/api/handlers/config/contactgroup_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 contactgroup +from surveil.api.handlers import handler + + +class ContactGroupHandler(handler.Handler): + """Fulfills a request on the contact group resource.""" + + def get(self, group_name): + """Return a contact group.""" + + g = self.request.mongo_connection.shinken.contactgroups.find_one( + {"contactgroup_name": group_name}, {'_id': 0} + ) + return contactgroup.ContactGroup(**g) + + def update(self, group_name, group): + """Modify an existing contact group.""" + group_dict = group.as_dict() + if "contactgroup_name" not in group_dict.keys(): + group_dict['contactgroup_name'] = group_name + + self.request.mongo_connection.shinken.contactgroups.update( + {"contactgroup_name": group_name}, + group_dict + ) + + def delete(self, group_name): + """Delete existing contact group.""" + self.request.mongo_connection.shinken.contactgroups.remove( + {"contactgroup_name": group_name} + ) + + def create(self, group): + """Create a new contact group.""" + self.request.mongo_connection.shinken.contactgroups.insert( + group.as_dict() + ) + + def get_all(self): + """Return all contact groups.""" + contactgroups = [g for g + in self.request.mongo_connection. + shinken.contactgroups.find( + {"register": {"$ne": "0"}}, + {'_id': 0} + )] + contactgroups = [contactgroup.ContactGroup(**g) for g in contactgroups] + return contactgroups diff --git a/surveil/tests/api/controllers/v2/config/test_contactgroup.py b/surveil/tests/api/controllers/v2/config/test_contactgroup.py new file mode 100644 index 0000000..138ca13 --- /dev/null +++ b/surveil/tests/api/controllers/v2/config/test_contactgroup.py @@ -0,0 +1,100 @@ +# 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 copy +import json + + +from surveil.api.datamodel.config import contactgroup +from surveil.tests.api import functionalTest + + +class TestContactGroupsController(functionalTest.FunctionalTest): + + def setUp(self): + super(TestContactGroupsController, self).setUp() + self.groups = [ + { + 'contactgroup_name': 'novell-admins', + 'members': 'jdoe,rtobert,tzach', + }, + { + 'contactgroup_name': 'linux-adminx', + 'members': 'linus,richard', + }, + ] + self.mongoconnection.shinken.contactgroups.insert( + copy.deepcopy(self.groups) + ) + + def test_get_all_contactgroups(self): + response = self.app.get('/v2/config/contactgroups') + + self.assert_count_equal_backport( + json.loads(response.body.decode()), + self.groups + ) + self.assertEqual(response.status_int, 200) + + def test_get_one_contactgroup(self): + response = self.app.get('/v2/config/contactgroups/novell-admins') + + self.assertEqual( + json.loads(response.body.decode()), + self.groups[0] + ) + + def test_create_contactgroup(self): + g = contactgroup.ContactGroup( + contactgroup_name='John', + members="marie,bob,joe", + ) + + self.app.post_json('/v2/config/contactgroups', g.as_dict()) + + self.assertIsNotNone( + self.mongoconnection.shinken.contactgroups.find_one(g.as_dict()) + ) + + def test_delete_contactgroup(self): + self.assertIsNotNone( + self.mongoconnection.shinken.contactgroups.find_one(self.groups[0]) + ) + + self.app.delete('/v2/config/contactgroups/novell-admins') + + self.assertIsNone( + self.mongoconnection.shinken.contactgroups.find_one(self.groups[0]) + ) + + def test_put_contactgroup(self): + self.assertEqual( + self.mongoconnection.shinken.contactgroups.find_one( + {'contactgroup_name': 'novell-admins'} + )['members'], + 'jdoe,rtobert,tzach' + ) + + self.app.put_json( + '/v2/config/contactgroups/novell-admins', + {"contactgroup_name": "novell-admins", + "members": "updated"} + ) + + self.assertEqual( + self.mongoconnection.shinken.contactgroups.find_one( + {'contactgroup_name': 'novell-admins'} + )['members'], + 'updated' + )