|
6 | 6 | import SoftLayer
|
7 | 7 | from SoftLayer.CLI.command import SLCommand
|
8 | 8 | from SoftLayer.CLI import environment
|
| 9 | +from SoftLayer.CLI import exceptions |
9 | 10 | from SoftLayer.CLI import formatting
|
10 | 11 |
|
11 | 12 | PACKAGE_STORAGE = 759
|
12 | 13 |
|
13 | 14 |
|
14 | 15 | @click.command(cls=SLCommand)
|
| 16 | +@click.argument('location', required=False) |
| 17 | +@click.option('--prices', '-p', is_flag=True, |
| 18 | + help='Use --prices to list the server item prices, and to list the Item Prices by location,' |
| 19 | + 'add it to the --prices option using location short name, e.g. --prices dal13') |
15 | 20 | @environment.pass_env
|
16 |
| -def cli(env): |
| 21 | +def cli(env, prices, location=None): |
17 | 22 | """List all options for ordering a block storage"""
|
18 | 23 |
|
19 | 24 | order_manager = SoftLayer.OrderingManager(env.client)
|
20 | 25 | items = order_manager.get_items(PACKAGE_STORAGE)
|
21 |
| - datacenters = order_manager.get_regions(PACKAGE_STORAGE) |
| 26 | + datacenters = order_manager.get_regions(PACKAGE_STORAGE, location) |
22 | 27 |
|
| 28 | + tables = [] |
23 | 29 | network = SoftLayer.NetworkManager(env.client)
|
24 |
| - |
25 | 30 | pods = network.get_closed_pods()
|
26 | 31 |
|
27 |
| - iops_table = formatting.Table(['Id', 'Description', 'KeyName'], title='IOPS') |
28 |
| - snapshot_table = formatting.Table(['Id', 'Description', 'KeyName'], title='Snapshot') |
29 |
| - storage_table = formatting.Table(['Id', 'Description', 'KeyName'], title='Storage') |
30 |
| - datacenter_table = formatting.Table(['Id', 'Description', 'KeyName'], title='Datacenter') |
31 |
| - |
32 |
| - storage_table.align['Description'] = 'l' |
33 |
| - storage_table.align['KeyName'] = 'l' |
34 |
| - storage_table.sortby = 'Id' |
35 |
| - |
36 |
| - for datacenter in datacenters: |
37 |
| - closure = [] |
38 |
| - for pod in pods: |
39 |
| - if datacenter['location']['location']['name'] in str(pod['name']): |
40 |
| - closure.append(pod['name']) |
41 |
| - |
42 |
| - notes = '-' |
43 |
| - if len(closure) > 0: |
44 |
| - notes = 'closed soon: %s' % (', '.join(closure)) |
45 |
| - datacenter_table.add_row([datacenter['location']['locationId'], |
46 |
| - datacenter.get('description'), |
47 |
| - datacenter['keyname'], notes]) |
48 |
| - |
49 |
| - for item in items: |
50 |
| - if item['itemCategory']['categoryCode'] == 'performance_storage_space': |
51 |
| - storage_table.add_row([item.get('id'), item.get('description'), |
52 |
| - item.get('keyName'), item.get('capacityMinimum') or '-']) |
53 |
| - |
54 |
| - if item['itemCategory']['categoryCode'] == 'storage_tier_level': |
55 |
| - iops_table.add_row([item.get('id'), item.get('description'), |
56 |
| - item.get('keyName')]) |
57 |
| - |
58 |
| - if item['itemCategory']['categoryCode'] == 'storage_snapshot_space': |
59 |
| - snapshot_table.add_row([item.get('id'), item.get('description'), |
60 |
| - item.get('keyName')]) |
61 |
| - |
62 |
| - env.fout(datacenter_table) |
63 |
| - env.fout(iops_table) |
64 |
| - env.fout(storage_table) |
65 |
| - env.fout(snapshot_table) |
| 32 | + if datacenters != []: |
| 33 | + datacenter_table = formatting.Table(['Id', 'Description', 'KeyName'], title='Datacenter') |
| 34 | + |
| 35 | + for datacenter in datacenters: |
| 36 | + closure = [] |
| 37 | + for pod in pods: |
| 38 | + if datacenter['location']['location']['name'] in str(pod['name']): |
| 39 | + closure.append(pod['name']) |
| 40 | + |
| 41 | + notes = '-' |
| 42 | + if len(closure) > 0: |
| 43 | + notes = 'closed soon: %s' % (', '.join(closure)) |
| 44 | + datacenter_table.add_row([datacenter['location']['locationId'], |
| 45 | + datacenter.get('description'), |
| 46 | + datacenter['keyname'], notes]) |
| 47 | + tables.append(datacenter_table) |
| 48 | + else: |
| 49 | + raise exceptions.CLIAbort('Location does not exit.') |
| 50 | + |
| 51 | + tables.append(_block_ios_get_table(items, prices)) |
| 52 | + tables.append(_block_storage_table(items, prices)) |
| 53 | + tables.append(_block_snapshot_get_table(items, prices)) |
| 54 | + env.fout(tables) |
| 55 | + |
| 56 | + |
| 57 | +def _block_ios_get_table(items, prices): |
| 58 | + if prices: |
| 59 | + table = formatting.Table(['Id', 'Description', 'KeyName', 'Prices'], title='IOPS') |
| 60 | + for block_item in items: |
| 61 | + if block_item['itemCategory']['categoryCode'] == 'storage_tier_level': |
| 62 | + table.add_row([block_item.get('id'), block_item.get('description'), |
| 63 | + block_item.get('keyName'), block_item['prices'][0]['recurringFee']]) |
| 64 | + else: |
| 65 | + table = formatting.Table(['Id', 'Description', 'KeyName'], title='IOPS') |
| 66 | + for block_item in items: |
| 67 | + if block_item['itemCategory']['categoryCode'] == 'storage_tier_level': |
| 68 | + table.add_row([block_item.get('id'), block_item.get('description'), |
| 69 | + block_item.get('keyName')]) |
| 70 | + table.sortby = 'Id' |
| 71 | + table.align = 'l' |
| 72 | + return table |
| 73 | + |
| 74 | + |
| 75 | +def _block_storage_table(items, prices): |
| 76 | + if prices: |
| 77 | + table = formatting.Table(['Id', 'Description', 'KeyName', 'Capacity Minimum', 'Prices'], title='Storage') |
| 78 | + for block_item in items: |
| 79 | + if block_item['itemCategory']['categoryCode'] == 'performance_storage_space': |
| 80 | + table.add_row([block_item.get('id'), block_item.get('description'), |
| 81 | + block_item.get('keyName'), block_item.get('capacityMinimum') or '-', |
| 82 | + block_item['prices'][0]['recurringFee']]) |
| 83 | + else: |
| 84 | + table = formatting.Table(['Id', 'Description', 'KeyName', 'Capacity Minimum'], title='Storage') |
| 85 | + for block_item in items: |
| 86 | + if block_item['itemCategory']['categoryCode'] == 'performance_storage_space': |
| 87 | + table.add_row([block_item.get('id'), block_item.get('description'), |
| 88 | + block_item.get('keyName'), block_item.get('capacityMinimum') or '-', ]) |
| 89 | + table.sortby = 'Id' |
| 90 | + table.align = 'l' |
| 91 | + return table |
| 92 | + |
| 93 | + |
| 94 | +def _block_snapshot_get_table(items, prices): |
| 95 | + if prices: |
| 96 | + table = formatting.Table(['Id', 'Description', 'KeyName', 'Prices'], title='Snapshot') |
| 97 | + for block_item in items: |
| 98 | + if block_item['itemCategory']['categoryCode'] == 'storage_snapshot_space': |
| 99 | + table.add_row([block_item.get('id'), block_item.get('description'), |
| 100 | + block_item.get('keyName'), block_item['prices'][0]['recurringFee']]) |
| 101 | + else: |
| 102 | + table = formatting.Table(['Id', 'Description', 'KeyName'], title='Snapshot') |
| 103 | + for block_item in items: |
| 104 | + if block_item['itemCategory']['categoryCode'] == 'storage_snapshot_space': |
| 105 | + table.add_row([block_item.get('id'), block_item.get('description'), |
| 106 | + block_item.get('keyName')]) |
| 107 | + table.sortby = 'Id' |
| 108 | + table.align = 'l' |
| 109 | + return table |
0 commit comments