/
query.py
301 lines (259 loc) · 10.6 KB
/
query.py
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
# -*- coding: utf-8 -*-
"""
:::info
The Query Service is a Palo Alto Networks cloud service which allows
for the storage and retrieval of data stored in the Cortex Data Lake.
Any type of textual data can be stored in the Cortex Data Lake. Palo
Alto Networks firewalls and software can write data to this service, as
can the software and services created by Palo Alto Network's various
partners.
:::
Examples:
Refer to the [examples provided with this library](https://github.com/PaloAltoNetworks/pan-cortex-data-lake-python/tree/master/examples).
"""
from __future__ import absolute_import
import logging
import time
from .exceptions import CortexError, HTTPError
from .httpclient import HTTPClient
from . import __version__
class QueryService(object):
"""A Cortex™ Query Service instance."""
def __init__(self, **kwargs):
"""
Parameters:
session (HTTPClient): [HTTPClient](httpclient.md#httpclient) object. Defaults to `None`.
url (str): URL to send API requests to. Later combined with `port` and `endpoint` parameter.
Args:
**kwargs: Supported [HTTPClient](httpclient.md#httpclient) parameters.
"""
self.kwargs = kwargs.copy() # used for __repr__
self.session = kwargs.pop("session", None)
self._httpclient = self.session or HTTPClient(**kwargs)
self._httpclient.stats.update(
{
"cancel_job": 0,
"create_query": 0,
"get_job": 0,
"list_jobs": 0,
"get_job_results": 0,
"records": 0,
}
)
self.stats = self._httpclient.stats
self.url = self._httpclient.url
self._debug = logging.getLogger(__name__).debug
def __repr__(self):
for k in self.kwargs.get("headers", {}):
if k.lower() == "authorization":
x = dict(self.kwargs["headers"].items())
x[k] = "*" * 6 # starrify token
return "{}({}, {})".format(
self.__class__.__name__,
", ".join(
"%s=%r" % (x, _)
for x, _ in self.kwargs.items()
if x != "headers"
),
"headers=%r" % x,
)
return "{}({})".format(
self.__class__.__name__, ", ".join("%s=%r" % x for x in self.kwargs.items())
)
def cancel_job(self, job_id=None, **kwargs):
"""Cancel a query job.
Args:
job_id (str): Specifies the ID of the query job.
**kwargs: Supported [HTTPClient.request()](httpclient.md#request) parameters.
Returns:
requests.Response: Requests [Response()](https://docs.python-requests.org/en/latest/api/#requests.Response) object.
Raises:
"""
endpoint = "/query/v2/jobs/{}".format(job_id)
r = self._httpclient.request(
method="DELETE", url=self.url, endpoint=endpoint, **kwargs
)
self.stats.cancel_job += 1
return r
def create_query(self, job_id=None, query_params=None, **kwargs):
"""Create a search request.
:::info
When submission is successful, http status code of `201` (Created)
is returned with a 'jobId' in response. Specifying a 'jobId' is
optional.
:::
Args:
job_id (str): Specifies the ID of the query job. (optional)
query_params (dict): Query parameters.
**kwargs: Supported [HTTPClient.request()](httpclient.md#request) parameters.
Returns:
requests.Response: Requests [Response()](https://docs.python-requests.org/en/latest/api/#requests.Response) object.
"""
json = kwargs.pop("json", {})
for name, value in [("jobId", job_id), ("params", query_params)]:
if value is not None:
json.update({name: value})
json.update(
{
"clientType": "cortex-data-lake-python",
"clientVersion": "%s" % __version__,
}
)
endpoint = "/query/v2/jobs"
r = self._httpclient.request(
method="POST", url=self.url, json=json, endpoint=endpoint, **kwargs
)
self.stats.create_query += 1
return r
def get_job(self, job_id=None, **kwargs):
"""Get specific job matching criteria.
Args:
job_id (str): Specifies the ID of the query job.
params (dict): Payload/request dictionary.
**kwargs: Supported [HTTPClient.request()](httpclient.md#request) parameters.
Returns:
requests.Response: Requests [Response()](https://docs.python-requests.org/en/latest/api/#requests.Response) object.
"""
endpoint = "/query/v2/jobs/{}".format(job_id)
r = self._httpclient.request(
method="GET", url=self.url, endpoint=endpoint, **kwargs
)
self.stats.get_job += 1
return r
def get_job_results(
self,
job_id=None,
max_wait=None,
offset=None,
page_cursor=None,
page_number=None,
page_size=None,
result_format=None,
**kwargs
):
"""Get results for a specific job_id.
Args:
job_id (str): Specifies the ID of the query job.
max_wait (int): How long to wait in ms for a job to complete. Max 2000.
offset (int): Along with pageSize, offset can be used to page through result set.
page_cursor (str): Token/handle that can be used to fetch more data.
page_number (int): Return the nth page from the result set as specified by this parameter.
page_size (int): If specified, limits the size of a batch of results to the specified value. If un-specified, backend picks a size that may provide best performance.
result_format (str): valuesArray or valuesDictionary.
**kwargs: Supported [HTTPClient.request()](httpclient.md#request) parameters.
Returns:
requests.Response: Requests [Response()](https://docs.python-requests.org/en/latest/api/#requests.Response) object.
"""
params = kwargs.pop("params", {})
for name, value in [
("maxWait", max_wait),
("offset", offset),
("pageCursor", page_cursor),
("pageNumber", page_number),
("pageSize", page_size),
("resultFormat", result_format),
]:
if value is not None:
params.update({name: value})
endpoint = "/query/v2/jobResults/{}".format(job_id)
r = self._httpclient.request(
method="GET", url=self.url, params=params, endpoint=endpoint, **kwargs
)
self.stats.get_job_results += 1
rows = r.json().get("rowsInPage")
if rows is not None:
self.stats.records += rows
return r
def iter_job_results(
self,
job_id=None,
max_wait=None,
offset=None,
page_cursor=None,
page_number=None,
page_size=None,
result_format=None,
**kwargs
):
"""Retrieve results iteratively in a non-greedy manner using scroll token.
Args:
job_id (str): Specifies the ID of the query job.
max_wait (int): How long to wait in ms for a job to complete. Max 2000.
offset (int): Along with pageSize, offset can be used to page through result set.
page_cursor (str): Token/handle that can be used to fetch more data.
page_number (int): Return the nth page from the result set as specified by this parameter.
page_size (int): If specified, limits the size of a batch of results to the specified value. If un-specified, backend picks a size that may provide best performance.
result_format (str): valuesArray or valuesJson.
**kwargs: Supported [HTTPClient.request()](httpclient.md#request) parameters.
Returns:
requests.Response: Requests [Response()](https://docs.python-requests.org/en/latest/api/#requests.Response) object.
"""
params = kwargs.pop("params", {})
enforce_json = kwargs.pop("enforce_json", True)
for name, value in [
("maxWait", max_wait),
("offset", offset),
("pageCursor", page_cursor),
("pageNumber", page_number),
("pageSize", page_size),
("resultFormat", result_format),
]:
if value is not None:
params.update({name: value})
while True:
r = self.get_job_results(
job_id=job_id, params=params, enforce_json=enforce_json, **kwargs
)
r_json = r.json()
if r_json["state"] == "DONE":
page_cursor = r_json["page"].get("pageCursor")
if page_cursor is not None:
params["pageCursor"] = page_cursor
yield r
else:
yield r
break
elif r_json["state"] in ("RUNNING", "PENDING"):
time.sleep(1)
continue
elif r_json["state"] == "FAILED":
yield r
break
else:
raise CortexError("Bad state: %s" % r_json["state"])
def list_jobs(
self,
max_jobs=None,
created_after=None,
state=None,
job_type=None,
tenant_id=None,
**kwargs
):
"""Get all jobs matching criteria.
Args:
limit (int): Max number of jobs.
created_after (int): List jobs created after this unix epoch UTC datetime.
state (str): Job state, e.g. 'RUNNING', 'PENDING', 'FAILED', 'DONE'.
job_type (str): Query type hint.
tenant_id (str): Tenant ID.
**kwargs: Supported [HTTPClient.request()](httpclient.md#request) parameters.
Returns:
requests.Response: Requests [Response()](https://docs.python-requests.org/en/latest/api/#requests.Response) object.
"""
params = kwargs.pop("params", {})
for name, value in [
("maxJobs", max_jobs),
("createdAfter", created_after),
("state", state),
("type", job_type),
("tenantId", tenant_id),
]:
if value is not None:
params.update({name: value})
endpoint = "/query/v2/jobs"
r = self._httpclient.request(
method="GET", url=self.url, params=params, endpoint=endpoint, **kwargs
)
self.stats.list_jobs += 1
return r