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

Commit 52add07

Browse files
committed
Merge pull request #52 from cruatta/issue-50-add-status-endpoint
Issue 50 - Add status endpoint
2 parents cf4f5a5 + 3a1dbfe commit 52add07

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

consul/base.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ def __init__(
171171
self.health = Consul.Health(self)
172172
self.session = Consul.Session(self)
173173
self.acl = Consul.ACL(self)
174+
self.status = Consul.Status(self)
174175

175176
class Event(object):
176177
"""
@@ -1565,3 +1566,27 @@ def callback(response):
15651566

15661567
return self.agent.http.put(
15671568
callback, '/v1/acl/destroy/%s' % acl_id, params=params)
1569+
1570+
class Status(object):
1571+
"""
1572+
The Status endpoints are used to get information about the status
1573+
of the Consul cluster.
1574+
"""
1575+
def __init__(self, agent):
1576+
self.agent = agent
1577+
1578+
def leader(self):
1579+
"""
1580+
This endpoint is used to get the Raft leader for the datacenter
1581+
in which the agent is running.
1582+
"""
1583+
return self.agent.http.get(
1584+
lambda x: json.loads(x.body), '/v1/status/leader')
1585+
1586+
def peers(self):
1587+
"""
1588+
This endpoint retrieves the Raft peers for the datacenter in which
1589+
the the agent is running.
1590+
"""
1591+
return self.agent.http.get(
1592+
lambda x: json.loads(x.body), '/v1/status/peers')

tests/test_std.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,3 +765,32 @@ def test_acl_implicit_token_use(self, acl_consul):
765765
acls = c.acl.list()
766766
assert set([x['ID'] for x in acls]) == \
767767
set(['anonymous', master_token])
768+
769+
def test_status_leader(self, consul_port):
770+
c = consul.Consul(port=consul_port)
771+
772+
agent_self = c.agent.self()
773+
port = agent_self['Config']['Ports']['Server']
774+
addr = agent_self['Config']['AdvertiseAddr']
775+
776+
addr_port = "{0}:{1}".format(addr, port)
777+
leader = c.status.leader()
778+
779+
assert leader == addr_port, \
780+
"Leader value was {0}, expected value " \
781+
"was {1}".format(leader, addr_port)
782+
783+
def test_status_peers(self, consul_port):
784+
785+
c = consul.Consul(port=consul_port)
786+
787+
agent_self = c.agent.self()
788+
port = agent_self['Config']['Ports']['Server']
789+
addr = agent_self['Config']['AdvertiseAddr']
790+
791+
addr_port = "{0}:{1}".format(addr, port)
792+
peers = c.status.peers()
793+
794+
assert addr_port in peers, \
795+
"Expected value '{0}' " \
796+
"in peer list but it was not present".format(addr_port)

0 commit comments

Comments
 (0)