From d49c752e2887f49e10e3f012428f9e0fb1318571 Mon Sep 17 00:00:00 2001 From: Fei Long Wang Date: Tue, 7 Jan 2014 13:38:36 +0800 Subject: [PATCH] Add /health support to the client The patch adds /health support of the client. This will allow users to check the status of shard from Marconi. Partially-Implements blueprint: python-marconiclient-v1 Change-Id: I5e6bbb79144a5808ef91b00bb8265936364cec30 --- examples/management.py | 25 +++++++++++++++++++++++++ marconiclient/queues/v1/api.py | 6 ++++++ marconiclient/queues/v1/client.py | 6 ++++++ marconiclient/queues/v1/core.py | 18 ++++++++++++++++++ tests/unit/queues/v1/test_client.py | 12 ++++++++++++ tests/unit/queues/v1/test_core.py | 9 +++++++++ 6 files changed, 76 insertions(+) create mode 100644 examples/management.py diff --git a/examples/management.py b/examples/management.py new file mode 100644 index 00000000..179648b7 --- /dev/null +++ b/examples/management.py @@ -0,0 +1,25 @@ +# Copyright 2014 IBM Corp. +# +# 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 marconiclient.queues import client + +URL = 'http://localhost:8888/v1' + + +def healthy(): + cli = client.Client(url=URL, version=1) + return True if cli.health() else False + +if __name__ == '__main__': + healthy() diff --git a/marconiclient/queues/v1/api.py b/marconiclient/queues/v1/api.py index 6f2aff07..3b15cc2b 100644 --- a/marconiclient/queues/v1/api.py +++ b/marconiclient/queues/v1/api.py @@ -161,4 +161,10 @@ class V1(api.Api): 'shard_name': {'type': 'string'}, } }, + + 'health': { + 'admin': True, + 'ref': 'health', + 'method': 'GET', + }, } diff --git a/marconiclient/queues/v1/client.py b/marconiclient/queues/v1/client.py index d4534d0e..9e6ac5a2 100644 --- a/marconiclient/queues/v1/client.py +++ b/marconiclient/queues/v1/client.py @@ -15,6 +15,7 @@ import uuid +from marconiclient.queues.v1 import core from marconiclient.queues.v1 import queues from marconiclient.queues.v1 import shard from marconiclient import transport @@ -112,3 +113,8 @@ class Client(object): :rtype: `shard.Shard` """ return shard.Shard(self, ref, **kwargs) + + def health(self): + """Gets the health status of Marconi server.""" + req, trans = self._request_and_transport() + return core.health(trans, req) diff --git a/marconiclient/queues/v1/core.py b/marconiclient/queues/v1/core.py index a26b2703..737a580a 100644 --- a/marconiclient/queues/v1/core.py +++ b/marconiclient/queues/v1/core.py @@ -267,3 +267,21 @@ def shard_delete(transport, request, shard_name): request.operation = 'shard_delete' request.params['shard_name'] = shard_name transport.send(request) + + +def health(transport, request, callback=None): + """Check the health of web head for load balancing + + :param transport: Transport instance to use + :type transport: `transport.base.Transport` + :param request: Request instance ready to be sent. + :type request: `transport.request.Request` + :param callback: Optional callable to use as callback. + If specified, this request will be sent asynchronously. + (IGNORED UNTIL ASYNC SUPPORT IS COMPLETE) + :type callback: Callable object. + """ + + request.operation = 'health' + resp = transport.send(request) + return resp.deserialized_content diff --git a/tests/unit/queues/v1/test_client.py b/tests/unit/queues/v1/test_client.py index 44f6303b..cdf6eb25 100644 --- a/tests/unit/queues/v1/test_client.py +++ b/tests/unit/queues/v1/test_client.py @@ -13,8 +13,12 @@ # See the License for the specific language governing permissions and # limitations under the License. +import mock + from marconiclient.queues import client +from marconiclient.queues.v1 import core from marconiclient.tests import base +from marconiclient.transport import response VERSION = 1 @@ -25,3 +29,11 @@ class TestClient(base.TestBase): cli = client.Client('http://example.com', VERSION, {}) self.assertIsNotNone(cli.transport()) + + def test_health(self): + cli = client.Client('http://example.com', + VERSION, {}) + with mock.patch.object(core, 'health', autospec=True) as core_health: + resp = response.Response(None, None) + core_health.return_value = resp + self.assertIsNotNone(cli.health()) diff --git a/tests/unit/queues/v1/test_core.py b/tests/unit/queues/v1/test_core.py index 35f2a02f..910e7785 100644 --- a/tests/unit/queues/v1/test_core.py +++ b/tests/unit/queues/v1/test_core.py @@ -187,3 +187,12 @@ class TestV1Core(base.TestBase): req = request.Request() core.shard_delete(self.transport, req, 'test_shard') + + def test_health(self): + with mock.patch.object(self.transport, 'send', + autospec=True) as send_method: + resp = response.Response(None, None) + send_method.return_value = resp + + req = request.Request() + core.health(self.transport, req)