Skip to content

Commit c2ca157

Browse files
author
Frederick Ross
committed
Added kinds to fetching and deleting inputs.
1 parent c7c8199 commit c2ca157

2 files changed

Lines changed: 31 additions & 13 deletions

File tree

‎splunklib/client.py‎

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,6 @@ class Endpoint(object):
325325
def __init__(self, service, path):
326326
self.service = service
327327
self.path = path if path.endswith('/') else path + '/'
328-
print "At init: %s" % self.path
329328

330329
def get(self, path_segment="", owner=None, app=None, sharing=None, **query):
331330
"""GET from *path_segment* relative to this endpoint.
@@ -374,10 +373,10 @@ def get(self, path_segment="", owner=None, app=None, sharing=None, **query):
374373
# self.path to the Endpoint is relative in the SDK, so passing
375374
# owner, app, sharing, etc. along will produce the correct
376375
# namespace in the final request.
377-
print "Path = %s" % self.path
378-
print "Segment = %s" % path_segment
379-
path = self.path + path_segment
380-
import pprint; pprint.pprint(path)
376+
if path_segment.startswith('/'):
377+
path = path_segment
378+
else:
379+
path = self.path + path_segment
381380
# ^-- This was "%s%s" % (self.path, path_segment).
382381
# That doesn't work, because self.path may be UrlEncoded.
383382
return self.service.get(path,
@@ -1175,13 +1174,18 @@ def __call__(self, *args):
11751174
return self.list(*args)
11761175

11771176
def __getitem__(self, key):
1177+
if isinstance(key, tuple) and len(key) == 2:
1178+
kind, key = key
1179+
else:
1180+
kind = None
11781181
candidate = None
11791182
for input in self.list():
1180-
if input.name == key:
1183+
if input.name == key and (kind is None or input.kind == kind):
11811184
if candidate is None:
11821185
candidate = input
11831186
else:
1184-
raise ValueError("Found multiple inputs named '%s'; please specify an input kind." % key)
1187+
raise ValueError("Found multiple inputs named '%s' (kinds: %s, %s); please specify an input kind." % \
1188+
(key, candidate.kind, input.kind))
11851189
if candidate is not None:
11861190
return candidate
11871191
else:
@@ -1205,12 +1209,16 @@ def create(self, kind, name, **kwargs):
12051209
self.service.post(kindpath, name=name, **kwargs)
12061210
return Input(self.service, _path(kindpath, name), kind)
12071211

1208-
def delete(self, name):
1212+
def delete(self, kind, name=None):
12091213
"""Removes an input from the collection.
12101214
12111215
:param `name`: The name of the input to remove.
12121216
"""
1213-
self.service.delete(self[name].path) # UNDONE: Should be item.remove()
1217+
if name is None:
1218+
name = kind
1219+
self.service.delete(self[name].path)
1220+
else:
1221+
self.service.delete(self[kind, name].path)
12141222
return self
12151223

12161224
def itemmeta(self, kind):
@@ -1262,8 +1270,6 @@ def list(self, *args):
12621270
# should be of type UrlEncoded, and all str should not
12631271
# be URL encoded.
12641272
path = urllib.unquote(state.links.alternate)
1265-
print "Alternate link: %s" % path
1266-
12671273
entity = Input(self.service, path, kind, state=state)
12681274
entities.append(entity)
12691275

‎tests/test_input.py‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ def test_crud(self):
106106

107107
inputs = service.inputs
108108

109-
if inputs.contains('9999'): inputs.delete('9999')
109+
for input in inputs:
110+
if input.name == '9999':
111+
inputs.delete(input.kind, input.name)
110112
self.assertFalse(inputs.contains('9999'))
111113
inputs.create("tcp", "9999", host="sdk-test")
112114
self.assertTrue(inputs.contains('9999'))
@@ -117,8 +119,18 @@ def test_crud(self):
117119
input_.refresh()
118120
self.assertEqual(input_['host'], "foo")
119121
self.assertEqual(input_['sourcetype'], "bar")
120-
inputs.delete('9999')
122+
123+
inputs.create("udp", "9999", host="sdk-test")
124+
self.assertRaises(ValueError, inputs.__getitem__, '9999')
125+
self.assertTrue(isinstance(inputs['udp', '9999'], client.Input))
126+
self.assertTrue(isinstance(inputs['tcp', '9999'], client.Input))
127+
128+
for input in inputs:
129+
if input.name == '9999':
130+
inputs.delete(input.kind, input.name)
121131
self.assertFalse(inputs.contains('9999'))
122132

133+
134+
123135
if __name__ == "__main__":
124136
testlib.main()

0 commit comments

Comments
 (0)