Debian Codegraph @

consul/api/query.py

raw symbols SCIP
1 from __future__ import annotations
2
3 import json
4 from typing import Optional
5
6 from consul.callback import CB
7
8
9 class Query:
10 def __init__(self, agent) -> None:
11 self.agent = agent
12
13 def list(self, dc=None, token: str | None = None):
14 """
15 Lists all the active queries. This is a privileged endpoint,
16 therefore you will only be able to get the prepared queries
17 which the token supplied has read privileges to.
18
19 *dc* is the datacenter that this agent will communicate with. By
20 default the datacenter of the host is used.
21
22 *token* is an optional `ACL token`_ to apply to this request.
23 """
24 params = []
25 if dc:
26 params.append(("dc", dc))
27
28 headers = self.agent.prepare_headers(token)
29 return self.agent.http.get(CB.json(), "/v1/query", params=params, headers=headers)
30
31 def _query_data(
32 self,
33 service=None,
34 name: Optional[str] = None,
35 session=None,
36 token: str | None = None,
37 nearestn=None,
38 datacenters=None,
39 onlypassing=None,
40 tags=None,
41 ttl: Optional[int] = None,
42 regexp=None,
43 ):
44 service_body = {
45 k: v
46 for k, v in {
47 "service": service,
48 "onlypassing": onlypassing,
49 "tags": tags,
50 "failover": {
51 k: v for k, v in {"nearestn": nearestn, "datacenters": datacenters}.items() if v is not None
52 },
53 }.items()
54 if v is not None
55 }
56
57 data = {
58 k: v
59 for k, v in {
60 "name": name,
61 "session": session,
62 "token": token or self.agent.token,
63 "dns": {"ttl": ttl} if ttl is not None else None,
64 "template": {k: v for k, v in {"type": "name_prefix_match", "regexp": regexp}.items() if v is not None},
65 "service": service_body,
66 }.items()
67 if v is not None
68 }
69 return json.dumps(data)
70
71 def create(
72 self,
73 service,
74 name: Optional[str] = None,
75 dc=None,
76 session=None,
77 token: str | None = None,
78 nearestn=None,
79 datacenters=None,
80 onlypassing=None,
81 tags=None,
82 ttl: Optional[int] = None,
83 regexp=None,
84 ):
85 """
86 Creates a new query. This is a privileged endpoint, and
87 requires a management token for a certain query name.*token* will
88 override this client's default token.
89
90 *service* is mandatory for new query. represent service name to
91 query.
92
93 *name* is an optional name for this query.
94
95 *dc* is the datacenter that this agent will communicate with. By
96 default the datacenter of the host is used.
97
98 *token* is an optional `ACL token`_ to apply to this request.
99
100 *nearestn* if set to a value greater than zero, then the query will
101 be forwarded to up to NearestN other datacenters based on their
102 estimated network round trip time using Network Coordinates from
103 the WAN gossip pool.
104
105 *datacenters* is a fixed list of remote datacenters to forward the
106 query to if there are no healthy nodes in the local datacenter.
107
108 *onlypassing* controls the behavior of the query's health check
109 filtering.
110
111 *tags* is a list of service tags to filter the query results.
112
113 *ttl* is a duration string that can use "s" as a suffix for
114 seconds. It controls how the TTL is set when query results are
115 served over DNS.
116
117 *regexp* is optional for template this option is only supported
118 in Consul 0.6.4 or later. The only option for type is
119 name_prefix_match so if you want a query template with no regexp
120 enter an empty string.
121
122 For more information about query
123 https://www.consul.io/docs/agent/http/query.html
124 """
125 path = "/v1/query"
126 params = None if dc is None else [("dc", dc)]
127 data = self._query_data(service, name, session, token, nearestn, datacenters, onlypassing, tags, ttl, regexp)
128 return self.agent.http.post(CB.json(), path, params=params, data=data)
129
130 def update(
131 self,
132 query_id,
133 service=None,
134 name: Optional[str] = None,
135 dc=None,
136 session=None,
137 token: str | None = None,
138 nearestn=None,
139 datacenters=None,
140 onlypassing=None,
141 tags=None,
142 ttl: Optional[int] = None,
143 regexp=None,
144 ):
145 """
146 This endpoint will update a certain query
147
148 *query_id* is the query id for update
149
150 all the other setting remains the same as the query create method
151 """
152 path = f"/v1/query/{query_id}"
153 params = None if dc is None else [("dc", dc)]
154 data = self._query_data(service, name, session, token, nearestn, datacenters, onlypassing, tags, ttl, regexp)
155 return self.agent.http.put(CB.boolean(), path, params=params, data=data)
156
157 def get(self, query_id, token: str | None = None, dc=None):
158 """
159 This endpoint will return information about a certain query
160
161 *query_id* the query id to retrieve information about
162
163 *token* is an optional `ACL token`_ to apply to this request.
164
165 *dc* is the datacenter that this agent will communicate with. By
166 default the datacenter of the host is used.
167 """
168 params = []
169 if dc:
170 params.append(("dc", dc))
171 headers = self.agent.prepare_headers(token)
172 return self.agent.http.get(CB.json(), f"/v1/query/{query_id}", params=params, headers=headers)
173
174 def delete(self, query_id, token: str | None = None, dc=None):
175 """
176 This endpoint will delete certain query
177
178 *query_id* the query id delete
179
180 *token* is an optional `ACL token`_ to apply to this request.
181
182 *dc* is the datacenter that this agent will communicate with. By
183 default the datacenter of the host is used.
184 """
185 params = []
186 if dc:
187 params.append(("dc", dc))
188 headers = self.agent.prepare_headers(token)
189 return self.agent.http.delete(CB.boolean(), f"/v1/query/{query_id}", params=params, headers=headers)
190
191 def execute(self, query, token: str | None = None, dc=None, near=None, limit: Optional[int] = None):
192 """
193 This endpoint will execute certain query
194
195 *query* name or query id to execute
196
197 *token* is an optional `ACL token`_ to apply to this request.
198
199 *dc* is the datacenter that this agent will communicate with. By
200 default the datacenter of the host is used.
201
202 *near* is a node name to sort the resulting list in ascending
203 order based on the estimated round trip time from that node
204
205 *limit* is used to limit the size of the list to the given number
206 of nodes. This is applied after any sorting or shuffling.
207 """
208 params = []
209 if dc:
210 params.append(("dc", dc))
211 if near:
212 params.append(("near", near))
213 if limit:
214 params.append(("limit", limit))
215 headers = self.agent.prepare_headers(token)
216 return self.agent.http.get(CB.json(), f"/v1/query/{query}/execute", params=params, headers=headers)
217
218 def explain(self, query, token: str | None = None, dc=None):
219 """
220 This endpoint shows a fully-rendered query for a given name
221
222 *query* name to explain. This cannot be query id.
223
224 *token* is an optional `ACL token`_ to apply to this request.
225
226 *dc* is the datacenter that this agent will communicate with. By
227 default the datacenter of the host is used.
228 """
229 params = []
230 if dc:
231 params.append(("dc", dc))
232 headers = self.agent.prepare_headers(token)
233 return self.agent.http.get(CB.json(), f"/v1/query/{query}/explain", params=params, headers=headers)