Skip to content

Commit ac706f7

Browse files
committed
Added Output mode JSON support for jobs.create
1 parent e162cc0 commit ac706f7

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

splunklib/client.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,12 @@ def _load_atom_entries(response):
226226

227227

228228
# Load the sid from the body of the given response
229-
def _load_sid(response):
230-
return _load_atom(response).response.sid
229+
def _load_sid(response, output_mode):
230+
if output_mode == "json":
231+
json_obj = json.loads(response.body.read())
232+
return json_obj.get('sid')
233+
else:
234+
return _load_atom(response).response.sid
231235

232236

233237
# Parse the given atom entry record into a generic entity state record
@@ -2968,7 +2972,7 @@ def create(self, query, **kwargs):
29682972
if kwargs.get("exec_mode", None) == "oneshot":
29692973
raise TypeError("Cannot specify exec_mode=oneshot; use the oneshot method instead.")
29702974
response = self.post(search=query, **kwargs)
2971-
sid = _load_sid(response)
2975+
sid = _load_sid(response, kwargs.get("output_mode", None))
29722976
return Job(self.service, sid)
29732977

29742978
def export(self, query, **params):
@@ -3184,7 +3188,7 @@ def dispatch(self, **kwargs):
31843188
:return: The :class:`Job`.
31853189
"""
31863190
response = self.post("dispatch", **kwargs)
3187-
sid = _load_sid(response)
3191+
sid = _load_sid(response, kwargs.get("output_mode", None))
31883192
return Job(self.service, sid)
31893193

31903194
@property

tests/test_job.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ def test_service_search(self):
4848
self.assertTrue(job.sid in self.service.jobs)
4949
job.cancel()
5050

51+
def test_create_job_with_output_mode_json(self):
52+
job = self.service.jobs.create(query='search index=_internal earliest=-1m | head 3', output_mode='json')
53+
self.assertTrue(job.sid in self.service.jobs)
54+
job.cancel()
55+
5156
def test_oneshot_with_garbage_fails(self):
5257
jobs = self.service.jobs
5358
self.assertRaises(TypeError, jobs.create, "abcd", exec_mode="oneshot")

0 commit comments

Comments
 (0)