Skip to content

Commit 32c1c3b

Browse files
Merge pull request softlayer#1319 from FernandoOjeda/fo_vs_create_options_order_item_list
Ordering price information improvements.
2 parents 3d2e2e4 + d1ff3fb commit 32c1c3b

10 files changed

Lines changed: 807 additions & 85 deletions

File tree

SoftLayer/CLI/hardware/create_options.py

Lines changed: 161 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,54 +9,179 @@
99

1010

1111
@click.command()
12+
@click.argument('location', required=False)
13+
@click.option('--prices', '-p', is_flag=True,
14+
help='Use --prices to list the server item prices, and to list the Item Prices by location,'
15+
'add it to the --prices option using location short name, e.g. --prices dal13')
1216
@environment.pass_env
13-
def cli(env):
17+
def cli(env, prices, location=None):
1418
"""Server order options for a given chassis."""
1519

1620
hardware_manager = hardware.HardwareManager(env.client)
17-
options = hardware_manager.get_create_options()
21+
options = hardware_manager.get_create_options(location)
1822

1923
tables = []
2024

2125
# Datacenters
2226
dc_table = formatting.Table(['Datacenter', 'Value'], title="Datacenters")
2327
dc_table.sortby = 'Value'
2428
dc_table.align = 'l'
25-
for location in options['locations']:
26-
dc_table.add_row([location['name'], location['key']])
29+
for location_info in options['locations']:
30+
dc_table.add_row([location_info['name'], location_info['key']])
2731
tables.append(dc_table)
2832

29-
# Presets
30-
preset_table = formatting.Table(['Size', 'Value'], title="Sizes")
31-
preset_table.sortby = 'Value'
32-
preset_table.align = 'l'
33-
for size in options['sizes']:
34-
preset_table.add_row([size['name'], size['key']])
35-
tables.append(preset_table)
36-
37-
# Operating systems
38-
os_table = formatting.Table(['OS', 'Key', 'Reference Code'], title="Operating Systems")
39-
os_table.sortby = 'Key'
40-
os_table.align = 'l'
41-
for operating_system in options['operating_systems']:
42-
os_table.add_row([operating_system['name'], operating_system['key'], operating_system['referenceCode']])
43-
tables.append(os_table)
44-
45-
# Port speed
46-
port_speed_table = formatting.Table(['Network', 'Speed', 'Key'], title="Network Options")
47-
port_speed_table.sortby = 'Speed'
48-
port_speed_table.align = 'l'
49-
50-
for speed in options['port_speeds']:
51-
port_speed_table.add_row([speed['name'], speed['speed'], speed['key']])
52-
tables.append(port_speed_table)
53-
54-
# Extras
55-
extras_table = formatting.Table(['Extra Option', 'Value'], title="Extras")
56-
extras_table.sortby = 'Value'
57-
extras_table.align = 'l'
58-
for extra in options['extras']:
59-
extras_table.add_row([extra['name'], extra['key']])
60-
tables.append(extras_table)
33+
if prices:
34+
_preset_prices_table(options['sizes'], tables)
35+
_os_prices_table(options['operating_systems'], tables)
36+
_port_speed_prices_table(options['port_speeds'], tables)
37+
_extras_prices_table(options['extras'], tables)
38+
else:
39+
# Presets
40+
preset_table = formatting.Table(['Size', 'Value'], title="Sizes")
41+
preset_table.sortby = 'Value'
42+
preset_table.align = 'l'
43+
for size in options['sizes']:
44+
preset_table.add_row([size['name'], size['key']])
45+
tables.append(preset_table)
46+
47+
# Operating systems
48+
os_table = formatting.Table(['OS', 'Key', 'Reference Code'], title="Operating Systems")
49+
os_table.sortby = 'Key'
50+
os_table.align = 'l'
51+
for operating_system in options['operating_systems']:
52+
os_table.add_row([operating_system['name'], operating_system['key'], operating_system['referenceCode']])
53+
tables.append(os_table)
54+
55+
# Port speed
56+
port_speed_table = formatting.Table(['Network', 'Speed', 'Key'], title="Network Options")
57+
port_speed_table.sortby = 'Speed'
58+
port_speed_table.align = 'l'
59+
for speed in options['port_speeds']:
60+
port_speed_table.add_row([speed['name'], speed['speed'], speed['key']])
61+
tables.append(port_speed_table)
62+
63+
# Extras
64+
extras_table = formatting.Table(['Extra Option', 'Value'], title="Extras")
65+
extras_table.sortby = 'Value'
66+
extras_table.align = 'l'
67+
for extra in options['extras']:
68+
extras_table.add_row([extra['name'], extra['key']])
69+
tables.append(extras_table)
6170

