Skip to content

Commit f8639a3

Browse files
author
Philippe Tang
committed
[DVPL-10898] Update get/post for v2-to-v1 fallback
1 parent 7070ca0 commit f8639a3

File tree

1 file changed

+34
-10
lines changed

1 file changed

+34
-10
lines changed

splunklib/client.py

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import datetime
6363
import json
6464
import logging
65+
import re
6566
import socket
6667
from datetime import datetime, timedelta
6768
from time import sleep
@@ -731,6 +732,25 @@ def __init__(self, service, path):
731732
self.service = service
732733
self.path = path
733734

735+
def get_api_version(self, path):
736+
"""Return the API version of the service used in the provided path.
737+
738+
Args:
739+
path (str): A fully-qualified endpoint path (for example, "/services/search/jobs").
740+
741+
Returns:
742+
int: Version of the API (for example, 1)
743+
"""
744+
# Default to v1 if undefined in the path
745+
# For example, "/services/search/jobs" is using API v1
746+
api_version = 1
747+
748+
versionSearch = re.search('(?:servicesNS\/[^/]+\/[^/]+|services)\/[^/]+\/v(\d+)\/', path)
749+
if versionSearch:
750+
api_version = int(versionSearch.group(1))
751+
752+
return api_version
753+
734754
def get(self, path_segment="", owner=None, app=None, sharing=None, **query):
735755
"""Performs a GET operation on the path segment relative to this endpoint.
736756
@@ -794,12 +814,14 @@ def get(self, path_segment="", owner=None, app=None, sharing=None, **query):
794814
# ^-- This was "%s%s" % (self.path, path_segment).
795815
# That doesn't work, because self.path may be UrlEncoded.
796816

797-
# Search API v2 fallback to v1:
798-
# - In v2, /results_preview, /events and /results do not support search params.
799-
# - Fallback from v2 to v1 if Splunk Version is < 9.
800-
if (PATH_JOBS_V2 in path and 'search' in query and path.endswith(tuple(["results_preview", "events", "results"]))) or self.service.splunk_version < (9,):
801-
path = path.replace(PATH_JOBS_V2, PATH_JOBS)
817+
# Get the API version from the path
818+
api_version = self.get_api_version(path)
802819

820+
# Search API v2+ fallback to v1:
821+
# - In v2+, /results_preview, /events and /results do not support search params.
822+
# - Fallback from v2+ to v1 if Splunk Version is < 9.
823+
if api_version >= 2 and ('search' in query and path.endswith(tuple(["results_preview", "events", "results"])) or self.service.splunk_version < (9,)):
824+
path = path.replace(PATH_JOBS_V2, PATH_JOBS)
803825
return self.service.get(path,
804826
owner=owner, app=app, sharing=sharing,
805827
**query)
@@ -860,12 +882,14 @@ def post(self, path_segment="", owner=None, app=None, sharing=None, **query):
860882
self.path = self.path + '/'
861883
path = self.service._abspath(self.path + path_segment, owner=owner, app=app, sharing=sharing)
862884

863-
# Search API v2 fallback to v1:
864-
# - In v2, /results_preview, /events and /results do not support search params.
865-
# - Fallback from v2 to v1 if Splunk Version is < 9.
866-
if (PATH_JOBS_V2 in path and 'search' in query and path.endswith(tuple(["results_preview", "events", "results"]))) or self.service.splunk_version < (9,):
885+
# Get the API version from the path
886+
api_version = self.get_api_version(path)
887+
888+
# Search API v2+ fallback to v1:
889+
# - In v2+, /results_preview, /events and /results do not support search params.
890+
# - Fallback from v2+ to v1 if Splunk Version is < 9.
891+
if api_version >= 2 and ('search' in query and path.endswith(tuple(["results_preview", "events", "results"])) or self.service.splunk_version < (9,)):
867892
path = path.replace(PATH_JOBS_V2, PATH_JOBS)
868-
869893
return self.service.post(path, owner=owner, app=app, sharing=sharing, **query)
870894

871895

0 commit comments

Comments
 (0)