Skip to content

Commit feab68d

Browse files
LB pool management commands
1 parent 1b907bf commit feab68d

3 files changed

Lines changed: 148 additions & 0 deletions

File tree

SoftLayer/CLI/loadbal/pools.py

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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')

SoftLayer/CLI/routes.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@
172172
('loadbal:health', 'SoftLayer.CLI.loadbal.health:cli'),
173173
('loadbal:member-add', 'SoftLayer.CLI.loadbal.members:add'),
174174
('loadbal:member-del', 'SoftLayer.CLI.loadbal.members:remove'),
175+
('loadbal:pool-add', 'SoftLayer.CLI.loadbal.pools:add'),
176+
('loadbal:pool-edit', 'SoftLayer.CLI.loadbal.pools:edit'),
177+
('loadbal:pool-del', 'SoftLayer.CLI.loadbal.pools:delete'),
175178

176179
('loadbal:ns-detail', 'SoftLayer.CLI.loadbal.ns_detail:cli'),
177180
('loadbal:ns-list', 'SoftLayer.CLI.loadbal.ns_list:cli'),

SoftLayer/managers/load_balancer.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,32 @@ def add_lb_member(self, identifier, member_id):
126126

127127
return result
128128

129+
def add_lb_listener(self, identifier, listener):
130+
"""Adds or update a listener to a LBaaS instance
131+
132+
When using this to update a listener, just include the 'listenerUuid' in the listener object
133+
See the following for listener configuration options
134+
https://sldn.softlayer.com/reference/datatypes/SoftLayer_Network_LBaaS_LoadBalancerProtocolConfiguration/
135+
136+
:param identifier: UUID of the LBaaS instance
137+
:param listener: Object with all listener configurations
138+
"""
139+
140+
result = self.client.call('SoftLayer_Network_LBaaS_Listener', 'updateLoadBalancerProtocols',
141+
identifier, [listener])
142+
return result
143+
144+
def remove_lb_listener(self, identifier, listener):
145+
"""Removes a listener to a LBaaS instance
146+
147+
:param identifier: UUID of the LBaaS instance
148+
:param listener: UUID of the Listner to be removed.
149+
"""
150+
151+
result = self.client.call('SoftLayer_Network_LBaaS_Listener', 'deleteLoadBalancerProtocols',
152+
identifier, [listener])
153+
return result
154+
129155
# Old things below this line
130156

131157
def get_lb_pkgs(self):

0 commit comments

Comments
 (0)