Skip to content
This repository was archived by the owner on Apr 15, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 37 additions & 3 deletions consul/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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*)
Expand Down
20 changes: 20 additions & 0 deletions tests/test_std.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down