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
2 changes: 1 addition & 1 deletion .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[bumpversion]
commit = True
tag = True
current_version = 0.2.5
current_version = 0.2.6
parse = (?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)(\-(?P<release>[a-z]+))?
serialize =
{major}.{minor}.{patch}-{release}
Expand Down
2 changes: 1 addition & 1 deletion consul/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '0.2.5'
__version__ = '0.2.6'

from consul.base import ACLDisabled # noqa
from consul.base import ACLPermissionDenied # noqa
Expand Down
53 changes: 41 additions & 12 deletions consul/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,10 @@ class ConsulCacheBase(metaclass=abc.ABCMeta):
calls to wait for changes since this query was last run.
"""

def __init__(self, watch_seconds: str, backoff_delay_seconds: int):
def __init__(self, watch_seconds: str, backoff_delay_seconds: int, caller: str):
self.cache = dict()
self.callbacks = []
self.caller = caller
self.watch_seconds = watch_seconds
self.backoff_delay_seconds = backoff_delay_seconds
self.index = None
Expand Down Expand Up @@ -397,8 +398,9 @@ def __init__(self,
backoff_delay_seconds: int,
service: str,
passing: bool,
dc: str):
super().__init__(watch_seconds, backoff_delay_seconds)
dc: str,
caller: str):
super().__init__(watch_seconds, backoff_delay_seconds, caller)
self.service = service
self.health_client = health_client
self.passing = passing
Expand All @@ -407,6 +409,7 @@ def __init__(self,
service=self.service,
passing=self.passing,
dc=self.dc,
caller=self.caller
)
self.cache = {self.service: service_health}

Expand All @@ -418,7 +421,8 @@ def _update_cache(self):
'passing': self.passing,
'index': self.index,
'wait': self.watch_seconds,
'dc': self.dc
'dc': self.dc,
'caller': self.caller
}
log.debug(f'Param for health query: {params}')
self.index, values = self.health_client.service(**params)
Expand Down Expand Up @@ -459,8 +463,9 @@ def __init__(self,
total_timeout: int,
recurse: bool,
consistency_mode: ConsistencyMode,
caller: str,
cache_initial_warmup_timeout=None):
super().__init__(watch_seconds, backoff_delay_seconds)
super().__init__(watch_seconds, backoff_delay_seconds, caller)
self.kv_client = kv_client
self.path = path
self.recurse = recurse
Expand All @@ -470,7 +475,8 @@ def __init__(self,
self.index, kv = kv_client.get(
key=path,
recurse=recurse,
total_timeout=self._get_warmup_timeout()
total_timeout=self._get_warmup_timeout(),
caller=self.caller
)
self.cache = {self.path: kv}

Expand All @@ -491,7 +497,8 @@ def _update_cache(self):
'wait': self.watch_seconds,
'total_timeout': self.total_timeout,
'consistency': self.consistency_mode,
'recurse': self.recurse
'recurse': self.recurse,
'caller': self.caller
}
log.debug(f'Param for kv query: {params}')
self.index, values = self.kv_client.get(**params)
Expand Down Expand Up @@ -1596,6 +1603,7 @@ def register(
ttl=None,
http=None,
timeout=None,
caller=None,
enable_tag_override=False):
"""
Add a new service to the local agent. There is more
Expand Down Expand Up @@ -1629,6 +1637,8 @@ def register(
*script*, *interval*, *ttl*, *http*, and *timeout* arguments
are deprecated. use *check* instead.

*caller* is a name of caller service.

*enable_tag_override* is an optional bool that enable you
to modify a service tags from servers(consul agent role server)
Default is set to False.
Expand All @@ -1637,7 +1647,7 @@ def register(
for more information
https://www.consul.io/docs/agent/services.html
"""

params = []
payload = {}

payload['name'] = name
Expand All @@ -1658,6 +1668,9 @@ def register(
if weights:
payload['weights'] = weights

if caller:
params.append(('caller', caller))

else:
payload.update(Check._compat(
script=script,
Expand All @@ -1675,22 +1688,28 @@ def register(
CB.bool(),
path='/v1/agent/service/register',
headers=headers,
params=params,
data=json.dumps(payload))

def deregister(self, service_id, token=None):
def deregister(self, service_id, caller=None, token=None):
"""
Used to remove a service from the local agent. The agent will
take care of deregistering the service with the Catalog. If
there is an associated check, that is also deregistered.
"""

params = []
headers = {}
token = token or self.agent.token
if token:
headers['X-Consul-Token'] = token
if caller:
params.append(('caller', caller))
return self.agent.http.put(
CB.bool(),
path='/v1/agent/service/deregister/%s' % service_id,
headers=headers
headers=headers,
params=params
)

def maintenance(self, service_id, enable, reason=None, token=None):
Expand Down Expand Up @@ -2935,7 +2954,8 @@ def service(self,
dc=None,
near=None,
token=None,
node_meta=None):
node_meta=None,
caller=None):
"""
Returns a tuple of (*index*, *nodes*)

Expand Down Expand Up @@ -2963,6 +2983,8 @@ def service(self,

*node_meta* is an optional meta data used for filtering, a
dictionary formatted as {k1:v1, k2:v2}.

*caller* is a name of caller service.
"""
params = []
headers = {}
Expand All @@ -2987,6 +3009,8 @@ def service(self,
for nodemeta_name, nodemeta_value in node_meta.items():
params.append(('node-meta', '{0}:{1}'.
format(nodemeta_name, nodemeta_value)))
if caller:
params.append(('caller', caller))
return self.agent.http.get(
CB.json(index=True),
path='/v1/health/service/%s' % service,
Expand Down Expand Up @@ -3167,7 +3191,8 @@ def get(
keys=False,
separator=None,
dc=None,
total_timeout=None):
total_timeout=None,
caller=None):
"""
Returns a tuple of (*index*, *value[s]*)

Expand Down Expand Up @@ -3203,6 +3228,8 @@ def get(
"Session": "adf4238a-882b-9ddc-4a9d-5b6758e4159e"
}

*caller* is a name of caller service.

Note, if the requested key does not exists *(index, None)* is
returned. It's then possible to long poll on the index for when the
key is created.
Expand Down Expand Up @@ -3234,6 +3261,8 @@ def get(
consistency = consistency or self.agent.consistency
if consistency in ('consistent', 'stale'):
params.append((consistency, '1'))
if caller:
params.append(('caller', caller))

one = False
decode = False
Expand Down
2 changes: 1 addition & 1 deletion sonar-project.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Required metadata
sonar.projectKey=com.github:poppyred:python-consul2
sonar.projectName=Python Consul2 HH
sonar.projectVersion=0.2.5
sonar.projectVersion=0.2.6

# Comma-separated paths to directories with sources (required)
sonar.sources=consul
Expand Down