Skip to content

Commit d7254a0

Browse files
#1345 refactored create-options a bit to have less code duplication
1 parent caefa17 commit d7254a0

File tree

2 files changed

+68
-130
lines changed

2 files changed

+68
-130
lines changed

SoftLayer/CLI/hardware/create_options.py

Lines changed: 66 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -30,148 +30,110 @@ def cli(env, prices, location=None):
3030
dc_table.add_row([location_info['name'], location_info['key']])
3131
tables.append(dc_table)
3232

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)
33+
tables.append(_preset_prices_table(options['sizes'], prices))
34+
tables.append(_os_prices_table(options['operating_systems'], prices))
35+
tables.append(_port_speed_prices_table(options['port_speeds'], prices))
36+
tables.append(_extras_prices_table(options['extras'], prices))
7037

38+
# since this is multiple tables, this is required for a valid JSON object to be rendered.
7139
env.fout(formatting.listing(tables, separator='\n'))
7240

7341

74-
def _preset_prices_table(sizes, tables):
42+
def _preset_prices_table(sizes, prices=False):
7543
"""Shows Server Preset options prices.
7644
7745
:param [] sizes: List of Hardware Server sizes.
78-
:param tables: Table formatting.
46+
:param prices: Create a price table or not
7947
"""
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-
if (_verify_prices("%.4f" % size['hourlyRecurringFee'])) or (_verify_prices("%.4f" % size['recurringFee'])):
85-
preset_prices_table.add_row([size['name'], size['key'], "%.4f" % size['hourlyRecurringFee'],
86-
"%.4f" % size['recurringFee']])
87-
tables.append(preset_prices_table)
48+
if prices:
49+
table = formatting.Table(['Size', 'Value', 'Hourly', 'Monthly'], title="Sizes")
50+
for size in sizes:
51+
if size.get('hourlyRecurringFee', 0) + size.get('recurringFee', 0) + 1 > 0:
52+
table.add_row([size['name'], size['key'], "%.4f" % size['hourlyRecurringFee'],
53+
"%.4f" % size['recurringFee']])
54+
else:
55+
table = formatting.Table(['Size', 'Value'], title="Sizes")
56+
for size in sizes:
57+
table.add_row([size['name'], size['key']])
58+
table.sortby = 'Value'
59+
table.align = 'l'
60+
return table
8861

8962

90-
def _os_prices_table(operating_systems, tables):
63+
def _os_prices_table(operating_systems, prices=False):
9164
"""Shows Server Operating Systems prices cost and capacity restriction.
9265
9366
:param [] operating_systems: List of Hardware Server operating systems.
94-
:param tables: Table formatting.
67+
:param prices: Create a price table or not
9568
"""
96-
os_prices_table = formatting.Table(['OS Key', 'Hourly', 'Monthly', 'Restriction'],
97-
title="Operating Systems Prices")
98-
os_prices_table.sortby = 'OS Key'
99-
os_prices_table.align = 'l'
100-
for operating_system in operating_systems:
101-
for price in operating_system['prices']:
102-
if (_verify_prices(_get_price_data(price, 'hourlyRecurringFee'))) or (
103-
_verify_prices(_get_price_data(price, 'recurringFee'))):
69+
if prices:
70+
table = formatting.Table(['Key', 'Hourly', 'Monthly', 'Restriction'],
71+
title="Operating Systems")
72+
for operating_system in operating_systems:
73+
for price in operating_system['prices']:
10474
cr_max = _get_price_data(price, 'capacityRestrictionMaximum')
10575
cr_min = _get_price_data(price, 'capacityRestrictionMinimum')
10676
cr_type = _get_price_data(price, 'capacityRestrictionType')
107-
os_prices_table.add_row(
77+
table.add_row(
10878
[operating_system['key'],
10979
_get_price_data(price, 'hourlyRecurringFee'),
11080
_get_price_data(price, 'recurringFee'),
11181
"%s - %s %s" % (cr_min, cr_max, cr_type)])
112-
tables.append(os_prices_table)
82+
else:
83+
table = formatting.Table(['OS', 'Key', 'Reference Code'], title="Operating Systems")
84+
for operating_system in operating_systems:
85+
table.add_row([operating_system['name'], operating_system['key'], operating_system['referenceCode']])
86+
87+
table.sortby = 'Key'
88+
table.align = 'l'
89+
return table
11390

11491

115-
def _port_speed_prices_table(port_speeds, tables):
92+
def _port_speed_prices_table(port_speeds, prices=False):
11693
"""Shows Server Port Speeds prices cost and capacity restriction.
11794
11895
:param [] port_speeds: List of Hardware Server Port Speeds.
119-
:param tables: Table formatting.
96+
:param prices: Create a price table or not
12097
"""
121-
port_speed_prices_table = formatting.Table(['Key', 'Speed', 'Hourly', 'Monthly', 'Restriction'],
122-
title="Network Options Prices")
123-
port_speed_prices_table.sortby = 'Speed'
124-
port_speed_prices_table.align = 'l'
125-
for speed in port_speeds:
126-
for price in speed['prices']:
127-
if (_verify_prices(_get_price_data(price, 'hourlyRecurringFee'))) or (
128-
_verify_prices(_get_price_data(price, 'recurringFee'))):
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-
port_speed_prices_table.add_row(
98+
if prices:
99+
table = formatting.Table(['Key', 'Speed', 'Hourly', 'Monthly'], title="Network Options")
100+
for speed in port_speeds:
101+
for price in speed['prices']:
102+
table.add_row(
133103
[speed['key'], speed['speed'],
134104
_get_price_data(price, 'hourlyRecurringFee'),
135-
_get_price_data(price, 'recurringFee'),
136-
"%s - %s %s" % (cr_min, cr_max, cr_type)])
137-
tables.append(port_speed_prices_table)
138-
139-
140-
def _verify_prices(prices):
141-
"""Verify the prices is higher to zero(0) or is '-'.
142-
143-
param prices: value to verify.
144-
Returns: true false.
145-
146-
"""
147-
if prices == '-':
148-
return True
105+
_get_price_data(price, 'recurringFee')])
149106
else:
150-
return float(prices) > 0
107+
table = formatting.Table(['Network', 'Speed', 'Key'], title="Network Options")
108+
for speed in port_speeds:
109+
table.add_row([speed['name'], speed['speed'], speed['key']])
110+
table.sortby = 'Speed'
111+
table.align = 'l'
112+
return table
151113

