Skip to content

Commit 70727ac

Browse files
fewa123fewshilpabijam
authored andcommitted
Build tenant based domain url
1 parent 2533131 commit 70727ac

2 files changed

Lines changed: 80 additions & 1 deletion

File tree

splunk_sdk/base_client.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,55 @@ def _wrapper(self, *args, **kwargs):
8484
return _wrapper
8585

8686

87+
def build_url(context: Context, route="", add_scheme=False, omit_tenant=False) -> str:
88+
"""
89+
Builds a partial (without scheme) or full URL from param "route" and based on tenant specification in the context.
90+
It supports both scenarios of adopting tenant-scoped hostname or legacy hostname. Default mode is legacy hostname.
91+
If context.tenant_scoped is True, tenant-scoped hostname is selected. Non-system tenant will be added to the domain.
92+
Region will be added to the domain for system tenant.
93+
If context.tenant_scoped is False, there will be no additional string added to domain name (legacy mode).
94+
95+
:param context: Context object which provides all necessary information
96+
:param route: API path to call
97+
:param add_scheme: set to True if needs to add scheme, default to False
98+
:param omit_tenant: set to True if tenant name need not to be specified in URL
99+
:return: The full URL.
100+
"""
101+
if route is None:
102+
route = ""
103+
route = route.strip()
104+
if route.startswith("/"):
105+
route = route[1:]
106+
if context.host is None or not context.host.strip():
107+
raise ValueError("Invalid value for context.host")
108+
host = context.host.strip()
109+
if context.port is not None and context.port:
110+
host = f"{host}:{context.port}"
111+
if context.tenant is None or not context.tenant.strip():
112+
raise ValueError("Invalid value for context.tenant")
113+
tenant = context.tenant.strip()
114+
if not omit_tenant or tenant == "system":
115+
route = f"{tenant}/{route}"
116+
if context.tenant_scoped is True:
117+
if tenant == "system":
118+
if context.region is None or not context.region.strip():
119+
raise ValueError("Invalid value for context.region")
120+
region = context.region.strip()
121+
url = f"region-{region}.{host}/{route}"
122+
else:
123+
url = f"{tenant}.{host}/{route}"
124+
else:
125+
url = f"{host}/{route}"
126+
127+
if add_scheme is True:
128+
if context.scheme is None or not context.scheme.strip():
129+
raise ValueError("Invalid value for context.scheme")
130+
scheme = context.scheme.strip()
131+
return f"{scheme}://{url}"
132+
else:
133+
return url
134+
135+
87136
class BaseClient(object):
88137
"""
89138
The BaseClient class encapsulates conventions, authorization, and URL handling

test/test_base_client.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from test.fixtures import get_client_auth_manager_scoped as client_auth_manager_scoped # NOQA
1919
from test.fixtures import get_service_principal_auth_manager as service_principal_auth_manager # NOQA
2020
from splunk_sdk.common.context import Context
21-
from splunk_sdk.base_client import BaseClient
21+
from splunk_sdk.base_client import BaseClient, build_url
2222
from splunk_sdk.identity import Identity as IdentityAndAccessControl
2323

2424
@pytest.mark.usefixtures("pkce_auth_manager") # NOQA
@@ -193,3 +193,33 @@ def test_base_client_instance_with_sp_auth(service_principal_auth_manager):
193193
assert (default_config is not None)
194194
assert (base_client is not None)
195195
_assert_sp_credentials_auth_context(base_client.auth_manager.context)
196+
197+
198+
def test_build_url():
199+
# normal tenant
200+
context = Context(host="test.splunk.com", tenant="tttt", tenant_scoped=True, scheme="http")
201+
assert ("http://tttt.test.splunk.com/tttt/v2/test" == build_url(context, "/v2/test", True))
202+
context = Context(host="test.splunk.com", tenant="tttt", tenant_scoped=False, port=9999)
203+
assert ("test.splunk.com:9999/tttt/v2/test" == build_url(context, "v2/test", False))
204+
context = Context(host="test.splunk.com", tenant="tttt", tenant_scoped=False, port=9999)
205+
assert ("test.splunk.com:9999/v2/test" == build_url(context, "v2/test", False, True))
206+
# system tenant
207+
context = Context(host="test.splunk.com", tenant="system", tenant_scoped=True, region="iad10")
208+
assert ("region-iad10.test.splunk.com/system/v2/test" == build_url(context, "/v2/test"))
209+
context = Context(host="test.splunk.com", tenant="system", tenant_scoped=False)
210+
assert ("test.splunk.com/system/v2/test" == build_url(context, "v2/test"))
211+
context = Context(host="test.splunk.com", tenant="system", tenant_scoped=False)
212+
assert ("https://test.splunk.com/system/v2/test" == build_url(context, "v2/test", True))
213+
context = Context(host="test.splunk.com", tenant="system", tenant_scoped=False)
214+
assert ("https://test.splunk.com/system/v2/test" == build_url(context, "v2/test", True, True))
215+
# input sanity test
216+
context = Context(host=" test.splunk.com ", tenant=" system ", tenant_scoped=True, region=" iad10 ")
217+
assert ("region-iad10.test.splunk.com/system/v2/test" == build_url(context, " v2/test "))
218+
with pytest.raises(ValueError):
219+
build_url(Context(host=None, tenant="system", tenant_scoped=False), "v2/test")
220+
with pytest.raises(ValueError):
221+
build_url(Context(tenant=" ", tenant_scoped=True), "v2/test")
222+
with pytest.raises(ValueError):
223+
build_url(Context(tenant_scoped=True), "v2/test")
224+
with pytest.raises(ValueError):
225+
build_url(Context(tenant_scoped=True, scheme=""), "v2/test", True)

0 commit comments

Comments
 (0)