Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LB protocol features #1999

Merged
merged 5 commits into from Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
66 changes: 66 additions & 0 deletions SoftLayer/CLI/loadbal/protocol_add.py
@@ -0,0 +1,66 @@
"""Add a new load balancer protocol."""
import click

import SoftLayer
from SoftLayer.CLI import environment
from SoftLayer.CLI import exceptions
from SoftLayer.CLI import formatting


# pylint: disable=unused-argument
def parse_proto(ctx, param, value):
"""Parses the frontend and backend cli options"""
proto = {'protocol': 'HTTP', 'port': 80}
splitout = value.split(':')
if len(splitout) != 2:
raise exceptions.ArgumentError("{}={} is not properly formatted.".format(param, value))
proto['protocol'] = splitout[0]
proto['port'] = int(splitout[1])
return proto


@click.command(cls=SoftLayer.CLI.command.SLCommand, )
@click.argument('identifier')
@click.option('--frontend', '-f', required=True, default='HTTP:80', show_default=True, callback=parse_proto,
help='PROTOCOL:PORT string for incoming internet connections.')
@click.option('--backend', '-b', required=True, default='HTTP:80', show_default=True, callback=parse_proto,
help='PROTOCOL:PORT string for connecting to backend servers.')
@click.option('--method', '-m', help="Balancing Method.", default='ROUNDROBIN', show_default=True,
type=click.Choice(['ROUNDROBIN', 'LEASTCONNECTION', 'WEIGHTED_RR']))
@click.option('--session', '-s', required=True,
help="Session stickiness type. Valid values are “SOURCE_IP” “HTTP_COOKIE”")
caberos marked this conversation as resolved.
Show resolved Hide resolved
@click.option('--max', help="Use a Public to Public loadbalancer.")
caberos marked this conversation as resolved.
Show resolved Hide resolved
@environment.pass_env
def cli(env, identifier, **args):
"""Add a new load balancer protocol."""

mgr = SoftLayer.LoadBalancerManager(env.client)

uuid = mgr.get_lb(identifier)['uuid']
caberos marked this conversation as resolved.
Show resolved Hide resolved

backend = args.get('backend')
frontend = args.get('frontend')
protocols = [
{
"backendPort": backend.get('port'),
"backendProtocol": backend.get('protocol'),
"frontendPort": frontend.get('port'),
"frontendProtocol": frontend.get('protocol'),
"loadBalancingMethod": args.get('method'),
"sessionType": args.get('session'),
"maxConn": args.get('max')
}
]

protocol = mgr.add_protocols(uuid, protocols)

table = formatting.KeyValueTable(['name', 'value'])
table.align['name'] = 'r'
table.align['value'] = 'l'
table.add_row(['Id', protocol.get('id')])
table.add_row(['UUI', protocol.get('uuid')])
table.add_row(['Address', protocol.get('address')])
table.add_row(['Type', SoftLayer.LoadBalancerManager.TYPE.get(protocol.get('type'))])
table.add_row(['Description', protocol.get('description')])
caberos marked this conversation as resolved.
Show resolved Hide resolved

env.fout(table)
31 changes: 31 additions & 0 deletions SoftLayer/CLI/loadbal/protocol_delete.py
@@ -0,0 +1,31 @@
"""Delete load balancer protocol."""
import click

import SoftLayer
from SoftLayer.CLI import environment
from SoftLayer.CLI import formatting


@click.command(cls=SoftLayer.CLI.command.SLCommand, )
@click.argument('identifier')
@click.option('--uuid', help="Load Balancer Uuid.")
@environment.pass_env
def cli(env, identifier, uuid):
"""delete a load balancer protocol."""

mgr = SoftLayer.LoadBalancerManager(env.client)

uuid_lb = mgr.get_lb(identifier)['uuid']

protocol = mgr.delete_protocol(uuid_lb, uuid)

table = formatting.KeyValueTable(['name', 'value'])
table.align['name'] = 'r'
table.align['value'] = 'l'
table.add_row(['Id', protocol.get('id')])
table.add_row(['UUI', protocol.get('uuid')])
table.add_row(['Address', protocol.get('address')])
table.add_row(['Type', SoftLayer.LoadBalancerManager.TYPE.get(protocol.get('type'))])
table.add_row(['Description', protocol.get('description')])
caberos marked this conversation as resolved.
Show resolved Hide resolved

env.fout(table)
67 changes: 67 additions & 0 deletions SoftLayer/CLI/loadbal/protocol_edit.py
@@ -0,0 +1,67 @@
"""Add a new load balancer protocol."""
import click

