Skip to content

Commit d594b96

Browse files
Ramkishor ChaladiRamkishor Chaladi
authored andcommitted
Merge branch 'master' of https://github.com/softlayer/softlayer-python into issue_1732
testing
2 parents 7aab797 + 5c2793e commit d594b96

File tree

8 files changed

+257
-1
lines changed

8 files changed

+257
-1
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
"""Add a new load balancer protocol."""
2+
import click
3+
4+
import SoftLayer
5+
from SoftLayer.CLI import environment
6+
from SoftLayer.CLI import exceptions
7+
from SoftLayer.CLI import formatting
8+
9+
10+
# pylint: disable=unused-argument
11+
def parse_proto(ctx, param, value):
12+
"""Parses the frontend and backend cli options"""
13+
proto = {'protocol': 'HTTP', 'port': 80}
14+
splitout = value.split(':')
15+
if len(splitout) != 2:
16+
raise exceptions.ArgumentError("{}={} is not properly formatted.".format(param, value))
17+
proto['protocol'] = splitout[0]
18+
proto['port'] = int(splitout[1])
19+
return proto
20+
21+
22+
@click.command(cls=SoftLayer.CLI.command.SLCommand, )
23+
@click.argument('identifier')
24+
@click.option('--frontend', '-f', required=True, default='HTTP:80', show_default=True, callback=parse_proto,
25+
help='PROTOCOL:PORT string for incoming internet connections.')
26+
@click.option('--backend', '-b', required=True, default='HTTP:80', show_default=True, callback=parse_proto,
27+
help='PROTOCOL:PORT string for connecting to backend servers.')
28+
@click.option('--method', '-m', help="Balancing Method.", default='ROUNDROBIN', show_default=True,
29+
type=click.Choice(['ROUNDROBIN', 'LEASTCONNECTION', 'WEIGHTED_RR']))
30+
@click.option('--session', '-s', required=True,
31+
help="Session stickiness type. Valid values are SOURCE_IP or HTTP_COOKIE ")
32+
@click.option('--max', help="Max Connections setting", type=int)
33+
@environment.pass_env
34+
def cli(env, identifier, **args):
35+
"""Add a new load balancer protocol."""
36+
37+
mgr = SoftLayer.LoadBalancerManager(env.client)
38+
39+
uuid = mgr.get_lb_uuid(identifier)
40+
41+
backend = args.get('backend')
42+
frontend = args.get('frontend')
43+
protocols = [
44+
{
45+
"backendPort": backend.get('port'),
46+
"backendProtocol": backend.get('protocol'),
47+
"frontendPort": frontend.get('port'),
48+
"frontendProtocol": frontend.get('protocol'),
49+
"loadBalancingMethod": args.get('method'),
50+
"sessionType": args.get('session'),
51+
"maxConn": args.get('max')
52+
}
53+
]
54+
55+
protocol = mgr.add_protocols(uuid, protocols)
56+
57+
table = formatting.KeyValueTable(['name', 'value'])
58+
table.align['name'] = 'r'
59+
table.align['value'] = 'l'
60+
table.add_row(['Id', protocol.get('id')])
61+
table.add_row(['UUI', protocol.get('uuid')])
62+
table.add_row(['Address', protocol.get('address')])
63+
table.add_row(['Type', mgr.get_lb_type(protocol.get('type'))])
64+
table.add_row(['Description', protocol.get('description')])
65+
66+
env.fout(table)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""Delete load balancer protocol."""
2+
import click
3+
4+
import SoftLayer
5+
from SoftLayer.CLI import environment
6+
from SoftLayer.CLI import formatting
7+
8+
9+
@click.command(cls=SoftLayer.CLI.command.SLCommand, )
10+
@click.argument('identifier')
11+
@click.option('--uuid', help="Load Balancer Uuid.")
12+
@environment.pass_env
13+
def cli(env, identifier, uuid):
14+
"""delete a load balancer protocol."""
15+
16+
mgr = SoftLayer.LoadBalancerManager(env.client)
17+
18+
uuid_lb = mgr.get_lb(identifier)['uuid']
19+
20+
protocol = mgr.delete_protocol(uuid_lb, uuid)
21+
22+
table = formatting.KeyValueTable(['name', 'value'])
23+
table.align['name'] = 'r'
24+
table.align['value'] = 'l'
25+
table.add_row(['Id', protocol.get('id')])
26+
table.add_row(['UUI', protocol.get('uuid')])
27+
table.add_row(['Address', protocol.get('address')])
28+
table.add_row(['Type', SoftLayer.LoadBalancerManager.TYPE.get(protocol.get('type'))])
29+
table.add_row(['Description', protocol.get('description')])
30+
31+
env.fout(table)
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
"""Add a new load balancer protocol."""
2+
import click
3+
4+
import SoftLayer
5+
from SoftLayer.CLI import environment
6+
from SoftLayer.CLI import exceptions
7+
from SoftLayer.CLI import formatting
8+
9+
10+
# pylint: disable=unused-argument
11+
def parse_proto(ctx, param, value):
12+
"""Parses the frontend and backend cli options"""
13+
proto = {'protocol': 'HTTP', 'port': 80}
14+
splitout = value.split(':')
15+
if len(splitout) != 2:
16+
raise exceptions.ArgumentError("{}={} is not properly formatted.".format(param, value))
17+
proto['protocol'] = splitout[0]
18+
proto['port'] = int(splitout[1])
19+
return proto
20+
21+
22+
@click.command(cls=SoftLayer.CLI.command.SLCommand, )
23+
@click.argument('identifier')
24+
@click.option('--uuid', help="Load Balancer Uuid.")
25+
@click.option('--frontend', '-f', required=True, default='HTTP:80', show_default=True, callback=parse_proto,
26+
help='PROTOCOL:PORT string for incoming internet connections.')
27+
@click.option('--backend', '-b', required=True, default='HTTP:80', show_default=True, callback=parse_proto,
28+
help='PROTOCOL:PORT string for connecting to backend servers.')
29+
@click.option('--method', '-m', help="Balancing Method.", default='ROUNDROBIN', show_default=True,
30+
type=click.Choice(['ROUNDROBIN', 'LEASTCONNECTION', 'WEIGHTED_RR']))
31+
@click.option('--session', '-s', required=True,
32+
help="Session stickiness type. Valid values are SOURCE_IP or HTTP_COOKIE ")
33+
@click.option('--max', help="Max Connections setting", type=int)
34+
@environment.pass_env
35+
def cli(env, identifier, **args):
36+
"""Edit a load balancer protocol."""
37+
38+
mgr = SoftLayer.LoadBalancerManager(env.client)
39+
40+
uuid = mgr.get_lb_uuid(identifier)
41+
42+
backend = args.get('backend')
43+
frontend = args.get('frontend')
44+
protocol_configurations = [
45+
{
46+
"backendPort": backend.get('port'),
47+
"backendProtocol": backend.get('protocol'),
48+
"frontendPort": frontend.get('port'),
49+
"frontendProtocol": frontend.get('protocol'),
50+
"loadBalancingMethod": args.get('method'),
51+
"sessionType": args.get('session'),
52+
"maxConn": args.get('max')
53+
}
54+
]
55+
protocol_configurations[0]['listenerUuid'] = args.get('uuid')
56+
protocol = mgr.add_protocols(uuid, protocol_configurations)
57+
58+
table = formatting.KeyValueTable(['name', 'value'])
59+
table.align['name'] = 'r'
60+
table.align['value'] = 'l'
61+
table.add_row(['Id', protocol.get('id')])
62+
table.add_row(['UUI', protocol.get('uuid')])
63+
table.add_row(['Address', protocol.get('address')])
64+
table.add_row(['Type', mgr.get_lb_type(protocol.get('type'))])
65+
table.add_row(['Description', protocol.get('description')])
66+
67+
env.fout(table)

SoftLayer/CLI/routes.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,9 @@
244244
('loadbal:order', 'SoftLayer.CLI.loadbal.order:order'),
245245
('loadbal:order-options', 'SoftLayer.CLI.loadbal.order:order_options'),
246246
('loadbal:cancel', 'SoftLayer.CLI.loadbal.order:cancel'),
247+
('loadbal:protocol-add', 'SoftLayer.CLI.loadbal.protocol_add:cli'),
248+
('loadbal:protocol-edit', 'SoftLayer.CLI.loadbal.protocol_edit:cli'),
249+
('loadbal:protocol-delete', 'SoftLayer.CLI.loadbal.protocol_delete:cli'),
247250

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

SoftLayer/managers/load_balancer.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
66
:license: MIT, see LICENSE for more details.
77
"""
8+
import SoftLayer
89
from SoftLayer import exceptions
910
from SoftLayer.managers import ordering
1011
from SoftLayer import utils
@@ -70,7 +71,7 @@ def get_lb(self, identifier, mask=None):
7071
:returns: SoftLayer_Network_LBaaS_LoadBalancer
7172
"""
7273
if mask is None:
73-
mask = "mask[healthMonitors, l7Pools, members, sslCiphers, " \
74+
mask = "mask[healthMonitors, l7Pools, uuid, members, sslCiphers, " \
7475
"listeners[defaultPool[healthMonitor, members, sessionAffinity],l7Policies]]"
7576

7677
this_lb = self.lbaas.getObject(id=identifier, mask=mask)
@@ -275,3 +276,38 @@ def cancel_lbaas(self, uuid):
275276
"""
276277

