Repository: softlayer/softlayer-python
Pull request #1193 (2019-12-03 21:38:11)

Fix order vs dedicated.
Fix order vs dedicated https://github.com/softlayer/softlayer-python/issues/1191.





Patch:
diff --git a/SoftLayer/managers/ordering.py b/SoftLayer/managers/ordering.py
index e2037891..0cd015e7 100644
--- a/SoftLayer/managers/ordering.py
+++ b/SoftLayer/managers/ordering.py
@@ -351,8 +351,9 @@ def get_price_id_list(self, package_keyname, item_keynames, core=None):
                   keynames in the given package
 
         """
-        mask = 'id, itemCategory, keyName, prices[categories]'
+        mask = 'id, capacity, itemCategory, keyName, prices[categories]'
         items = self.list_items(package_keyname, mask=mask)
+        item_capacity = self.get_item_capacity(items, item_keynames)
 
         prices = []
         category_dict = {"gpu0": -1, "pcie_slot0": -1}
@@ -374,7 +375,10 @@ def get_price_id_list(self, package_keyname, item_keynames, core=None):
             # in which the order is made
             item_category = matching_item['itemCategory']['categoryCode']
             if item_category not in category_dict:
-                price_id = self.get_item_price_id(core, matching_item['prices'])
+                if core is None:
+                    price_id = self.get_item_price_id(item_capacity, matching_item['prices'])
+                else:
+                    price_id = self.get_item_price_id(core, matching_item['prices'])
             else:
                 # GPU and PCIe items has two generic prices and they are added to the list
                 # according to the number of items in the order.
@@ -404,6 +408,17 @@ def get_item_price_id(core, prices):
                     price_id = price['id']
         return price_id
 
+    def get_item_capacity(self, items, item_keynames):
+        """Get item capacity."""
+        item_capacity = None
+        for item_keyname in item_keynames:
+            for item in items:
+                if item['keyName'] == item_keyname:
+                    if "GUEST_CORE" in item["keyName"]:
+                        item_capacity = item['capacity']
+                        break
+        return item_capacity
+
     def get_preset_prices(self, preset):
         """Get preset item prices.
 


Test patch:
diff --git a/tests/managers/ordering_tests.py b/tests/managers/ordering_tests.py
index 40f9f5db..a76289ca 100644
--- a/tests/managers/ordering_tests.py
+++ b/tests/managers/ordering_tests.py
@@ -309,7 +309,26 @@ def test_get_price_id_list(self):
 
             prices = self.ordering.get_price_id_list('PACKAGE_KEYNAME', ['ITEM1', 'ITEM2'], "8")
 
-        list_mock.assert_called_once_with('PACKAGE_KEYNAME', mask='id, itemCategory, keyName, prices[categories]')
+        list_mock.assert_called_once_with('PACKAGE_KEYNAME', mask='id, capacity, itemCategory, keyName, '
+                                                                  'prices[categories]')
+        self.assertEqual([price1['id'], price2['id']], prices)
+
+    def test_get_price_id_list_no_core(self):
+        category1 = {'categoryCode': 'cat1'}
+        price1 = {'id': 1234, 'locationGroupId': None, 'categories': [{"categoryCode": "guest_core"}],
+                  'itemCategory': [category1]}
+        item1 = {'id': 1111, 'keyName': 'ITEM1', 'itemCategory': category1, 'prices': [price1]}
+        category2 = {'categoryCode': 'cat2'}
+        price2 = {'id': 5678, 'locationGroupId': None, 'categories': [category2]}
+        item2 = {'id': 2222, 'keyName': 'ITEM2', 'itemCategory': category2, 'prices': [price2]}
+
+        with mock.patch.object(self.ordering, 'list_items') as list_mock:
+            list_mock.return_value = [item1, item2]
+
+            prices = self.ordering.get_price_id_list('PACKAGE_KEYNAME', ['ITEM1', 'ITEM2'], None)
+
+        list_mock.assert_called_once_with('PACKAGE_KEYNAME', mask='id, capacity, itemCategory, keyName, '
+                                                                  'prices[categories]')
         self.assertEqual([price1['id'], price2['id']], prices)
 
     def test_get_price_id_list_item_not_found(self):
@@ -323,7 +342,8 @@ def test_get_price_id_list_item_not_found(self):
             exc = self.assertRaises(exceptions.SoftLayerError,
                                     self.ordering.get_price_id_list,
                                     'PACKAGE_KEYNAME', ['ITEM2'], "8")
-        list_mock.assert_called_once_with('PACKAGE_KEYNAME', mask='id, itemCategory, keyName, prices[categories]')
+        list_mock.assert_called_once_with('PACKAGE_KEYNAME', mask='id, capacity, itemCategory, keyName, '
+                                                                  'prices[categories]')
         self.assertEqual("Item ITEM2 does not exist for package PACKAGE_KEYNAME", str(exc))
 
     def test_get_price_id_list_gpu_items_with_two_categories(self):
@@ -337,7 +357,8 @@ def test_get_price_id_list_gpu_items_with_two_categories(self):
 
             prices = self.ordering.get_price_id_list('PACKAGE_KEYNAME', ['ITEM1', 'ITEM1'], "8")
 
-            list_mock.assert_called_once_with('PACKAGE_KEYNAME', mask='id, itemCategory, keyName, prices[categories]')
+            list_mock.assert_called_once_with('PACKAGE_KEYNAME', mask='id, capacity, itemCategory, keyName, '
+                                                                      'prices[categories]')
             self.assertEqual([price2['id'], price1['id']], prices)
 
     def test_generate_no_complex_type(self):
@@ -587,7 +608,8 @@ def test_location_group_id_none(self):
 
             prices = self.ordering.get_price_id_list('PACKAGE_KEYNAME', ['ITEM1', 'ITEM2'], "8")
 
-        list_mock.assert_called_once_with('PACKAGE_KEYNAME', mask='id, itemCategory, keyName, prices[categories]')
+        list_mock.assert_called_once_with('PACKAGE_KEYNAME', mask='id, capacity, itemCategory, keyName, '
+                                                                  'prices[categories]')
         self.assertEqual([price1['id'], price2['id']], prices)
 
     def test_location_groud_id_empty(self):
@@ -604,7 +626,8 @@ def test_location_groud_id_empty(self):
 
             prices = self.ordering.get_price_id_list('PACKAGE_KEYNAME', ['ITEM1', 'ITEM2'], "8")
 
-        list_mock.assert_called_once_with('PACKAGE_KEYNAME', mask='id, itemCategory, keyName, prices[categories]')
+        list_mock.assert_called_once_with('PACKAGE_KEYNAME', mask='id, capacity, itemCategory, keyName, '
+                                                                  'prices[categories]')
         self.assertEqual([price1['id'], price2['id']], prices)
 
     def test_get_item_price_id_without_capacity_restriction(self):
@@ -672,3 +695,20 @@ def test_clean_quote_verify(self):
         order_container = call.args[0]
         self.assertNotIn('testProperty', order_container)
         self.assertNotIn('reservedCapacityId', order_container)
+
+    def test_get_item_capacity(self):
+
+        items = [{
+            "capacity": "1",
+            "id": 6131,
+            "keyName": "OS_RHEL_7_X_LAMP_64_BIT",
+        },
+            {
+                "capacity": "1",
+                "id": 10201,
+                "keyName": "GUEST_CORE_1_DEDICATED",
+            }]
+
+        item_capacity = self.ordering.get_item_capacity(items, ['GUEST_CORE_1_DEDICATED', 'OS_RHEL_7_X_LAMP_64_BIT'])
+
+        self.assertEqual(1, int(item_capacity))