Skip to content

Commit d6d0f81

Browse files
author
Jeremy Cook
committed
[DVPL-10898] Use the v2 endpoint of search where applicable
1 parent 4de603b commit d6d0f81

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

splunklib/client.py

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
PATH_INDEXES = "data/indexes/"
100100
PATH_INPUTS = "data/inputs/"
101101
PATH_JOBS = "search/jobs/"
102+
PATH_JOBS_V2 = "search/v2/jobs/"
102103
PATH_LOGGER = "/services/server/logger/"
103104
PATH_MESSAGES = "messages/"
104105
PATH_MODULAR_INPUTS = "data/modular-inputs"
@@ -2658,10 +2659,10 @@ def oneshot(self, path, **kwargs):
26582659

26592660

26602661
class Job(Entity):
2662+
26612663
"""This class represents a search job."""
26622664
def __init__(self, service, sid, **kwargs):
2663-
path = PATH_JOBS + sid
2664-
Entity.__init__(self, service, path, skip_refresh=True, **kwargs)
2665+
Entity.__init__(self, service, '', skip_refresh=True, **kwargs)
26652666
self.sid = sid
26662667

26672668
# The Job entry record is returned at the root of the response
@@ -2714,8 +2715,13 @@ def events(self, **kwargs):
27142715
:return: The ``InputStream`` IO handle to this job's events.
27152716
"""
27162717
kwargs['segmentation'] = kwargs.get('segmentation', 'none')
2717-
kwargs.pop('search', None)
2718-
return self.get("events", **kwargs).body
2718+
path = "{path}{sid}/events"
2719+
2720+
# Splunk version doesn't support v2 (pre-9.0) or the 'search' arg is included (which is v1 specific)
2721+
if self.splunk_version < (9,) or 'search' in kwargs:
2722+
return self.get(path.format(PATH_JOBS, self.sid), **kwargs).body
2723+
else:
2724+
return self.get(path.format(PATH_JOBS_V2, self.sid), **kwargs).body
27192725

27202726
def finalize(self):
27212727
"""Stops the job and provides intermediate results for retrieval.
@@ -2803,8 +2809,13 @@ def results(self, **query_params):
28032809
:return: The ``InputStream`` IO handle to this job's results.
28042810
"""
28052811
query_params['segmentation'] = query_params.get('segmentation', 'none')
2806-
query_params.pop('search', None)
2807-
return self.get("results", **query_params).body
2812+
path = "{path}{sid}/results"
2813+
2814+
# Splunk version doesn't support v2 (pre-9.0) or the 'search' arg is included (which is v1 specific)
2815+
if self.splunk_version < (9,) or 'search' in query_params:
2816+
return self.get(path.format(PATH_JOBS, self.sid), **query_params).body
2817+
else:
2818+
return self.get(path.format(PATH_JOBS_V2, self.sid), **query_params).body
28082819

28092820
def preview(self, **query_params):
28102821
"""Returns a streaming handle to this job's preview search results.
@@ -2845,8 +2856,13 @@ def preview(self, **query_params):
28452856
:return: The ``InputStream`` IO handle to this job's preview results.
28462857
"""
28472858
query_params['segmentation'] = query_params.get('segmentation', 'none')
2848-
query_params.pop('search', None)
2849-
return self.get("results_preview", **query_params).body
2859+
path = "{path}{sid}/results_preview"
2860+
2861+
# Splunk version doesn't support v2 (pre-9.0) or the 'search' arg is included (which is v1 specific)
2862+
if self.splunk_version < (9,) or 'search' in query_params:
2863+
return self.get(path.format(PATH_JOBS, self.sid), **query_params).body
2864+
else:
2865+
return self.get(path.format(PATH_JOBS_V2, self.sid), **query_params).body
28502866

28512867
def searchlog(self, **kwargs):
28522868
"""Returns a streaming handle to this job's search log.
@@ -2935,7 +2951,12 @@ class Jobs(Collection):
29352951
"""This class represents a collection of search jobs. Retrieve this
29362952
collection using :meth:`Service.jobs`."""
29372953
def __init__(self, service):
2938-
Collection.__init__(self, service, PATH_JOBS, item=Job)
2954+
# Splunk 9 introduces the v2 endpoint
2955+
if self.splunk_version >= (9,):
2956+
path = PATH_JOBS_V2
2957+
else:
2958+
path = PATH_JOBS
2959+
Collection.__init__(self, service, path, item=Job)
29392960
# The count value to say list all the contents of this
29402961
# Collection is 0, not -1 as it is on most.
29412962
self.null_count = 0

0 commit comments

Comments
 (0)