| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from consul.callback import CB |
| 4 |
|
| 5 |
|
| 6 |
class Health: |
| 7 |
|
| 8 |
def __init__(self, agent) -> None: |
| 9 |
self.agent = agent |
| 10 |
|
| 11 |
def _service( |
| 12 |
self, |
| 13 |
internal_uri, |
| 14 |
index=None, |
| 15 |
wait=None, |
| 16 |
passing=None, |
| 17 |
tag=None, |
| 18 |
dc=None, |
| 19 |
near=None, |
| 20 |
token: str | None = None, |
| 21 |
node_meta=None, |
| 22 |
): |
| 23 |
params = [] |
| 24 |
if index: |
| 25 |
params.append(("index", index)) |
| 26 |
if wait: |
| 27 |
params.append(("wait", wait)) |
| 28 |
if passing: |
| 29 |
params.append(("passing", "1")) |
| 30 |
if tag is not None: |
| 31 |
if not isinstance(tag, list): |
| 32 |
tag = [tag] |
| 33 |
for tag_item in tag: |
| 34 |
params.append(("tag", tag_item)) |
| 35 |
dc = dc or self.agent.dc |
| 36 |
if dc: |
| 37 |
params.append(("dc", dc)) |
| 38 |
if near: |
| 39 |
params.append(("near", near)) |
| 40 |
if node_meta: |
| 41 |
for nodemeta_name, nodemeta_value in node_meta.items(): |
| 42 |
params.append(("node-meta", f"{nodemeta_name}:{nodemeta_value}")) |
| 43 |
headers = self.agent.prepare_headers(token) |
| 44 |
return self.agent.http.get(CB.json(index=True), internal_uri, params=params, headers=headers) |
| 45 |
|
| 46 |
def service(self, service: str, **kwargs): |
| 47 |
""" |
| 48 |
Returns a tuple of (*index*, *nodes*) |
| 49 |
|
| 50 |
*index* is the current Consul index, suitable for making subsequent |
| 51 |
calls to wait for changes since this query was last run. |
| 52 |
|
| 53 |
*wait* the maximum duration to wait (e.g. '10s') to retrieve |
| 54 |
a given index. this parameter is only applied if *index* is also |
| 55 |
specified. the wait time by default is 5 minutes. |
| 56 |
|
| 57 |
*nodes* are the nodes providing the given service. |
| 58 |
|
| 59 |
Calling with *passing* set to True will filter results to only |
| 60 |
those nodes whose checks are currently passing. |
| 61 |
|
| 62 |
Calling with *tag* will filter the results by tag, multiple tags |
| 63 |
using list possible. |
| 64 |
|
| 65 |
*dc* is the datacenter of the node and defaults to this agents |
| 66 |
datacenter. |
| 67 |
|
| 68 |
*near* is a node name to sort the resulting list in ascending |
| 69 |
order based on the estimated round trip time from that node |
| 70 |
|
| 71 |
*token* is an optional `ACL token`_ to apply to this request. |
| 72 |
|
| 73 |
*node_meta* is an optional meta data used for filtering, a |
| 74 |
dictionary formatted as {k1:v1, k2:v2}. |
| 75 |
""" |
| 76 |
internal_uri = f"/v1/health/service/{service}" |
| 77 |
return self._service(internal_uri=internal_uri, **kwargs) |
| 78 |
|
| 79 |
def connect(self, service, **kwargs): |
| 80 |
""" |
| 81 |
Returns a tuple of (*index*, *nodes*) of the nodes providing |
| 82 |
connect-capable *service* in the *dc* datacenter. *dc* defaults |
| 83 |
to the current datacenter of this agent. |
| 84 |
|
| 85 |
Request arguments and response format are the same as health.service |
| 86 |
""" |
| 87 |
internal_uri = f"/v1/health/connect/{service}" |
| 88 |
return self._service(internal_uri=internal_uri, **kwargs) |
| 89 |
|
| 90 |
def checks(self, service, index=None, wait=None, dc=None, near=None, token: str | None = None, node_meta=None): |
| 91 |
""" |
| 92 |
Returns a tuple of (*index*, *checks*) with *checks* being the |
| 93 |
checks associated with the service. |
| 94 |
|
| 95 |
*service* is the name of the service being checked. |
| 96 |
|
| 97 |
*index* is the current Consul index, suitable for making subsequent |
| 98 |
calls to wait for changes since this query was last run. |
| 99 |
|
| 100 |
*wait* the maximum duration to wait (e.g. '10s') to retrieve |
| 101 |
a given index. this parameter is only applied if *index* is also |
| 102 |
specified. the wait time by default is 5 minutes. |
| 103 |
|
| 104 |
*dc* is the datacenter of the node and defaults to this agents |
| 105 |
datacenter. |
| 106 |
|
| 107 |
*near* is a node name to sort the resulting list in ascending |
| 108 |
order based on the estimated round trip time from that node |
| 109 |
|
| 110 |
*token* is an optional `ACL token`_ to apply to this request. |
| 111 |
|
| 112 |
*node_meta* is an optional meta data used for filtering, a |
| 113 |
dictionary formatted as {k1:v1, k2:v2}. |
| 114 |
""" |
| 115 |
params = [] |
| 116 |
if index: |
| 117 |
params.append(("index", index)) |
| 118 |
if wait: |
| 119 |
params.append(("wait", wait)) |
| 120 |
dc = dc or self.agent.dc |
| 121 |
if dc: |
| 122 |
params.append(("dc", dc)) |
| 123 |
if near: |
| 124 |
params.append(("near", near)) |
| 125 |
if node_meta: |
| 126 |
for nodemeta_name, nodemeta_value in node_meta.items(): |
| 127 |
params.append(("node-meta", f"{nodemeta_name}:{nodemeta_value}")) |
| 128 |
headers = self.agent.prepare_headers(token) |
| 129 |
return self.agent.http.get(CB.json(index=True), f"/v1/health/checks/{service}", params=params, headers=headers) |
| 130 |
|
| 131 |
def state(self, name: str, index=None, wait=None, dc=None, near=None, token: str | None = None, node_meta=None): |
| 132 |
""" |
| 133 |
Returns a tuple of (*index*, *nodes*) |
| 134 |
|
| 135 |
*name* is a supported state. From the Consul docs: |
| 136 |
|
| 137 |
The supported states are any, unknown, passing, warning, or |
| 138 |
critical. The any state is a wildcard that can be used to |
| 139 |
return all checks. |
| 140 |
|
| 141 |
*index* is the current Consul index, suitable for making subsequent |
| 142 |
calls to wait for changes since this query was last run. |
| 143 |
|
| 144 |
*wait* the maximum duration to wait (e.g. '10s') to retrieve |
| 145 |
a given index. this parameter is only applied if *index* is also |
| 146 |
specified. the wait time by default is 5 minutes. |
| 147 |
|
| 148 |
*dc* is the datacenter of the node and defaults to this agents |
| 149 |
datacenter. |
| 150 |
|
| 151 |
*near* is a node name to sort the resulting list in ascending |
| 152 |
order based on the estimated round trip time from that node |
| 153 |
|
| 154 |
*token* is an optional `ACL token`_ to apply to this request. |
| 155 |
|
| 156 |
*node_meta* is an optional meta data used for filtering, a |
| 157 |
dictionary formatted as {k1:v1, k2:v2}. |
| 158 |
|
| 159 |
*nodes* are the nodes providing the given service. |
| 160 |
""" |
| 161 |
assert name in ["any", "unknown", "passing", "warning", "critical"] |
| 162 |
params = [] |
| 163 |
if index: |
| 164 |
params.append(("index", index)) |
| 165 |
if wait: |
| 166 |
params.append(("wait", wait)) |
| 167 |
dc = dc or self.agent.dc |
| 168 |
if dc: |
| 169 |
params.append(("dc", dc)) |
| 170 |
if near: |
| 171 |
params.append(("near", near)) |
| 172 |
if node_meta: |
| 173 |
for nodemeta_name, nodemeta_value in node_meta.items(): |
| 174 |
params.append(("node-meta", f"{nodemeta_name}:{nodemeta_value}")) |
| 175 |
headers = self.agent.prepare_headers(token) |
| 176 |
return self.agent.http.get(CB.json(index=True), f"/v1/health/state/{name}", params=params, headers=headers) |
| 177 |
|
| 178 |
def node(self, node, index=None, wait=None, dc=None, token: str | None = None): |
| 179 |
""" |
| 180 |
Returns a tuple of (*index*, *checks*) |
| 181 |
|
| 182 |
*index* is the current Consul index, suitable for making subsequent |
| 183 |
calls to wait for changes since this query was last run. |
| 184 |
|
| 185 |
*wait* the maximum duration to wait (e.g. '10s') to retrieve |
| 186 |
a given index. this parameter is only applied if *index* is also |
| 187 |
specified. the wait time by default is 5 minutes. |
| 188 |
|
| 189 |
*dc* is the datacenter of the node and defaults to this agents |
| 190 |
datacenter. |
| 191 |
|
| 192 |
*token* is an optional `ACL token`_ to apply to this request. |
| 193 |
|
| 194 |
*nodes* are the nodes providing the given service. |
| 195 |
""" |
| 196 |
params = [] |
| 197 |
if index: |
| 198 |
params.append(("index", index)) |
| 199 |
if wait: |
| 200 |
params.append(("wait", wait)) |
| 201 |
dc = dc or self.agent.dc |
| 202 |
if dc: |
| 203 |
params.append(("dc", dc)) |
| 204 |
|
| 205 |
headers = self.agent.prepare_headers(token) |
| 206 |
return self.agent.http.get(CB.json(index=True), f"/v1/health/node/{node}", params=params, headers=headers) |