Skip to content

Commit

Permalink
feat: add replace_existing_checks option to agent.service.register() (#…
Browse files Browse the repository at this point in the history
…87)

* revert: add replace_existing_checks option to Catalog.register()

this param is for agent.service.register

* feat: add replace_existing_checks option to agent.service.register()

- Introduced `replace_existing_checks` parameter in the `register()` method to allow deletion of missing health checks from the request.
- Ensures idempotent registration of services and their checks without needing manual deregistration of checks.

* Release version 1.5.3
  • Loading branch information
cpaillet authored Oct 1, 2024
1 parent a435112 commit daac265
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 21 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Change log

## 1.5.3

- **feature:** add replace_existing_checks option to agent.service.register()
- **revert:** add replace_existing_checks option to Catalog.register()

## 1.5.2

- **feature:** add replace_existing_checks option to Catalog.register()
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__ = "1.5.2"
__version__ = "1.5.3"

from consul.check import Check
from consul.exceptions import ACLDisabled, ACLPermissionDenied, ConsulException, NotFound, Timeout
Expand Down
10 changes: 8 additions & 2 deletions consul/api/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ def register(
timeout=None,
enable_tag_override=False,
extra_checks=None,
replace_existing_checks=False,
):
"""
Add a new service to the local agent. There is more
Expand Down Expand Up @@ -184,6 +185,11 @@ def register(
*script*, *interval*, *ttl*, *http*, and *timeout* arguments
are deprecated. use *check* instead.
*replace_existing_checks* Missing health checks from the request will
be deleted from the agent.
Using this parameter allows to idempotently register a service and its
checks without having to manually deregister checks.
*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 Down Expand Up @@ -214,15 +220,15 @@ def register(
payload["checks"] = [check] + extra_checks
if weights:
payload["weights"] = weights

else:
payload.update(
Check._compat( # pylint: disable=protected-access
script=script, interval=interval, ttl=ttl, http=http, timeout=timeout
)
)

params = []
if replace_existing_checks:
params.append(("replace-existing-checks", "true"))
headers = self.agent.prepare_headers(token)
return self.agent.http.put(
CB.bool(), "/v1/agent/service/register", params=params, headers=headers, data=json.dumps(payload)
Expand Down
19 changes: 1 addition & 18 deletions consul/api/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,7 @@ class Catalog:
def __init__(self, agent):
self.agent = agent

def register(
self,
node,
address,
service=None,
check=None,
dc=None,
token=None,
node_meta=None,
replace_existing_checks=False,
):
def register(self, node, address, service=None, check=None, dc=None, token=None, node_meta=None):
"""
A low level mechanism for directly registering or updating entries
in the catalog. It is usually recommended to use
Expand Down Expand Up @@ -70,11 +60,6 @@ def register(
*node_meta* is an optional meta data used for filtering, a
dictionary formatted as {k1:v1, k2:v2}.
*replace_existing_checks* Missing health checks from the request will
be deleted from the agent.
Using this parameter allows to idempotently register a service and its
checks without having to manually deregister checks.
This manipulates the health check entry, but does not setup a
script or TTL to actually update the status. The full documentation
is `here <https://consul.io/docs/agent/http.html#catalog>`_.
Expand All @@ -94,8 +79,6 @@ def register(
if token:
data["WriteRequest"] = {"Token": token}
params.append(("token", token))
if replace_existing_checks:
params.append(("replace-existing-checks", "true"))
if node_meta:
for nodemeta_name, nodemeta_value in node_meta.items():
params.append(("node-meta", f"{nodemeta_name}:{nodemeta_value}"))
Expand Down

0 comments on commit daac265

Please sign in to comment.