Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
197 changes: 161 additions & 36 deletions SoftLayer/CLI/hardware/create_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,54 +9,179 @@


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

hardware_manager = hardware.HardwareManager(env.client)
options = hardware_manager.get_create_options()
options = hardware_manager.get_create_options(location)

tables = []

# Datacenters
dc_table = formatting.Table(['Datacenter', 'Value'], title="Datacenters")
dc_table.sortby = 'Value'
dc_table.align = 'l'
for location in options['locations']:
dc_table.add_row([location['name'], location['key']])
for location_info in options['locations']:
dc_table.add_row([location_info['name'], location_info['key']])
tables.append(dc_table)

# Presets
preset_table = formatting.Table(['Size', 'Value'], title="Sizes")
preset_table.sortby = 'Value'
preset_table.align = 'l'
for size in options['sizes']:
preset_table.add_row([size['name'], size['key']])
tables.append(preset_table)

# Operating systems
os_table = formatting.Table(['OS', 'Key', 'Reference Code'], title="Operating Systems")
os_table.sortby = 'Key'
os_table.align = 'l'
for operating_system in options['operating_systems']:
os_table.add_row([operating_system['name'], operating_system['key'], operating_system['referenceCode']])
tables.append(os_table)

# Port speed
port_speed_table = formatting.Table(['Network', 'Speed', 'Key'], title="Network Options")
port_speed_table.sortby = 'Speed'
port_speed_table.align = 'l'

for speed in options['port_speeds']:
port_speed_table.add_row([speed['name'], speed['speed'], speed['key']])
tables.append(port_speed_table)

# Extras
extras_table = formatting.Table(['Extra Option', 'Value'], title="Extras")
extras_table.sortby = 'Value'
extras_table.align = 'l'
for extra in options['extras']:
extras_table.add_row([extra['name'], extra['key']])
tables.append(extras_table)
if prices:
_preset_prices_table(options['sizes'], tables)
_os_prices_table(options['operating_systems'], tables)
_port_speed_prices_table(options['port_speeds'], tables)
_extras_prices_table(options['extras'], tables)
else:
# Presets
preset_table = formatting.Table(['Size', 'Value'], title="Sizes")
preset_table.sortby = 'Value'
preset_table.align = 'l'
for size in options['sizes']:
preset_table.add_row([size['name'], size['key']])
tables.append(preset_table)

# Operating systems
os_table = formatting.Table(['OS', 'Key', 'Reference Code'], title="Operating Systems")
os_table.sortby = 'Key'
os_table.align = 'l'
for operating_system in options['operating_systems']:
os_table.add_row([operating_system['name'], operating_system['key'], operating_system['referenceCode']])
tables.append(os_table)

# Port speed
port_speed_table = formatting.Table(['Network', 'Speed', 'Key'], title="Network Options")
port_speed_table.sortby = 'Speed'
port_speed_table.align = 'l'
for speed in options['port_speeds']:
port_speed_table.add_row([speed['name'], speed['speed'], speed['key']])
tables.append(port_speed_table)

# Extras
extras_table = formatting.Table(['Extra Option', 'Value'], title="Extras")
extras_table.sortby = 'Value'
extras_table.align = 'l'
for extra in options['extras']:
extras_table.add_row([extra['name'], extra['key']])
tables.append(extras_table)

env.fout(formatting.listing(tables, separator='\n'))


def _preset_prices_table(sizes, tables):
"""Shows Server Preset options prices.

:param [] sizes: List of Hardware Server sizes.
:param tables: Table formatting.
"""
preset_prices_table = formatting.Table(['Size', 'Value', 'Hourly', 'Monthly'], title="Sizes Prices")
preset_prices_table.sortby = 'Value'
preset_prices_table.align = 'l'
for size in sizes:
preset_prices_table.add_row([size['name'], size['key'], "%.4f" % size['hourlyRecurringFee'],
"%.4f" % size['recurringFee']])
tables.append(preset_prices_table)


