Skip to content

Commit 79ac147

Browse files
Merge pull request #1194 from FernandoOjeda/fo_hardware_detail_bandwidth
Fix hardware detail bandwidth allocation.
2 parents d7aec15 + c5d413d commit 79ac147

File tree

4 files changed

+36
-10
lines changed

4 files changed

+36
-10
lines changed

SoftLayer/CLI/hardware/detail.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,17 @@ def cli(env, identifier, passwords, price):
9292

9393

9494
def _bw_table(bw_data):
95-
"""Generates a bandwidth useage table"""
95+
"""Generates a bandwidth usage table"""
9696
table = formatting.Table(['Type', 'In GB', 'Out GB', 'Allotment'])
97-
for bw_point in bw_data.get('useage'):
97+
for bw_point in bw_data.get('usage'):
9898
bw_type = 'Private'
9999
allotment = 'N/A'
100100
if bw_point['type']['alias'] == 'PUBLIC_SERVER_BW':
101101
bw_type = 'Public'
102-
allotment = utils.lookup(bw_data, 'allotment', 'amount')
103-
if allotment is None:
102+
if not bw_data.get('allotment'):
104103
allotment = '-'
104+
else:
105+
allotment = utils.lookup(bw_data, 'allotment', 'amount')
105106

106107
table.add_row([bw_type, bw_point['amountIn'], bw_point['amountOut'], allotment])
107108
return table

SoftLayer/managers/hardware.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -697,8 +697,10 @@ def get_bandwidth_allocation(self, instance_id):
697697
a_mask = "mask[allocation[amount]]"
698698
allotment = self.client.call('Hardware_Server', 'getBandwidthAllotmentDetail', id=instance_id, mask=a_mask)
699699
u_mask = "mask[amountIn,amountOut,type]"
700-
useage = self.client.call('Hardware_Server', 'getBillingCycleBandwidthUsage', id=instance_id, mask=u_mask)
701-
return {'allotment': allotment.get('allocation'), 'useage': useage}
700+
usage = self.client.call('Hardware_Server', 'getBillingCycleBandwidthUsage', id=instance_id, mask=u_mask)
701+
if allotment:
702+
return {'allotment': allotment.get('allocation'), 'usage': usage}
703+
return {'allotment': allotment, 'usage': usage}
702704

703705

704706
def _get_extra_price_id(items, key_name, hourly, location):

tests/CLI/modules/server_tests.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,17 @@ def test_detail_vs_empty_tag(self):
127127
['example-tag'],
128128
)
129129

130+
def test_detail_empty_allotment(self):
131+
mock = self.set_mock('SoftLayer_Hardware_Server', 'getBandwidthAllotmentDetail')
132+
mock.return_value = None
133+
result = self.run_command(['server', 'detail', '100'])
134+
135+
self.assert_no_fail(result)
136+
self.assertEqual(
137+
json.loads(result.output)['Bandwidth'][0]['Allotment'],
138+
'-',
139+
)
140+
130141
def test_list_servers(self):
131142
result = self.run_command(['server', 'list', '--tag=openstack'])
132143

tests/managers/hardware_tests.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@
88

99
import mock
1010

11-
1211
import SoftLayer
1312

1413
from SoftLayer import fixtures
1514
from SoftLayer import managers
1615
from SoftLayer import testing
1716

18-
1917
MINIMAL_TEST_CREATE_ARGS = {
2018
'size': 'S1270_8GB_2X1TBSATA_NORAID',
2119
'hostname': 'unicorn',
@@ -446,11 +444,25 @@ def test_get_bandwidth_allocation(self):
446444
self.assert_called_with('SoftLayer_Hardware_Server', 'getBandwidthAllotmentDetail', identifier=1234)
447445
self.assert_called_with('SoftLayer_Hardware_Server', 'getBillingCycleBandwidthUsage', identifier=1234)
448446
self.assertEqual(result['allotment']['amount'], '250')
449-
self.assertEqual(result['useage'][0]['amountIn'], '.448')
447+
self.assertEqual(result['usage'][0]['amountIn'], '.448')
448+
449+
def test_get_bandwidth_allocation_with_allotment(self):
450+
mock = self.set_mock('SoftLayer_Hardware_Server', 'getBandwidthAllotmentDetail')
451+
mock.return_value = {
452+
"allocationId": 11111,
453+
"id": 22222,
454+
"allocation": {
455+
"amount": "2000"
456+
}
457+
}
458+
459+
result = self.hardware.get_bandwidth_allocation(1234)
460+
461+
self.assertEqual(2000, int(result['allotment']['amount']))
450462

451463
def test_get_bandwidth_allocation_no_allotment(self):
452464
mock = self.set_mock('SoftLayer_Hardware_Server', 'getBandwidthAllotmentDetail')
453-
mock.return_value = {}
465+
mock.return_value = None
454466

455467
result = self.hardware.get_bandwidth_allocation(1234)
456468

0 commit comments

Comments
 (0)