diff --git a/doc/source/webapi/v2/config.rst b/doc/source/webapi/v2/config.rst index e0fe4cd..81f0898 100644 --- a/doc/source/webapi/v2/config.rst +++ b/doc/source/webapi/v2/config.rst @@ -54,6 +54,12 @@ Business impact modulations .. rest-controller:: surveil.api.controllers.v2.config.businessimpactmodulations:BusinessImpactModulationsController :webprefix: /v2/config/businessimpactmodulations +Check modulations +=========================== + +.. rest-controller:: surveil.api.controllers.v2.config.checkmodulations:CheckModulationsController + :webprefix: /v2/config/checkmodulations + Notification ways ================= @@ -75,5 +81,8 @@ types documentation .. autotype:: surveil.api.datamodel.config.businessimpactmodulation.BuisnessImpactModulation :members: +.. autotype:: surveil.api.datamodel.config.checkmodulation.CheckModulation + :members: + .. autotype:: surveil.api.datamodel.config.notificationway.NotificationWay :members: diff --git a/surveil/api/controllers/v2/config/__init__.py b/surveil/api/controllers/v2/config/__init__.py index 6e4dd33..2cacd55 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 businessimpactmodulations +from surveil.api.controllers.v2.config import checkmodulations from surveil.api.controllers.v2.config import commands from surveil.api.controllers.v2.config import contactgroups from surveil.api.controllers.v2.config import contacts @@ -43,4 +44,5 @@ class ConfigController(rest.RestController): businessimpactmodulations = (businessimpactmodulations. BusinessImpactModulationsController()) notificationways = notificationways.NotificationWaysController() + checkmodulations = checkmodulations.CheckModulationsController() # engine = EngineController() diff --git a/surveil/api/controllers/v2/config/checkmodulations.py b/surveil/api/controllers/v2/config/checkmodulations.py new file mode 100644 index 0000000..c5c83b5 --- /dev/null +++ b/surveil/api/controllers/v2/config/checkmodulations.py @@ -0,0 +1,69 @@ +# 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 checkmodulation +from surveil.api.handlers.config import checkmodulation_handler +from surveil.common import util + + +class CheckModulationsController(rest.RestController): + + @util.policy_enforce(['authenticated']) + @wsme_pecan.wsexpose([checkmodulation.CheckModulation]) + def get_all(self): + """Returns all check modulations.""" + handler = checkmodulation_handler.CheckModulationHandler(pecan.request) + checkmodulations = handler.get_all() + return checkmodulations + + @util.policy_enforce(['authenticated']) + @wsme_pecan.wsexpose(checkmodulation.CheckModulation, unicode) + def get_one(self, checkmodulation_name): + """Returns a specific check modulation.""" + handler = checkmodulation_handler.CheckModulationHandler(pecan.request) + checkmodulation = handler.get(checkmodulation_name) + return checkmodulation + + @util.policy_enforce(['authenticated']) + @wsme_pecan.wsexpose(body=checkmodulation.CheckModulation, status_code=201) + def post(self, data): + """Create a new check modulation. + + :param data: a check modulation within the request body. + """ + handler = checkmodulation_handler.CheckModulationHandler(pecan.request) + handler.create(data) + + @util.policy_enforce(['authenticated']) + @wsme_pecan.wsexpose(checkmodulation.CheckModulation, + unicode, status_code=204) + def delete(self, checkmodulation_name): + """Returns a specific check modulation.""" + handler = checkmodulation_handler.CheckModulationHandler(pecan.request) + handler.delete(checkmodulation_name) + + @util.policy_enforce(['authenticated']) + @wsme_pecan.wsexpose(checkmodulation.CheckModulation, + unicode, + body=checkmodulation.CheckModulation, + status_code=204) + def put(self, checkmodulation_name, checkmodulation): + """Update a specific check modulation.""" + handler = checkmodulation_handler.CheckModulationHandler(pecan.request) + handler.update(checkmodulation_name, checkmodulation) \ No newline at end of file diff --git a/surveil/api/datamodel/config/checkmodulation.py b/surveil/api/datamodel/config/checkmodulation.py new file mode 100644 index 0000000..afc751f --- /dev/null +++ b/surveil/api/datamodel/config/checkmodulation.py @@ -0,0 +1,34 @@ +# 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 wsme +import wsme.types as wtypes + +from surveil.api.datamodel import types + + +class CheckModulation(types.Base): + checkmodulation_name = wsme.wsattr(wtypes.text, mandatory=True) + + check_command = wsme.wsattr(wtypes.text, mandatory=True) + + check_period = wsme.wsattr(wtypes.text, mandatory=True) + + @classmethod + def sample(cls): + return cls( + checkmodulation_name='ping_night', + check_command='check_ping_night', + check_period='night' + ) diff --git a/surveil/api/handlers/config/checkmodulation_handler.py b/surveil/api/handlers/config/checkmodulation_handler.py new file mode 100644 index 0000000..b3f4881 --- /dev/null +++ b/surveil/api/handlers/config/checkmodulation_handler.py @@ -0,0 +1,57 @@ +# 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 checkmodulation +from surveil.api.handlers import handler + + +class CheckModulationHandler(handler.Handler): + """Fulfills a request on the check modulation resource.""" + + def get(self, checkmodulation_name): + """Return a check modulation.""" + c = self.request.mongo_connection.shinken.checkmodulations.find_one( + {"checkmodulation_name": checkmodulation_name} + ) + return checkmodulation.CheckModulation(**c) + + def update(self, checkmodulation_name, checkmodulation): + """Modify existing check modulation.""" + checkmodulation_dict = checkmodulation.as_dict() + if "checkmodulation_name" not in checkmodulation_dict.keys(): + checkmodulation_dict['checkmodulation_name'] = checkmodulation_name + + self.request.mongo_connection.shinken.checkmodulations.update( + {"checkmodulation_name": checkmodulation_name}, + checkmodulation_dict + ) + + def delete(self, checkmodulation_name): + """Delete an existing check modulation.""" + self.request.mongo_connection.shinken.checkmodulations.remove( + {"checkmodulation_name": checkmodulation_name} + ) + + def create(self, data): + """Create a new check modulation.""" + self.request.mongo_connection.shinken.checkmodulations.insert( + data.as_dict() + ) + + def get_all(self): + """Return all check modulation.""" + find = self.request.mongo_connection.shinken.checkmodulations.find() + checkmodulations = [c for c in find] + + return [checkmodulation.CheckModulation(**c) for c in checkmodulations] diff --git a/surveil/tests/api/controllers/v2/config/test_checkmodulation.py b/surveil/tests/api/controllers/v2/config/test_checkmodulation.py new file mode 100644 index 0000000..eecdfda --- /dev/null +++ b/surveil/tests/api/controllers/v2/config/test_checkmodulation.py @@ -0,0 +1,117 @@ +# 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 TestCheckModulationsController(functionalTest.FunctionalTest): + + def setUp(self): + super(TestCheckModulationsController, self).setUp() + self.checkmodulations = [ + { + 'checkmodulation_name': 'ping_night', + 'check_command': 'check_ping_night', + 'check_period': 'night' + }, + { + 'checkmodulation_name': 'ping_day', + 'check_command': 'check_ping_day', + 'check_period': 'day', + }, + ] + self.mongoconnection.shinken.checkmodulations.insert( + copy.deepcopy(self.checkmodulations) + ) + + def test_get_all_checkmodulations(self): + response = self.get('/v2/config/checkmodulations') + + self.assertItemsEqual( + json.loads(response.body.decode()), + [ + {'checkmodulation_name': 'ping_day', + 'check_command': 'check_ping_day', + 'check_period': 'day'}, + {'checkmodulation_name': 'ping_night', + 'check_command': 'check_ping_night', + 'check_period': 'night'} + ] + ) + self.assertEqual(response.status_int, 200) + + def test_get_one_checkmodulation(self): + response = self.get('/v2/config/checkmodulations/ping_night') + + self.assertEqual( + json.loads(response.body.decode()), + {'checkmodulation_name': 'ping_night', + 'check_command': 'check_ping_night', + 'check_period': 'night'} + ) + + def test_create_checkmodulation(self): + t = {"checkmodulation_name": "ping_evening", + "check_command": "check_ping_evening", + "check_period": "evening" + } + + self.post_json('/v2/config/checkmodulations', t) + self.assertIsNotNone( + self.mongoconnection.shinken.checkmodulations.find_one( + {"checkmodulation_name": 'ping_evening', + "check_command": "check_ping_evening", + "check_period": "evening"}) + ) + + def test_delete_checkmodulation(self): + self.assertIsNotNone( + self.mongoconnection.shinken.checkmodulations.find_one( + {"checkmodulation_name": 'ping_night'} + ) + ) + + self.delete('/v2/config/checkmodulations/ping_night') + + self.assertIsNone( + self.mongoconnection.shinken.checkmodulations.find_one( + {"checkmodulation_name": 'ping_night'} + ) + ) + + def test_put_checkmodulation(self): + self.assertEqual( + self.mongoconnection.shinken.checkmodulations.find_one( + {'checkmodulation_name': 'ping_night'} + )['check_command'], + 'check_ping_night' + ) + + self.put_json( + '/v2/config/checkmodulations/ping_night', + {"checkmodulation_name": "ping_night", + "check_command": "updated", + "check_period": 'night'} + ) + + self.assertEqual( + self.mongoconnection.shinken.checkmodulations.find_one( + {'checkmodulation_name': 'ping_night'} + )['check_command'], + 'updated' + )