def _os_prices_table(operating_systems, tables):
"""Shows Server Operating Systems prices cost and capacity restriction.

:param [] operating_systems: List of Hardware Server operating systems.
:param tables: Table formatting.
"""
os_prices_table = formatting.Table(['OS Key', 'Hourly', 'Monthly', 'Restriction'],
title="Operating Systems Prices")
os_prices_table.sortby = 'OS Key'
os_prices_table.align = 'l'
for operating_system in operating_systems:
for price in operating_system['prices']:
cr_max = _get_price_data(price, 'capacityRestrictionMaximum')
cr_min = _get_price_data(price, 'capacityRestrictionMinimum')
cr_type = _get_price_data(price, 'capacityRestrictionType')
os_prices_table.add_row(
[operating_system['key'],
_get_price_data(price, 'hourlyRecurringFee'),
_get_price_data(price, 'recurringFee'),
"%s - %s %s" % (cr_min, cr_max, cr_type)])
tables.append(os_prices_table)


def _port_speed_prices_table(port_speeds, tables):
"""Shows Server Port Speeds prices cost and capacity restriction.

:param [] port_speeds: List of Hardware Server Port Speeds.
:param tables: Table formatting.
"""
port_speed_prices_table = formatting.Table(['Key', 'Speed', 'Hourly', 'Monthly', 'Restriction'],
title="Network Options Prices")
port_speed_prices_table.sortby = 'Speed'
port_speed_prices_table.align = 'l'
for speed in port_speeds:
for price in speed['prices']:
cr_max = _get_price_data(price, 'capacityRestrictionMaximum')
cr_min = _get_price_data(price, 'capacityRestrictionMinimum')
cr_type = _get_price_data(price, 'capacityRestrictionType')
port_speed_prices_table.add_row(
[speed['key'], speed['speed'],
_get_price_data(price, 'hourlyRecurringFee'),
_get_price_data(price, 'recurringFee'),
"%s - %s %s" % (cr_min, cr_max, cr_type)])
tables.append(port_speed_prices_table)


def _extras_prices_table(extras, tables):
"""Shows Server extras prices cost and capacity restriction.

:param [] extras: List of Hardware Server Extras.
:param tables: Table formatting.
"""
extras_prices_table = formatting.Table(['Extra Option Key', 'Hourly', 'Monthly', 'Restriction'],
title="Extras Prices")
extras_prices_table.align = 'l'
for extra in extras:
for price in extra['prices']:
cr_max = _get_price_data(price, 'capacityRestrictionMaximum')
cr_min = _get_price_data(price, 'capacityRestrictionMinimum')
cr_type = _get_price_data(price, 'capacityRestrictionType')
extras_prices_table.add_row(
[extra['key'],
_get_price_data(price, 'hourlyRecurringFee'),
_get_price_data(price, 'recurringFee'),
"%s - %s %s" % (cr_min, cr_max, cr_type)])
tables.append(extras_prices_table)


def _get_price_data(price, item):
"""Get a specific data from HS price.

:param price: Hardware Server price.
:param string item: Hardware Server price data.
"""
result = '-'
if item in price:
result = price[item]
return result


def _location_item_prices(location_prices, tables):
"""Get a specific data from HS price.

:param price: Hardware Server price.
:param string item: Hardware Server price data.
"""
location_prices_table = formatting.Table(['keyName', 'priceId', 'Hourly', 'Monthly', 'Restriction'])
location_prices_table.sortby = 'keyName'
location_prices_table.align = 'l'
for price in location_prices:
cr_max = _get_price_data(price, 'capacityRestrictionMaximum')
cr_min = _get_price_data(price, 'capacityRestrictionMinimum')
cr_type = _get_price_data(price, 'capacityRestrictionType')
location_prices_table.add_row(
[price['item']['keyName'], price['id'],
_get_price_data(price, 'hourlyRecurringFee'),
_get_price_data(price, 'recurringFee'),
"%s - %s %s" % (cr_min, cr_max, cr_type)])
tables.append(location_prices_table)
86 changes: 80 additions & 6 deletions SoftLayer/CLI/order/item_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,20 @@
from SoftLayer.utils import lookup

COLUMNS = ['category', 'keyName', 'description', 'priceId']
COLUMNS_ITEM_PRICES = ['keyName', 'priceId', 'Hourly', 'Monthly', 'Restriction']
COLUMNS_ITEM_PRICES_LOCATION = ['keyName', 'priceId', 'Hourly', 'Monthly', 'Restriction']


