Skip to content

Commit 76a7fbf

Browse files
Merge pull request #1351 from FernandoOjeda/ft_vs_create_options_prices
Add prices to vs create-options.
2 parents 335579f + 2d1e2bc commit 76a7fbf

File tree

5 files changed

+386
-69
lines changed

5 files changed

+386
-69
lines changed

SoftLayer/CLI/virt/create_options.py

Lines changed: 217 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,89 +9,267 @@
99

1010

1111
@click.command(short_help="Get options to use for creating virtual servers.")
12+
@click.argument('location', required=False)
1213
@click.option('--vsi-type', required=False, show_default=True, default='PUBLIC_CLOUD_SERVER',
13-
type=click.Choice(['TRANSIENT_CLOUD_SERVER', 'SUSPEND_CLOUD_SERVER', 'PUBLIC_CLOUD_SERVER']),
14-
help="Display options for a specific virtual server packages, for default is PUBLIC_CLOUD_SERVER, "
15-
"choose between TRANSIENT_CLOUD_SERVER, SUSPEND_CLOUD_SERVER, PUBLIC_CLOUD_SERVER")
14+
type=click.Choice(['PUBLIC_CLOUD_SERVER', 'TRANSIENT_CLOUD_SERVER', 'SUSPEND_CLOUD_SERVER',
15+
'CLOUD_SERVER']),
16+
help="VS keyName type.")
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')
1620
@environment.pass_env
17-
def cli(env, vsi_type):
21+
def cli(env, vsi_type, prices, location=None):
1822
"""Virtual server order options."""
1923

2024
vsi = SoftLayer.VSManager(env.client)
21-
options = vsi.get_create_options(vsi_type)
25+
options = vsi.get_create_options(vsi_type, location)
2226

2327
tables = []
2428

2529
# Datacenters
2630
dc_table = formatting.Table(['datacenter', 'Value'], title="Datacenters")
2731
dc_table.sortby = 'Value'
2832
dc_table.align = 'l'
29-
30-
for location in options['locations']:
31-
dc_table.add_row([location['name'], location['key']])
33+
for location_info in options['locations']:
34+
dc_table.add_row([location_info['name'], location_info['key']])
3235
tables.append(dc_table)
3336

34-
# Operation system
35-
os_table = formatting.Table(['OS', 'Key', 'Reference Code'], title="Operating Systems")
36-
os_table.sortby = 'Key'
37-
os_table.align = 'l'
37+
if vsi_type == 'CLOUD_SERVER':
38+
tables.append(guest_core_prices_table(options['guest_core'], prices))
39+
tables.append(ram_prices_table(options['ram'], prices))
40+
else:
41+
tables.append(preset_prices_table(options['sizes'], prices))
42+
tables.append(os_prices_table(options['operating_systems'], prices))
43+
tables.append(port_speed_prices_table(options['port_speed'], prices))
44+
tables.append(database_prices_table(options['database'], prices))
45+
tables.append(guest_disk_prices_table(options['guest_disk'], prices))
46+
tables.append(extras_prices_table(options['extras'], prices))
47+
48+
env.fout(tables)
3849

39-
for operating_system in options['operating_systems']:
40-
os_table.add_row([operating_system['name'], operating_system['key'], operating_system['referenceCode']])
41-
tables.append(os_table)
4250

43-
# Sizes
51+
def preset_prices_table(sizes, prices=False):
52+
"""Shows Server Preset options prices.
53+
54+
:param [] sizes: List of Hardware Server sizes.
55+
:param prices: Include pricing information or not.
56+
"""
57+
preset_price_table = formatting.Table(['Size', 'Value', 'Hourly', 'Monthly'], title="Sizes Prices")
58+
preset_price_table.sortby = 'Value'
59+
preset_price_table.align = 'l'
60+
4461
preset_table = formatting.Table(['Size', 'Value'], title="Sizes")
4562
preset_table.sortby = 'Value'
4663
preset_table.align = 'l'
4764

