From 3c1d336b43e7473e10ee86776308ff3382681a64 Mon Sep 17 00:00:00 2001 From: Cameron Ruatta Date: Wed, 16 Sep 2015 21:59:15 -0700 Subject: [PATCH 1/4] Adding Health/checks endpoint --- consul/base.py | 45 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/consul/base.py b/consul/base.py index 37393d77..69a59201 100644 --- a/consul/base.py +++ b/consul/base.py @@ -1108,9 +1108,7 @@ def service( '/v1/catalog/service/%s' % service, params=params) class Health(object): - # TODO: All of the health endpoints support blocking queries and all - # consistency modes - # TODO: still need to add node and checks endpoints + # TODO: All of the health endpoints support all consistency modes def __init__(self, agent): self.agent = agent @@ -1162,6 +1160,47 @@ def callback(response): callback, '/v1/health/service/%s' % service, params=params) + def checks(self, + service, + index=None, + wait=None, + dc=None): + """ + Returns a tuple of (*index*, *checks*) with *checks* being the + checks associated with the service. + + *service* is the name of the service being checked. + + + *index* is the current Consul index, suitable for making subsequent + calls to wait for changes since this query was last run. + + *wait* the maximum duration to wait (e.g. '10s') to retrieve + a given index. this parameter is only applied if *index* is also + specified. the wait time by default is 5 minutes. + + *dc* is the datacenter of the node and defaults to this agents + datacenter. + + """ + params = {} + if index: + params['index'] = index + if wait: + params['wait'] = wait + dc = dc or self.agent.dc + if dc: + params['dc'] = dc + + def callback(response): + data = json.loads(response.body) + return response.headers['X-Consul-Index'], data + + return self.agent.http.get( + callback, + '/v1/health/checks/%s' % service, params=params) + + def state(self, name, index=None, wait=None, dc=None): """ Returns a tuple of (*index*, *nodes*) From 5b30a480fd3cffd04752ec24ed78c603b5f2ea3f Mon Sep 17 00:00:00 2001 From: Cameron Ruatta Date: Thu, 17 Sep 2015 17:32:43 -0700 Subject: [PATCH 2/4] Adding the test --- tests/test_std.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/test_std.py b/tests/test_std.py index 8a65ce47..225e9ad6 100644 --- a/tests/test_std.py +++ b/tests/test_std.py @@ -567,6 +567,22 @@ def test_health_node(self, consul_port): index, checks = c.health.node(node) assert node in [check["Node"] for check in checks] + def test_health_checks(self, consul_port): + c = consul.Consul(self, consul_port) + + # Assert there are no services except the Serf Health check + index, nodes = c.health.state('any') + assert [node['ServiceID'] for node in nodes] == [''] + + c.agent.service.register( + 'foo', service_id='foobar', check=Check.ttl('9999s')) + + time.sleep(40/1000.00) + + index, checks = c.health.checks('foobar') + + assert len(checks) == 1 + def test_session(self, consul_port): c = consul.Consul(port=consul_port) From f85155a61045623a318c616c435335c2b2adc3aa Mon Sep 17 00:00:00 2001 From: Cameron Ruatta Date: Sat, 19 Sep 2015 12:55:28 -0700 Subject: [PATCH 3/4] Fixing the test for test_health_checks --- tests/test_std.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/tests/test_std.py b/tests/test_std.py index 225e9ad6..55967876 100644 --- a/tests/test_std.py +++ b/tests/test_std.py @@ -568,20 +568,24 @@ def test_health_node(self, consul_port): assert node in [check["Node"] for check in checks] def test_health_checks(self, consul_port): - c = consul.Consul(self, consul_port) - - # Assert there are no services except the Serf Health check - index, nodes = c.health.state('any') - assert [node['ServiceID'] for node in nodes] == [''] + c = consul.Consul(port=consul_port) c.agent.service.register( - 'foo', service_id='foobar', check=Check.ttl('9999s')) + 'foobar', service_id='foobar', check=Check.ttl('10s')) time.sleep(40/1000.00) index, checks = c.health.checks('foobar') - assert len(checks) == 1 + assert [check['ServiceID'] for check in checks] == ['foobar'] + assert [check['CheckID'] for check in checks] == ['service:foobar'] + + c.agent.service.deregister('foobar') + + time.sleep(40/1000.0) + + index, checks = c.health.checks('foobar') + assert len(checks) == 0 def test_session(self, consul_port): c = consul.Consul(port=consul_port) From 78d05ecf1eda501a5546371e76fd427fc04e6ee5 Mon Sep 17 00:00:00 2001 From: Cameron Ruatta Date: Sat, 19 Sep 2015 13:09:08 -0700 Subject: [PATCH 4/4] flake8 fixes for Health checks --- consul/base.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/consul/base.py b/consul/base.py index 69a59201..abec901f 100644 --- a/consul/base.py +++ b/consul/base.py @@ -1160,11 +1160,7 @@ def callback(response): callback, '/v1/health/service/%s' % service, params=params) - def checks(self, - service, - index=None, - wait=None, - dc=None): + def checks(self, service, index=None, wait=None, dc=None): """ Returns a tuple of (*index*, *checks*) with *checks* being the checks associated with the service. @@ -1200,7 +1196,6 @@ def callback(response): callback, '/v1/health/checks/%s' % service, params=params) - def state(self, name, index=None, wait=None, dc=None): """ Returns a tuple of (*index*, *nodes*)