|
| 1 | +"""Get storage details for a virtual server.""" |
| 2 | +# :license: MIT, see LICENSE for more details. |
| 3 | + |
| 4 | +import click |
| 5 | + |
| 6 | +import SoftLayer |
| 7 | +from SoftLayer.CLI import environment |
| 8 | +from SoftLayer.CLI import formatting |
| 9 | +from SoftLayer.CLI import helpers |
| 10 | + |
| 11 | + |
| 12 | +@click.command() |
| 13 | +@click.argument('identifier') |
| 14 | +@environment.pass_env |
| 15 | +def cli(env, identifier): |
| 16 | + """Get storage details for a virtual server.""" |
| 17 | + |
| 18 | + vsi = SoftLayer.VSManager(env.client) |
| 19 | + vsi_id = helpers.resolve_id(vsi.resolve_ids, identifier, 'VS') |
| 20 | + iscsi_storage_data = vsi.get_storage_details(vsi_id, "ISCSI") |
| 21 | + nas_storage_data = vsi.get_storage_details(vsi_id, "NAS") |
| 22 | + storage_credentials = vsi.get_storage_credentials(vsi_id) |
| 23 | + portable_storage = vsi.get_portable_storage(vsi_id) |
| 24 | + local_disks = vsi.get_local_disks(vsi_id) |
| 25 | + |
| 26 | + table_credentials = formatting.Table(['Username', 'Password', 'IQN'], title="Block Storage Details \n iSCSI") |
| 27 | + if storage_credentials: |
| 28 | + table_credentials.add_row([storage_credentials['credential']['username'], |
| 29 | + storage_credentials['credential']['password'], |
| 30 | + storage_credentials['name']]) |
| 31 | + |
| 32 | + table_iscsi = formatting.Table(['LUN name', 'capacity', 'Target address', 'Location', 'Notes']) |
| 33 | + for iscsi in iscsi_storage_data: |
| 34 | + table_iscsi.add_row([iscsi['username'], iscsi['capacityGb'], |
| 35 | + iscsi['serviceResourceBackendIpAddress'], |
| 36 | + iscsi['allowedVirtualGuests'][0]['datacenter']['longName'], |
| 37 | + iscsi.get('notes', None)]) |
| 38 | + |
| 39 | + table_portable = formatting.Table(['Description', 'Capacity'], title="Portable Storage") |
| 40 | + for portable in portable_storage: |
| 41 | + table_portable.add_row([portable.get('description', None), portable.get('capacity', None)]) |
| 42 | + |
| 43 | + table_nas = formatting.Table(['Volume name', 'capacity', 'Host Name', 'Location', 'Notes'], |
| 44 | + title="File Storage Details") |
| 45 | + for nas in nas_storage_data: |
| 46 | + table_nas.add_row([nas['username'], nas['capacityGb'], |
| 47 | + nas['serviceResourceBackendIpAddress'], |
| 48 | + nas['allowedVirtualGuests'][0]['datacenter']['longName'], |
| 49 | + nas.get('notes', None)]) |
| 50 | + |
| 51 | + table_local_disks = formatting.Table(['Type', 'Name', 'Capacity'], title="Other storage details") |
| 52 | + for disks in local_disks: |
| 53 | + if 'diskImage' in disks: |
| 54 | + table_local_disks.add_row([get_local_type(disks), disks['mountType'], |
| 55 | + str(disks['diskImage']['capacity']) + " " + str(disks['diskImage']['units'])]) |
| 56 | + |
| 57 | + env.fout(table_credentials) |
| 58 | + env.fout(table_iscsi) |
| 59 | + env.fout(table_portable) |
| 60 | + env.fout(table_nas) |
| 61 | + env.fout(table_local_disks) |
| 62 | + |
| 63 | + |
| 64 | +def get_local_type(disks): |
| 65 | + """Returns the virtual server local disk type. |
| 66 | +
|
| 67 | + :param disks: virtual serve local disks. |
| 68 | + """ |
| 69 | + disk_type = 'System' |
| 70 | + if 'SWAP' in disks['diskImage']['description']: |
| 71 | + disk_type = 'Swap' |
| 72 | + |
| 73 | + return disk_type |
0 commit comments