Repository: softlayer/softlayer-python
Pull request #1194 (2019-12-05 23:02:28)

Fix hardware detail bandwidth allocation.
Fix hardware detail bandwidth allocation https://github.com/softlayer/softlayer-python/issues/1192.


Patch:
diff --git a/SoftLayer/CLI/hardware/detail.py b/SoftLayer/CLI/hardware/detail.py
index 3a55f1b6..89f0cb0e 100644
--- a/SoftLayer/CLI/hardware/detail.py
+++ b/SoftLayer/CLI/hardware/detail.py
@@ -92,16 +92,17 @@ def cli(env, identifier, passwords, price):
 
 
 def _bw_table(bw_data):
-    """Generates a bandwidth useage table"""
+    """Generates a bandwidth usage table"""
     table = formatting.Table(['Type', 'In GB', 'Out GB', 'Allotment'])
-    for bw_point in bw_data.get('useage'):
+    for bw_point in bw_data.get('usage'):
         bw_type = 'Private'
         allotment = 'N/A'
         if bw_point['type']['alias'] == 'PUBLIC_SERVER_BW':
             bw_type = 'Public'
-            allotment = utils.lookup(bw_data, 'allotment', 'amount')
-            if allotment is None:
+            if not bw_data.get('allotment'):
                 allotment = '-'
+            else:
+                allotment = utils.lookup(bw_data, 'allotment', 'amount')
 
         table.add_row([bw_type, bw_point['amountIn'], bw_point['amountOut'], allotment])
     return table
diff --git a/SoftLayer/managers/hardware.py b/SoftLayer/managers/hardware.py
index d442d42e..fa4ee42a 100644
--- a/SoftLayer/managers/hardware.py
+++ b/SoftLayer/managers/hardware.py
@@ -697,8 +697,10 @@ def get_bandwidth_allocation(self, instance_id):
         a_mask = "mask[allocation[amount]]"
         allotment = self.client.call('Hardware_Server', 'getBandwidthAllotmentDetail', id=instance_id, mask=a_mask)
         u_mask = "mask[amountIn,amountOut,type]"
-        useage = self.client.call('Hardware_Server', 'getBillingCycleBandwidthUsage', id=instance_id, mask=u_mask)
-        return {'allotment': allotment.get('allocation'), 'useage': useage}
+        usage = self.client.call('Hardware_Server', 'getBillingCycleBandwidthUsage', id=instance_id, mask=u_mask)
+        if allotment:
+            return {'allotment': allotment.get('allocation'), 'usage': usage}
+        return {'allotment': allotment, 'usage': usage}
 
 
 def _get_extra_price_id(items, key_name, hourly, location):


Test patch:
diff --git a/tests/CLI/modules/server_tests.py b/tests/CLI/modules/server_tests.py
index 70d4c316..29ec65d4 100644
--- a/tests/CLI/modules/server_tests.py
+++ b/tests/CLI/modules/server_tests.py
@@ -127,6 +127,17 @@ def test_detail_vs_empty_tag(self):
             ['example-tag'],
         )
 
+    def test_detail_empty_allotment(self):
+        mock = self.set_mock('SoftLayer_Hardware_Server', 'getBandwidthAllotmentDetail')
+        mock.return_value = None
+        result = self.run_command(['server', 'detail', '100'])
+
+        self.assert_no_fail(result)
+        self.assertEqual(
+            json.loads(result.output)['Bandwidth'][0]['Allotment'],
+            '-',
+        )
+
     def test_list_servers(self):
         result = self.run_command(['server', 'list', '--tag=openstack'])
 
diff --git a/tests/managers/hardware_tests.py b/tests/managers/hardware_tests.py
index cf10224a..fbbdb221 100644
--- a/tests/managers/hardware_tests.py
+++ b/tests/managers/hardware_tests.py
@@ -8,14 +8,12 @@
 
 import mock
 
-
 import SoftLayer
 
 from SoftLayer import fixtures
 from SoftLayer import managers
 from SoftLayer import testing
 
-
 MINIMAL_TEST_CREATE_ARGS = {
     'size': 'S1270_8GB_2X1TBSATA_NORAID',
     'hostname': 'unicorn',
@@ -446,11 +444,25 @@ def test_get_bandwidth_allocation(self):
         self.assert_called_with('SoftLayer_Hardware_Server', 'getBandwidthAllotmentDetail', identifier=1234)
         self.assert_called_with('SoftLayer_Hardware_Server', 'getBillingCycleBandwidthUsage', identifier=1234)
         self.assertEqual(result['allotment']['amount'], '250')
-        self.assertEqual(result['useage'][0]['amountIn'], '.448')
+        self.assertEqual(result['usage'][0]['amountIn'], '.448')
+
+    def test_get_bandwidth_allocation_with_allotment(self):
+        mock = self.set_mock('SoftLayer_Hardware_Server', 'getBandwidthAllotmentDetail')
+        mock.return_value = {
+            "allocationId": 11111,
+            "id": 22222,
+            "allocation": {
+                "amount": "2000"
+            }
+        }
+
+        result = self.hardware.get_bandwidth_allocation(1234)
+
+        self.assertEqual(2000, int(result['allotment']['amount']))
 
     def test_get_bandwidth_allocation_no_allotment(self):
         mock = self.set_mock('SoftLayer_Hardware_Server', 'getBandwidthAllotmentDetail')
-        mock.return_value = {}
+        mock.return_value = None
 
         result = self.hardware.get_bandwidth_allocation(1234)