Skip to content

Commit f320e5e

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents 9e63321 + e5a61b5 commit f320e5e

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

qpylib/ariel.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,37 @@ def results(self, search_id, start=0, end=0, api_version='latest'):
108108
.format(search_id, response.content))
109109
return response.json()
110110

111+
def delete(self, search_id, api_version='latest'):
112+
''' Deletes a previous Ariel search.
113+
search_id: Ariel search ID
114+
api_version: QRadar API version to use, defaults to latest.
115+
Returns a tuple containing search status and search ID.
116+
Raises ArielError if the search could not be deleted.
117+
'''
118+
response = qpylib.REST('DELETE', ArielSearch.SEARCH_ENDPOINT.format(search_id),
119+
headers=self._build_headers(api_version))
120+
if response.status_code != 200:
121+
raise ArielError('Ariel search {0} could not be deleted: HTTP {1} was returned'
122+
.format(search_id, response.status_code))
123+
response_json = response.json()
124+
return (response_json['status'], response_json['search_id'])
125+
126+
def cancel(self, search_id, api_version='latest'):
127+
''' Cancels an ongoing Ariel search.
128+
search_id: Ariel search ID.
129+
api_version: QRadar API version to use, defaults to latest.
130+
Returns a tuple containing search status and search ID.
131+
Raises ArielError if the search could not be cancelled.
132+
'''
133+
response = qpylib.REST('POST', ArielSearch.SEARCH_ENDPOINT.format(search_id),
134+
headers=self._build_headers(api_version),
135+
params={'status': 'CANCELLED'})
136+
if response.status_code != 200:
137+
raise ArielError('Ariel search {0} could not be cancelled: HTTP {1} was returned'
138+
.format(search_id, response.status_code))
139+
response_json = response.json()
140+
return (response_json['status'], response_json['search_id'])
141+
111142
@staticmethod
112143
def _build_headers(api_version):
113144
headers = {'Accept': 'application/json'}

test/test_ariel.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
POST_SEARCH = '{0}?query_expression=select+stuff+from+db'.format(ARIEL_URL)
1414
GET_SEARCH = '{0}/{1}'.format(ARIEL_URL, SEARCH_ID)
1515
GET_RESULTS = '{0}/{1}/results'.format(ARIEL_URL, SEARCH_ID)
16+
DELETE_SEARCH = '{0}/{1}'.format(ARIEL_URL, SEARCH_ID)
17+
CANCEL_SEARCH = '{0}/{1}?status=CANCELLED'.format(ARIEL_URL, SEARCH_ID)
1618

1719
@pytest.fixture(scope='module', autouse=True)
1820
def pre_testing_setup():
@@ -153,3 +155,37 @@ def test_results_range_start_and_end():
153155
assert results_json['result3'] == 42
154156
assert results_json['result4'] == 99
155157
assert responses.calls[0].request.headers['Range'] == 'items=3-4'
158+
159+
@responses.activate
160+
def test_search_delete_failure():
161+
responses.add('DELETE', DELETE_SEARCH, status=404,
162+
json={})
163+
with pytest.raises(ArielError, match='Ariel search {0} could not be deleted'
164+
.format(SEARCH_ID)):
165+
ArielSearch().delete(SEARCH_ID)
166+
167+
@responses.activate
168+
def test_search_delete_success():
169+
responses.add('DELETE', DELETE_SEARCH, status=200,
170+
json={'status': 'COMPLETED', 'search_id': SEARCH_ID})
171+
status, search_id = ArielSearch().delete(SEARCH_ID)
172+
173+
assert status == 'COMPLETED'
174+
assert search_id == SEARCH_ID
175+
176+
@responses.activate
177+
def test_search_cancel_failure():
178+
responses.add('POST', CANCEL_SEARCH, status=404,
179+
json={})
180+
with pytest.raises(ArielError, match='Ariel search {0} could not be cancelled'
181+
.format(SEARCH_ID)):
182+
ArielSearch().cancel(SEARCH_ID)
183+
184+
@responses.activate
185+
def test_search_cancel_success():
186+
responses.add('POST', CANCEL_SEARCH, status=200,
187+
json={'status': 'COMPLETED', 'search_id': SEARCH_ID})
188+
status, search_id = ArielSearch().cancel(SEARCH_ID)
189+
190+
assert status == 'COMPLETED'
191+
assert search_id == SEARCH_ID

0 commit comments

Comments
 (0)