import SoftLayer
from SoftLayer.CLI import environment
from SoftLayer.CLI import exceptions
from SoftLayer.CLI import formatting


# pylint: disable=unused-argument
def parse_proto(ctx, param, value):
"""Parses the frontend and backend cli options"""
proto = {'protocol': 'HTTP', 'port': 80}
splitout = value.split(':')
if len(splitout) != 2:
raise exceptions.ArgumentError("{}={} is not properly formatted.".format(param, value))
proto['protocol'] = splitout[0]
proto['port'] = int(splitout[1])
return proto


@click.command(cls=SoftLayer.CLI.command.SLCommand, )
@click.argument('identifier')
@click.option('--uuid', help="Load Balancer Uuid.")
@click.option('--frontend', '-f', required=True, default='HTTP:80', show_default=True, callback=parse_proto,
help='PROTOCOL:PORT string for incoming internet connections.')
@click.option('--backend', '-b', required=True, default='HTTP:80', show_default=True, callback=parse_proto,
help='PROTOCOL:PORT string for connecting to backend servers.')
@click.option('--method', '-m', help="Balancing Method.", default='ROUNDROBIN', show_default=True,
type=click.Choice(['ROUNDROBIN', 'LEASTCONNECTION', 'WEIGHTED_RR']))
@click.option('--session', '-s', required=True,
help="Session stickiness type. Valid values are “SOURCE_IP” “HTTP_COOKIE”")
@click.option('--max', help="Use a Public to Public loadbalancer.")
@environment.pass_env
def cli(env, identifier, **args):
"""Edit a load balancer protocol."""

mgr = SoftLayer.LoadBalancerManager(env.client)

uuid_lb = mgr.get_lb(identifier)['uuid']

backend = args.get('backend')
frontend = args.get('frontend')
protocol_configurations = [
{
"backendPort": backend.get('port'),
"backendProtocol": backend.get('protocol'),
"frontendPort": frontend.get('port'),
"frontendProtocol": frontend.get('protocol'),
"loadBalancingMethod": args.get('method'),
"sessionType": args.get('session'),
"maxConn": args.get('max')
}
]
protocol_configurations[0]['listenerUuid'] = args.get('uuid')
protocol = mgr.add_protocols(uuid_lb, protocol_configurations)

table = formatting.KeyValueTable(['name', 'value'])
table.align['name'] = 'r'
table.align['value'] = 'l'
table.add_row(['Id', protocol.get('id')])
table.add_row(['UUI', protocol.get('uuid')])
table.add_row(['Address', protocol.get('address')])
table.add_row(['Type', SoftLayer.LoadBalancerManager.TYPE.get(protocol.get('type'))])
caberos marked this conversation as resolved.
Show resolved Hide resolved
table.add_row(['Description', protocol.get('description')])
caberos marked this conversation as resolved.
Show resolved Hide resolved

env.fout(table)
3 changes: 3 additions & 0 deletions SoftLayer/CLI/routes.py
Expand Up @@ -244,6 +244,9 @@
('loadbal:order', 'SoftLayer.CLI.loadbal.order:order'),
('loadbal:order-options', 'SoftLayer.CLI.loadbal.order:order_options'),
('loadbal:cancel', 'SoftLayer.CLI.loadbal.order:cancel'),
('loadbal:protocol-add', 'SoftLayer.CLI.loadbal.protocol_add:cli'),
('loadbal:protocol-edit', 'SoftLayer.CLI.loadbal.protocol_edit:cli'),
('loadbal:protocol-delete', 'SoftLayer.CLI.loadbal.protocol_delete:cli'),

('loadbal:ns-detail', 'SoftLayer.CLI.loadbal.ns_detail:cli'),
('loadbal:ns-list', 'SoftLayer.CLI.loadbal.ns_list:cli'),
Expand Down
22 changes: 21 additions & 1 deletion SoftLayer/managers/load_balancer.py
Expand Up @@ -70,7 +70,7 @@ def get_lb(self, identifier, mask=None):
:returns: SoftLayer_Network_LBaaS_LoadBalancer
"""
if mask is None:
mask = "mask[healthMonitors, l7Pools, members, sslCiphers, " \
mask = "mask[healthMonitors, l7Pools, uuid, members, sslCiphers, " \
"listeners[defaultPool[healthMonitor, members, sessionAffinity],l7Policies]]"

this_lb = self.lbaas.getObject(id=identifier, mask=mask)
Expand Down Expand Up @@ -275,3 +275,23 @@ def cancel_lbaas(self, uuid):
"""

return self.lbaas.cancelLoadBalancer(uuid)

