From a7cdaaaa6e851c5037b6d383bb663aa8b4e3065d Mon Sep 17 00:00:00 2001 From: aviau Date: Tue, 28 Apr 2015 16:45:22 -0400 Subject: [PATCH] Config API: added hostgroups Change-Id: I0105dd4b6901fd039e4f991cf96abee82b8dc137 --- surveil/api/controllers/v2/config/__init__.py | 3 +- .../api/controllers/v2/config/hostgroups.py | 62 +++++++++++ surveil/api/datamodel/config/hostgroup.py | 36 +++++++ .../api/handlers/config/hostgroup_handler.py | 62 +++++++++++ .../controllers/v2/config/test_hostgroup.py | 100 ++++++++++++++++++ 5 files changed, 262 insertions(+), 1 deletion(-) create mode 100644 surveil/api/controllers/v2/config/hostgroups.py create mode 100644 surveil/api/datamodel/config/hostgroup.py create mode 100644 surveil/api/handlers/config/hostgroup_handler.py create mode 100644 surveil/tests/api/controllers/v2/config/test_hostgroup.py diff --git a/surveil/api/controllers/v2/config/__init__.py b/surveil/api/controllers/v2/config/__init__.py index 525ebb2..b1fe1bd 100644 --- a/surveil/api/controllers/v2/config/__init__.py +++ b/surveil/api/controllers/v2/config/__init__.py @@ -14,6 +14,7 @@ from surveil.api.controllers.v2.config import commands from surveil.api.controllers.v2.config import contacts +from surveil.api.controllers.v2.config import hostgroups 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 @@ -34,7 +35,7 @@ class ConfigController(rest.RestController): timeperiods = timeperiods.TimePeriodsController() realms = realms.RealmsController() servicegroups = servicegroup.ServiceGroupsController() - # hostgroups = HostGroupsController() + hostgroups = hostgroups.HostGroupsController() # contactgroups = ContactGroupsController() # notificationways = NotificationWayController() # engine = EngineController() diff --git a/surveil/api/controllers/v2/config/hostgroups.py b/surveil/api/controllers/v2/config/hostgroups.py new file mode 100644 index 0000000..9324533 --- /dev/null +++ b/surveil/api/controllers/v2/config/hostgroups.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 hostgroup +from surveil.api.handlers.config import hostgroup_handler + + +class HostGroupsController(rest.RestController): + + @wsme_pecan.wsexpose([hostgroup.HostGroup]) + def get_all(self): + """Returns all host groups.""" + handler = hostgroup_handler.HostGroupHandler(pecan.request) + host_groups = handler.get_all() + return host_groups + + @wsme_pecan.wsexpose(hostgroup.HostGroup, unicode) + def get_one(self, group_name): + """Returns a host group.""" + handler = hostgroup_handler.HostGroupHandler(pecan.request) + hostgroup = handler.get(group_name) + return hostgroup + + @wsme_pecan.wsexpose(body=hostgroup.HostGroup, status_code=201) + def post(self, data): + """Create a new host group. + + :param data: a host group within the request body. + """ + handler = hostgroup_handler.HostGroupHandler(pecan.request) + handler.create(data) + + @wsme_pecan.wsexpose(hostgroup.HostGroup, unicode, status_code=204) + def delete(self, group_name): + """Returns a specific host group.""" + handler = hostgroup_handler.HostGroupHandler(pecan.request) + handler.delete(group_name) + + @wsme_pecan.wsexpose(hostgroup.HostGroup, + unicode, + body=hostgroup.HostGroup, + status_code=204) + def put(self, group_name, hostgroup): + """Update a specific host group.""" + handler = hostgroup_handler.HostGroupHandler(pecan.request) + handler.update(group_name, hostgroup) diff --git a/surveil/api/datamodel/config/hostgroup.py b/surveil/api/datamodel/config/hostgroup.py new file mode 100644 index 0000000..aae146f --- /dev/null +++ b/surveil/api/datamodel/config/hostgroup.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 HostGroup(types.Base): + hostgroup_name = wsme.wsattr(wtypes.text, mandatory=True) + members = wsme.wsattr(wtypes.text, mandatory=False) + alias = wsme.wsattr(wtypes.text, mandatory=False) + hostgroup_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( + hostgroup_name='dbservices', + alias='Novell Servers', + members='netware1,netware2,netware3,netware4' + ) diff --git a/surveil/api/handlers/config/hostgroup_handler.py b/surveil/api/handlers/config/hostgroup_handler.py new file mode 100644 index 0000000..62e229c --- /dev/null +++ b/surveil/api/handlers/config/hostgroup_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 hostgroup +from surveil.api.handlers import handler + + +class HostGroupHandler(handler.Handler): + """Fulfills a request on the host group resource.""" + + def get(self, group_name): + """Return a host group.""" + + g = self.request.mongo_connection.shinken.hostgroups.find_one( + {"hostgroup_name": group_name}, {'_id': 0} + ) + return hostgroup.HostGroup(**g) + + def update(self, group_name, group): + """Modify an existing host group.""" + group_dict = group.as_dict() + if "hostgroup_name" not in group_dict.keys(): + group_dict['hostgroup_name'] = group_name + + self.request.mongo_connection.shinken.hostgroups.update( + {"hostgroup_name": group_name}, + group_dict + ) + + def delete(self, group_name): + """Delete existing host group.""" + self.request.mongo_connection.shinken.hostgroups.remove( + {"hostgroup_name": group_name} + ) + + def create(self, group): + """Create a new host group.""" + self.request.mongo_connection.shinken.hostgroups.insert( + group.as_dict() + ) + + def get_all(self): + """Return all host groups.""" + hostgroups = [g for g + in self.request.mongo_connection. + shinken.hostgroups.find( + {"register": {"$ne": "0"}}, + {'_id': 0} + )] + hostgroups = [hostgroup.HostGroup(**g) for g in hostgroups] + return hostgroups diff --git a/surveil/tests/api/controllers/v2/config/test_hostgroup.py b/surveil/tests/api/controllers/v2/config/test_hostgroup.py new file mode 100644 index 0000000..1d6f26c --- /dev/null +++ b/surveil/tests/api/controllers/v2/config/test_hostgroup.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 hostgroup +from surveil.tests.api import functionalTest + + +class TestHostGroupsController(functionalTest.FunctionalTest): + + def setUp(self): + super(TestHostGroupsController, self).setUp() + self.groups = [ + { + 'hostgroup_name': 'novell-servers', + 'members': 'netware1,netware2,netware3,netware4', + }, + { + 'hostgroup_name': 'otherservers', + 'members': 'googul,sfl', + }, + ] + self.mongoconnection.shinken.hostgroups.insert( + copy.deepcopy(self.groups) + ) + + def test_get_all_hostgroups(self): + response = self.app.get('/v2/config/hostgroups') + + self.assert_count_equal_backport( + json.loads(response.body.decode()), + self.groups + ) + self.assertEqual(response.status_int, 200) + + def test_get_one_hostgroups(self): + response = self.app.get('/v2/config/hostgroups/novell-servers') + + self.assertEqual( + json.loads(response.body.decode()), + self.groups[0] + ) + + def test_create_hostgroup(self): + s = hostgroup.HostGroup( + hostgroup_name='John', + members="marie,bob,joe", + ) + + self.app.post_json('/v2/config/hostgroups', s.as_dict()) + + self.assertIsNotNone( + self.mongoconnection.shinken.hostgroups.find_one(s.as_dict()) + ) + + def test_delete_hostgroup(self): + self.assertIsNotNone( + self.mongoconnection.shinken.hostgroups.find_one(self.groups[0]) + ) + + self.app.delete('/v2/config/hostgroups/novell-servers') + + self.assertIsNone( + self.mongoconnection.shinken.hostgroups.find_one(self.groups[0]) + ) + + def test_put_hostgroup(self): + self.assertEqual( + self.mongoconnection.shinken.hostgroups.find_one( + {'hostgroup_name': 'novell-servers'} + )['members'], + 'netware1,netware2,netware3,netware4' + ) + + self.app.put_json( + '/v2/config/hostgroups/novell-servers', + {"hostgroup_name": "novell-servers", + "members": "updated"} + ) + + self.assertEqual( + self.mongoconnection.shinken.hostgroups.find_one( + {'hostgroup_name': 'novell-servers'} + )['members'], + 'updated' + )