48-
for size in options['sizes']:
65+
for size in sizes:
66+
if (size['hourlyRecurringFee'] > 0) or (size['recurringFee'] > 0):
67+
preset_price_table.add_row([size['name'], size['key'], "%.4f" % size['hourlyRecurringFee'],
68+
"%.4f" % size['recurringFee']])
4969
preset_table.add_row([size['name'], size['key']])
50-
tables.append(preset_table)
70+
if prices:
71+
return preset_price_table
72+
return preset_table
73+
74+
75+
def os_prices_table(operating_systems, prices=False):
76+
"""Shows Server Operating Systems prices cost and capacity restriction.
77+
78+
:param [] operating_systems: List of Hardware Server operating systems.
79+
:param prices: Include pricing information or not.
80+
"""
81+
os_price_table = formatting.Table(['OS Key', 'Hourly', 'Monthly', 'Restriction'],
82+
title="Operating Systems Prices")
83+
os_price_table.sortby = 'OS Key'
84+
os_price_table.align = 'l'
85+
86+
os_table = formatting.Table(['OS', 'Key', 'Reference Code'], title="Operating Systems")
87+
os_table.sortby = 'Key'
88+
os_table.align = 'l'
89+
90+
for operating_system in operating_systems:
91+
for price in operating_system['prices']:
92+
cr_max = _get_price_data(price, 'capacityRestrictionMaximum')
93+
cr_min = _get_price_data(price, 'capacityRestrictionMinimum')
94+
cr_type = _get_price_data(price, 'capacityRestrictionType')
95+
os_price_table.add_row(
96+
[operating_system['key'],
97+
_get_price_data(price, 'hourlyRecurringFee'),
98+
_get_price_data(price, 'recurringFee'),
99+
"%s - %s %s" % (cr_min, cr_max, cr_type)])
100+
os_table.add_row([operating_system['name'], operating_system['key'], operating_system['referenceCode']])
101+
if prices:
102+
return os_price_table
103+
return os_table
104+
105+
106+
def port_speed_prices_table(port_speeds, prices=False):
107+
"""Shows Server Port Speeds prices cost and capacity restriction.
108+
109+
:param [] port_speeds: List of Hardware Server Port Speeds.
110+
:param prices: Include pricing information or not.
111+
"""
112+
port_speed_price_table = formatting.Table(['Key', 'Speed', 'Hourly', 'Monthly'], title="Network Options Prices")
113+
port_speed_price_table.sortby = 'Speed'
114+
port_speed_price_table.align = 'l'
115+
116+
port_speed_table = formatting.Table(['network', 'Key'], title="Network Options")
117+
port_speed_table.sortby = 'Key'
118+
port_speed_table.align = 'l'
119+
120+
for speed in port_speeds:
121+
for price in speed['prices']:
122+
port_speed_price_table.add_row(
123+
[speed['key'], speed['speed'],
124+
_get_price_data(price, 'hourlyRecurringFee'),
125+
_get_price_data(price, 'recurringFee')])
126+
port_speed_table.add_row([speed['name'], speed['key']])
127+
if prices:
128+
return port_speed_price_table
129+
return port_speed_table
130+
131+
132+
def extras_prices_table(extras, prices=False):
133+
"""Shows Server extras prices cost and capacity restriction.
134+
135+
:param [] extras: List of Hardware Server Extras.
136+
:param prices: Include pricing information or not.
137+
"""
138+
extras_price_table = formatting.Table(['Extra Option Key', 'Hourly', 'Monthly'], title="Extras Prices")
139+
extras_price_table.align = 'l'
140+
141+
extras_table = formatting.Table(['Extra Option', 'Value'], title="Extras")
142+
extras_table.sortby = 'Value'
143+
extras_table.align = 'l'
144+
145+
for extra in extras:
146+
for price in extra['prices']:
147+
extras_price_table.add_row(
148+
[extra['key'],
149+
_get_price_data(price, 'hourlyRecurringFee'),
150+
_get_price_data(price, 'recurringFee')])
151+
extras_table.add_row([extra['name'], extra['key']])
152+
if prices:
153+
return extras_price_table
154+
return extras_table
155+
156+
157+
def ram_prices_table(ram_list, prices=False):
158+
"""Shows Server Port Speeds prices cost and capacity restriction.
159+
160+
:param [] ram_list: List of Virtual Server Ram.
161+
:param prices: Include pricing information or not.
162+
"""
163+
ram_price_table = formatting.Table(['Key', 'Hourly', 'Monthly'], title="Ram Prices")
164+
ram_price_table.sortby = 'Key'
165+
ram_price_table.align = 'l'
51166

52-
# RAM
53167
ram_table = formatting.Table(['memory', 'Value'], title="RAM")
54168
ram_table.sortby = 'Value'
55169
ram_table.align = 'l'
56170

57-
for ram in options['ram']:
171+
for ram in ram_list:
172+
for price in ram['prices']:
173+
ram_price_table.add_row(
174+
[ram['key'],
175+
_get_price_data(price, 'hourlyRecurringFee'),
176+
_get_price_data(price, 'recurringFee')])
58177
ram_table.add_row([ram['name'], ram['key']])
59-
tables.append(ram_table)
178+
if prices:
179+
return ram_price_table
180+
return ram_table
181+
182+
183+
def database_prices_table(database_list, prices=False):
184+
"""Shows Server Port Speeds prices cost and capacity restriction.
185+
186+
:param [] database_list: List of Virtual Server database.
187+
:param prices: Include pricing information or not.
188+
"""
189+
database_price_table = formatting.Table(['Key', 'Hourly', 'Monthly', 'Restriction'], title="Data Base Prices")
190+
database_price_table.sortby = 'Key'
191+
database_price_table.align = 'l'
60192