6271
env.fout(formatting.listing(tables, separator='\n'))
72+
73+
74+
def _preset_prices_table(sizes, tables):
75+
"""Shows Server Preset options prices.
76+
77+
:param [] sizes: List of Hardware Server sizes.
78+
:param tables: Table formatting.
79+
"""
80+
preset_prices_table = formatting.Table(['Size', 'Value', 'Hourly', 'Monthly'], title="Sizes Prices")
81+
preset_prices_table.sortby = 'Value'
82+
preset_prices_table.align = 'l'
83+
for size in sizes:
84+
preset_prices_table.add_row([size['name'], size['key'], "%.4f" % size['hourlyRecurringFee'],
85+
"%.4f" % size['recurringFee']])
86+
tables.append(preset_prices_table)
87+
88+
89+
def _os_prices_table(operating_systems, tables):
90+
"""Shows Server Operating Systems prices cost and capacity restriction.
91+
92+
:param [] operating_systems: List of Hardware Server operating systems.
93+
:param tables: Table formatting.
94+
"""
95+
os_prices_table = formatting.Table(['OS Key', 'Hourly', 'Monthly', 'Restriction'],
96+
title="Operating Systems Prices")
97+
os_prices_table.sortby = 'OS Key'
98+
os_prices_table.align = 'l'
99+
for operating_system in operating_systems:
100+
for price in operating_system['prices']:
101+
cr_max = _get_price_data(price, 'capacityRestrictionMaximum')
102+
cr_min = _get_price_data(price, 'capacityRestrictionMinimum')
103+
cr_type = _get_price_data(price, 'capacityRestrictionType')
104+
os_prices_table.add_row(
105+
[operating_system['key'],
106+
_get_price_data(price, 'hourlyRecurringFee'),
107+
_get_price_data(price, 'recurringFee'),
108+
"%s - %s %s" % (cr_min, cr_max, cr_type)])
109+
tables.append(os_prices_table)
110+
111+
112+
def _port_speed_prices_table(port_speeds, tables):
113+
"""Shows Server Port Speeds prices cost and capacity restriction.
114+
115+
:param [] port_speeds: List of Hardware Server Port Speeds.
116+
:param tables: Table formatting.
117+
"""
118+
port_speed_prices_table = formatting.Table(['Key', 'Speed', 'Hourly', 'Monthly', 'Restriction'],
119+
title="Network Options Prices")
120+
port_speed_prices_table.sortby = 'Speed'
121+
port_speed_prices_table.align = 'l'
122+
for speed in port_speeds:
123+
for price in speed['prices']:
124+
cr_max = _get_price_data(price, 'capacityRestrictionMaximum')
125+
cr_min = _get_price_data(price, 'capacityRestrictionMinimum')
126+
cr_type = _get_price_data(price, 'capacityRestrictionType')
127+
port_speed_prices_table.add_row(
128+
[speed['key'], speed['speed'],
129+
_get_price_data(price, 'hourlyRecurringFee'),
130+
_get_price_data(price, 'recurringFee'),
131+
"%s - %s %s" % (cr_min, cr_max, cr_type)])
132+
tables.append(port_speed_prices_table)
133+
134+
135+
def _extras_prices_table(extras, tables):
136+
"""Shows Server extras prices cost and capacity restriction.
137+
138+
:param [] extras: List of Hardware Server Extras.
139+
:param tables: Table formatting.
140+
"""
141+
extras_prices_table = formatting.Table(['Extra Option Key', 'Hourly', 'Monthly', 'Restriction'],
142+
title="Extras Prices")
143+
extras_prices_table.align = 'l'
144+
for extra in extras:
145+
for price in extra['prices']:
146+
cr_max = _get_price_data(price, 'capacityRestrictionMaximum')
147+
cr_min = _get_price_data(price, 'capacityRestrictionMinimum')
148+
cr_type = _get_price_data(price, 'capacityRestrictionType')
149+
extras_prices_table.add_row(
150+
[extra['key'],
151+
_get_price_data(price, 'hourlyRecurringFee'),
152+
_get_price_data(price, 'recurringFee'),
153+
"%s - %s %s" % (cr_min, cr_max, cr_type)])
154+
tables.append(extras_prices_table)
155+
156+
157+
def _get_price_data(price, item):
158+
"""Get a specific data from HS price.
159+
160+
:param price: Hardware Server price.
161+
:param string item: Hardware Server price data.
162+
"""
163+
result = '-'
164+
if item in price:
165+
result = price[item]
166+
return result
167+
168+
169+
def _location_item_prices(location_prices, tables):
170+
"""Get a specific data from HS price.
171+
172+
:param price: Hardware Server price.
173+
:param string item: Hardware Server price data.
174+
"""
175+
location_prices_table = formatting.Table(['keyName', 'priceId', 'Hourly', 'Monthly', 'Restriction'])
176+
location_prices_table.sortby = 'keyName'
177+
location_prices_table.align = 'l'
178+
for price in location_prices:
179+
cr_max = _get_price_data(price, 'capacityRestrictionMaximum')
180+
cr_min = _get_price_data(price, 'capacityRestrictionMinimum')
181+
cr_type = _get_price_data(price, 'capacityRestrictionType')
182+
location_prices_table.add_row(
183+
[price['item']['keyName'], price['id'],
184+
_get_price_data(price, 'hourlyRecurringFee'),
185+
_get_price_data(price, 'recurringFee'),
186+
"%s - %s %s" % (cr_min, cr_max, cr_type)])
187+
tables.append(location_prices_table)

