File: options.py

package info (click to toggle)
python-softlayer 6.2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,508 kB
  • sloc: python: 57,195; makefile: 133; xml: 97; sh: 59
file content (132 lines) | stat: -rw-r--r-- 5,778 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
"""List all options for ordering a block storage."""
# :license: MIT, see LICENSE for more details.

import click

import SoftLayer
from SoftLayer.CLI.command import SLCommand
from SoftLayer.CLI import environment
from SoftLayer.CLI import exceptions
from SoftLayer.CLI import formatting

PACKAGE_STORAGE = 759


@click.command(cls=SLCommand)
@click.argument('location', required=False)
@click.option('--prices', '-p', is_flag=True,
              help='Use --prices to list the server item prices, and to list the Item Prices by location,'
                   'add it to the --prices option using location short name, e.g. --prices dal13')
@environment.pass_env
def cli(env, prices, location=None):
    """List all options for ordering a block storage

    Example::
        slcli block volume-options
        This command lists all options for creating a \
block storage volume, including storage type, volume size, OS type, IOPS, tier level, datacenter, and snapshot size.

        slcli block volume-options --prices
        This command lists all options for creating a \
block storage volume, including storage type, volume size, OS type, IOPS, tier level, \
datacenter, and snapshot size along with prices.

        slcli block volume-options --prices dal03
        This command lists all options for creating a \
block storage volume, including storage type, volume size, \
OS type, IOPS, tier level, datacenter, and snapshot size along with prices for a given location.
"""

    order_manager = SoftLayer.OrderingManager(env.client)
    items = order_manager.get_items(PACKAGE_STORAGE, mask="mask[categories]")
    datacenters = order_manager.get_regions(PACKAGE_STORAGE, location)

    tables = []
    network = SoftLayer.NetworkManager(env.client)
    pods = network.get_closed_pods()

    if datacenters != []:
        datacenter_table = formatting.Table(['Id', 'Description', 'KeyName'], title='Datacenter')

        for datacenter in datacenters:
            closure = []
            for pod in pods:
                if datacenter['location']['location']['name'] in str(pod['name']):
                    closure.append(pod['name'])

            notes = '-'
            if len(closure) > 0:
                notes = 'closed soon: %s' % (', '.join(closure))
            datacenter_table.add_row([datacenter['location']['locationId'],
                                      datacenter.get('description'),
                                      datacenter['keyname'], notes])
        tables.append(datacenter_table)
    else:
        raise exceptions.CLIAbort('Location does not exit.')

    tables.append(_block_ios_get_table(items, prices))
    tables.append(_block_storage_table(items, prices))
    tables.append(_block_snapshot_get_table(items, prices))
    env.fout(tables)


def _block_ios_get_table(items, prices):
    if prices:
        table = formatting.Table(['Id', 'Description', 'KeyName', 'Prices'], title='IOPS')
        for block_item in items:
            if block_item['itemCategory']['categoryCode'] == 'storage_tier_level':
                table.add_row([block_item.get('id'), block_item.get('description'),
                               block_item.get('keyName'), block_item['prices'][0]['recurringFee']])
    else:
        table = formatting.Table(['Id', 'Description', 'KeyName'], title='IOPS')
        for block_item in items:
            if block_item['itemCategory']['categoryCode'] == 'storage_tier_level':
                table.add_row([block_item.get('id'), block_item.get('description'),
                               block_item.get('keyName')])
    table.sortby = 'KeyName'
    table.align = 'l'
    return table


def _block_storage_table(items, prices):
    if prices:
        table = formatting.Table(['Id', 'Description', 'KeyName', 'Capacity Minimum', 'Prices'], title='Storage')
        for block_item in items:
            if block_item['itemCategory']['categoryCode'] == 'performance_storage_space':
                table.add_row([block_item.get('id'), block_item.get('description'),
                               block_item.get('keyName'), block_item.get('capacityMinimum') or '-',
                               block_item['prices'][0]['recurringFee']])
    else:
        table = formatting.Table(['Id', 'Description', 'KeyName', 'Capacity Minimum'], title='Storage')
        for block_item in items:
            if block_item['itemCategory']['categoryCode'] == 'performance_storage_space':
                table.add_row([block_item.get('id'), block_item.get('description'),
                               block_item.get('keyName'), block_item.get('capacityMinimum') or '-', ])
    table.sortby = 'KeyName'
    table.align = 'l'
    return table


def _block_snapshot_get_table(items, prices):
    if prices:
        table = formatting.Table(['Id', 'Description', 'KeyName', 'Prices'], title='Snapshot')
        for block_item in items:
            if block_item['itemCategory']['categoryCode'] == 'storage_snapshot_space':
                table.add_row([block_item.get('id'), block_item.get('description'),
                               block_item.get('keyName'), block_item['prices'][0]['recurringFee']])
    else:
        table = formatting.Table(['Id', 'Description', 'KeyName'], title='Snapshot')
        for block_item in items:
            if is_snapshot_category(block_item.get('categories', [])):
                table.add_row([block_item.get('id'), block_item.get('description'), block_item.get('keyName')])
    table.sortby = 'KeyName'
    table.align = 'l'
    return table


def is_snapshot_category(categories):
    """Checks if storage_snapshot_space is one of the categories"""
    for item in categories:
        if item.get('categoryCode') == "storage_snapshot_space":
            return True
    return False