diff --git a/consul/base.py b/consul/base.py index 37393d77..abec901f 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,42 @@ 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*) diff --git a/tests/test_std.py b/tests/test_std.py index 8a65ce47..55967876 100644 --- a/tests/test_std.py +++ b/tests/test_std.py @@ -567,6 +567,26 @@ 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(port=consul_port) + + c.agent.service.register( + 'foobar', service_id='foobar', check=Check.ttl('10s')) + + time.sleep(40/1000.00) + + index, checks = c.health.checks('foobar') + + 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)