SoftLayer/CLI/order/item_list.py

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,20 @@
88
from SoftLayer.utils import lookup
99

1010
COLUMNS = ['category', 'keyName', 'description', 'priceId']
11+
COLUMNS_ITEM_PRICES = ['keyName', 'priceId', 'Hourly', 'Monthly', 'Restriction']
12+
COLUMNS_ITEM_PRICES_LOCATION = ['keyName', 'priceId', 'Hourly', 'Monthly', 'Restriction']
1113

1214

1315
@click.command()
16+
@click.argument('location', required=False, nargs=-1, type=click.UNPROCESSED)
1417
@click.argument('package_keyname')
1518
@click.option('--keyword', help="A word (or string) used to filter item names.")
1619
@click.option('--category', help="Category code to filter items by")
20+
@click.option('--prices', '-p', is_flag=True, help='Use --prices to list the server item prices, and to list the '
21+
'Item Prices by location, add it to the --prices option using '
22+
'location KeyName, e.g. --prices AMSTERDAM02')
1723
@environment.pass_env
18-
def cli(env, package_keyname, keyword, category):
24+
def cli(env, location, package_keyname, keyword, category, prices):
1925
"""List package items used for ordering.
2026
2127
The item keyNames listed can be used with `slcli order place` to specify
@@ -34,9 +40,10 @@ def cli(env, package_keyname, keyword, category):
3440
slcli order item-list BARE_METAL_SERVER --category os --keyword ubuntu
3541
3642
"""
37-
table = formatting.Table(COLUMNS)
3843
manager = ordering.OrderingManager(env.client)
3944