152114

153-
def _extras_prices_table(extras, tables):
115+
def _extras_prices_table(extras, prices=False):
154116
"""Shows Server extras prices cost and capacity restriction.
155117
156118
:param [] extras: List of Hardware Server Extras.
157-
:param tables: Table formatting.
119+
:param prices: Create a price table or not
158120
"""
159-
extras_prices_table = formatting.Table(['Extra Option Key', 'Hourly', 'Monthly', 'Restriction'],
160-
title="Extras Prices")
161-
extras_prices_table.align = 'l'
162-
for extra in extras:
163-
for price in extra['prices']:
164-
if (_verify_prices(_get_price_data(price, 'hourlyRecurringFee'))) or (
165-
_verify_prices(_get_price_data(price, 'recurringFee'))):
166-
cr_max = _get_price_data(price, 'capacityRestrictionMaximum')
167-
cr_min = _get_price_data(price, 'capacityRestrictionMinimum')
168-
cr_type = _get_price_data(price, 'capacityRestrictionType')
169-
extras_prices_table.add_row(
121+
if prices:
122+
table = formatting.Table(['Key', 'Hourly', 'Monthly'], title="Extras")
123+
124+
for extra in extras:
125+
for price in extra['prices']:
126+
table.add_row(
170127
[extra['key'],
171128
_get_price_data(price, 'hourlyRecurringFee'),
172-
_get_price_data(price, 'recurringFee'),
173-
"%s - %s %s" % (cr_min, cr_max, cr_type)])
174-
tables.append(extras_prices_table)
129+
_get_price_data(price, 'recurringFee')])
130+
else:
131+
table = formatting.Table(['Extra Option', 'Key'], title="Extras")
132+
for extra in extras:
133+
table.add_row([extra['name'], extra['key']])
134+
table.sortby = 'Key'
135+
table.align = 'l'
136+
return table
175137

176138

177139
def _get_price_data(price, item):
@@ -184,26 +146,3 @@ def _get_price_data(price, item):
184146
if item in price:
185147
result = price[item]
186148
return result
187-
188-
189-
def _location_item_prices(location_prices, tables):
190-
"""Get a specific data from HS price.
191-
192-
:param price: Hardware Server price.
193-
:param string item: Hardware Server price data.
194-
"""
195-
location_prices_table = formatting.Table(['keyName', 'priceId', 'Hourly', 'Monthly', 'Restriction'])
196-
location_prices_table.sortby = 'keyName'
197-
location_prices_table.align = 'l'
198-
for price in location_prices:
199-
if (_verify_prices(_get_price_data(price, 'hourlyRecurringFee'))) or (
200-
_verify_prices(_get_price_data(price, 'recurringFee'))):
201-
cr_max = _get_price_data(price, 'capacityRestrictionMaximum')
202-
cr_min = _get_price_data(price, 'capacityRestrictionMinimum')
203-
cr_type = _get_price_data(price, 'capacityRestrictionType')
204-
location_prices_table.add_row(
205-
[price['item']['keyName'], price['id'],
206-
_get_price_data(price, 'hourlyRecurringFee'),
207-
_get_price_data(price, 'recurringFee'),
208-
"%s - %s %s" % (cr_min, cr_max, cr_type)])
209-
tables.append(location_prices_table)

tests/CLI/modules/server_tests.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -368,16 +368,15 @@ def test_create_options_prices(self):
368368
self.assert_no_fail(result)
369369
output = json.loads(result.output)
370370
self.assertEqual(output[2][0]['Monthly'], str(0.1))
371-
self.assertEqual(output[2][0]['OS Key'], 'OS_UBUNTU_14_04_LTS_TRUSTY_TAHR_64_BIT')
371+
self.assertEqual(output[2][0]['Key'], 'OS_UBUNTU_14_04_LTS_TRUSTY_TAHR_64_BIT')
372372

373373
def test_create_options_location(self):
374374
result = self.run_command(['server', 'create-options', '--prices', 'dal13'])
375375

376376
self.assert_no_fail(result)
377377
output = json.loads(result.output)
378-
print(output)
379378
self.assertEqual(output[2][0]['Monthly'], str(0.1))
380-
self.assertEqual(output[2][0]['OS Key'], 'OS_UBUNTU_14_04_LTS_TRUSTY_TAHR_64_BIT')
379+
self.assertEqual(output[2][0]['Key'], 'OS_UBUNTU_14_04_LTS_TRUSTY_TAHR_64_BIT')
381380

382381
@mock.patch('SoftLayer.HardwareManager.place_order')
383382
def test_create_server(self, order_mock):

0 commit comments

Comments
 (0)