1010import riemann_client .client
1111import riemann_client .transport
1212
13- TRANSPORT_CLASSES = {
14- 'udp' : riemann_client .transport .UDPTransport ,
15- 'tcp' : riemann_client .transport .TCPTransport
13+ __all__ = ['main' ]
14+
15+
16+ def udp_transport_factory (args ):
17+ return riemann_client .transport .UDPTransport (args .host , args .port )
18+
19+
20+ def tcp_transport_factory (args ):
21+ return riemann_client .transport .TCPTransport (args .host , args .port )
22+
23+
24+ def tls_transport_factory (args ):
25+ if args .ca_certs is None :
26+ parser .error ('--ca-certs must be set when using the TLS transport' )
27+ return riemann_client .transport .TLSTransport (
28+ args .host , args .port , args .ca_certs )
29+
30+
31+ TRANSPORT_FACTORIES = {
32+ 'udp' : udp_transport_factory ,
33+ 'tcp' : tcp_transport_factory ,
34+ 'tls' : tls_transport_factory
1635}
1736
1837parser = argparse .ArgumentParser (add_help = False , description = (
4261 help = "The port to connect to the Riemann server on (environ: %(default)s)" )
4362
4463parser .add_argument (
45- '-t' , '--transport' , choices = TRANSPORT_CLASSES .keys (), default = 'tcp' ,
64+ '-c' , '--ca-certs' , type = str , metavar = 'CERT' ,
65+ help = "The path to the CA certificate bundle to use with the TLS transport" )
66+
67+ parser .add_argument (
68+ '-t' , '--transport' , choices = TRANSPORT_FACTORIES .keys (), default = 'tcp' ,
4669 help = "The transport to use (default: %(default)s)" )
4770
4871subparsers = parser .add_subparsers (dest = 'subparser' )
@@ -86,9 +109,7 @@ def send_function(args, client):
86109
87110
88111def query_function (args , client ):
89- """Queries the Riemann index and outputs the returned events as JSON
90-
91- UDP is not supported when querying"""
112+ """Queries the Riemann index and outputs the returned events as JSON"""
92113 print (json .dumps (client .query (args .query ), sort_keys = True , indent = 2 ))
93114
94115query = subparsers .add_parser ('query' , help = 'Query the Riemann index' )
@@ -99,13 +120,12 @@ def query_function(args, client):
99120def main ():
100121 args = parser .parse_args ()
101122
102- transport_class = TRANSPORT_CLASSES [args .transport ]
103- transport = transport_class (args .host , args .port )
123+ transport = TRANSPORT_FACTORIES [args .transport ](args )
104124
105125 with riemann_client .client .Client (transport = transport ) as client :
106126 try :
107127 args .function (args , client )
108128 except riemann_client .transport .RiemannError as exception :
109- print ("The Riemann server responded with an error: {}" .format (
129+ print ("The Riemann server responded with an error: {0 }" .format (
110130 exception .message ), file = sys .stderr )
111131 exit (1 )
0 commit comments