From b722bd2b9502cc33505d83bfca60584f6c9874d0 Mon Sep 17 00:00:00 2001 From: aviau Date: Fri, 29 Aug 2014 09:53:30 -0400 Subject: [PATCH] Added check result POST for services Change-Id: Ia0a22453afcf173134a6d22ad2b593346560aee5 --- doc/source/webapi/v1.rst | 6 ++ surveil/api/controllers/v1/hosts.py | 47 +++++++++++ .../tests/api/controllers/v1/test_hosts.py | 80 ++++++++++++++----- 3 files changed, 112 insertions(+), 21 deletions(-) diff --git a/doc/source/webapi/v1.rst b/doc/source/webapi/v1.rst index 1f63bc6..5548eed 100644 --- a/doc/source/webapi/v1.rst +++ b/doc/source/webapi/v1.rst @@ -22,9 +22,15 @@ Hosts .. rest-controller:: surveil.api.controllers.v1.hosts:HostServicesSubController :webprefix: /v1/hosts/(host_name)/services +.. rest-controller:: surveil.api.controllers.v1.hosts:HostServiceSubController + :webprefix: /v1/hosts/(host_name)/services/(service_name) + .. rest-controller:: surveil.api.controllers.v1.hosts:HostCheckResultsSubController :webprefix: /v1/hosts/(host_name)/results +.. rest-controller:: surveil.api.controllers.v1.hosts:ServiceCheckResultsSubController + :webprefix: /v1/hosts/(host_name)/services/(service_description)/results + .. autotype:: surveil.api.controllers.v1.datamodel.checkresult.CheckResult :members: diff --git a/surveil/api/controllers/v1/hosts.py b/surveil/api/controllers/v1/hosts.py index f7d3b3b..780e2eb 100644 --- a/surveil/api/controllers/v1/hosts.py +++ b/surveil/api/controllers/v1/hosts.py @@ -22,6 +22,49 @@ from surveil.api.controllers.v1.datamodel import host from surveil.api.controllers.v1.datamodel import service +class ServiceCheckResultsSubController(rest.RestController): + + @wsme_pecan.wsexpose(body=checkresult.CheckResult, status_code=204) + def post(self, data): + """Submit a new check result. + + :param data: a check result within the request body. + """ + result = data.as_dict() + result['host_name'] = pecan.request.context['host_name'] + + result['service_description'] = pecan.request.context[ + 'service_description' + ] + + requests.post( + pecan.request.ws_arbiter_url + "/push_check_result", + data=result + ) + + +class HostServiceSubController(rest.RestController): + results = ServiceCheckResultsSubController() + + def __init__(self, service_description): + pecan.request.context['service_description'] = service_description + self._id = service_description + + @wsme_pecan.wsexpose(service.Service) + def get(self): + """Returns a specific service.""" + mongo_s = pecan.request.mongo_connection.shinken.services.find_one( + { + "host_name": pecan.request.context['host_name'], + "service_description": pecan.request.context[ + 'service_description' + ] + } + ) + + return service.Service(**mongo_s) + + class HostServicesSubController(rest.RestController): @wsme_pecan.wsexpose([service.Service]) @@ -38,6 +81,10 @@ class HostServicesSubController(rest.RestController): return services + @pecan.expose() + def _lookup(self, service_description, *remainder): + return HostServiceSubController(service_description), remainder + class HostCheckResultsSubController(rest.RestController): diff --git a/surveil/tests/api/controllers/v1/test_hosts.py b/surveil/tests/api/controllers/v1/test_hosts.py index 8e14aec..107567d 100644 --- a/surveil/tests/api/controllers/v1/test_hosts.py +++ b/surveil/tests/api/controllers/v1/test_hosts.py @@ -49,6 +49,25 @@ class TestHostController(functionalTest.FunctionalTest): copy.deepcopy(self.hosts) ) + self.services = [ + { + "host_name": "bogus-router", + "service_description": "service-example", + "check_command": "check-disk!/dev/sdb1", + "max_check_attempts": "5", + "check_interval": "5", + "retry_interval": "3", + "check_period": "24x7", + "notification_interval": "30", + "notification_period": "24x7", + "contacts": "surveil-ptl,surveil-bob", + "contact_groups": "linux-admins" + } + ] + self.mongoconnection.shinken.services.insert( + copy.deepcopy(self.services) + ) + def test_get_all_hosts(self): response = self.app.get('/v1/hosts') @@ -120,34 +139,53 @@ class TestHostController(functionalTest.FunctionalTest): self.assertEqual(response.status_int, 201) def test_get_associated_services(self): - services = [ - { - "host_name": "bogus-router", - "service_description": "check-", - "check_command": "check-disk!/dev/sdb1", - "max_check_attempts": "5", - "check_interval": "5", - "retry_interval": "3", - "check_period": "24x7", - "notification_interval": "30", - "notification_period": "24x7", - "contacts": "surveil-ptl,surveil-bob", - "contact_groups": "linux-admins" - } - ] - self.mongoconnection.shinken.services.insert( - copy.deepcopy(services[0]) - ) - response = self.app.get('/v1/hosts/bogus-router/services') self.assertEqual( - services, + self.services, + json.loads(response.body.decode()) + ) + + def test_get_specific_service(self): + response = self.app.get( + '/v1/hosts/bogus-router/services/service-example' + ) + + self.assertEqual( + self.services[0], json.loads(response.body.decode()) ) @httpretty.activate - def test_submit_result(self): + def test_submit_service_result(self): + httpretty.register_uri(httpretty.POST, + self.ws_arbiter_url + "/push_check_result") + + check_result = { + "return_code": "0", + "output": "TEST OUTPUT", + "time_stamp": "1409149234" + } + + response = self.app.post_json( + "/v1/hosts/bogus-router/services/service-example/results", + params=check_result + ) + + self.assertEqual(response.status_int, 204) + self.assertEqual( + httpretty.last_request().parsed_body, + { + u'output': [u'TEST OUTPUT'], + u'return_code': [u'0'], + u'service_description': [u'service-example'], + u'host_name': [u'bogus-router'], + u'time_stamp': [u'1409149234'] + } + ) + + @httpretty.activate + def test_submit_host_result(self): httpretty.register_uri(httpretty.POST, self.ws_arbiter_url + "/push_check_result")