Skip to content

Commit d4ff81e

Browse files
committed
Add namespace args (app/owner/sharing) to Context.fullpath logic and write units for fullpath behavior.
1 parent d3731c0 commit d4ff81e

File tree

4 files changed

+178
-120
lines changed

4 files changed

+178
-120
lines changed

splunklib/binding.py

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@
3838
# provided values as appropriate.
3939
#
4040
# Finally, if no namspacing is specified the library will make use of the
41-
# `/services` branch of the REST API which provides a non-namespaced view of
42-
# Splunk resources.
41+
# `/services` branch of the REST API which provides a namespaced view of
42+
# Splunk resources equivelent to using owner={currentUser} and app={defaultApp}
4343
#
4444

4545
import httplib
@@ -77,20 +77,20 @@ def namespace(**kwargs):
7777
kwargs, which may contain any of `sharing`, `owner` and `app`."""
7878
sharing = kwargs.get('sharing', None)
7979
if sharing in ["system"]:
80-
return {
80+
return record({
8181
'sharing': sharing,
8282
'owner': "nobody",
83-
'app': "system" }
83+
'app': "system" })
8484
if sharing in ["global", "app"]:
85-
return {
85+
return record({
8686
'sharing': sharing,
8787
'owner': "nobody",
88-
'app': kwargs.get('app', None)}
88+
'app': kwargs.get('app', None)})
8989
if sharing in ["user", None]:
90-
return {
90+
return record({
9191
'sharing': sharing,
9292
'owner': kwargs.get('owner', None),
93-
'app': kwargs.get('app', None)}
93+
'app': kwargs.get('app', None)})
9494
raise ValueError("Invalid value for argument: 'sharing'")
9595

9696
class Context(object):
@@ -102,13 +102,7 @@ def __init__(self, handler=None, **kwargs):
102102
self.scheme = kwargs.get("scheme", DEFAULT_SCHEME)
103103
self.host = kwargs.get("host", DEFAULT_HOST)
104104
self.port = kwargs.get("port", DEFAULT_PORT)
105-
106-
# The default namespace values for this context
107-
result = namespace(**kwargs)
108-
self.app = result['app']
109-
self.owner = result['owner']
110-
self.sharing = result['sharing']
111-
105+
self.namespace = namespace(**kwargs)
112106
self.username = kwargs.get("username", "")
113107
self.password = kwargs.get("password", "")
114108

@@ -158,15 +152,27 @@ def logout(self):
158152
self.token = None
159153
return self
160154

161-
def fullpath(self, path):
162-
"""If the given path is a fragment, qualify with segments corresponding
163-
to the binding context's namespace args."""
155+
def fullpath(self, path, **kwargs):
156+
"""Returns a full resource path given a potential path fragment and
157+
then completing with namespace segments using the namespace args,
158+
if provided, otherwise using the context namespace values."""
164159
if path.startswith('/'):
165160
return path
166-
if self.app is None and self.owner is None:
161+
162+
# Use namespace kwargs if any provided, otherwise use context defaults
163+
if 'app' in kwargs or 'owner' in kwargs or 'sharing' in kwargs:
164+
ns = namespace(**kwargs)
165+
else:
166+
ns = self.namespace
167+
168+
# If no app or owner are specified, then use the /services endpoint
169+
if ns.app is None and ns.owner is None:
167170
return "/services/%s" % path
168-
oname = "-" if self.owner is None else self.owner
169-
aname = "-" if self.app is None else self.app
171+
172+
# At least one of app or owner is specified, so use the /serviceNS
173+
# endpoint and if only one is specified, then wildcard the other.
174+
oname = "-" if ns.owner is None else ns.owner
175+
aname = "-" if ns.app is None else ns.app
170176
return "/servicesNS/%s/%s/%s" % (oname, aname, path)
171177

172178
# Convet the given path into a fully qualified URL by first qualifying

tests/runtests.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
os.environ["COVERAGE_PROCESS_START"] = "../tests/.coveragerc"
2323

2424
files = [
25+
"test_module.py",
2526
"test_data.py",
2627
"test_binding.py",
2728
"test_collection.py",

tests/splunklib.baseline

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', '__version__', '__version_info__', 'binding', 'data']
1+
['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', '__version__', '__version_info__', 'binding', 'client', 'data', 'results']

0 commit comments

Comments
 (0)