Repository: softlayer/softlayer-python
Pull request #810 (2017-03-30 21:43:18)

Add getting package by key for use in hardware manager
This addresses the issue I opened, #809.


Patch:
diff --git a/SoftLayer/managers/hardware.py b/SoftLayer/managers/hardware.py
index 58cec024..e137ee6f 100644
--- a/SoftLayer/managers/hardware.py
+++ b/SoftLayer/managers/hardware.py
@@ -411,13 +411,13 @@ def _get_package(self):
 regions[location[location[priceGroups]]]
 '''
 
-        package_type = 'BARE_METAL_CPU_FAST_PROVISION'
-        packages = self.ordering_manager.get_packages_of_type([package_type],
-                                                              mask=mask)
-        if len(packages) != 1:
+        package_keyname = 'BARE_METAL_SERVER'
+        package = self.ordering_manager.get_package_by_key(package_keyname,
+                                                           mask=mask)
+        if package is None:
             raise SoftLayer.SoftLayerError("Ordering package not found")
 
-        return packages[0]
+        return package
 
     def _generate_create_dict(self,
                               size=None,
diff --git a/SoftLayer/managers/ordering.py b/SoftLayer/managers/ordering.py
index 798a8464..acaaa390 100644
--- a/SoftLayer/managers/ordering.py
+++ b/SoftLayer/managers/ordering.py
@@ -199,3 +199,25 @@ def order_quote(self, quote_id, extra, quantity=1):
         container = self.generate_order_template(quote_id, extra,
                                                  quantity=quantity)
         return self.client['Product_Order'].placeOrder(container)
+
+    def get_package_by_key(self, package_keyname, mask=None):
+        """Get a single package with a given key.
+
+        If no packages are found, returns None
+
+        :param package_keyname string representing the package key name
+                            we are interested in.
+        :param string mask: Mask to specify the properties we want to retrieve
+        """
+        package_service = self.client['Product_Package']
+        _filter = {
+            'keyName': {
+                'operation': package_keyname,
+            },
+        }
+
+        packages = package_service.getAllObjects(mask=mask, filter=_filter)
+        if len(packages) == 0:
+            return None
+        else:
+            return packages.pop()
diff --git a/SoftLayer/managers/vs.py b/SoftLayer/managers/vs.py
index 9d0741c0..467bd4ce 100644
--- a/SoftLayer/managers/vs.py
+++ b/SoftLayer/managers/vs.py
@@ -823,11 +823,14 @@ def _get_package_items(self):
         ]
         mask = "mask[%s]" % ','.join(mask)
 
-        package_type = "VIRTUAL_SERVER_INSTANCE"
-        package_id = self.ordering_manager.get_package_id_by_type(package_type)
-        package_service = self.client['Product_Package']
+        package_keyname = "CLOUD_SERVER"
+        package = self.ordering_manager.get_package_by_key(package_keyname)
+
+        if package is None:
+            raise ValueError("No package found for key: " + package_keyname)
 
-        return package_service.getItems(id=package_id, mask=mask)
+        package_service = self.client['Product_Package']
+        return package_service.getItems(id=package['id'], mask=mask)
 
     def _get_price_id_for_upgrade(self, package_items, option, value,
                                   public=True):


Test patch:
diff --git a/tests/managers/ordering_tests.py b/tests/managers/ordering_tests.py
index c1d2d29d..0119b16b 100644
--- a/tests/managers/ordering_tests.py
+++ b/tests/managers/ordering_tests.py
@@ -123,3 +123,17 @@ def test_generate_order_template_extra_quantity(self):
         self.assertRaises(ValueError,
                           self.ordering.generate_order_template,
                           1234, [], quantity=1)
+
+    def test_get_package_by_key_returns_if_found(self):
+        package_keyname = "BARE_METAL_SERVER"
+        mask = "mask[id, name]"
+        package = self.ordering.get_package_by_key(package_keyname, mask)
+        self.assertIsNotNone(package)
+
+    def test_get_package_by_key_returns_none_if_not_found(self):
+        mock = self.set_mock('SoftLayer_Product_Package', 'getAllObjects')
+        mock.return_value = []
+
+        package = self.ordering.get_package_by_key("WILLY_NILLY_SERVERS")
+
+        self.assertIsNone(package)