Skip to content

Refactor loadbal order-options #1521

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 4, 2021
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
97 changes: 52 additions & 45 deletions SoftLayer/CLI/loadbal/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,53 +89,60 @@ def order_options(env, datacenter):
net_mgr = SoftLayer.NetworkManager(env.client)
package = mgr.lbaas_order_options()

for region in package['regions']:
dc_name = utils.lookup(region, 'location', 'location', 'name')

# Skip locations if they are not the one requested.
if datacenter and dc_name != datacenter:
continue
this_table = formatting.Table(
['Prices', 'Private Subnets'],
title="{}: {}".format(region['keyname'], region['description'])
)

l_groups = []
for group in region['location']['location']['groups']:
l_groups.append(group.get('id'))

# Price lookups
prices = []
price_table = formatting.KeyValueTable(['KeyName', 'Cost'])
for item in package['items']:
i_price = {'keyName': item['keyName']}
for price in item.get('prices', []):
if not price.get('locationGroupId'):
i_price['default_price'] = price.get('hourlyRecurringFee')
elif price.get('locationGroupId') in l_groups:
i_price['region_price'] = price.get('hourlyRecurringFee')
prices.append(i_price)
for price in prices:
if price.get('region_price'):
price_table.add_row([price.get('keyName'), price.get('region_price')])
else:
price_table.add_row([price.get('keyName'), price.get('default_price')])

# Vlan/Subnet Lookups
mask = "mask[networkVlan,podName,addressSpace]"
subnets = net_mgr.list_subnets(datacenter=dc_name, network_space='PRIVATE', mask=mask)
subnet_table = formatting.Table(['Id', 'Subnet', 'Vlan'])

for subnet in subnets:
# Only show these types, easier to filter here than in an API call.
if subnet.get('subnetType') != 'PRIMARY' and subnet.get('subnetType') != 'ADDITIONAL_PRIMARY':
if not datacenter:
data_table = formatting.KeyValueTable(['Datacenters', 'City'])
for region in package['regions']:
data_table.add_row([region['description'].split('-')[0], region['description'].split('-')[1]])
# print(region)
env.fout(data_table)
click.secho("Use `slcli lb order-options --datacenter <DC>` "
"to find pricing information and private subnets for that specific site.")

else:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is a lot of logic below, maybe you can get a chance to move it and encapsulate it within the manager module.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of this logic is just table formatting, so I'm ok with it not being in a manager specifically, unless we want to use it in more than just this one command. Breaking it up into a few smaller sub-functions that return Table objects would make this logic a bit more readable though.

for region in package['regions']:
dc_name = utils.lookup(region, 'location', 'location', 'name')

# Skip locations if they are not the one requested.
if datacenter and dc_name != datacenter.lower():
continue
space = "{}/{}".format(subnet.get('networkIdentifier'), subnet.get('cidr'))
vlan = "{}.{}".format(subnet['podName'], subnet['networkVlan']['vlanNumber'])
subnet_table.add_row([subnet.get('id'), space, vlan])
this_table.add_row([price_table, subnet_table])

env.fout(this_table)
l_groups = []
for group in region['location']['location']['groups']:
l_groups.append(group.get('id'))

# Price lookups
prices = []
price_table = formatting.KeyValueTable(['KeyName', 'Cost'], title='Prices')
for item in package['items']:
i_price = {'keyName': item['keyName']}
for price in item.get('prices', []):
if not price.get('locationGroupId'):
i_price['default_price'] = price.get('hourlyRecurringFee')
elif price.get('locationGroupId') in l_groups:
i_price['region_price'] = price.get('hourlyRecurringFee')
prices.append(i_price)
for price in prices:
if price.get('region_price'):
price_table.add_row([price.get('keyName'), price.get('region_price')])
else:
price_table.add_row([price.get('keyName'), price.get('default_price')])

# Vlan/Subnet Lookups
mask = "mask[networkVlan,podName,addressSpace]"
subnets = net_mgr.list_subnets(datacenter=dc_name, network_space='PRIVATE', mask=mask)
subnet_table = formatting.Table(['Id', 'Subnet', 'Vlan'], title='Private subnet')

for subnet in subnets:
# Only show these types, easier to filter here than in an API call.
if subnet.get('subnetType') != 'PRIMARY' and \
subnet.get('subnetType') != 'ADDITIONAL_PRIMARY':
continue
space = "{}/{}".format(subnet.get('networkIdentifier'), subnet.get('cidr'))
vlan = "{}.{}".format(subnet['podName'], subnet['networkVlan']['vlanNumber'])
subnet_table.add_row([subnet.get('id'), space, vlan])

env.fout(price_table)
env.fout(subnet_table)


@click.command()
Expand Down
7 changes: 3 additions & 4 deletions tests/CLI/modules/loadbal_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,11 +258,10 @@ def test_verify_order(self):
self.assert_no_fail(result)

def test_order_options(self):
mock = self.set_mock('SoftLayer_Product_Package', 'getAllObjects')
mock.return_value = SoftLayer_Product_Package.getAllObjectsLoadbal
fault_string = 'Use `slcli lb order-options --datacenter <DC>`' \
' to find pricing information and private subnets for that specific site.'
result = self.run_command(['loadbal', 'order-options'])

self.assert_no_fail(result)
self.assertIn(fault_string, result.output)

def test_order_options_with_datacenter(self):
mock = self.set_mock('SoftLayer_Product_Package', 'getAllObjects')
Expand Down