From 7cd944524fd514c121c793eb0cc38f54da469ea0 Mon Sep 17 00:00:00 2001 From: Nikolay Gribanov Date: Mon, 8 Feb 2021 08:43:54 +0300 Subject: [PATCH 1/2] HH-123745 add weight support --- consul/base.py | 21 +++++++++++++++++++++ tests/test_std.py | 21 +++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/consul/base.py b/consul/base.py index 9d27dbd..2b412be 100755 --- a/consul/base.py +++ b/consul/base.py @@ -284,6 +284,20 @@ def cb(response): return cb +# +# Convenience to define weight + +class Weight(object): + """ + There object for set weights parameters like this + {'passing': 100, 'warning': 100} + """ + + @classmethod + def weights(cls, passing, warning): + return {'passing': passing, 'warning': warning} + + class HTTPClient(six.with_metaclass(abc.ABCMeta, object)): def __init__(self, host='127.0.0.1', port=8500, scheme='http', verify=True, cert=None, timeout=None): @@ -1364,6 +1378,7 @@ def register( check=None, token=None, meta=None, + weights=None, # *deprecated* use check parameter script=None, interval=None, @@ -1396,6 +1411,10 @@ def register( *meta* specifies arbitrary KV metadata linked to the service formatted as {k1:v1, k2:v2}. + *weights* specifies weights for the service. + If this field is not provided weights + will default to {"Passing": 1, "Warning": 1}. + *script*, *interval*, *ttl*, *http*, and *timeout* arguments are deprecated. use *check* instead. @@ -1425,6 +1444,8 @@ def register( payload['meta'] = meta if check: payload['check'] = check + if weights: + payload['weights'] = weights else: payload.update(Check._compat( diff --git a/tests/test_std.py b/tests/test_std.py index 2ff1099..68712fa 100644 --- a/tests/test_std.py +++ b/tests/test_std.py @@ -8,6 +8,7 @@ import consul import consul.std +from consul.base import Weight Check = consul.Check @@ -348,6 +349,26 @@ def test_agent_register_enable_tag_override(self, consul_port): # Cleanup tasks c.agent.check.deregister('foo') + def test_agent_register_enable_weights(self, consul_port): + c = consul.Consul(port=consul_port) + index, nodes = c.health.service("foo1") + assert nodes == [] + + c.agent.service.register('foo', weights=Weight.weights(10, 10)) + assert c.agent.services()['foo']['Weights'] == {"Passing": 10, "Warning": 10} + # Cleanup tasks + c.agent.check.deregister('foo') + + def test_agent_register_disable_weights(self, consul_port): + c = consul.Consul(port=consul_port) + index, nodes = c.health.service("foo1") + assert nodes == [] + + c.agent.service.register('foo') + assert c.agent.services()['foo']['Weights'] == {"Passing": 1, "Warning": 1} + # Cleanup tasks + c.agent.check.deregister('foo') + def test_agent_service_maintenance(self, consul_port): c = consul.Consul(port=consul_port) From 430873d78041ef05438dca5ce8ae4c3c401b331c Mon Sep 17 00:00:00 2001 From: Nikolay Gribanov Date: Thu, 11 Feb 2021 10:09:49 +0300 Subject: [PATCH 2/2] fix length --- tests/test_std.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/test_std.py b/tests/test_std.py index 68712fa..c27280f 100644 --- a/tests/test_std.py +++ b/tests/test_std.py @@ -355,7 +355,8 @@ def test_agent_register_enable_weights(self, consul_port): assert nodes == [] c.agent.service.register('foo', weights=Weight.weights(10, 10)) - assert c.agent.services()['foo']['Weights'] == {"Passing": 10, "Warning": 10} + weight = {"Passing": 10, "Warning": 10} + assert c.agent.services()['foo']['Weights'] == weight # Cleanup tasks c.agent.check.deregister('foo') @@ -365,7 +366,8 @@ def test_agent_register_disable_weights(self, consul_port): assert nodes == [] c.agent.service.register('foo') - assert c.agent.services()['foo']['Weights'] == {"Passing": 1, "Warning": 1} + weight = {"Passing": 1, "Warning": 1} + assert c.agent.services()['foo']['Weights'] == weight # Cleanup tasks c.agent.check.deregister('foo')