Skip to content

Commit 3b517cb

Browse files
Merge pull request #1705 from caberos/issue1694
block/file volume-options improvement 3
2 parents 248a52a + e89e151 commit 3b517cb

File tree

3 files changed

+178
-86
lines changed

3 files changed

+178
-86
lines changed

SoftLayer/CLI/block/options.py

+86-42
Original file line numberDiff line numberDiff line change
@@ -6,60 +6,104 @@
66
import SoftLayer
77
from SoftLayer.CLI.command import SLCommand
88
from SoftLayer.CLI import environment
9+
from SoftLayer.CLI import exceptions
910
from SoftLayer.CLI import formatting
1011

1112
PACKAGE_STORAGE = 759
1213

1314

1415
@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')
1520
@environment.pass_env
16-
def cli(env):
21+
def cli(env, prices, location=None):
1722
"""List all options for ordering a block storage"""
1823

1924
order_manager = SoftLayer.OrderingManager(env.client)
2025
items = order_manager.get_items(PACKAGE_STORAGE)
21-
datacenters = order_manager.get_regions(PACKAGE_STORAGE)
26+
datacenters = order_manager.get_regions(PACKAGE_STORAGE, location)
2227

28+
tables = []
2329
network = SoftLayer.NetworkManager(env.client)
24-
2530
pods = network.get_closed_pods()
2631

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

SoftLayer/CLI/file/options.py

+87-42
Original file line numberDiff line numberDiff line change
@@ -6,60 +6,105 @@
66
import SoftLayer
77
from SoftLayer.CLI.command import SLCommand
88
from SoftLayer.CLI import environment
9+
from SoftLayer.CLI import exceptions
910
from SoftLayer.CLI import formatting
1011

1112
PACKAGE_STORAGE = 759
1213

1314

1415
@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')
1520
@environment.pass_env
16-
def cli(env):
17-
"""List all options for ordering a file storage"""
21+
def cli(env, prices, location=None):
22+
"""List all options for ordering a block storage"""
1823

1924
order_manager = SoftLayer.OrderingManager(env.client)
2025
items = order_manager.get_items(PACKAGE_STORAGE)
21-
datacenters = order_manager.get_regions(PACKAGE_STORAGE)
26+
datacenters = order_manager.get_regions(PACKAGE_STORAGE, location)
2227

28+
tables = []
2329
network = SoftLayer.NetworkManager(env.client)
2430

2531
pods = network.get_closed_pods()
2632

