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
25 changes: 25 additions & 0 deletions consul/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ def __init__(
self.health = Consul.Health(self)
self.session = Consul.Session(self)
self.acl = Consul.ACL(self)
self.status = Consul.Status(self)

class Event(object):
"""
Expand Down Expand Up @@ -1544,3 +1545,27 @@ def callback(response):

return self.agent.http.put(
callback, '/v1/acl/destroy/%s' % acl_id, params=params)

class Status(object):
"""
The Status endpoints are used to get information about the status
of the Consul cluster.
"""
def __init__(self, agent):
self.agent = agent

def leader(self):
"""
This endpoint is used to get the Raft leader for the datacenter
in which the agent is running.
"""
return self.agent.http.get(
lambda x: json.loads(x.body), '/v1/status/leader')

def peers(self):
"""
This endpoint retrieves the Raft peers for the datacenter in which
the the agent is running.
"""
return self.agent.http.get(
lambda x: json.loads(x.body), '/v1/status/peers')
29 changes: 29 additions & 0 deletions tests/test_std.py
Original file line number Diff line number Diff line change
Expand Up @@ -758,3 +758,32 @@ def test_acl_implicit_token_use(self, acl_consul):
acls = c.acl.list()
assert set([x['ID'] for x in acls]) == \
set(['anonymous', master_token])

def test_status_leader(self, consul_port):
c = consul.Consul(port=consul_port)

agent_self = c.agent.self()
port = agent_self['Config']['Ports']['Server']
addr = agent_self['Config']['AdvertiseAddr']

addr_port = "{0}:{1}".format(addr, port)
leader = c.status.leader()

assert leader == addr_port, \
"Leader value was {0}, expected value " \
"was {1}".format(leader, addr_port)

def test_status_peers(self, consul_port):

c = consul.Consul(port=consul_port)

agent_self = c.agent.self()
port = agent_self['Config']['Ports']['Server']
addr = agent_self['Config']['AdvertiseAddr']

addr_port = "{0}:{1}".format(addr, port)
peers = c.status.peers()

assert addr_port in peers, \
"Expected value '{0}' " \
"in peer list but it was not present".format(addr_port)