277278
return self.lbaas.cancelLoadBalancer(uuid)
279+
280+
def add_protocols(self, uuid, protocols):
281+
"""This sample shows how to add protocols to the LBaaS.
282+
283+
https://softlayer.github.io/reference/services/SoftLayer_Network_LBaaS_Listener/updateLoadBalancerProtocols/
284+
:param uuid string: UUID of the LBaaS instance to add a new protocol
285+
:param protocols SoftLayer_Network_LBaaS_LoadBalancerProtocolConfiguration[]: protocol configurations
286+
"""
287+
return self.client.call('SoftLayer_Network_LBaaS_Listener', 'updateLoadBalancerProtocols',
288+
uuid, protocols)
289+
290+
def delete_protocol(self, uuid_lb, uuid):
291+
"""This sample shows how to delete protocols to the LBaaS.
292+
293+
https://softlayer.github.io/reference/services/SoftLayer_Network_LBaaS_Listener/updateLoadBalancerProtocols/
294+
:param uuid string: UUID of the LBaaS instance to add a new protocol
295+
:param protocols SoftLayer_Network_LBaaS_LoadBalancerProtocolConfiguration[]: protocol configurations
296+
"""
297+
return self.client.call('SoftLayer_Network_LBaaS_Listener', 'deleteLoadBalancerProtocols',
298+
uuid_lb, [uuid])
299+
300+
def get_lb_uuid(self, identifier):
301+
"""this sample show the uuid from loadbalancer.
302+
303+
:param identifier int: loadbalancer identifier.
304+
"""
305+
load_balancer = self.lbaas.getObject(id=identifier, mask="mask[id,uuid]")
306+
return load_balancer.get('uuid')
307+
308+
def get_lb_type(self, lb_type):
309+
"""return the loadbalancer type.
310+
311+
:param lb_type: load balancer type
312+
"""
313+
return SoftLayer.LoadBalancerManager.TYPE.get(lb_type)

