|
| 1 | +"""List hosts with access to volume.""" |
| 2 | +# :license: MIT, see LICENSE for more details. |
| 3 | + |
| 4 | +import SoftLayer |
| 5 | +from SoftLayer.CLI import columns as column_helper |
| 6 | +from SoftLayer.CLI import environment |
| 7 | +from SoftLayer.CLI import formatting |
| 8 | + |
| 9 | +import click |
| 10 | + |
| 11 | +COLUMNS = [ |
| 12 | + column_helper.Column('id', ('id',)), |
| 13 | + column_helper.Column('hostname', ('hostname',)), |
| 14 | + column_helper.Column('type', ('type',)), |
| 15 | + column_helper.Column('primaryBackendIpAddress', ('primaryBackendIpAddress',)), |
| 16 | +] |
| 17 | + |
| 18 | +DEFAULT_COLUMNS = [ |
| 19 | + 'id', |
| 20 | + 'hostname', |
| 21 | + 'type', |
| 22 | + 'primaryBackendIpAddress', |
| 23 | +] |
| 24 | + |
| 25 | + |
| 26 | +@click.command() |
| 27 | +@click.argument('volume_id') |
| 28 | +@click.option('--sortby', help='Column to sort by', default='hostname') |
| 29 | +@click.option('--columns', |
| 30 | + callback=column_helper.get_formatter(COLUMNS), |
| 31 | + help='Columns to display. Options: {0}'.format( |
| 32 | + ', '.join(column.name for column in COLUMNS)), |
| 33 | + default=','.join(DEFAULT_COLUMNS)) |
| 34 | +@environment.pass_env |
| 35 | +def cli(env, columns, sortby, volume_id): |
| 36 | + """List block storage.""" |
| 37 | + block_manager = SoftLayer.BlockStorageManager(env.client) |
| 38 | + access_list = block_manager.get_block_volume_access_list(volume_id=volume_id) |
| 39 | + table = formatting.Table(columns.columns) |
| 40 | + table.sortby = sortby |
| 41 | + |
| 42 | + if access_list: |
| 43 | + if 'allowedVirtualGuests' in access_list.keys(): |
| 44 | + for host in access_list['allowedVirtualGuests']: |
| 45 | + host['type'] = 'VIRTUAL' |
| 46 | + host['hostname'] = "{0}.{1}".format( |
| 47 | + host['hostname'], |
| 48 | + host['domain']) |
| 49 | + table.add_row([value or formatting.blank() |
| 50 | + for value in columns.row(host)]) |
| 51 | + |
| 52 | + if 'allowedHardware' in access_list.keys(): |
| 53 | + for host in access_list['allowedHardware']: |
| 54 | + host['type'] = 'HARDWARE' |
| 55 | + host['hostname'] = "{0}.{1}".format( |
| 56 | + host['hostname'], |
| 57 | + host['domain']) |
| 58 | + table.add_row([value or formatting.blank() |
| 59 | + for value in columns.row(host)]) |
| 60 | + |
| 61 | + if 'allowedSubnets' in access_list.keys(): |
| 62 | + for host in access_list['allowedSubnets']: |
| 63 | + host['type'] = 'SUBNET' |
| 64 | + if 'note' in host.keys(): |
| 65 | + host['hostname'] = "{0}/{1} ({3})".format( |
| 66 | + host['networkIdentifier'], |
| 67 | + host['cidr'], |
| 68 | + host['note'], |
| 69 | + ) |
| 70 | + else: |
| 71 | + host['hostname'] = "{0}/{1}".format( |
| 72 | + host['networkIdentifier'], |
| 73 | + host['cidr'] |
| 74 | + ) |
| 75 | + table.add_row([value or formatting.blank() |
| 76 | + for value in columns.row(host)]) |
| 77 | + |
| 78 | + if 'allowedIpAddresses' in access_list.keys(): |
| 79 | + for host in access_list['allowedIpAddresses']: |
| 80 | + host['type'] = 'IP' |
| 81 | + if 'note' in host.keys(): |
| 82 | + host['hostname'] = "{0} ({1})".format( |
| 83 | + host['ipAddress'], |
| 84 | + host['note'] |
| 85 | + ) |
| 86 | + else: |
| 87 | + host['hostname'] = host['ipAddress'] |
| 88 | + table.add_row([value or formatting.blank() |
| 89 | + for value in columns.row(host)]) |
| 90 | + |
| 91 | + env.fout(table) |
| 92 | + else: |
| 93 | + click.echo("No authorized hosts found.") |
0 commit comments