Skip to content

Commit b13e4d7

Browse files
author
Frederick Ross
committed
Implement Inputs.oneshot method.
1 parent 49dd847 commit b13e4d7

2 files changed

Lines changed: 32 additions & 2 deletions

File tree

‎splunklib/client.py‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ class EntityDeletedException(Exception):
149149
class InvalidNameException(Exception):
150150
pass
151151

152+
class OperationFailedException(Exception):
153+
pass
154+
152155
def trailing(template, *targets):
153156
"""Substring of *template* following all *targets*.
154157
@@ -1827,8 +1830,16 @@ def iter(self, **kwargs):
18271830
for item in self.list(**kwargs):
18281831
yield item
18291832

1830-
def oneshot(self, **kwargs):
1831-
pass
1833+
def oneshot(self, path, **kwargs):
1834+
try:
1835+
self.post('oneshot', name=path, **kwargs)
1836+
except HTTPError as he:
1837+
if he.status == 400:
1838+
raise OperationFailedException(he.message)
1839+
else:
1840+
raise
1841+
1842+
18321843

18331844
class Job(Entity):
18341845
"""This class represents a search job."""

‎tests/test_input.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,25 @@ def test_inputs_list_on_one_kind_with_search(self):
6161
found = [x.name for x in self.service.inputs.list('monitor', search=search)]
6262
self.assertEqual(expected, found)
6363

64+
def test_oneshot(self):
65+
index_name = testlib.tmpname()
66+
index = self.service.indexes.create(index_name)
67+
self.service.restart(timeout=120)
68+
index = self.service.indexes[index_name]
69+
eventCount = int(index['totalEventCount'])
70+
from os import path
71+
testpath = path.dirname(path.abspath(__file__))
72+
self.service.inputs.oneshot(path.join(testpath, 'testfile.txt'), index=index_name)
73+
testlib.retry(index, 'totalEventCount', str(eventCount+1), step=1)
74+
self.assertEqual(index['totalEventCount'], str(eventCount+1))
75+
76+
def test_oneshot_on_nonexistant_file(self):
77+
name = testlib.tmpname()
78+
from os import path
79+
self.assertFalse(path.exists(name))
80+
self.assertRaises(client.OperationFailedException,
81+
self.service.inputs.oneshot, name)
82+
6483

6584
class TestInput(testlib.TestCase):
6685
def setUp(self):

0 commit comments

Comments
 (0)