Skip to content

Commit 49dd847

Browse files
author
Frederick Ross
committed
Add test that role names are case insensitive. Remove calls to list in Inputs.__getitem__ and __contains__ to reduce round trips to server.
1 parent 255a638 commit 49dd847

3 files changed

Lines changed: 86 additions & 25 deletions

File tree

‎splunklib/client.py‎

Lines changed: 73 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1562,9 +1562,16 @@ class Input(Entity):
15621562
"""This class represents a Splunk input. This class is the base for all
15631563
typed input classes and is also used when the client does not recognize an
15641564
input kind."""
1565-
def __init__(self, service, path, kind, **kwargs):
1565+
def __init__(self, service, path, kind=None, **kwargs):
15661566
Entity.__init__(self, service, path, **kwargs)
1567-
self.kind = kind
1567+
if kind is None:
1568+
for kind, kind_path in INPUT_KINDMAP.iteritems():
1569+
if kind_path in path:
1570+
self.kind = kind
1571+
break
1572+
assert self.kind is not None
1573+
else:
1574+
self.kind = kind
15681575

15691576
# Directory of known input kinds that maps from input kind to path relative
15701577
# to data/inputs, eg: inputs of kind 'splunktcp' map to a relative path
@@ -1592,37 +1599,80 @@ class Inputs(Collection):
15921599
that indicates the specific type of input."""
15931600

15941601
def __init__(self, service, kindmap=None):
1595-
Collection.__init__(self, service, PATH_INPUTS)
1602+
Collection.__init__(self, service, PATH_INPUTS, item=Input)
15961603
self._kindmap = kindmap if kindmap is not None else INPUT_KINDMAP
15971604

15981605
def __getitem__(self, key):
15991606
if isinstance(key, tuple) and len(key) == 2:
1607+
# Fetch a single kind
16001608
kind, key = key
1601-
else:
1602-
kind = None
1603-
candidate = None
1604-
for input in self.list():
1605-
if input.name == key and (kind is None or input.kind == kind):
1606-
if candidate is None:
1607-
candidate = input
1609+
try:
1610+
kind_path = self._kindmap[kind]
1611+
response = self.get(kind_path + "/" + key)
1612+
entries = self._load_list(response)
1613+
if len(entries) > 1:
1614+
raise AmbiguousReferenceException("Found multiple inputs of kind %s named %s." % (kind, key))
1615+
elif len(entries) == 0:
1616+
raise KeyError((kind,key))
1617+
else:
1618+
return entries[0]
1619+
except HTTPError as he:
1620+
if he.status == 404: # No entity matching kind and key
1621+
raise KeyError((kind,key))
16081622
else:
1609-
raise AmbiguousReferenceException(
1610-
"Found multiple inputs named '%s' (kinds: %s, %s); please specify an input kind." % \
1611-
(key, candidate.kind, input.kind))
1612-
if candidate is not None:
1613-
return candidate
1623+
raise
16141624
else:
1615-
raise KeyError(key)
1625+
# Iterate over all the kinds looking for matches.
1626+
kind = None
1627+
candidate = None
1628+
for kind, kind_path in self._kindmap.iteritems():
1629+
try:
1630+
response = self.get(kind_path + "/" + key)
1631+
entries = self._load_list(response)
1632+
if len(entries) > 1:
1633+
raise AmbiguousReferenceException("Found multiple inputs of kind %s named %s." % (kind, key))
1634+
elif len(entries) == 0:
1635+
pass
1636+
else:
1637+
if candidate is not None: # Already found at least one candidate
1638+
raise AmbiguousReferenceException("Found multiple inputs named %s, please specify a kind" % key)
1639+
candidate = entries[0]
1640+
except HTTPError as he:
1641+
if he.status == 404:
1642+
pass # Just carry on to the next kind.
1643+
else:
1644+
raise
1645+
if candidate is None:
1646+
raise KeyError(key) # Never found a match
1647+
else:
1648+
return candidate
16161649

16171650
def __contains__(self, key):
1618-
try:
1619-
self.__getitem__(key)
1620-
return True
1621-
except KeyError:
1651+
if isinstance(key, tuple) and len(key) == 2:
1652+
# If we specify a kind, this will shortcut properly
1653+
try:
1654+
self.__getitem__(key)
1655+
return True
1656+
except KeyError:
1657+
return False
1658+
else:
1659+
# Without a kind, we want to minimize the number of round trips to the server, so we
1660+
# reimplement some of the behavior of __getitem__ in order to be able to stop searching
1661+
# on the first hit.
1662+
for kind, kind_path in self._kindmap.iteritems():
1663+
try:
1664+
response = self.get(kind_path + "/" + key)
1665+
entries = self._load_list(response)
1666+
if len(entries) > 0:
1667+
return True
1668+
else:
1669+
pass
1670+
except HTTPError as he:
1671+
if he.status == 404:
1672+
pass # Just carry on to the next kind.
1673+
else:
1674+
raise
16221675
return False
1623-
except AmbiguousReferenceException:
1624-
return True
1625-
16261676

16271677
def create(self, kind, name, **kwargs):
16281678
"""Creates an input of a specific kind in this collection, with any

‎tests/test_input.py‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,12 @@ def setUp(self):
6868
inputs = self.service.inputs
6969
self._test_entities = {}
7070
for test_input in test_inputs:
71-
self._test_entities[test_input['kind']] = \
72-
inputs.create(**test_input)
71+
if (test_input['kind'], test_input['name']) not in self.service.inputs:
72+
self._test_entities[test_input['kind']] = \
73+
inputs.create(**test_input)
74+
else:
75+
self._test_entities[test_input['kind']] = \
76+
inputs[test_input['kind'], test_input['name']]
7377

7478
def tearDown(self):
7579
super(TestInput, self).tearDown()
@@ -88,6 +92,7 @@ def test_create(self):
8892
entity = self._test_entities[kind]
8993
entity = inputs[kind, name]
9094
self.check_entity(entity)
95+
self.assertTrue(isinstance(entity, client.Input))
9196
self.assertEqual(entity.name, name)
9297
self.assertEqual(entity.kind, kind)
9398
self.assertEqual(entity.host, host)

‎tests/test_role.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ def test_read(self):
4343
role.refresh()
4444
self.check_role(role)
4545

46+
def test_read_case_insensitive(self):
47+
for role in self.service.roles:
48+
a = self.service.roles[role.name.upper()]
49+
b = self.service.roles[role.name.lower()]
50+
self.assertEqual(a.name, b.name)
51+
4652
def test_create(self):
4753
self.assertTrue(self.role_name in self.service.roles)
4854
self.check_entity(self.role)

0 commit comments

Comments
 (0)