Skip to content

Commit

Permalink
feat: add replace_existing_checks option to Catalog.register()
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
cpaillet committed Sep 27, 2024
1 parent e2b1dbb commit bc610fd
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion consul/api/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,17 @@ 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):
def register(
self,
node,
address,
service=None,
check=None,
dc=None,
token=None,
node_meta=None,
replace_existing_checks=False,
):
"""
A low level mechanism for directly registering or updating entries
in the catalog. It is usually recommended to use
Expand Down Expand Up @@ -60,6 +70,11 @@ def register(self, node, address, service=None, check=None, dc=None, token=None,
*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 @@ -79,6 +94,8 @@ def register(self, node, address, service=None, check=None, dc=None, token=None,
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 bc610fd

Please sign in to comment.