File: catalog.py

package info (click to toggle)
python-consul 1.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 484 kB
  • sloc: python: 2,858; makefile: 197
file content (389 lines) | stat: -rw-r--r-- 14,219 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
from __future__ import annotations

import json

from consul.callback import CB


class Catalog:
    def __init__(self, agent) -> None:
        self.agent = agent

    def register(self, node, address, service=None, check=None, dc=None, token: str | None = None, node_meta=None):
        """
        A low level mechanism for directly registering or updating entries
        in the catalog. It is usually recommended to use
        agent.service.register and agent.check.register, as they are
        simpler and perform anti-entropy.

        *node* is the name of the node to register.

        *address* is the ip of the node.

        *service* is an optional service to register. if supplied this is a
        dict::

            {
                "Service": "redis",
                "ID": "redis1",
                "Tags": [
                    "master",
                    "v1"
                ],
                "Port": 8000
            }

        where

            *Service* is required and is the name of the service

            *ID* is optional, and will be set to *Service* if not provided.
            Note *ID* must be unique for the given *node*.

            *Tags* and *Port* are optional.

        *check* is an optional check to register. if supplied this is a
        dict::

            {
                "Node": "foobar",
                "CheckID": "service:redis1",
                "Name": "Redis health check",
                "Notes": "Script based health check",
                "Status": "passing",
                "ServiceID": "redis1"
            }

        *dc* is the datacenter of the node and defaults to this agents
        datacenter.

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

        *node_meta* is an optional meta data used for filtering, a
        dictionary formatted as {k1:v1, k2:v2}.

        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>`_.

        Returns *True* on success.
        """
        data = {"node": node, "address": address}
        params = []
        dc = dc or self.agent.dc
        if dc:
            data["datacenter"] = dc
        if service:
            data["service"] = service
        if check:
            data["check"] = check
        token = token or self.agent.token
        if token:
            data["WriteRequest"] = {"Token": token}
            params.append(("token", token))
        if node_meta:
            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.put(
            CB.boolean(), "/v1/catalog/register", data=json.dumps(data), params=params, headers=headers
        )

    def deregister(self, node, service_id=None, check_id=None, dc=None, token: str | None = None):
        """
        A low level mechanism for directly removing entries in the catalog.
        It is usually recommended to use the agent APIs, as they are
        simpler and perform anti-entropy.

        *node* and *dc* specify which node on which datacenter to remove.
        If *service_id* and *check_id* are not provided, all associated
        services and checks are deleted. Otherwise only one of *service_id*
        and *check_id* should be provided and only that service or check
        will be removed.

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

        Returns *True* on success.
        """
        assert not (service_id and check_id)
        data = {"node": node}
        dc = dc or self.agent.dc
        if dc:
            data["datacenter"] = dc
        if service_id:
            data["serviceid"] = service_id
        if check_id:
            data["checkid"] = check_id
        token = token or self.agent.token
        if token:
            data["WriteRequest"] = {"Token": token}
        headers = self.agent.prepare_headers(token)
        return self.agent.http.put(CB.boolean(), "/v1/catalog/deregister", headers=headers, data=json.dumps(data))

    def datacenters(self):
        """
        Returns all the datacenters that are known by the Consul server.
        """
        return self.agent.http.get(CB.json(), "/v1/catalog/datacenters")

    def nodes(
        self, index=None, wait=None, consistency=None, dc=None, near=None, token: str | None = None, node_meta=None
    ):
        """
        Returns a tuple of (*index*, *nodes*) of all nodes known
        about in the *dc* datacenter. *dc* defaults to the current
        datacenter of this agent.

        *index* is the current Consul index, suitable for making subsequent
        calls to wait for changes since this query was last run.

        *wait* the maximum duration to wait (e.g. '10s') to retrieve
        a given index. this parameter is only applied if *index* is also
        specified. the wait time by default is 5 minutes.

        *near* is a node name to sort the resulting list in ascending
        order based on the estimated round trip time from that node

        *consistency* can be either 'default', 'consistent' or 'stale'. if
        not specified *consistency* will the consistency level this client
        was configured with.

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

        *node_meta* is an optional meta data used for filtering, a
        dictionary formatted as {k1:v1, k2:v2}.

        The response looks like this::

            (index, [
                {
                    "Node": "baz",
                    "Address": "10.1.10.11"
                },
                {
                    "Node": "foobar",
                    "Address": "10.1.10.12"
                }
            ])
        """
        params = []
        dc = dc or self.agent.dc
        if dc:
            params.append(("dc", dc))
        if index:
            params.append(("index", index))
            if wait:
                params.append(("wait", wait))
        if near:
            params.append(("near", near))

        consistency = consistency or self.agent.consistency
        if consistency in ("consistent", "stale"):
            params.append((consistency, "1"))
        if node_meta:
            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/nodes", params=params, headers=headers)

    def services(self, index=None, wait=None, consistency=None, dc=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
        datacenter of this agent.

        *index* is the current Consul index, suitable for making subsequent
        calls to wait for changes since this query was last run.

        *wait* the maximum duration to wait (e.g. '10s') to retrieve
        a given index. this parameter is only applied if *index* is also
        specified. the wait time by default is 5 minutes.

        *consistency* can be either 'default', 'consistent' or 'stale'. if
        not specified *consistency* will the consistency level this client
        was configured with.

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

        *node_meta* is an optional meta data used for filtering, a
        dictionary formatted as {k1:v1, k2:v2}.

        The response looks like this::

            (index, {
                "consul": [],
                "redis": [],
                "postgresql": [
                    "master",
                    "slave"
                ]
            })

        The main keys are the service names and the list provides all the
        known tags for a given service.
        """
        params = []
        dc = dc or self.agent.dc
        if dc:
            params.append(("dc", dc))
        if index:
            params.append(("index", index))
            if wait:
                params.append(("wait", wait))
        consistency = consistency or self.agent.consistency
        if consistency in ("consistent", "stale"):
            params.append((consistency, "1"))
        if node_meta:
            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)

    def node(self, node, index=None, wait=None, consistency=None, dc=None, token: str | None = None):
        """
        Returns a tuple of (*index*, *services*) of all services provided
        by *node*.

        *index* is the current Consul index, suitable for making subsequent
        calls to wait for changes since this query was last run.

        *wait* the maximum duration to wait (e.g. '10s') to retrieve
        a given index. this parameter is only applied if *index* is also
        specified. the wait time by default is 5 minutes.

        *consistency* can be either 'default', 'consistent' or 'stale'. if
        not specified *consistency* will the consistency level this client
        was configured with.

        *dc* is the datacenter of the node and defaults to this agents
        datacenter.

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

        The response looks like this::

            (index, {
                "Node": {
                    "Node": "foobar",
                    "Address": "10.1.10.12"
                },
                "Services": {
                    "consul": {
                        "ID": "consul",
                        "Service": "consul",
                        "Tags": null,
                        "Port": 8300
                    },
                    "redis": {
                        "ID": "redis",
                        "Service": "redis",
                        "Tags": [
                            "v1"
                        ],
                        "Port": 8000
                    }
                }
            })
        """
        params = []
        dc = dc or self.agent.dc
        if dc:
            params.append(("dc", dc))
        if index:
            params.append(("index", index))
            if wait:
                params.append(("wait", wait))
        consistency = consistency or self.agent.consistency
        if consistency in ("consistent", "stale"):
            params.append((consistency, "1"))
        headers = self.agent.prepare_headers(token)
        return self.agent.http.get(CB.json(index=True), f"/v1/catalog/node/{node}", params=params, headers=headers)

    def _service(
        self,
        internal_uri,
        index=None,
        wait=None,
        tag=None,
        consistency=None,
        dc=None,
        near=None,
        token: str | None = None,
        node_meta=None,
    ):
        params = []
        dc = dc or self.agent.dc
        if dc:
            params.append(("dc", dc))
        if tag:
            params.append(("tag", tag))
        if index:
            params.append(("index", index))
            if wait:
                params.append(("wait", wait))
        if near:
            params.append(("near", near))
        consistency = consistency or self.agent.consistency
        if consistency in ("consistent", "stale"):
            params.append((consistency, "1"))
        if node_meta:
            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), internal_uri, params=params, headers=headers)

    def service(self, service: str, **kwargs):
        """
        Returns a tuple of (*index*, *nodes*) of the nodes providing
        *service* in the *dc* datacenter. *dc* defaults to the current
        datacenter of this agent.

        *index* is the current Consul index, suitable for making subsequent
        calls to wait for changes since this query was last run.

        *wait* the maximum duration to wait (e.g. '10s') to retrieve
        a given index. this parameter is only applied if *index* is also
        specified. the wait time by default is 5 minutes.

        If *tag* is provided, the list of nodes returned will be filtered
        by that tag.

        *near* is a node name to sort the resulting list in ascending
        order based on the estimated round trip time from that node

        *consistency* can be either 'default', 'consistent' or 'stale'. if
        not specified *consistency* will the consistency level this client
        was configured with.

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

        *node_meta* is an optional meta data used for filtering, a
        dictionary formatted as {k1:v1, k2:v2}.

        The response looks like this::

            (index, [
                {
                    "Node": "foobar",
                    "Address": "10.1.10.12",
                    "ServiceID": "redis",
                    "ServiceName": "redis",
                    "ServiceTags": null,
                    "ServicePort": 8000
                }
            ])
        """
        internal_uri = f"/v1/catalog/service/{service}"
        return self._service(internal_uri=internal_uri, **kwargs)

    def connect(self, service: str, **kwargs):
        """
        Returns a tuple of (*index*, *nodes*) of the nodes providing
        connect-capable *service* in the *dc* datacenter. *dc* defaults
        to the current datacenter of this agent.

        Request arguments and response format are the same as catalog.service
        """
        internal_uri = f"/v1/catalog/connect/{service}"
        return self._service(internal_uri=internal_uri, **kwargs)