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

#26 add weight support #28

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
21 changes: 21 additions & 0 deletions consul/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -1364,6 +1378,7 @@ def register(
check=None,
token=None,
meta=None,
weights=None,
# *deprecated* use check parameter
script=None,
interval=None,
Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -1425,6 +1444,8 @@ def register(
payload['meta'] = meta
if check:
payload['check'] = check
if weights:
payload['weights'] = weights

else:
payload.update(Check._compat(
Expand Down
23 changes: 23 additions & 0 deletions tests/test_std.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import consul
import consul.std
from consul.base import Weight

Check = consul.Check

Expand Down Expand Up @@ -348,6 +349,28 @@ 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))
weight = {"Passing": 10, "Warning": 10}
assert c.agent.services()['foo']['Weights'] == weight
# 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')
weight = {"Passing": 1, "Warning": 1}
assert c.agent.services()['foo']['Weights'] == weight
# Cleanup tasks
c.agent.check.deregister('foo')

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

Expand Down