@click.command()
@click.argument('location', required=False, nargs=-1, type=click.UNPROCESSED)
@click.argument('package_keyname')
@click.option('--keyword', help="A word (or string) used to filter item names.")
@click.option('--category', help="Category code to filter items by")
@click.option('--prices', '-p', is_flag=True, help='Use --prices to list the server item prices, and to list the '
'Item Prices by location, add it to the --prices option using '
'location KeyName, e.g. --prices AMSTERDAM02')
@environment.pass_env
def cli(env, package_keyname, keyword, category):
def cli(env, location, package_keyname, keyword, category, prices):
"""List package items used for ordering.

The item keyNames listed can be used with `slcli order place` to specify
Expand All @@ -34,9 +40,10 @@ def cli(env, package_keyname, keyword, category):
slcli order item-list BARE_METAL_SERVER --category os --keyword ubuntu

"""
table = formatting.Table(COLUMNS)
manager = ordering.OrderingManager(env.client)

tables = []

_filter = {'items': {}}
if keyword:
_filter['items']['description'] = {'operation': '*= %s' % keyword}
Expand All @@ -47,10 +54,19 @@ def cli(env, package_keyname, keyword, category):
sorted_items = sort_items(items)

categories = sorted_items.keys()
for catname in sorted(categories):
for item in sorted_items[catname]:
table.add_row([catname, item['keyName'], item['description'], get_price(item)])
env.fout(table)
if prices:
_item_list_prices(categories, sorted_items, tables)
if location:
location = location[0]
location_prices = manager.get_item_prices_by_location(location, package_keyname)
_location_item_prices(location_prices, location, tables)
else:
table_items_detail = formatting.Table(COLUMNS)
for catname in sorted(categories):
for item in sorted_items[catname]:
table_items_detail.add_row([catname, item['keyName'], item['description'], get_price(item)])
tables.append(table_items_detail)
env.fout(formatting.listing(tables, separator='\n'))


def sort_items(items):
Expand All @@ -73,3 +89,61 @@ def get_price(item):
if not price.get('locationGroupId'):
return price.get('id')
return 0


def _item_list_prices(categories, sorted_items, tables):
"""Add the item prices cost and capacity restriction to the table"""
table_prices = formatting.Table(COLUMNS_ITEM_PRICES)
for catname in sorted(categories):
for item in sorted_items[catname]:
for price in item['prices']:
if not price.get('locationGroupId'):
cr_max = _get_price_data(price, 'capacityRestrictionMaximum')
cr_min = _get_price_data(price, 'capacityRestrictionMinimum')
cr_type = _get_price_data(price, 'capacityRestrictionType')
table_prices.add_row([item['keyName'], price['id'],
get_item_price_data(price, 'hourlyRecurringFee'),
get_item_price_data(price, 'recurringFee'),
"%s - %s %s" % (cr_min, cr_max, cr_type)])
tables.append(table_prices)


def get_item_price_data(price, item_attribute):
"""Given an SoftLayer_Product_Item_Price, returns its default price data"""
result = '-'
if item_attribute in price:
result = price[item_attribute]
return result


def _location_item_prices(location_prices, location, tables):
"""Get a specific data from HS price.

:param price: Hardware Server price.
:param string item: Hardware Server price data.
"""
location_prices_table = formatting.Table(COLUMNS_ITEM_PRICES_LOCATION, title="Item Prices for %s" % location)
location_prices_table.sortby = 'keyName'
location_prices_table.align = 'l'
for price in location_prices:
cr_max = _get_price_data(price, 'capacityRestrictionMaximum')
cr_min = _get_price_data(price, 'capacityRestrictionMinimum')
cr_type = _get_price_data(price, 'capacityRestrictionType')
location_prices_table.add_row(
[price['item']['keyName'], price['id'],
_get_price_data(price, 'hourlyRecurringFee'),
_get_price_data(price, 'recurringFee'),
"%s - %s %s" % (cr_min, cr_max, cr_type)])
tables.append(location_prices_table)


def _get_price_data(price, item):
"""Get a specific data from HS price.

:param price: Hardware Server price.
:param string item: Hardware Server price data.
"""
result = '-'
if item in price:
result = price[item]
return result
Loading