Skip to content

Commit

Permalink
update version checks compatible with cloud versions
Browse files Browse the repository at this point in the history
  • Loading branch information
ashah-splunk committed Sep 12, 2022
1 parent 6a24337 commit 137a0ef
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
35 changes: 29 additions & 6 deletions splunklib/client.py
Expand Up @@ -421,6 +421,7 @@ def __init__(self, **kwargs):
super(Service, self).__init__(**kwargs)
self._splunk_version = None
self._kvstore_owner = None
self._instance_type = None

@property
def apps(self):
Expand Down Expand Up @@ -572,7 +573,8 @@ def parse(self, query, **kwargs):
:type kwargs: ``dict``
:return: A semantic map of the parsed search query.
"""
if self.splunk_version >= (9,0,2):
# if self.splunk_version >= (9,0,2):
if not self.disable_v2_api:
return self.post("search/v2/parser", q=query, **kwargs)
return self.get("search/parser", q=query, **kwargs)

Expand Down Expand Up @@ -695,6 +697,22 @@ def splunk_version(self):
self._splunk_version = tuple([int(p) for p in self.info['version'].split('.')])
return self._splunk_version

@property
def splunk_instance(self):
if self._instance_type is None :
splunk_info = self.info;
if hasattr(splunk_info, 'instance_type') :
self._instance_type = splunk_info['instance_type']
else:
self._instance_type = ''
return self._instance_type

@property
def disable_v2_api(self):
if self.splunk_instance == 'cloud':
return self.splunk_version < (9,0,2209)
return self.splunk_version < (9,0,2)

@property
def kvstore_owner(self):
"""Returns the KVStore owner for this instance of Splunk.
Expand Down Expand Up @@ -2722,7 +2740,8 @@ def __init__(self, service, sid, **kwargs):
# Default to v2 in Splunk Version 9+
path = "{path}{sid}"
# Formatting path based on the Splunk Version
if service.splunk_version < (9,0,2):
#if service.splunk_version < (9,0,2):
if service.disable_v2_api:
path = path.format(path=PATH_JOBS, sid=sid)
else:
path = path.format(path=PATH_JOBS_V2, sid=sid)
Expand Down Expand Up @@ -2782,7 +2801,8 @@ def events(self, **kwargs):
kwargs['segmentation'] = kwargs.get('segmentation', 'none')

# Search API v1(GET) and v2(POST)
if self.service.splunk_version < (9,0,2):
# if self.service.splunk_version < (9,0,2):
if self.service.disable_v2_api:
return self.get("events", **kwargs).body
return self.post("events", **kwargs).body

Expand Down Expand Up @@ -2874,7 +2894,8 @@ def results(self, **query_params):
query_params['segmentation'] = query_params.get('segmentation', 'none')

# Search API v1(GET) and v2(POST)
if self.service.splunk_version < (9,0,2):
# if self.service.splunk_version < (9,0,2):
if self.service.disable_v2_api:
return self.get("results", **query_params).body
return self.post("results", **query_params).body

Expand Down Expand Up @@ -2919,7 +2940,8 @@ def preview(self, **query_params):
query_params['segmentation'] = query_params.get('segmentation', 'none')

# Search API v1(GET) and v2(POST)
if self.service.splunk_version < (9,0,2):
# if self.service.splunk_version < (9,0,2):
if self.service.disable_v2_api:
return self.get("results_preview", **query_params).body
return self.post("results_preview", **query_params).body

Expand Down Expand Up @@ -3011,7 +3033,8 @@ class Jobs(Collection):
collection using :meth:`Service.jobs`."""
def __init__(self, service):
# Splunk 9 introduces the v2 endpoint
if service.splunk_version >= (9,0,2):
# if service.splunk_version >= (9,0,2):
if not service.disable_v2_api:
path = PATH_JOBS_V2
else:
path = PATH_JOBS
Expand Down
2 changes: 1 addition & 1 deletion tests/test_job.py
Expand Up @@ -401,7 +401,7 @@ def test_v1_job_fallback(self):
n_results = len([x for x in results_r if isinstance(x, dict)])

# Fallback test for Splunk Version 9.0.2+
if self.service.splunk_version >= (9, 0, 2):
if not self.service.disable_v2_api:
self.assertTrue(client.PATH_JOBS_V2 in self.job.path)
self.assertEqual(n_events, n_preview, n_results)

Expand Down

0 comments on commit 137a0ef

Please sign in to comment.