Skip to content

Commit 905eb59

Browse files
author
Frederick Ross
committed
Make all Input related calls that take both name and kind have name first.
The only breaking change is Inputs.create, which has been noted in the change log. Removed a couple of UNDONE comments.
1 parent 5833bdb commit 905eb59

File tree

3 files changed

+28
-30
lines changed

3 files changed

+28
-30
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## 1.0
44

5+
* The argument order of Inputs.create has been swapped to have name first. This is consistent with
6+
all other collections and all other operations on Inputs.
57
* All the .contains methods on collections have been removed. Use Python's `in` operator instead.
68
* Confs has been renamed to Configurations, and ConfFile to ConfigurationFile.
79
* Stanza.submit now takes a dictionary of key/value pairs specifying the stanza instead of a raw string.

splunklib/client.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1945,7 +1945,6 @@ def update(self, **kwargs):
19451945
# Inputs is a "kinded" collection, which is a heterogenous collection where
19461946
# each item is tagged with a kind, that provides a single merged view of all
19471947
# input kinds.
1948-
# UNDONE: contains needs to take a kind arg to disambiguate
19491948
class Inputs(Collection):
19501949
"""This class represents a collection of inputs. The collection is
19511950
heterogeneous and each member of the collection contains a *kind* property
@@ -1958,7 +1957,7 @@ def __init__(self, service, kindmap=None):
19581957
def __getitem__(self, key):
19591958
if isinstance(key, tuple) and len(key) == 2:
19601959
# Fetch a single kind
1961-
kind, key = key
1960+
key, kind = key
19621961
try:
19631962
response = self.get(self.kindpath(kind) + "/" + key)
19641963
entries = self._load_list(response)
@@ -2026,10 +2025,12 @@ def __contains__(self, key):
20262025
raise
20272026
return False
20282027

2029-
def create(self, kind, name, **kwargs):
2028+
def create(self, name, kind, **kwargs):
20302029
"""Creates an input of a specific kind in this collection, with any
20312030
arguments you specify.
20322031
2032+
:param `name`: The input name.
2033+
:type name: ``string``
20332034
:param `kind`: The kind of input:
20342035
20352036
- "ad": Active Directory
@@ -2053,8 +2054,6 @@ def create(self, kind, name, **kwargs):
20532054
- "win-wmi-collections": WMI
20542055
20552056
:type kind: ``string``
2056-
:param `name`: The input name.
2057-
:type name: ``string``
20582057
:param `kwargs`: Additional arguments (optional). For more about the
20592058
available parameters, see `Input parameters <http://dev.splunk.com/view/SP-CAAAEE6#inputparams>`_ on Splunk Developer Portal.
20602059
@@ -2075,7 +2074,7 @@ def create(self, kind, name, **kwargs):
20752074
)
20762075
return Input(self.service, path, kind)
20772076

2078-
def delete(self, kind, name=None):
2077+
def delete(self, name, kind=None):
20792078
"""Removes an input from the collection.
20802079
20812080
:param `kind`: The kind of input:
@@ -2106,11 +2105,10 @@ def delete(self, kind, name=None):
21062105
21072106
:return: The :class:`Inputs` collection.
21082107
"""
2109-
if name is None:
2110-
name = kind
2108+
if kind is None:
21112109
self.service.delete(self[name].path)
21122110
else:
2113-
self.service.delete(self[kind, name].path)
2111+
self.service.delete(self[name, kind].path)
21142112
return self
21152113

21162114
def itemmeta(self, kind):
@@ -2308,8 +2306,6 @@ def list(self, *kinds, **kwargs):
23082306
else:
23092307
raise
23102308

2311-
# UNDONE: Should use _load_list for the following, but need to
2312-
# pass kind to the `item` method.
23132309
entries = _load_atom_entries(response)
23142310
if entries is None: continue # No inputs to process
23152311
for entry in entries:

tests/test_input.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,29 +44,29 @@ def create_tcp_input(self, base_port, kind, **options):
4444
port = base_port
4545
while True: # Find the next unbound port
4646
try:
47-
input = self.service.inputs.create(kind, str(port), **options)
47+
input = self.service.inputs.create(str(port), kind, **options)
4848
return input
4949
except client.HTTPError as he:
5050
if he.status == 400:
5151
port += 1
5252

5353
def test_create_tcp_port(self):
5454
for kind in ['tcp', 'splunktcp']:
55-
input = self.service.inputs.create(kind, str(self.base_port))
55+
input = self.service.inputs.create(str(self.base_port), kind)
5656
self.check_entity(input)
5757
input.delete()
5858

5959
def test_cannot_create_with_restrictToHost_in_name(self):
6060
self.assertRaises(
6161
client.HTTPError,
62-
lambda: self.service.inputs.create('tcp', 'boris:10000')
62+
lambda: self.service.inputs.create('boris:10000', 'tcp')
6363
)
6464