def add_protocols(self, uuid, protocols):
"""This sample shows how to add protocols to the LBaaS.

https://softlayer.github.io/reference/services/SoftLayer_Network_LBaaS_Listener/updateLoadBalancerProtocols/
:param uuid string: UUID of the LBaaS instance to add a new protocol
:param protocols SoftLayer_Network_LBaaS_LoadBalancerProtocolConfiguration[]: protocol configurations
"""
return self.client.call('SoftLayer_Network_LBaaS_Listener', 'updateLoadBalancerProtocols',
uuid, protocols)

def delete_protocol(self, uuid_lb, uuid):
"""This sample shows how to delete protocols to the LBaaS.

https://softlayer.github.io/reference/services/SoftLayer_Network_LBaaS_Listener/updateLoadBalancerProtocols/
:param uuid string: UUID of the LBaaS instance to add a new protocol
:param protocols SoftLayer_Network_LBaaS_LoadBalancerProtocolConfiguration[]: protocol configurations
"""
return self.client.call('SoftLayer_Network_LBaaS_Listener', 'deleteLoadBalancerProtocols',
uuid_lb, [uuid])
9 changes: 9 additions & 0 deletions docs/cli/loadbal.rst
Expand Up @@ -52,6 +52,15 @@ LBaaS Commands
.. click:: SoftLayer.CLI.loadbal.order:cancel
:prog: loadbal cancel
:show-nested:
.. click:: SoftLayer.CLI.loadbal.protocol_add:cli
:prog: loadbal protocol-add
:show-nested:
.. click:: SoftLayer.CLI.loadbal.protocol_edit:cli
:prog: loadbal protocol-edit
:show-nested:
.. click:: SoftLayer.CLI.loadbal.protocol_delete:cli
:prog: loadbal protocol-delete
:show-nested:


NetScaler Commands
Expand Down
20 changes: 20 additions & 0 deletions tests/CLI/modules/loadbal_tests.py
Expand Up @@ -315,3 +315,23 @@ def test_ns_detail(self):
result = self.run_command(['loadbal', 'ns-detail', '11111'])

self.assert_no_fail(result)

def test_lb_protocol_add(self):
result = self.run_command(['lb', 'protocol-add', '1111111', '--backend', 'HTTP:216',
'--frontend', 'HTTP:217', '--method', 'ROUNDROBIN', '-s', 'SOURCE_IP',
'--max', '190'])
self.assert_no_fail(result)

def test_lb_protocol_edit(self):
result = self.run_command(['lb', 'protocol-edit', '1111111',
'--uuid', '4d088a44-8c42-4f24-a437-9461ff4c9851', '--backend', 'HTTP:216',
'--frontend', 'HTTP:217', '--method', 'ROUNDROBIN', '-s', 'SOURCE_IP',
'--max', '190'])

self.assert_no_fail(result)

def test_lb_protocol_delete(self):
result = self.run_command(['lb', 'protocol-delete', '1111111',
'--uuid', '4d088a44-8c42-4f24-a437-9461ff4c9851'])

self.assert_no_fail(result)
24 changes: 24 additions & 0 deletions tests/managers/loadbal_tests.py
Expand Up @@ -9,6 +9,7 @@
"""
import SoftLayer
from SoftLayer import exceptions
from SoftLayer import fixtures
from SoftLayer.fixtures import SoftLayer_Network_LBaaS_LoadBalancer
from SoftLayer import testing

Expand Down Expand Up @@ -209,3 +210,26 @@ def test_get_lbaas_by_name_fails(self):
load_bal_mock.return_value = []
name = 'test'
self.assertRaises(exceptions.SoftLayerError, self.lb_mgr.get_lbaas_by_name, name)

def test_lbaas_update_protocol(self):
uuid = '1a1aa111-4474-4e16-9f02-4de959229b85'
protocol_configurations = [
{
"backendPort": 52,
"backendProtocol": 'HTTP',
"frontendPort": 216,
"frontendProtocol": 'HTTP',
"loadBalancingMethod": 'ROUNDROBIN',
"sessionType": 'SOURCE_IP',
"maxConn": 50
}
]
result = self.lb_mgr.add_protocols(uuid, protocol_configurations)
self.assertEqual(fixtures.SoftLayer_Network_LBaaS_Listener.updateLoadBalancerProtocols, result)

def test_lbaas_delete_protocol(self):
uuid = '1a1aa111-4474-4e16-9f02-4de959229b85'
uuid_delete = 'abba-aabb-cc'

result = self.lb_mgr.delete_protocol(uuid, uuid_delete)
self.assertEqual(fixtures.SoftLayer_Network_LBaaS_Listener.deleteLoadBalancerProtocols, result)