45+
tables = []
46+
4047
_filter = {'items': {}}
4148
if keyword:
4249
_filter['items']['description'] = {'operation': '*= %s' % keyword}
@@ -47,10 +54,19 @@ def cli(env, package_keyname, keyword, category):
4754
sorted_items = sort_items(items)
4855

4956
categories = sorted_items.keys()
50-
for catname in sorted(categories):
51-
for item in sorted_items[catname]:
52-
table.add_row([catname, item['keyName'], item['description'], get_price(item)])
53-
env.fout(table)
57+
if prices:
58+
_item_list_prices(categories, sorted_items, tables)
59+
if location:
60+
location = location[0]
61+
location_prices = manager.get_item_prices_by_location(location, package_keyname)
62+
_location_item_prices(location_prices, location, tables)
63+
else:
64+
table_items_detail = formatting.Table(COLUMNS)
65+
for catname in sorted(categories):
66+
for item in sorted_items[catname]:
67+
table_items_detail.add_row([catname, item['keyName'], item['description'], get_price(item)])
68+
tables.append(table_items_detail)
69+
env.fout(formatting.listing(tables, separator='\n'))
5470

5571

5672
def sort_items(items):
@@ -73,3 +89,61 @@ def get_price(item):
7389
if not price.get('locationGroupId'):
7490
return price.get('id')
7591
return 0
92+
93+
94+
def _item_list_prices(categories, sorted_items, tables):
95+
"""Add the item prices cost and capacity restriction to the table"""
96+
table_prices = formatting.Table(COLUMNS_ITEM_PRICES)
97+
for catname in sorted(categories):
98+
for item in sorted_items[catname]:
99+
for price in item['prices']:
100+
if not price.get('locationGroupId'):
101+
cr_max = _get_price_data(price, 'capacityRestrictionMaximum')
102+
cr_min = _get_price_data(price, 'capacityRestrictionMinimum')
103+
cr_type = _get_price_data(price, 'capacityRestrictionType')
104+
table_prices.add_row([item['keyName'], price['id'],
105+
get_item_price_data(price, 'hourlyRecurringFee'),
106+
get_item_price_data(price, 'recurringFee'),
107+
"%s - %s %s" % (cr_min, cr_max, cr_type)])
108+
tables.append(table_prices)
109+
110+
111+
def get_item_price_data(price, item_attribute):
112+
"""Given an SoftLayer_Product_Item_Price, returns its default price data"""
113+
result = '-'
114+
if item_attribute in price:
115+
result = price[item_attribute]
116+
return result
117+
118+
119+
def _location_item_prices(location_prices, location, tables):
120+
"""Get a specific data from HS price.
121+
122+
:param price: Hardware Server price.
123+
:param string item: Hardware Server price data.
124+
"""
125+
location_prices_table = formatting.Table(COLUMNS_ITEM_PRICES_LOCATION, title="Item Prices for %s" % location)
126+
location_prices_table.sortby = 'keyName'
127+
location_prices_table.align = 'l'
128+
for price in location_prices:
129+
cr_max = _get_price_data(price, 'capacityRestrictionMaximum')
130+
cr_min = _get_price_data(price, 'capacityRestrictionMinimum')
131+
cr_type = _get_price_data(price, 'capacityRestrictionType')
132+
location_prices_table.add_row(
133+
[price['item']['keyName'], price['id'],
134+
_get_price_data(price, 'hourlyRecurringFee'),
135+
_get_price_data(price, 'recurringFee'),
136+
"%s - %s %s" % (cr_min, cr_max, cr_type)])
137+
tables.append(location_prices_table)
138+
139+
140+
def _get_price_data(price, item):
141+
"""Get a specific data from HS price.
142+
143+
:param price: Hardware Server price.
144+
:param string item: Hardware Server price data.
145+
"""
146+
result = '-'
147+
if item in price:
148+
result = price[item]
149+
return result

0 commit comments

Comments
 (0)