1+ """Manage LBaaS Pools/Listeners."""
2+ import click
3+
4+ import SoftLayer
5+ from SoftLayer .CLI import environment , formatting , helpers
6+ from SoftLayer .exceptions import SoftLayerAPIError
7+ from SoftLayer import utils
8+ from pprint import pprint as pp
9+
10+
11+ def sticky_option (ctx , param , value ):
12+ if value :
13+ return 'SOURCE_IP'
14+ return None
15+
16+ @click .command ()
17+ @click .argument ('identifier' )
18+ @click .option ('--frontProtocol' , '-P' , default = 'HTTP' , type = click .Choice (['HTTP' , 'HTTPS' , 'TCP' ]), show_default = True ,
19+ help = "Protocol type to use for incoming connections" )
20+ @click .option ('--backProtocol' , '-p' , type = click .Choice (['HTTP' , 'HTTPS' , 'TCP' ]),
21+ help = "Protocol type to use when connecting to backend servers. Defaults to whatever --frontProtocol is." )
22+ @click .option ('--frontPort' , '-f' , required = True , type = int , help = "Internet side port" )
23+ @click .option ('--backPort' , '-b' , required = True , type = int , help = "Private side port" )
24+ @click .option ('--method' , '-m' , default = 'ROUNDROBIN' , show_default = True , help = "Balancing Method" ,
25+ type = click .Choice (['ROUNDROBIN' , 'LEASTCONNECTION' , 'WEIGHTED_RR' ]))
26+ @click .option ('--connections' , '-c' , type = int , help = "Maximum number of connections to allow." )
27+ @click .option ('--sticky' , '-s' , is_flag = True , callback = sticky_option , help = "Make sessions sticky based on source_ip." )
28+ @environment .pass_env
29+ def add (env , identifier , ** args ):
30+ """Adds a listener to the identifier LB"""
31+
32+ mgr = SoftLayer .LoadBalancerManager (env .client )
33+ uuid , lbid = mgr .get_lbaas_uuid_id (identifier )
34+
35+ new_listener = {
36+ 'backendPort' : args .get ('backport' ),
37+ 'backendProtocol' : args .get ('backprotocol' ) if args .get ('backprotocol' ) else args .get ('frontprotocol' ),
38+ 'frontendPort' : args .get ('frontport' ),
39+ 'frontendProtocol' : args .get ('frontprotocol' ),
40+ 'loadBalancingMethod' : args .get ('method' ),
41+ 'maxConn' : args .get ('connections' , None ),
42+ 'sessionType' : args .get ('sticky' ),
43+ 'tlsCertificateId' : None
44+ }
45+
46+ try :
47+ result = mgr .add_lb_listener (uuid , new_listener )
48+ click .secho ("Success" , fg = 'green' )
49+ except SoftLayerAPIError as e :
50+ click .secho ("ERROR: {}" .format (e .faultString ), fg = 'red' )
51+
52+
53+ @click .command ()
54+ @click .argument ('identifier' )
55+ @click .argument ('listener' )
56+ @click .option ('--frontProtocol' , '-P' , type = click .Choice (['HTTP' , 'HTTPS' , 'TCP' ]),
57+ help = "Protocol type to use for incoming connections" )
58+ @click .option ('--backProtocol' , '-p' , type = click .Choice (['HTTP' , 'HTTPS' , 'TCP' ]),
59+ help = "Protocol type to use when connecting to backend servers. Defaults to whatever --frontProtocol is." )
60+ @click .option ('--frontPort' , '-f' , type = int , help = "Internet side port" )
61+ @click .option ('--backPort' , '-b' , type = int , help = "Private side port" )
62+ @click .option ('--method' , '-m' , help = "Balancing Method" ,
63+ type = click .Choice (['ROUNDROBIN' , 'LEASTCONNECTION' , 'WEIGHTED_RR' ]))
64+ @click .option ('--connections' , '-c' , type = int , help = "Maximum number of connections to allow." )
65+ @click .option ('--sticky' , '-s' , is_flag = True , callback = sticky_option , help = "Make sessions sticky based on source_ip." )
66+ @environment .pass_env
67+ def edit (env , identifier , listener , ** args ):
68+ """Updates a listener's configuration.
69+
70+ LISTENER should be a UUID, and can be found from `slcli lb detail <IDENTIFIER>`
71+ """
72+
73+ mgr = SoftLayer .LoadBalancerManager (env .client )
74+ uuid , lbid = mgr .get_lbaas_uuid_id (identifier )
75+
76+
77+ new_listener = {
78+ 'listenerUuid' : listener
79+ }
80+
81+ arg_to_option = {
82+ 'frontprotocol' : 'frontendProtocol' ,
83+ 'backprotocol' : 'backendProtocol' ,
84+ 'frontport' : 'frontendPort' ,
85+ 'backport' : 'backendPort' ,
86+ 'method' : 'loadBalancingMethod' ,
87+ 'connections' : 'maxConn' ,
88+ 'sticky' : 'sessionType' ,
89+ 'sslcert' : 'tlsCertificateId'
90+ }
91+
92+ for arg in args .keys ():
93+ if args [arg ]:
94+ new_listener [arg_to_option [arg ]] = args [arg ]
95+
96+ try :
97+ result = mgr .add_lb_listener (uuid , new_listener )
98+ click .secho ("Success" , fg = 'green' )
99+ except SoftLayerAPIError as e :
100+ click .secho ("ERROR: {}" .format (e .faultString ), fg = 'red' )
101+
102+
103+ @click .command ()
104+ @click .argument ('identifier' )
105+ @click .argument ('listener' )
106+ @environment .pass_env
107+ def delete (env , identifier , listener ):
108+ """Removes the listener from identified LBaaS instance
109+
110+ LISTENER should be a UUID, and can be found from `slcli lb detail <IDENTIFIER>`
111+ """
112+
113+ mgr = SoftLayer .LoadBalancerManager (env .client )
114+ uuid , lbid = mgr .get_lbaas_uuid_id (identifier )
115+ try :
116+ result = mgr .remove_lb_listener (uuid , listener )
117+ click .secho ("Success" , fg = 'green' )
118+ except SoftLayerAPIError as e :
119+ click .secho ("ERROR: {}" .format (e .faultString ), fg = 'red' )
0 commit comments