6565
def test_create_tcp_ports_with_restrictToHost(self):
6666
for kind in ['tcp', 'splunktcp']: # Multiplexed UDP ports are not supported
6767
# Make sure we can create two restricted inputs on the same port
68-
boris = self.service.inputs.create(kind, str(self.base_port), restrictToHost='boris')
69-
natasha = self.service.inputs.create(kind, str(self.base_port), restrictToHost='natasha')
68+
boris = self.service.inputs.create(str(self.base_port), kind, restrictToHost='boris')
69+
natasha = self.service.inputs.create(str(self.base_port), kind, restrictToHost='natasha')
7070
# And that they both function
7171
boris.refresh()
7272
natasha.refresh()
@@ -77,21 +77,21 @@ def test_create_tcp_ports_with_restrictToHost(self):
7777

7878
def test_restricted_to_unrestricted_collision(self):
7979
for kind in ['tcp', 'splunktcp', 'udp']:
80-
restricted = self.service.inputs.create(kind, str(self.base_port), restrictToHost='boris')
80+
restricted = self.service.inputs.create(str(self.base_port), kind, restrictToHost='boris')
8181
self.assertTrue('boris:' + str(self.base_port) in self.service.inputs)
8282
self.assertRaises(
8383
client.HTTPError,
84-
lambda: self.service.inputs.create(kind, str(self.base_port))
84+
lambda: self.service.inputs.create(str(self.base_port), kind)
8585
)
8686
restricted.delete()
8787

8888
def test_unrestricted_to_restricted_collision(self):
8989
for kind in ['tcp', 'splunktcp', 'udp']:
90-
unrestricted = self.service.inputs.create(kind, str(self.base_port))
90+
unrestricted = self.service.inputs.create(str(self.base_port), kind)
9191
self.assertTrue(str(self.base_port) in self.service.inputs)
9292
self.assertRaises(
9393
client.HTTPError,
94-
lambda: self.service.inputs.create(kind, str(self.base_port), restrictToHost='boris')
94+
lambda: self.service.inputs.create(str(self.base_port), kind, restrictToHost='boris')
9595
)
9696
unrestricted.delete()
9797

@@ -177,11 +177,11 @@ def setUp(self):
177177
self._test_entities = {}
178178

179179
self._test_entities['tcp'] = \
180-
inputs.create('tcp', unrestricted_port, host='sdk-test')
180+
inputs.create(unrestricted_port, 'tcp', host='sdk-test')
181181
self._test_entities['udp'] = \
182-
inputs.create('udp', unrestricted_port, host='sdk-test')
182+
inputs.create(unrestricted_port, 'udp', host='sdk-test')
183183
self._test_entities['restrictedTcp'] = \
184-
inputs.create('tcp', restricted_port, restrictToHost='boris')
184+
inputs.create(restricted_port, 'tcp', restrictToHost='boris')
185185

186186
def tearDown(self):
187187
super(TestInput, self).tearDown()
@@ -210,10 +210,10 @@ def test_lists_modular_inputs(self):
210210
self.uncheckedRestartSplunk()
211211

212212
inputs = self.service.inputs
213-
if ('test2','abcd') not in inputs:
214-
inputs.create('test2', 'abcd', field1='boris')
213+
if ('abcd','test2') not in inputs:
214+
inputs.create('abcd', 'test2', field1='boris')
215215

216-
input = inputs['test2', 'abcd']
216+
input = inputs['abcd', 'test2']
217217
self.assertEqual(input.field1, 'boris')
218218

219219

@@ -232,7 +232,7 @@ def test_read(self):
232232
inputs = self.service.inputs
233233
for this_entity in self._test_entities.itervalues():
234234
kind, name = this_entity.kind, this_entity.name
235-
read_entity = inputs[kind, name]
235+
read_entity = inputs[name, kind]
236236
self.assertEqual(this_entity.kind, read_entity.kind)
237237
self.assertEqual(this_entity.name, read_entity.name)
238238
self.assertEqual(this_entity.host, read_entity.host)
@@ -253,16 +253,16 @@ def test_delete(self):
253253
name = input_entity.name
254254
kind = input_entity.kind
255255
self.assertTrue(name in inputs)
256-
self.assertTrue((kind,name) in inputs)
256+
self.assertTrue((name,kind) in inputs)
257257
if remaining == 0:
258258
inputs.delete(name)
259259
self.assertFalse(name in inputs)
260260
else:
261261
if not name.startswith('boris'):
262262
self.assertRaises(client.AmbiguousReferenceException,
263263
inputs.delete, name)
264-
self.service.inputs.delete(kind, name)
265-
self.assertFalse((kind,name) in inputs)
264+
self.service.inputs.delete(name, kind)
265+
self.assertFalse((name, kind) in inputs)
266266
self.assertRaises(client.HTTPError,
267267
input_entity.refresh)
268268
remaining -= 1

0 commit comments

Comments
 (0)