Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Oct 1, 2024
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
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