@@ -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
0 commit comments