61-
# Data base
62193
database_table = formatting.Table(['database', 'Value'], title="Databases")
63194
database_table.sortby = 'Value'
64195
database_table.align = 'l'
65196

66-
for database in options['database']:
197+
for database in database_list:
198+
for price in database['prices']:
199+
cr_max = _get_price_data(price, 'capacityRestrictionMaximum')
200+
cr_min = _get_price_data(price, 'capacityRestrictionMinimum')
201+
cr_type = _get_price_data(price, 'capacityRestrictionType')
202+
database_price_table.add_row(
203+
[database['key'],
204+
_get_price_data(price, 'hourlyRecurringFee'),
205+
_get_price_data(price, 'recurringFee'),
206+
"%s - %s %s" % (cr_min, cr_max, cr_type)])
67207
database_table.add_row([database['name'], database['key']])
68-
tables.append(database_table)
208+
if prices:
209+
return database_price_table
210+
return database_table
211+
212+
213+
def guest_core_prices_table(guest_core_list, prices=False):
214+
"""Shows Server Port Speeds prices cost and capacity restriction.
215+
216+
:param [] guest_core_list: List of Virtual Server guest_core.
217+
:param prices: Include pricing information or not.
218+
"""
219+
guest_core_price_table = formatting.Table(['Key', 'Hourly', 'Monthly'], title="Guest Core Prices")
220+
guest_core_price_table.sortby = 'Key'
221+
guest_core_price_table.align = 'l'
69222

70-
# Guest_core
71223
guest_core_table = formatting.Table(['cpu', 'Value', 'Capacity'], title="Guest_core")
72224
guest_core_table.sortby = 'Value'
73225
guest_core_table.align = 'l'
74226

75-
for guest_core in options['guest_core']:
227+
for guest_core in guest_core_list:
228+
for price in guest_core['prices']:
229+
guest_core_price_table.add_row(
230+
[guest_core['key'],
231+
_get_price_data(price, 'hourlyRecurringFee'),
232+
_get_price_data(price, 'recurringFee')])
76233
guest_core_table.add_row([guest_core['name'], guest_core['key'], guest_core['capacity']])
77-
tables.append(guest_core_table)
234+
if prices:
235+
return guest_core_price_table
236+
return guest_core_table
237+
238+
239+
def guest_disk_prices_table(guest_disk_list, prices=False):
240+
"""Shows Server Port Speeds prices cost and capacity restriction.
241+
242+
:param [] guest_disk_list: List of Virtual Server guest_disk.
243+
:param prices: Include pricing information or not.
244+
"""
245+
guest_disk_price_table = formatting.Table(['Key', 'Hourly', 'Monthly'], title="Guest Disk Prices")
246+
guest_disk_price_table.sortby = 'Key'
247+
guest_disk_price_table.align = 'l'
78248

79-
# Guest_core
80249
guest_disk_table = formatting.Table(['guest_disk', 'Value', 'Capacity', 'Disk'], title="Guest_disks")
81250
guest_disk_table.sortby = 'Value'
82251
guest_disk_table.align = 'l'
83252

84-
for guest_disk in options['guest_disk']:
85-
guest_disk_table.add_row([guest_disk['name'], guest_disk['key'], guest_disk['capacity'], guest_disk['disk']])
86-
tables.append(guest_disk_table)
253+
for guest_disk in guest_disk_list:
254+
for price in guest_disk['prices']:
255+
guest_disk_price_table.add_row(
256+
[guest_disk['key'],
257+
_get_price_data(price, 'hourlyRecurringFee'),
258+
_get_price_data(price, 'recurringFee')])
259+
guest_disk_table.add_row(
260+
[guest_disk['name'], guest_disk['key'], guest_disk['capacity'], guest_disk['disk']])
261+
if prices:
262+
return guest_disk_price_table
263+
return guest_disk_table
87264

88-
# Port speed
89-
port_speed_table = formatting.Table(['network', 'Key'], title="Network Options")
90-
port_speed_table.sortby = 'Key'
91-
port_speed_table.align = 'l'
92265

93-
for speed in options['port_speed']:
94-
port_speed_table.add_row([speed['name'], speed['key']])
95-
tables.append(port_speed_table)
266+
def _get_price_data(price, item):
267+
"""Get a specific data from HS price.
96268
97-
env.fout(formatting.listing(tables, separator='\n'))
269+
:param price: Hardware Server price.
270+
:param string item: Hardware Server price data.
271+
"""
272+
result = '-'
273+
if item in price:
274+
result = price[item]
275+
return result

0 commit comments

Comments
 (0)