Repository: softlayer/softlayer-python
Pull request #1213 (2020-01-23 18:08:03)

Fix vs detail.


Patch:
diff --git a/SoftLayer/CLI/virt/detail.py b/SoftLayer/CLI/virt/detail.py
index bf93a834..131117df 100644
--- a/SoftLayer/CLI/virt/detail.py
+++ b/SoftLayer/CLI/virt/detail.py
@@ -126,14 +126,15 @@ def cli(env, identifier, passwords=False, price=False):
 def _bw_table(bw_data):
     """Generates a bandwidth useage 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/vs.py b/SoftLayer/managers/vs.py
index 24a264b7..b1391bc3 100644
--- a/SoftLayer/managers/vs.py
+++ b/SoftLayer/managers/vs.py
@@ -1074,8 +1074,10 @@ def get_bandwidth_allocation(self, instance_id):
         a_mask = "mask[allocation[amount]]"
         allotment = self.client.call('Virtual_Guest', 'getBandwidthAllotmentDetail', id=instance_id, mask=a_mask)
         u_mask = "mask[amountIn,amountOut,type]"
-        useage = self.client.call('Virtual_Guest', 'getBillingCycleBandwidthUsage', id=instance_id, mask=u_mask)
-        return {'allotment': allotment.get('allocation'), 'useage': useage}
+        usage = self.client.call('Virtual_Guest', 'getBillingCycleBandwidthUsage', id=instance_id, mask=u_mask)
+        if allotment:
+            return {'allotment': allotment.get('allocation'), 'usage': usage}
+        return {'allotment': allotment, 'usage': usage}
 
     # pylint: disable=inconsistent-return-statements
     def _get_price_id_for_upgrade(self, package_items, option, value, public=True):


Test patch:
diff --git a/tests/CLI/modules/vs/vs_tests.py b/tests/CLI/modules/vs/vs_tests.py
index 3e57cb20..2c79f42b 100644
--- a/tests/CLI/modules/vs/vs_tests.py
+++ b/tests/CLI/modules/vs/vs_tests.py
@@ -205,6 +205,17 @@ def test_detail_vs_empty_tag(self):
             ['example-tag'],
         )
 
+    def test_detail_vs_empty_allotment(self):
+        mock = self.set_mock('SoftLayer_Virtual_Guest', 'getBandwidthAllotmentDetail')
+        mock.return_value = None
+        result = self.run_command(['vs', 'detail', '100'])
+
+        self.assert_no_fail(result)
+        self.assertEqual(
+            json.loads(result.output)['Bandwidth'][0]['Allotment'],
+            '-',
+        )
+
     def test_detail_vs_dedicated_host_not_found(self):
         ex = SoftLayerAPIError('SoftLayer_Exception', 'Not found')
         mock = self.set_mock('SoftLayer_Virtual_DedicatedHost', 'getObject')
diff --git a/tests/managers/vs/vs_tests.py b/tests/managers/vs/vs_tests.py
index 14d41966..47fcaf20 100644
--- a/tests/managers/vs/vs_tests.py
+++ b/tests/managers/vs/vs_tests.py
@@ -902,12 +902,26 @@ def test_get_bandwidth_allocation(self):
         self.assert_called_with('SoftLayer_Virtual_Guest', 'getBandwidthAllotmentDetail', identifier=1234)
         self.assert_called_with('SoftLayer_Virtual_Guest', '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_no_allotment(self):
         mock = self.set_mock('SoftLayer_Virtual_Guest', 'getBandwidthAllotmentDetail')
-        mock.return_value = {}
+        mock.return_value = None
 
         result = self.vs.get_bandwidth_allocation(1234)
 
         self.assertEqual(None, result['allotment'])
+
+    def test_get_bandwidth_allocation_with_allotment(self):
+        mock = self.set_mock('SoftLayer_Virtual_Guest', 'getBandwidthAllotmentDetail')
+        mock.return_value = {
+            "allocationId": 11111,
+            "id": 22222,
+            "allocation": {
+                "amount": "2000"
+            }
+        }
+
+        result = self.vs.get_bandwidth_allocation(1234)
+
+        self.assertEqual(2000, int(result['allotment']['amount']))