Skip to content
This repository was archived by the owner on Apr 15, 2024. It is now read-only.

Commit 9008e92

Browse files
committed
Merge pull request #67 from cruatta/master
Adding /v1/health/checks endpoint
2 parents 2dd8fe1 + 78d05ec commit 9008e92

2 files changed

Lines changed: 57 additions & 3 deletions

File tree

consul/base.py

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,9 +1108,7 @@ def service(
11081108
'/v1/catalog/service/%s' % service, params=params)
11091109

11101110
class Health(object):
1111-
# TODO: All of the health endpoints support blocking queries and all
1112-
# consistency modes
1113-
# TODO: still need to add node and checks endpoints
1111+
# TODO: All of the health endpoints support all consistency modes
11141112
def __init__(self, agent):
11151113
self.agent = agent
11161114

@@ -1162,6 +1160,42 @@ def callback(response):
11621160
callback,
11631161
'/v1/health/service/%s' % service, params=params)
11641162

1163+
def checks(self, service, index=None, wait=None, dc=None):
1164+
"""
1165+
Returns a tuple of (*index*, *checks*) with *checks* being the
1166+
checks associated with the service.
1167+
1168+
*service* is the name of the service being checked.
1169+
1170+
1171+
*index* is the current Consul index, suitable for making subsequent
1172+
calls to wait for changes since this query was last run.
1173+
1174+
*wait* the maximum duration to wait (e.g. '10s') to retrieve
1175+
a given index. this parameter is only applied if *index* is also
1176+
specified. the wait time by default is 5 minutes.
1177+
1178+
*dc* is the datacenter of the node and defaults to this agents
1179+
datacenter.
1180+
1181+
"""
1182+
params = {}
1183+
if index:
1184+
params['index'] = index
1185+
if wait:
1186+
params['wait'] = wait
1187+
dc = dc or self.agent.dc
1188+
if dc:
1189+
params['dc'] = dc
1190+
1191+
def callback(response):
1192+
data = json.loads(response.body)
1193+
return response.headers['X-Consul-Index'], data
1194+
1195+
return self.agent.http.get(
1196+
callback,
1197+
'/v1/health/checks/%s' % service, params=params)
1198+
11651199
def state(self, name, index=None, wait=None, dc=None):
11661200
"""
11671201
Returns a tuple of (*index*, *nodes*)

tests/test_std.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,26 @@ def test_health_node(self, consul_port):
567567
index, checks = c.health.node(node)
568568
assert node in [check["Node"] for check in checks]
569569

570+
def test_health_checks(self, consul_port):
571+
c = consul.Consul(port=consul_port)
572+
573+
c.agent.service.register(
574+
'foobar', service_id='foobar', check=Check.ttl('10s'))
575+
576+
time.sleep(40/1000.00)
577+
578+
index, checks = c.health.checks('foobar')
579+
580+
assert [check['ServiceID'] for check in checks] == ['foobar']
581+
assert [check['CheckID'] for check in checks] == ['service:foobar']
582+
583+
c.agent.service.deregister('foobar')
584+
585+
time.sleep(40/1000.0)
586+
587+
index, checks = c.health.checks('foobar')
588+
assert len(checks) == 0
589+
570590
def test_session(self, consul_port):
571591
c = consul.Consul(port=consul_port)
572592

0 commit comments

Comments
 (0)