1"""Get storage details for a virtual server."""
2# :license: MIT, see LICENSE for more details.
3
4import click
5
6import SoftLayer
7from SoftLayer.CLI import environment
8from SoftLayer.CLI import formatting
9from SoftLayer.CLI import helpers
10
11
12@click.command()
13@click.argument('identifier')
14@environment.pass_env
15def 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 = get_local_storage_table(local_disks)
52    table_local_disks.title = "Other storage details"
53
54    env.fout(table_credentials)
55    env.fout(table_iscsi)
56    env.fout(table_portable)
57    env.fout(table_nas)
58    env.fout(table_local_disks)
59
60
61def get_local_type(disks):
62    """Returns the virtual server local disk type.
63
64    :param disks: virtual server local disks.
65    """
66    disk_type = 'System'
67    if 'SWAP' in disks.get('diskImage', {}).get('description', []):
68        disk_type = 'Swap'
69
70    return disk_type
71
72
73def get_local_storage_table(local_disks):
74    """Returns a formatting local disk table
75
76      :param local_disks: virtual server local disks.
77      """
78    table_local_disks = formatting.Table(['Type', 'Name', 'Drive', 'Capacity'])
79    for disk in local_disks:
80        if 'diskImage' in disk:
81            table_local_disks.add_row([
82                get_local_type(disk),
83                disk['mountType'],
84                disk['device'],
85                "{capacity} {unit}".format(capacity=disk['diskImage']['capacity'],
86                                           unit=disk['diskImage']['units'])
87            ])
88    return table_local_disks
89