Skip to content

Commit 3f83362

Browse files
author
Frederick Ross
committed
Workaround for bug in Splunk 5.0: restrictToHost is removed from a TCP input unless always specified on an update.
Now Input.update implicitly adds restrictToHost onto the dict of things to update if it's not already there and it needs to be.
1 parent 15b58dd commit 3f83362

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

splunklib/client.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1683,19 +1683,26 @@ def __init__(self, service, path, kind=None, **kwargs):
16831683
self.kind = 'splunktcp'
16841684

16851685
def update(self, **kwargs):
1686-
if 'restrictToHost' in kwargs and self.service.splunk_version < (5,):
1686+
if self.kind not in ['tcp', 'splunktcp', 'tcp/raw', 'tcp/cooked']:
1687+
result = super(Input, self).update(**kwargs)
1688+
return result
1689+
elif 'restrictToHost' in kwargs and self.service.splunk_version < (5,):
16871690
raise IllegalOperationException("Updating restrictToHost has no effect before Splunk 5.0")
1688-
port = self.name.split(':', 1)[-1]
1689-
result = super(Input, self).update(**kwargs)
1690-
if 'restrictToHost' in kwargs:
1691+
else:
1692+
port = self.name.split(':', 1)[-1]
1693+
new_kwargs = kwargs.copy()
1694+
if 'restrictToHost' not in new_kwargs and ':' in self.name:
1695+
new_kwargs['restrictToHost'] = self['restrictToHost']
1696+
result = super(Input, self).update(**new_kwargs)
16911697
if self.path.endswith('/'):
16921698
base_path, name = self.path.rsplit('/', 2)[:-1]
16931699
else:
16941700
base_path, name = self.path.rsplit('/', 1)
1695-
self.path = base_path + '/' + kwargs['restrictToHost'] + ':' + port
1696-
return result
1697-
1698-
1701+
if 'restrictToHost' in new_kwargs:
1702+
self.path = base_path + '/' + new_kwargs['restrictToHost'] + ':' + port
1703+
else:
1704+
self.path = base_path + '/' + port
1705+
return result
16991706

17001707
# Inputs is a "kinded" collection, which is a heterogenous collection where
17011708
# each item is tagged with a kind, that provides a single merged view of all

tests/test_input.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,13 @@ def test_update_restrictToHost(self):
9898
self.check_entity(boris)
9999
boris.delete()
100100

101+
def test_update_nonrestrictToHost(self):
102+
for kind in ['tcp', 'splunktcp']:
103+
input = self.service.inputs.create(kind, str(self.base_port), restrictToHost='boris')
104+
input.update(host='meep')
105+
input.refresh()
106+
self.assertTrue(input.name.startswith('boris'))
107+
101108
class TestRead(testlib.SDKTestCase):
102109
def test_read(self):
103110
inputs = self.service.inputs
@@ -165,14 +172,18 @@ def setUp(self):
165172
inputs = self.service.inputs
166173
tcp_port = str(highest_port(self.service, 10000, 'tcp', 'splunktcp')+1)
167174
udp_port = str(highest_port(self.service, 10000, 'udp')+1)
175+
restricted_tcp_port = str(highest_port(self.service, int(tcp_port)+1, 'tcp', 'splunktcp')+1)
168176
test_inputs = [{'kind': 'tcp', 'name': tcp_port, 'host': 'sdk-test'},
169-
{'kind': 'udp', 'name': udp_port, 'host': 'sdk-test'}]
177+
{'kind': 'udp', 'name': udp_port, 'host': 'sdk-test'},
178+
{'kind': 'tcp', 'name': 'boris:' + restricted_tcp_port, 'host': 'sdk-test'}]
170179
self._test_entities = {}
171180

172181
self._test_entities['tcp'] = \
173182
inputs.create('tcp', str(tcp_port), host='sdk-test')
174183
self._test_entities['udp'] = \
175184
inputs.create('udp', str(udp_port), host='sdk-test')
185+
self._test_entities['restrictedTcp'] = \
186+
inputs.create('tcp', restricted_tcp_port, restrictToHost='boris')
176187

177188
def tearDown(self):
178189
super(TestInput, self).tearDown()
@@ -232,11 +243,10 @@ def test_update(self):
232243
inputs = self.service.inputs
233244
for entity in self._test_entities.itervalues():
234245
kind, name = entity.kind, entity.name
235-
kwargs = {'host': 'foo', 'sourcetype': 'bar'}
246+
kwargs = {'host': 'foo'}
236247
entity.update(**kwargs)
237248
entity.refresh()
238249
self.assertEqual(entity.host, kwargs['host'])
239-
self.assertEqual(entity.sourcetype, kwargs['sourcetype'])
240250

241251
def test_delete(self):
242252
inputs = self.service.inputs

0 commit comments

Comments
 (0)