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 tag filter to Catalog.services #92

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
15 changes: 13 additions & 2 deletions consul/api/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def nodes(
headers = self.agent.prepare_headers(token)
return self.agent.http.get(CB.json(index=True), "/v1/catalog/nodes", params=params, headers=headers)

def services(self, index=None, wait=None, consistency=None, dc=None, token: str | None = None, node_meta=None):
def services(self, index=None, wait=None, consistency=None, dc=None, tag=None, token: str | None = None, node_meta=None):
"""
Returns a tuple of (*index*, *services*) of all services known
about in the *dc* datacenter. *dc* defaults to the current
Expand All @@ -204,6 +204,9 @@ def services(self, index=None, wait=None, consistency=None, dc=None, token: str
not specified *consistency* will the consistency level this client
was configured with.

If *tag* is provided, the list of services returned will be filtered
on that tag.

*token* is an optional `ACL token`_ to apply to this request.

*node_meta* is an optional meta data used for filtering, a
Expand All @@ -224,6 +227,7 @@ def services(self, index=None, wait=None, consistency=None, dc=None, token: str
known tags for a given service.
"""
params = []
results = {}
dc = dc or self.agent.dc
if dc:
params.append(("dc", dc))
Expand All @@ -238,7 +242,14 @@ def services(self, index=None, wait=None, consistency=None, dc=None, token: str
for nodemeta_name, nodemeta_value in node_meta.items():
params.append(("node-meta", f"{nodemeta_name}:{nodemeta_value}"))
headers = self.agent.prepare_headers(token)
return self.agent.http.get(CB.json(index=True), "/v1/catalog/services", params=params, headers=headers)
index, consul_services = self.agent.http.get(CB.json(index=True), "/v1/catalog/services", params=params, headers=headers)
if tag:
for key in consul_services:
for consul_tag in consul_services[key]:
if consul_tag == tag:
results[key] = consul_services[key]
return (index, results)
return (index, consul_services)

def node(self, node, index=None, wait=None, consistency=None, dc=None, token: str | None = None):
"""
Expand Down
Loading