docs/cli/loadbal.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ LBaaS Commands
5252
.. click:: SoftLayer.CLI.loadbal.order:cancel
5353
:prog: loadbal cancel
5454
:show-nested:
55+
.. click:: SoftLayer.CLI.loadbal.protocol_add:cli
56+
:prog: loadbal protocol-add
57+
:show-nested:
58+
.. click:: SoftLayer.CLI.loadbal.protocol_edit:cli
59+
:prog: loadbal protocol-edit
60+
:show-nested:
61+
.. click:: SoftLayer.CLI.loadbal.protocol_delete:cli
62+
:prog: loadbal protocol-delete
63+
:show-nested:
5564

5665

5766
NetScaler Commands

tests/CLI/modules/loadbal_tests.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,3 +315,23 @@ def test_ns_detail(self):
315315
result = self.run_command(['loadbal', 'ns-detail', '11111'])
316316

317317
self.assert_no_fail(result)
318+
319+
def test_lb_protocol_add(self):
320+
result = self.run_command(['lb', 'protocol-add', '1111111', '--backend', 'HTTP:216',
321+
'--frontend', 'HTTP:217', '--method', 'ROUNDROBIN', '-s', 'SOURCE_IP',
322+
'--max', '190'])
323+
self.assert_no_fail(result)
324+
325+
def test_lb_protocol_edit(self):
326+
result = self.run_command(['lb', 'protocol-edit', '1111111',
327+
'--uuid', '4d088a44-8c42-4f24-a437-9461ff4c9851', '--backend', 'HTTP:216',
328+
'--frontend', 'HTTP:217', '--method', 'ROUNDROBIN', '-s', 'SOURCE_IP',
329+
'--max', '190'])
330+
331+
self.assert_no_fail(result)
332+
333+
def test_lb_protocol_delete(self):
334+
result = self.run_command(['lb', 'protocol-delete', '1111111',
335+
'--uuid', '4d088a44-8c42-4f24-a437-9461ff4c9851'])
336+
337+
self.assert_no_fail(result)

tests/managers/loadbal_tests.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"""
1010
import SoftLayer
1111
from SoftLayer import exceptions
12+
from SoftLayer import fixtures
1213
from SoftLayer.fixtures import SoftLayer_Network_LBaaS_LoadBalancer
1314
from SoftLayer import testing
1415

@@ -209,3 +210,26 @@ def test_get_lbaas_by_name_fails(self):
209210
load_bal_mock.return_value = []
210211
name = 'test'
211212
self.assertRaises(exceptions.SoftLayerError, self.lb_mgr.get_lbaas_by_name, name)
213+
214+
def test_lbaas_update_protocol(self):
215+
uuid = '1a1aa111-4474-4e16-9f02-4de959229b85'
216+
protocol_configurations = [
217+
{
218+
"backendPort": 52,
219+
"backendProtocol": 'HTTP',
220+
"frontendPort": 216,
221+
"frontendProtocol": 'HTTP',
222+
"loadBalancingMethod": 'ROUNDROBIN',
223+
"sessionType": 'SOURCE_IP',
224+
"maxConn": 50
225+
}
226+
]
227+
result = self.lb_mgr.add_protocols(uuid, protocol_configurations)
228+
self.assertEqual(fixtures.SoftLayer_Network_LBaaS_Listener.updateLoadBalancerProtocols, result)
229+
230+
def test_lbaas_delete_protocol(self):
231+
uuid = '1a1aa111-4474-4e16-9f02-4de959229b85'
232+
uuid_delete = 'abba-aabb-cc'
233+
234+
result = self.lb_mgr.delete_protocol(uuid, uuid_delete)
235+
self.assertEqual(fixtures.SoftLayer_Network_LBaaS_Listener.deleteLoadBalancerProtocols, result)

0 commit comments

Comments
 (0)