| 1 | from consul.callback import CB |
| 2 | |
| 3 | |
| 4 | class Coordinate: |
| 5 | def __init__(self, agent) -> None: |
| 6 | self.agent = agent |
| 7 | |
| 8 | def datacenters(self): |
| 9 | """ |
| 10 | Returns the WAN network coordinates for all Consul servers, |
| 11 | organized by DCs. |
| 12 | """ |
| 13 | return self.agent.http.get(CB.json(), "/v1/coordinate/datacenters") |
| 14 | |
| 15 | def nodes(self, dc=None, index=None, wait=None, consistency=None): |
| 16 | """ |
| 17 | *dc* is the datacenter that this agent will communicate with. By |
| 18 | default the datacenter of the host is used. |
| 19 | |
| 20 | *index* is the current Consul index, suitable for making subsequent |
| 21 | calls to wait for changes since this query was last run. |
| 22 | |
| 23 | *wait* the maximum duration to wait (e.g. '10s') to retrieve |
| 24 | a given index. this parameter is only applied if *index* is also |
| 25 | specified. the wait time by default is 5 minutes. |
| 26 | |
| 27 | *consistency* can be either 'default', 'consistent' or 'stale'. if |
| 28 | not specified *consistency* will the consistency level this client |
| 29 | was configured with. |
| 30 | """ |
| 31 | params = [] |
| 32 | if dc: |
| 33 | params.append(("dc", dc)) |
| 34 | if index: |
| 35 | params.append(("index", index)) |
| 36 | if wait: |
| 37 | params.append(("wait", wait)) |
| 38 | consistency = consistency or self.agent.consistency |
| 39 | if consistency in ("consistent", "stale"): |
| 40 | params.append((consistency, "1")) |
| 41 | return self.agent.http.get(CB.json(index=True), "/v1/coordinate/nodes", params=params) |