Repository: softlayer/softlayer-python
Pull request #1996 (2023-06-19 16:18:59)

Error on hardware create with Operation system #1638
Hi @allmightyspiff,

Please **review** this **pull** **request**.

**Fixes**: #1638 

**Title:** 
Error on hardware create with Operation system #1638

**Description:**
Previously with reference code for os parameter different error message was displayed, so that issue has been fixed in this pull request.

Please find the screen shot.

**Screen Shot:** 
<img width="1229" alt="Screenshot 2023-06-19 at 11 24 19 PM" src="https://github.com/softlayer/softlayer-python/assets/125332006/bc48c3f4-fb12-4433-8c78-16fb1a9e05e6">

Thanks,
Ramkishor Chaladi.



Patch:
diff --git a/SoftLayer/managers/ordering.py b/SoftLayer/managers/ordering.py
index 9bd25c90..8e6a34d8 100644
--- a/SoftLayer/managers/ordering.py
+++ b/SoftLayer/managers/ordering.py
@@ -10,6 +10,8 @@
 
 from SoftLayer import exceptions
 
+from SoftLayer import utils
+
 CATEGORY_MASK = '''id, isRequired, itemCategory[id, name, categoryCode]'''
 
 ITEM_MASK = '''id, keyName, description, itemCategory, categories, prices'''
@@ -32,6 +34,8 @@ def __init__(self, client):
         self.order_svc = client['Product_Order']
         self.billing_svc = client['Billing_Order']
         self.package_preset = client['Product_Package_Preset']
+        self.package_mask = 'id, description, capacity, itemCategory, keyName, prices[categories], ' \
+            'softwareDescription[id,referenceCode,longDescription]'
 
     def get_packages_of_type(self, package_types, mask=None):
         """Get packages that match a certain type.
@@ -370,7 +374,8 @@ def get_price_id_list(self, package_keyname, item_keynames, core=None):
                   keynames in the given package
 
         """
-        mask = 'id, description, capacity, itemCategory, keyName, prices[categories]'
+        mask = 'id, description, capacity, itemCategory, keyName, prices[categories], ' \
+            'softwareDescription[id,referenceCode,longDescription]'
         items = self.list_items(package_keyname, mask=mask)
         item_capacity = self.get_item_capacity(items, item_keynames)
 
@@ -378,14 +383,18 @@ def get_price_id_list(self, package_keyname, item_keynames, core=None):
         category_dict = {"gpu0": -1, "pcie_slot0": -1}
 
         for item_keyname in item_keynames:
-            try:
-                # Need to find the item in the package that has a matching
-                # keyName with the current item we are searching for
-                matching_item = [i for i in items
-                                 if i['keyName'] == item_keyname][0]
-            except IndexError as ex:
+            matching_item = []
+            # Need to find the item in the package that has a matching
+            # keyName with the current item we are searching for
+            for i in items:
+                reference_code = utils.lookup(i, 'softwareDescription', 'referenceCode')
+                if i['keyName'] == item_keyname or reference_code == item_keyname:
+                    matching_item.append(i)
+
+            if len(matching_item) == 0:
                 message = f"Item {item_keyname} does not exist for package {package_keyname}"
-                raise exceptions.SoftLayerError(message) from ex
+                raise exceptions.SoftLayerError(message)
+            matching_item = matching_item[0]
 
             # we want to get the price ID that has no location attached to it,
             # because that is the most generic price. verifyOrder/placeOrder


Test patch:
diff --git a/tests/managers/ordering_tests.py b/tests/managers/ordering_tests.py
index 9bf11a04..7d86c005 100644
--- a/tests/managers/ordering_tests.py
+++ b/tests/managers/ordering_tests.py
@@ -316,8 +316,7 @@ 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, description, capacity, itemCategory, keyName, '
-                                                                  'prices[categories]')
+        list_mock.assert_called_once_with('PACKAGE_KEYNAME', mask=self.ordering.package_mask)
         self.assertEqual([price1['id'], price2['id']], prices)
 
     def test_get_price_id_list_no_core(self):
@@ -335,22 +334,24 @@ def test_get_price_id_list_no_core(self):
             prices = self.ordering.get_price_id_list('PACKAGE_KEYNAME', ['ITEM1', 'ITEM2'], None)
 
         list_mock.assert_called_once_with('PACKAGE_KEYNAME', mask='id, description, capacity, itemCategory, keyName, '
-                                                                  'prices[categories]')
+                                          'prices[categories], softwareDescription[id,referenceCode,longDescription]')
         self.assertEqual([price1['id'], price2['id']], prices)
 
     def test_get_price_id_list_item_not_found(self):
         category1 = {'categoryCode': 'cat1'}
         price1 = {'id': 1234, 'locationGroupId': '', 'categories': [category1]}
-        item1 = {'id': 1111, 'keyName': 'ITEM1', 'itemCategory': category1, 'prices': [price1]}
+        softwareDescription1 = {'id': 1234, 'longDescription': 'ABCD 1.2-34', 'referenceCode': 'ABCD_9_32'}
+        item1 = {'id': 1111, 'keyName': 'ITEM1', 'itemCategory': category1,
+                 'prices': [price1], 'softwareDescription': softwareDescription1, }
 
         with mock.patch.object(self.ordering, 'list_items') as list_mock:
             list_mock.return_value = [item1]
 
             exc = self.assertRaises(exceptions.SoftLayerError,
                                     self.ordering.get_price_id_list,
-                                    'PACKAGE_KEYNAME', ['ITEM2'], "8")
+                                    'PACKAGE_KEYNAME', ['ITEM2'], "12")
         list_mock.assert_called_once_with('PACKAGE_KEYNAME', mask='id, description, capacity, itemCategory, keyName, '
-                                                                  'prices[categories]')
+                                          'prices[categories], softwareDescription[id,referenceCode,longDescription]')
         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):
@@ -365,7 +366,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, description, capacity, itemCategory, '
-                                                                      'keyName, ' 'prices[categories]')
+                                              'keyName, prices[categories], '
+                                              'softwareDescription[id,referenceCode,longDescription]')
             self.assertEqual([price2['id'], price1['id']], prices)
 
     def test_generate_no_complex_type(self):
@@ -620,7 +622,7 @@ 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, description, capacity, itemCategory, keyName, '
-                                                                  'prices[categories]')
+                                          'prices[categories], softwareDescription[id,referenceCode,longDescription]')
         self.assertEqual([price1['id'], price2['id']], prices)
 
     def test_location_groud_id_empty(self):
@@ -637,8 +639,9 @@ 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, description, capacity, itemCategory, keyName, '
-                                                                  'prices[categories]')
+        list_mock.assert_called_once_with('PACKAGE_KEYNAME', mask='id, description, capacity, itemCategory, '
+                                          'keyName, prices[categories], '
+                                          'softwareDescription[id,referenceCode,longDescription]')
         self.assertEqual([price1['id'], price2['id']], prices)
 
     def test_get_item_price_id_without_capacity_restriction(self):