diff --git a/surveil/api/controllers/v2/actions/__init__.py b/surveil/api/controllers/v2/actions/__init__.py index ba43fac..babc6e6 100644 --- a/surveil/api/controllers/v2/actions/__init__.py +++ b/surveil/api/controllers/v2/actions/__init__.py @@ -14,8 +14,11 @@ from pecan import rest +from surveil.api.controllers.v2.actions import acknowledge + class ActionsController(rest.RestController): + acknowledge = acknowledge.AcknowledgeController() # externalcommands = ExternalCommandsController() # engine = EngineController() pass diff --git a/surveil/api/controllers/v2/actions/acknowledge.py b/surveil/api/controllers/v2/actions/acknowledge.py new file mode 100644 index 0000000..8de16d4 --- /dev/null +++ b/surveil/api/controllers/v2/actions/acknowledge.py @@ -0,0 +1,40 @@ +# 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 requests +import wsmeext.pecan as wsme_pecan + +from surveil.api.datamodel.actions import acknowledgement +from surveil.api.datamodel import info + + +class AcknowledgeController(rest.RestController): + + @wsme_pecan.wsexpose(info.Info, + body=acknowledgement.Acknowledgement, + status_code=200) + def post(self, ack): + """Acknowledge a host/service.""" + + data = ack.as_dict() + data.update({'action': 'add'}) + + requests.post( + pecan.request.ws_arbiter_url + "/acknowledge", + data=data + ) + + return info.Info(message='Acknowledgement received. %s' % data) diff --git a/surveil/api/datamodel/actions/__init__.py b/surveil/api/datamodel/actions/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/surveil/api/datamodel/actions/acknowledgement.py b/surveil/api/datamodel/actions/acknowledgement.py new file mode 100644 index 0000000..b3702dd --- /dev/null +++ b/surveil/api/datamodel/actions/acknowledgement.py @@ -0,0 +1,50 @@ +# 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 Acknowledgement(types.Base): + host_name = wsme.wsattr(wtypes.text, mandatory=True) + """The name of the host""" + + service_description = wsme.wsattr(wtypes.text, mandatory=False) + + time_stamp = wsme.wsattr(wtypes.text, mandatory=False) + + sticky = wsme.wsattr(int, mandatory=False) + + notify = wsme.wsattr(int, mandatory=False) + + persistent = wsme.wsattr(int, mandatory=False) + + author = wsme.wsattr(wtypes.text, mandatory=False) + + comment = wsme.wsattr(wtypes.text, mandatory=False) + + @classmethod + def sample(cls): + return cls( + host_name="localhost", + service_description="ws-arbiter", + time_stamp="", + sticky=1, + notify=0, + persistent=1, + author="aviau", + comment="Working on it.", + ) diff --git a/surveil/tests/api/controllers/v2/actions/test_acknowledge.py b/surveil/tests/api/controllers/v2/actions/test_acknowledge.py new file mode 100644 index 0000000..3ed4bc5 --- /dev/null +++ b/surveil/tests/api/controllers/v2/actions/test_acknowledge.py @@ -0,0 +1,38 @@ +# 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 httpretty + +from surveil.tests.api import functionalTest + + +class TestAcknowledgeController(functionalTest.FunctionalTest): + + @httpretty.activate + def test_acknowledge(self): + httpretty.register_uri(httpretty.POST, + self.ws_arbiter_url + "/acknowledge") + + ack = { + "host_name": "localhost" + } + + response = self.app.post_json("/v2/actions/acknowledge/", params=ack) + + self.assertEqual(response.status_int, 200) + + self.assertEqual(httpretty.last_request().body, + 'action=add&host_name=localhost') + self.assertEqual(httpretty.last_request().path, + '/acknowledge')