Skip to content
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
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}.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will be setted
так пожалуй будет правильнее

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will default to из официальной доки https://www.consul.io/api-docs/agent/service#weights

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

это агент выставляет по умолчанию по единичке или кто?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Так точно!


*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
21 changes: 21 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,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)

Expand Down