From 60b016cf29d965b491a126b18c10a8fcea30a67b Mon Sep 17 00:00:00 2001 From: Cameron Ruatta Date: Mon, 8 Jun 2015 10:56:33 -0700 Subject: [PATCH 1/5] Adding /v1/status endpoint --- consul/base.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/consul/base.py b/consul/base.py index 7c4ce5dc..8acf5eaf 100644 --- a/consul/base.py +++ b/consul/base.py @@ -1544,3 +1544,28 @@ 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') From 710c11ebd105a36ded7c7ac0dbde8fd4ff0be800 Mon Sep 17 00:00:00 2001 From: Cameron Ruatta Date: Mon, 8 Jun 2015 12:54:45 -0700 Subject: [PATCH 2/5] Adding reference to Status class in init --- consul/base.py | 1 + 1 file changed, 1 insertion(+) diff --git a/consul/base.py b/consul/base.py index 8acf5eaf..2edb5dbf 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): """ From 8a2ffa8be06593548e6dc2139ef5f56c4d975388 Mon Sep 17 00:00:00 2001 From: Cameron Ruatta Date: Mon, 8 Jun 2015 12:55:58 -0700 Subject: [PATCH 3/5] pep8 spacing mistake --- consul/base.py | 1 - 1 file changed, 1 deletion(-) diff --git a/consul/base.py b/consul/base.py index 2edb5dbf..d7b87a8a 100644 --- a/consul/base.py +++ b/consul/base.py @@ -1562,7 +1562,6 @@ def leader(self): 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 From ce70297735da06c7e218ddc07b64fcb926e71d6a Mon Sep 17 00:00:00 2001 From: Cameron Ruatta Date: Mon, 8 Jun 2015 12:57:01 -0700 Subject: [PATCH 4/5] Triple quotes instead of single quotes on these docstrings --- consul/base.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/consul/base.py b/consul/base.py index d7b87a8a..baf4aedd 100644 --- a/consul/base.py +++ b/consul/base.py @@ -1547,25 +1547,25 @@ def callback(response): 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') From 3a1dbfe354af1ae80d3bcf3c4bda967ca10cd707 Mon Sep 17 00:00:00 2001 From: Cameron Ruatta Date: Mon, 8 Jun 2015 14:11:32 -0700 Subject: [PATCH 5/5] Tests for status --- tests/test_std.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) 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)