Skip to content

Commit 30a7eda

Browse files
Merge pull request #1521 from caberos/issue1520
Refactor loadbal order-options
2 parents 5dde6fa + eee1c28 commit 30a7eda

File tree

2 files changed

+55
-49
lines changed

2 files changed

+55
-49
lines changed

Diff for: SoftLayer/CLI/loadbal/order.py

+52-45
Original file line numberDiff line numberDiff line change
@@ -89,53 +89,60 @@ 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("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.lower():
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])
137108

138-
env.fout(this_table)
109+
l_groups = []
110+
for group in region['location']['location']['groups']:
111+
l_groups.append(group.get('id'))
112+
113+
# Price lookups
114+
prices = []
115+
price_table = formatting.KeyValueTable(['KeyName', 'Cost'], title='Prices')
116+
for item in package['items']:
117+
i_price = {'keyName': item['keyName']}
118+
for price in item.get('prices', []):
119+
if not price.get('locationGroupId'):
120+
i_price['default_price'] = price.get('hourlyRecurringFee')
121+
elif price.get('locationGroupId') in l_groups:
122+
i_price['region_price'] = price.get('hourlyRecurringFee')
123+
prices.append(i_price)
124+
for price in prices:
125+
if price.get('region_price'):
126+
price_table.add_row([price.get('keyName'), price.get('region_price')])
127+
else:
128+
price_table.add_row([price.get('keyName'), price.get('default_price')])
129+
130+
# Vlan/Subnet Lookups
131+
mask = "mask[networkVlan,podName,addressSpace]"
132+
subnets = net_mgr.list_subnets(datacenter=dc_name, network_space='PRIVATE', mask=mask)
133+
subnet_table = formatting.Table(['Id', 'Subnet', 'Vlan'], title='Private subnet')
134+
135+
for subnet in subnets:
136+
# Only show these types, easier to filter here than in an API call.
137+
if subnet.get('subnetType') != 'PRIMARY' and \
138+
subnet.get('subnetType') != 'ADDITIONAL_PRIMARY':
139+
continue
140+
space = "{}/{}".format(subnet.get('networkIdentifier'), subnet.get('cidr'))
141+
vlan = "{}.{}".format(subnet['podName'], subnet['networkVlan']['vlanNumber'])
142+
subnet_table.add_row([subnet.get('id'), space, vlan])
143+
144+
env.fout(price_table)
145+
env.fout(subnet_table)
139146

140147

141148
@click.command()

Diff for: tests/CLI/modules/loadbal_tests.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -258,11 +258,10 @@ 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(fault_string, result.output)
266265

267266
def test_order_options_with_datacenter(self):
268267
mock = self.set_mock('SoftLayer_Product_Package', 'getAllObjects')

0 commit comments

Comments
 (0)