diff --git a/surveil/api/controllers/v2/actions/__init__.py b/surveil/api/controllers/v2/actions/__init__.py index b764bce..82dc9fd 100644 --- a/surveil/api/controllers/v2/actions/__init__.py +++ b/surveil/api/controllers/v2/actions/__init__.py @@ -16,11 +16,13 @@ from pecan import rest from surveil.api.controllers.v2.actions import acknowledge from surveil.api.controllers.v2.actions import downtime +from surveil.api.controllers.v2.actions import recheck class ActionsController(rest.RestController): acknowledge = acknowledge.AcknowledgeController() downtime = downtime.DowntimeController() + recheck = recheck.RecheckController() # externalcommands = ExternalCommandsController() # engine = EngineController() pass diff --git a/surveil/api/controllers/v2/actions/recheck.py b/surveil/api/controllers/v2/actions/recheck.py new file mode 100644 index 0000000..844f589 --- /dev/null +++ b/surveil/api/controllers/v2/actions/recheck.py @@ -0,0 +1,41 @@ +# 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 recheck +from surveil.api.datamodel import info +from surveil.common import util + + +class RecheckController(rest.RestController): + + @util.policy_enforce(['authenticated']) + @wsme_pecan.wsexpose(info.Info, + body=recheck.Recheck, + status_code=200) + def post(self, rc): + """Schedule a forced check.""" + + data = rc.as_dict() + + requests.post( + pecan.request.ws_arbiter_url + "/recheck", + data=data + ) + + return info.Info(message='Recheck received.') diff --git a/surveil/api/datamodel/actions/recheck.py b/surveil/api/datamodel/actions/recheck.py new file mode 100644 index 0000000..919dea8 --- /dev/null +++ b/surveil/api/datamodel/actions/recheck.py @@ -0,0 +1,37 @@ +# 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 Recheck(types.Base): + host_name = wsme.wsattr(wtypes.text, mandatory=True) + """The name of the host""" + + service_description = wsme.wsattr(wtypes.text, mandatory=False) + """The service description""" + + time_stamp = wsme.wsattr(int, mandatory=False) + """Time stamp for the recheck""" + + @classmethod + def sample(cls): + return cls( + host_name="localhost", + service_description="ws-arbiter", + time_stamp=1430150469, + ) diff --git a/surveil/tests/api/controllers/v2/actions/__init__.py b/surveil/tests/api/controllers/v2/actions/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/surveil/tests/api/controllers/v2/actions/test_acknowledge.py b/surveil/tests/api/controllers/v2/actions/test_acknowledge.py index 19c3cca..7421515 100644 --- a/surveil/tests/api/controllers/v2/actions/test_acknowledge.py +++ b/surveil/tests/api/controllers/v2/actions/test_acknowledge.py @@ -32,8 +32,9 @@ class TestAcknowledgeController(functionalTest.FunctionalTest): self.assertEqual(response.status_int, 200) - self.assertEqual(httpretty.last_request().body, - 'action=add&host_name=localhost') + self.assert_count_equal_backport(httpretty.last_request().body.decode() + .split('&'), + ['host_name=localhost', 'action=add']) self.assertEqual(httpretty.last_request().path, '/acknowledge') @@ -50,7 +51,9 @@ class TestAcknowledgeController(functionalTest.FunctionalTest): self.assertEqual(response.status_int, 200) - self.assertEqual(httpretty.last_request().body, - 'duration=86400&action=delete&host_name=localhost') + self.assert_count_equal_backport(httpretty.last_request().body.decode() + .split('&'), + ['host_name=localhost', + 'action=delete']) self.assertEqual(httpretty.last_request().path, '/downtime') diff --git a/surveil/tests/api/controllers/v2/actions/test_downtime.py b/surveil/tests/api/controllers/v2/actions/test_downtime.py index 0079951..b9b5f21 100644 --- a/surveil/tests/api/controllers/v2/actions/test_downtime.py +++ b/surveil/tests/api/controllers/v2/actions/test_downtime.py @@ -33,8 +33,10 @@ class TestDowntimeController(functionalTest.FunctionalTest): self.assertEqual(response.status_int, 200) - self.assertEqual(httpretty.last_request().body, - 'duration=86400&action=add&host_name=localhost') + self.assert_count_equal_backport(httpretty.last_request().body.decode() + .split('&'), + ['host_name=localhost', 'action=add', + 'duration=86400']) self.assertEqual(httpretty.last_request().path, '/downtime') @@ -52,7 +54,9 @@ class TestDowntimeController(functionalTest.FunctionalTest): self.assertEqual(response.status_int, 200) - self.assertEqual(httpretty.last_request().body, - 'duration=86400&action=delete&host_name=localhost') + self.assert_count_equal_backport(httpretty.last_request().body.decode() + .split('&'), + ['host_name=localhost', + 'action=delete', 'duration=86400']) self.assertEqual(httpretty.last_request().path, '/downtime') diff --git a/surveil/tests/api/controllers/v2/actions/test_recheck.py b/surveil/tests/api/controllers/v2/actions/test_recheck.py new file mode 100644 index 0000000..26defca --- /dev/null +++ b/surveil/tests/api/controllers/v2/actions/test_recheck.py @@ -0,0 +1,39 @@ +# 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 TestRecheckController(functionalTest.FunctionalTest): + + @httpretty.activate + def test_recheck_post(self): + httpretty.register_uri(httpretty.POST, + self.ws_arbiter_url + "/recheck") + + recheck = { + "host_name": "localhost", + } + + response = self.post_json("/v2/actions/recheck/", params=recheck) + + self.assertEqual(response.status_int, 200) + + self.assert_count_equal_backport(httpretty.last_request().body.decode() + .split('&'), + ['host_name=localhost']) + self.assertEqual(httpretty.last_request().path, + '/recheck') diff --git a/surveil/tests/api/functionalTest.py b/surveil/tests/api/functionalTest.py index 09eeaf3..f2aa2ce 100644 --- a/surveil/tests/api/functionalTest.py +++ b/surveil/tests/api/functionalTest.py @@ -98,7 +98,8 @@ class FunctionalTest(base.BaseTestCase): return func for action in ('get', 'post', 'put', 'delete', - 'post', 'post_json', 'put_json'): + 'post', 'post_json', 'put_json', + 'delete_json'): setattr(self, action, make_action(action)) def tearDown(self):