File: storage_utils.py

package info (click to toggle)
python-softlayer 6.1.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,100 kB
  • sloc: python: 53,771; makefile: 289; sh: 57
file content (167 lines) | stat: -rw-r--r-- 4,876 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
"""Utility functions for use with File and Block commands."""
# :license: MIT, see LICENSE for more details.

from SoftLayer.CLI import columns as column_helper
from SoftLayer.CLI import formatting

DEFAULT_NOTES_SIZE = 20


def reduce_notes(volumes, env):
    """Reduces all long notes found in the volumes list just if the format output is different from a JSON format.

    :param list volumes: An list of storage volumes
    :param env :A environment console.
    """
    if env.format_output_is_json():
        return

    for volume in volumes:
        if len(volume.get('notes', '')) > DEFAULT_NOTES_SIZE:
            shortened_notes = volume['notes'][:DEFAULT_NOTES_SIZE]
            volume['notes'] = shortened_notes


def build_output_table(env, volumes, columns, sortby):
    """Builds a formatting table for a list of volumes.

        :param env :A Environment console.
        :param list volumes: An list of storage volumes
        :param columns :A ColumnFormatter for column names
        :param str sortby :A string to sort by.
        """
    table = formatting.Table(columns.columns)
    if sortby in table.columns:
        table.sortby = sortby

    reduce_notes(volumes, env)
    for volume in volumes:
        table.add_row([value or formatting.blank()
                       for value in columns.row(volume)])
    return table


def _format_name(obj):
    if obj['type'] == 'VIRTUAL':
        return "{0}.{1}".format(obj['hostname'], obj['domain'])

    elif obj['type'] == 'HARDWARE':
        return "{0}.{1}".format(obj['hostname'], obj['domain'])

    elif obj['type'] == 'SUBNET':
        name = "{0}/{1}".format(
            obj['networkIdentifier'],
            obj['cidr']
        )
        if 'note' in obj.keys():
            name = "{0} ({1})".format(name, obj['note'])

        return name

    elif obj['type'] == 'IP':
        name = obj['ipAddress']
        if 'note' in obj.keys():
            name = "{0} ({1})".format(name, obj['note'])

        return name
    else:
        raise ValueError('Unknown type %s' % obj['type'])


COLUMNS = [
    column_helper.Column('id', ('id',)),
    column_helper.Column('name', _format_name, """
allowedVirtualGuests[hostname,domain],
allowedHardware[hostname,domain],
allowedSubnets[networkIdentifier,cidr,note],
allowedIpAddresses[ipAddress,note],
"""),
    column_helper.Column('type', ('type',)),
    column_helper.Column(
        'private_ip_address',
        ('primaryBackendIpAddress',),
        """
allowedVirtualGuests.primaryBackendIpAddress
allowedHardware.primaryBackendIpAddress
allowedSubnets.primaryBackendIpAddress
allowedIpAddresses.primaryBackendIpAddress
"""),
    column_helper.Column(
        'source_subnet',
        ('allowedHost', 'sourceSubnet',),
        """
allowedVirtualGuests.allowedHost.sourceSubnet
allowedHardware.allowedHost.sourceSubnet
allowedSubnets.allowedHost.sourceSubnet
allowedIpAddresses.allowedHost.sourceSubnet
"""),
    column_helper.Column(
        'host_iqn',
        ('allowedHost', 'name',),
        """
allowedVirtualGuests.allowedHost.name
allowedHardware.allowedHost.name
allowedSubnets.allowedHost.name
allowedIpAddresses.allowedHost.name
"""),
    column_helper.Column(
        'username',
        ('allowedHost', 'credential', 'username',),
        """
allowedVirtualGuests.allowedHost.credential.username
allowedHardware.allowedHost.credential.username
allowedSubnets.allowedHost.credential.username
allowedIpAddresses.allowedHost.credential.username
"""),
    column_helper.Column(
        'password',
        ('allowedHost', 'credential', 'password',),
        """
allowedVirtualGuests.allowedHost.credential.password
allowedHardware.allowedHost.credential.password
allowedSubnets.allowedHost.credential.password
allowedIpAddresses.allowedHost.credential.password
"""),
    column_helper.Column(
        'allowed_host_id',
        ('allowedHost', 'id',),
        """
allowedVirtualGuests.allowedHost.id
allowedHardware.allowedHost.id
allowedSubnets.allowedHost.id
allowedIpAddresses.allowedHost.id
"""),
]

DEFAULT_COLUMNS = [
    'id',
    'name',
    'type',
    'private_ip_address',
    'source_subnet',
    'host_iqn',
    'username',
    'password',
    'allowed_host_id',
]


REPLICATION_PARTNER_COLUMNS = [
    column_helper.Column('ID', ('id',)),
    column_helper.Column('Username', ('username',), mask="username"),
    column_helper.Column('Account ID', ('accountId',), mask="accountId"),
    column_helper.Column('Capacity (GB)', ('capacityGb',), mask="capacityGb"),
    column_helper.Column('Hardware ID', ('hardwareId',), mask="hardwareId"),
    column_helper.Column('Guest ID', ('guestId',), mask="guestId"),
    column_helper.Column('Host ID', ('hostId',), mask="hostId"),
]

REPLICATION_PARTNER_DEFAULT = [
    'ID',
    'Username',
    'Account ID',
    'Capacity (GB)',
    'Hardware ID',
    'Guest ID',
    'Host ID'
]