|
| 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) |
0 commit comments