@@ -1670,7 +1670,7 @@ def __init__(self, service, path, kind=None, **kwargs):
16701670 Entity .__init__ (self , service , path , ** kwargs )
16711671 if kind is None :
16721672 path_segments = path .split ('/' )
1673- i = path_segments .index ('inputs' )+ 1
1673+ i = path_segments .index ('inputs' ) + 1
16741674 if path_segments [i ] == 'tcp' :
16751675 self .kind = path_segments [i ] + '/' + path_segments [i + 1 ]
16761676 else :
@@ -1684,28 +1684,49 @@ def __init__(self, service, path, kind=None, **kwargs):
16841684 if self .kind == 'splunktcp' :
16851685 self .kind = 'tcp/cooked'
16861686
1687+ @property
1688+ def restrictToHost (self ):
1689+ if 'restrictToHost' in self ._state .content :
1690+ return self ._state .content ['restrictToHost' ]
1691+ else :
1692+ return ''
1693+
16871694 def update (self , ** kwargs ):
16881695 if self .kind not in ['tcp' , 'splunktcp' , 'tcp/raw' , 'tcp/cooked' ]:
16891696 result = super (Input , self ).update (** kwargs )
16901697 return result
1691- elif 'restrictToHost' in kwargs and self .service .splunk_version < (5 ,):
1692- raise IllegalOperationException ("Updating restrictToHost has no effect before Splunk 5.0" )
16931698 else :
1699+ # TCP inputs have a property 'restrictToHost' which requires special care.
1700+
1701+ # There is a bug in Splunk < 5.0. Don't bother trying to update restrictToHost.
1702+ if 'restrictToHost' in kwargs and self .service .splunk_version < (5 ,):
1703+ raise IllegalOperationException ("Updating restrictToHost has no effect before Splunk 5.0" )
1704+
1705+ # In Splunk 4.x, if you update without restrictToHost as one of the fields,
1706+ # restrictToHost keeps its previous state. In 5.0, it is set to empty string.
1707+ # Thus, we must pass it every time. This doesn't actually introduce a race
1708+ # condition because if someone else has set restrictToHost to a new value on this
1709+ # TCP input, our update request will fail, since our reference to it still uses
1710+ # the old path.
1711+ to_update = kwargs .copy ()
1712+ to_update ['restrictToHost' ] = kwargs .get ('restrictToHost' , self ['restrictToHost' ])
1713+
1714+ # Do the actual update operation.
1715+ result = super (Input , self ).update (** to_update )
1716+
1717+ # Now we must update the path in case it changed.
1718+ # The pieces we break it into are:
1719+ # https://localhost:8089/services/data/inputs/tcp/raw/ boris: 10000
1720+ # |------------------ base_path -----------------------| | host || port|
1721+ assert self .path .endswith ('/' )
1722+ base_path = self .path .rsplit ('/' , 2 )[0 ]
1723+ host = to_update ['restrictToHost' ] + ':' if to_update ['restrictToHost' ] != '' else ''
16941724 port = self .name .split (':' , 1 )[- 1 ]
1695- new_kwargs = kwargs .copy ()
1696- if 'restrictToHost' not in new_kwargs and ':' in self .name :
1697- new_kwargs ['restrictToHost' ] = self ['restrictToHost' ]
1698- result = super (Input , self ).update (** new_kwargs )
1699- if self .path .endswith ('/' ):
1700- base_path , name = self .path .rsplit ('/' , 2 )[:- 1 ]
1701- else :
1702- base_path , name = self .path .rsplit ('/' , 1 )
1703- if 'restrictToHost' in new_kwargs and new_kwargs ['restrictToHost' ] != '' :
1704- self .path = base_path + '/' + new_kwargs ['restrictToHost' ] + ':' + port
1705- else :
1706- self .path = base_path + '/' + port
1725+ self .path = base_path + '/' + host + port
1726+
17071727 return result
17081728
1729+
17091730# Inputs is a "kinded" collection, which is a heterogenous collection where
17101731# each item is tagged with a kind, that provides a single merged view of all
17111732# input kinds.
@@ -1803,15 +1824,16 @@ def create(self, kind, name, **kwargs):
18031824 :return: The new input.
18041825 """
18051826 kindpath = self .kindpath (kind )
1806- if (kindpath == 'tcp/raw' or kindpath == 'tcp/cooked' or kindpath == 'udp' ) and \
1807- 'restrictToHost' in kwargs :
1808- post_name = name
1809- path_name = kwargs ['restrictToHost' ] + ':' + name
1810- else :
1811- post_name = name
1812- path_name = name
1813- self .post (kindpath , name = post_name , ** kwargs )
1814- path = _path (self .path + kindpath , path_name )
1827+ self .post (kindpath , name = name , ** kwargs )
1828+
1829+ # If we created an input with restrictToHost set, then
1830+ # its path will be <restrictToHost>:<name>, not just <name>,
1831+ # and we have to adjust accordingly.
1832+ path = _path (
1833+ self .path + kindpath ,
1834+ '%s:%s' % (kwargs ['restrictToHost' ], name ) \
1835+ if kwargs .has_key ('restrictToHost' ) else name
1836+ )
18151837 return Input (self .service , path , kind )
18161838
18171839 def delete (self , kind , name = None ):
0 commit comments