diff --git a/consul/base.py b/consul/base.py index 7c4ce5dc..baf4aedd 100644 --- a/consul/base.py +++ b/consul/base.py @@ -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): """ @@ -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') diff --git a/tests/test_std.py b/tests/test_std.py index 138f9739..f420716a 100644 --- a/tests/test_std.py +++ b/tests/test_std.py @@ -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)