|
| 1 | +"""List Layer7 policies""" |
| 2 | +import click |
| 3 | + |
| 4 | +import SoftLayer |
| 5 | +from SoftLayer.CLI import environment |
| 6 | +from SoftLayer.CLI import formatting |
| 7 | + |
| 8 | + |
| 9 | +@click.command() |
| 10 | +@click.option('--protocol-id', '-p', |
| 11 | + required=False, |
| 12 | + type=int, |
| 13 | + help="Front-end Protocol identifier") |
| 14 | +@environment.pass_env |
| 15 | +def policies(env, protocol_id): |
| 16 | + """List policies of the front-end protocol (listener).""" |
| 17 | + mgr = SoftLayer.LoadBalancerManager(env.client) |
| 18 | + |
| 19 | + if protocol_id: |
| 20 | + l7policies = mgr.get_l7policies(protocol_id) |
| 21 | + table = generate_l7policies_table(l7policies, protocol_id) |
| 22 | + else: |
| 23 | + l7policies = mgr.get_all_l7policies() |
| 24 | + table = l7policies_table(l7policies) |
| 25 | + env.fout(table) |
| 26 | + |
| 27 | + |
| 28 | +def generate_l7policies_table(l7policies, identifier): |
| 29 | + """Takes a list of Layer7 policies and makes a table""" |
| 30 | + table = formatting.Table([ |
| 31 | + 'Id', 'UUID', 'Name', 'Action', 'Redirect', 'Priority', 'Create Date' |
| 32 | + ], title=f"Layer7 policies - protocol ID {identifier}") |
| 33 | + |
| 34 | + table.align['Name'] = 'l' |
| 35 | + table.align['Action'] = 'l' |
| 36 | + table.align['Redirect'] = 'l' |
| 37 | + for l7policy in sorted(l7policies, key=lambda data: data.get('priority')): |
| 38 | + table.add_row([ |
| 39 | + l7policy.get('id'), |
| 40 | + l7policy.get('uuid'), |
| 41 | + l7policy.get('name'), |
| 42 | + l7policy.get('action'), |
| 43 | + l7policy.get('redirectL7PoolId') or l7policy.get('redirectUrl') or formatting.blank(), |
| 44 | + l7policy.get('priority'), |
| 45 | + l7policy.get('createDate'), |
| 46 | + ]) |
| 47 | + return table |
| 48 | + |
| 49 | + |
| 50 | +def l7policies_table(listeners): |
| 51 | + """Takes a dict of (protocols: policies list) and makes a list of tables""" |
| 52 | + tables = [] |
| 53 | + for listener_id, list_policy in listeners.items(): |
| 54 | + tables.append(generate_l7policies_table(list_policy, listener_id)) |
| 55 | + return tables |
0 commit comments