27-
iops_table = formatting.Table(['Id', 'Description', 'KeyName'], title='IOPS')
28-
snapshot_table = formatting.Table(['Id', 'Description', 'KeyName'], title='Snapshot')
29-
file_storage_table = formatting.Table(['Id', 'Description', 'KeyName'], title='Storage')
30-
datacenter_table = formatting.Table(['Id', 'Description', 'KeyName'], title='Datacenter')
31-
32-
file_storage_table.align['Description'] = 'l'
33-
file_storage_table.align['KeyName'] = 'l'
34-
file_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_file in items:
50-
if item_file['itemCategory']['categoryCode'] == 'performance_storage_space':
51-
file_storage_table.add_row([item_file.get('id'), item_file.get('description'),
52-
item_file.get('keyName')])
53-
54-
if item_file['itemCategory']['categoryCode'] == 'storage_tier_level':
55-
iops_table.add_row([item_file.get('id'), item_file.get('description'),
56-
item_file.get('keyName')])
57-
58-
if item_file['itemCategory']['categoryCode'] == 'storage_snapshot_space':
59-
snapshot_table.add_row([item_file.get('id'), item_file.get('description'),
60-
item_file.get('keyName')])
61-
62-
env.fout(datacenter_table)
63-
env.fout(iops_table)
64-
env.fout(file_storage_table)
65-
env.fout(snapshot_table)
33+
if datacenters != []:
34+
datacenter_table = formatting.Table(['Id', 'Description', 'KeyName'], title='Datacenter')
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+
tables.append(datacenter_table)
49+
else:
50+
raise exceptions.CLIAbort('Location does not exit.')
51+
52+
tables.append(_file_ios_get_table(items, prices))
53+
tables.append(_file_storage_table(items, prices))
54+
tables.append(_file_snapshot_get_table(items, prices))
55+
env.fout(tables)
56+
57+
58+
def _file_ios_get_table(items, prices):
59+
if prices:
60+
table = formatting.Table(['Id', 'Description', 'KeyName', 'Prices'], title='IOPS')
61+
for item in items:
62+
if item['itemCategory']['categoryCode'] == 'storage_tier_level':
63+
table.add_row([item.get('id'), item.get('description'),
64+
item.get('keyName'), item['prices'][0]['recurringFee']])
65+
else:
66+
table = formatting.Table(['Id', 'Description', 'KeyName'], title='IOPS')
67+
for item in items:
68+
if item['itemCategory']['categoryCode'] == 'storage_tier_level':
69+
table.add_row([item.get('id'), item.get('description'),
70+
item.get('keyName')])
71+
table.sortby = 'Id'
72+
table.align = 'l'
73+
return table
74+
75+
76+
def _file_storage_table(items, prices):
77+
if prices:
78+
table = formatting.Table(['Id', 'Description', 'KeyName', 'Capacity Minimum', 'Prices'], title='Storage')
79+
for item in items:
80+
if item['itemCategory']['categoryCode'] == 'performance_storage_space':
81+
table.add_row([item.get('id'), item.get('description'),
82+
item.get('keyName'), item.get('capacityMinimum') or '-',
83+
item['prices'][0]['recurringFee']])
84+
else:
85+
table = formatting.Table(['Id', 'Description', 'KeyName', 'Capacity Minimum'], title='Storage')
86+
for item in items:
87+
if item['itemCategory']['categoryCode'] == 'performance_storage_space':
88+
table.add_row([item.get('id'), item.get('description'),
89+
item.get('keyName'), item.get('capacityMinimum') or '-', ])
90+
table.sortby = 'Id'
91+
table.align = 'l'
92+
return table
93+
94+
95+
def _file_snapshot_get_table(items, prices):
96+
if prices:
97+
table = formatting.Table(['Id', 'Description', 'KeyName', 'Prices'], title='Snapshot')
98+
for item in items:
99+
if item['itemCategory']['categoryCode'] == 'storage_snapshot_space':
100+
table.add_row([item.get('id'), item.get('description'),
101+
item.get('keyName'), item['prices'][0]['recurringFee']])
102+
else:
103+
table = formatting.Table(['Id', 'Description', 'KeyName'], title='Snapshot')
104+
for item in items:
105+
if item['itemCategory']['categoryCode'] == 'storage_snapshot_space':
106+
table.add_row([item.get('id'), item.get('description'),
107+
item.get('keyName')])
108+
table.sortby = 'Id'
109+
table.align = 'l'
110+
return table

SoftLayer/managers/ordering.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -728,9 +728,12 @@ def get_items(self, package_id, storage_filter=None):
728728
return self.client.call('SoftLayer_Product_Package', 'getItems', filter=storage_filter,
729729
id=package_id)
730730

731-
def get_regions(self, package_id):
731+
def get_regions(self, package_id, location=None):
732732
"""returns the all regions.
733733
734734
:param int package_id: The package for which to get the items.
735735
"""
736-
return self.client.call('SoftLayer_Product_Package', 'getRegions', id=package_id)
736+
_filter = ''
737+
if location:
738+
_filter = {"regions": {"location": {"location": {"name": {"operation": location}}}}}
739+
return self.client.call('SoftLayer_Product_Package', 'getRegions', id=package_id, filter=_filter)

0 commit comments

Comments
 (0)