From 45ea20ea19622df0089bd8f236377230a6dd54cc Mon Sep 17 00:00:00 2001 From: aviau Date: Tue, 28 Apr 2015 16:20:38 -0400 Subject: [PATCH] Config API: added servicegroups Change-Id: I5c0864b9d5318719f7392c29951948ddfba5f1fe --- surveil/api/controllers/v2/config/__init__.py | 3 +- .../api/controllers/v2/config/servicegroup.py | 62 +++++++++++ surveil/api/datamodel/config/servicegroup.py | 36 +++++++ .../handlers/config/servicegroup_handler.py | 62 +++++++++++ .../v2/config/test_servicegroup.py | 100 ++++++++++++++++++ 5 files changed, 262 insertions(+), 1 deletion(-) create mode 100644 surveil/api/controllers/v2/config/servicegroup.py create mode 100644 surveil/api/datamodel/config/servicegroup.py create mode 100644 surveil/api/handlers/config/servicegroup_handler.py create mode 100644 surveil/tests/api/controllers/v2/config/test_servicegroup.py diff --git a/surveil/api/controllers/v2/config/__init__.py b/surveil/api/controllers/v2/config/__init__.py index c0afb9d..525ebb2 100644 --- a/surveil/api/controllers/v2/config/__init__.py +++ b/surveil/api/controllers/v2/config/__init__.py @@ -17,6 +17,7 @@ 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 servicegroup from surveil.api.controllers.v2.config import services from surveil.api.controllers.v2.config import timeperiods @@ -32,8 +33,8 @@ class ConfigController(rest.RestController): contacts = contacts.ContactsController() timeperiods = timeperiods.TimePeriodsController() realms = realms.RealmsController() + servicegroups = servicegroup.ServiceGroupsController() # hostgroups = HostGroupsController() # contactgroups = ContactGroupsController() - # servicegroups = ServiceGroupsController() # notificationways = NotificationWayController() # engine = EngineController() diff --git a/surveil/api/controllers/v2/config/servicegroup.py b/surveil/api/controllers/v2/config/servicegroup.py new file mode 100644 index 0000000..3cfab59 --- /dev/null +++ b/surveil/api/controllers/v2/config/servicegroup.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 servicegroup +from surveil.api.handlers.config import servicegroup_handler + + +class ServiceGroupsController(rest.RestController): + + @wsme_pecan.wsexpose([servicegroup.ServiceGroup]) + def get_all(self): + """Returns all service groups.""" + handler = servicegroup_handler.ServiceGroupHandler(pecan.request) + service_groups = handler.get_all() + return service_groups + + @wsme_pecan.wsexpose(servicegroup.ServiceGroup, unicode) + def get_one(self, group_name): + """Returns a service group.""" + handler = servicegroup_handler.ServiceGroupHandler(pecan.request) + servicegroup = handler.get(group_name) + return servicegroup + + @wsme_pecan.wsexpose(body=servicegroup.ServiceGroup, status_code=201) + def post(self, data): + """Create a new service group. + + :param data: a service group within the request body. + """ + handler = servicegroup_handler.ServiceGroupHandler(pecan.request) + handler.create(data) + + @wsme_pecan.wsexpose(servicegroup.ServiceGroup, unicode, status_code=204) + def delete(self, group_name): + """Returns a specific service group.""" + handler = servicegroup_handler.ServiceGroupHandler(pecan.request) + handler.delete(group_name) + + @wsme_pecan.wsexpose(servicegroup.ServiceGroup, + unicode, + body=servicegroup.ServiceGroup, + status_code=204) + def put(self, group_name, servicegroup): + """Update a specific service group.""" + handler = servicegroup_handler.ServiceGroupHandler(pecan.request) + handler.update(group_name, servicegroup) diff --git a/surveil/api/datamodel/config/servicegroup.py b/surveil/api/datamodel/config/servicegroup.py new file mode 100644 index 0000000..45ed2da --- /dev/null +++ b/surveil/api/datamodel/config/servicegroup.py @@ -0,0 +1,36 @@ +# 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 ServiceGroup(types.Base): + servicegroup_name = wsme.wsattr(wtypes.text, mandatory=True) + members = wsme.wsattr(wtypes.text, mandatory=True) + alias = wsme.wsattr(wtypes.text, mandatory=False) + servicegroup_members = wsme.wsattr(wtypes.text, mandatory=False) + notes = wsme.wsattr(wtypes.text, mandatory=False) + notes_url = wsme.wsattr(wtypes.text, mandatory=False) + action_url = wsme.wsattr(wtypes.text, mandatory=False) + + @classmethod + def sample(cls): + return cls( + servicegroup_name='dbservices', + alias='Database Services', + members='ms1,SQL Server,ms1,SQL Serverc Agent,ms1,SQL DTC' + ) diff --git a/surveil/api/handlers/config/servicegroup_handler.py b/surveil/api/handlers/config/servicegroup_handler.py new file mode 100644 index 0000000..d102c4e --- /dev/null +++ b/surveil/api/handlers/config/servicegroup_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 servicegroup +from surveil.api.handlers import handler + + +class ServiceGroupHandler(handler.Handler): + """Fulfills a request on the service group resource.""" + + def get(self, group_name): + """Return a service group.""" + + s = self.request.mongo_connection.shinken.servicegroups.find_one( + {"servicegroup_name": group_name}, {'_id': 0} + ) + return servicegroup.ServiceGroup(**s) + + def update(self, group_name, group): + """Modify an existing service group.""" + group_dict = group.as_dict() + if "servicegroup_name" not in group_dict.keys(): + group_dict['servicegroup_name'] = group_name + + self.request.mongo_connection.shinken.servicegroups.update( + {"servicegroup_name": group_name}, + group_dict + ) + + def delete(self, group_name): + """Delete existing service group.""" + self.request.mongo_connection.shinken.servicegroups.remove( + {"servicegroup_name": group_name} + ) + + def create(self, group): + """Create a new service group.""" + self.request.mongo_connection.shinken.servicegroups.insert( + group.as_dict() + ) + + def get_all(self): + """Return all service groups.""" + servicegroups = [c for c + in self.request.mongo_connection. + shinken.servicegroups.find( + {"register": {"$ne": "0"}}, + {'_id': 0} + )] + servicegroups = [servicegroup.ServiceGroup(**s) for s in servicegroups] + return servicegroups diff --git a/surveil/tests/api/controllers/v2/config/test_servicegroup.py b/surveil/tests/api/controllers/v2/config/test_servicegroup.py new file mode 100644 index 0000000..7b269be --- /dev/null +++ b/surveil/tests/api/controllers/v2/config/test_servicegroup.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 servicegroup +from surveil.tests.api import functionalTest + + +class TestServiceGroupsController(functionalTest.FunctionalTest): + + def setUp(self): + super(TestServiceGroupsController, self).setUp() + self.groups = [ + { + 'servicegroup_name': 'dbservices', + 'members': 'ms1,SQL Server,ms1,SQL Serverc Agent,ms1,SQL DTC', + }, + { + 'servicegroup_name': 'otherservices', + 'members': 'some,other,member', + }, + ] + self.mongoconnection.shinken.servicegroups.insert( + copy.deepcopy(self.groups) + ) + + def test_get_all_servicegroups(self): + response = self.app.get('/v2/config/servicegroups') + + self.assert_count_equal_backport( + json.loads(response.body.decode()), + self.groups + ) + self.assertEqual(response.status_int, 200) + + def test_get_one_servicegroup(self): + response = self.app.get('/v2/config/servicegroups/dbservices') + + self.assertEqual( + json.loads(response.body.decode()), + self.groups[0] + ) + + def test_create_servicegroup(self): + s = servicegroup.ServiceGroup( + servicegroup_name='John', + members="marie,bob,joe", + ) + + self.app.post_json('/v2/config/servicegroups', s.as_dict()) + + self.assertIsNotNone( + self.mongoconnection.shinken.servicegroups.find_one(s.as_dict()) + ) + + def test_delete_servicegroup(self): + self.assertIsNotNone( + self.mongoconnection.shinken.servicegroups.find_one(self.groups[0]) + ) + + self.app.delete('/v2/config/servicegroups/dbservices') + + self.assertIsNone( + self.mongoconnection.shinken.servicegroups.find_one(self.groups[0]) + ) + + def test_put_servicegroup(self): + self.assertEqual( + self.mongoconnection.shinken.servicegroups.find_one( + {'servicegroup_name': 'dbservices'} + )['members'], + 'ms1,SQL Server,ms1,SQL Serverc Agent,ms1,SQL DTC' + ) + + self.app.put_json( + '/v2/config/servicegroups/dbservices', + {"servicegroup_name": "dbservices", + "members": "updated"} + ) + + self.assertEqual( + self.mongoconnection.shinken.servicegroups.find_one( + {'servicegroup_name': 'dbservices'} + )['members'], + 'updated' + )