Skip to content

Commit 9a2752b

Browse files
caberoscaberos
authored andcommitted
Refactor loadbal order-options
1 parent 544fcb9 commit 9a2752b

File tree

2 files changed

+61
-50
lines changed

2 files changed

+61
-50
lines changed

SoftLayer/CLI/loadbal/order.py

Lines changed: 57 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -89,53 +89,64 @@ def order_options(env, datacenter):
8989
net_mgr = SoftLayer.NetworkManager(env.client)
9090
package = mgr.lbaas_order_options()
9191

92-
for region in package['regions']:
93-
dc_name = utils.lookup(region, 'location', 'location', 'name')
94-
95-
# Skip locations if they are not the one requested.
96-
if datacenter and dc_name != datacenter:
97-
continue
98-
this_table = formatting.Table(
99-
['Prices', 'Private Subnets'],
100-
title="{}: {}".format(region['keyname'], region['description'])
101-
)
102-
103-
l_groups = []
104-
for group in region['location']['location']['groups']:
105-
l_groups.append(group.get('id'))
106-
107-
# Price lookups
108-
prices = []
109-
price_table = formatting.KeyValueTable(['KeyName', 'Cost'])
110-
for item in package['items']:
111-
i_price = {'keyName': item['keyName']}
112-
for price in item.get('prices', []):
113-
if not price.get('locationGroupId'):
114-
i_price['default_price'] = price.get('hourlyRecurringFee')
115-
elif price.get('locationGroupId') in l_groups:
116-
i_price['region_price'] = price.get('hourlyRecurringFee')
117-
prices.append(i_price)
118-
for price in prices:
119-
if price.get('region_price'):
120-
price_table.add_row([price.get('keyName'), price.get('region_price')])
121-
else:
122-
price_table.add_row([price.get('keyName'), price.get('default_price')])
123-
124-
# Vlan/Subnet Lookups
125-
mask = "mask[networkVlan,podName,addressSpace]"
126-
subnets = net_mgr.list_subnets(datacenter=dc_name, network_space='PRIVATE', mask=mask)
127-
subnet_table = formatting.Table(['Id', 'Subnet', 'Vlan'])
128-
129-
for subnet in subnets:
130-
# Only show these types, easier to filter here than in an API call.
131-
if subnet.get('subnetType') != 'PRIMARY' and subnet.get('subnetType') != 'ADDITIONAL_PRIMARY':
92+
if not datacenter:
93+
data_table = formatting.KeyValueTable(['Datacenters', 'City'])
94+
for region in package['regions']:
95+
data_table.add_row([region['description'].split('-')[0], region['description'].split('-')[1]])
96+
# print(region)
97+
env.fout(data_table)
98+
click.secho("ERROR: Use `slcli lb order-options --datacenter <DC>` "
99+
"to find pricing information and private subnets for that specific site.")
100+
101+
else:
102+
for region in package['regions']:
103+
dc_name = utils.lookup(region, 'location', 'location', 'name')
104+
105+
# Skip locations if they are not the one requested.
106+
if datacenter and dc_name != datacenter:
132107
continue
133-
space = "{}/{}".format(subnet.get('networkIdentifier'), subnet.get('cidr'))
134-
vlan = "{}.{}".format(subnet['podName'], subnet['networkVlan']['vlanNumber'])
135-
subnet_table.add_row([subnet.get('id'), space, vlan])
136-
this_table.add_row([price_table, subnet_table])
137-
138-
env.fout(this_table)
108+
this_table = formatting.Table(
109+
['Prices', 'Private Subnets'],
110+
title="{}: {}".format(region['keyname'], region['description'])
111+
)
112+
113+
l_groups = []
114+
for group in region['location']['location']['groups']:
115+
l_groups.append(group.get('id'))
116+
117+
# Price lookups
118+
prices = []
119+
price_table = formatting.KeyValueTable(['KeyName', 'Cost'])
120+
for item in package['items']:
121+
i_price = {'keyName': item['keyName']}
122+
for price in item.get('prices', []):
123+
if not price.get('locationGroupId'):
124+
i_price['default_price'] = price.get('hourlyRecurringFee')
125+
elif price.get('locationGroupId') in l_groups:
126+
i_price['region_price'] = price.get('hourlyRecurringFee')
127+
prices.append(i_price)
128+
for price in prices:
129+
if price.get('region_price'):
130+
price_table.add_row([price.get('keyName'), price.get('region_price')])
131+
else:
132+
price_table.add_row([price.get('keyName'), price.get('default_price')])
133+
134+
# Vlan/Subnet Lookups
135+
mask = "mask[networkVlan,podName,addressSpace]"
136+
subnets = net_mgr.list_subnets(datacenter=dc_name, network_space='PRIVATE', mask=mask)
137+
subnet_table = formatting.Table(['Id', 'Subnet', 'Vlan'])
138+
139+
for subnet in subnets:
140+
# Only show these types, easier to filter here than in an API call.
141+
if subnet.get('subnetType') != 'PRIMARY' and \
142+
subnet.get('subnetType') != 'ADDITIONAL_PRIMARY':
143+
continue
144+
space = "{}/{}".format(subnet.get('networkIdentifier'), subnet.get('cidr'))
145+
vlan = "{}.{}".format(subnet['podName'], subnet['networkVlan']['vlanNumber'])
146+
subnet_table.add_row([subnet.get('id'), space, vlan])
147+
this_table.add_row([price_table, subnet_table])
148+
149+
env.fout(this_table)
139150

140151

141152
@click.command()

tests/CLI/modules/loadbal_tests.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,11 +258,11 @@ def test_verify_order(self):
258258
self.assert_no_fail(result)
259259

260260
def test_order_options(self):
261-
mock = self.set_mock('SoftLayer_Product_Package', 'getAllObjects')
262-
mock.return_value = SoftLayer_Product_Package.getAllObjectsLoadbal
261+
fault_string = 'Use `slcli lb order-options --datacenter <DC>`' \
262+
' to find pricing information and private subnets for that specific site.'
263263
result = self.run_command(['loadbal', 'order-options'])
264-
265-
self.assert_no_fail(result)
264+
self.assertIn("ERROR: {}".format(fault_string),
265+
result.output)
266266

267267
def test_order_options_with_datacenter(self):
268268
mock = self.set_mock('SoftLayer_Product_Package', 'getAllObjects')

0 commit comments

Comments
 (0)