Skip to content

Commit

Permalink
Merge pull request #1054 from FernandoOjeda/fo_suspend_cloud_server
Browse files Browse the repository at this point in the history
Fixed ordering to use correct priceId when an item has a capacity restriction (like windows server).
  • Loading branch information
allmightyspiff committed Oct 11, 2018
2 parents d63f92a + d989dfd commit 3a50f46
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 12 deletions.
5 changes: 5 additions & 0 deletions SoftLayer/fixtures/SoftLayer_Product_Package.py
Original file line number Diff line number Diff line change
Expand Up @@ -1346,6 +1346,11 @@
"hourlyRecurringFee": ".093",
"id": 204015,
"recurringFee": "62",
"categories": [
{
"categoryCode": "guest_core"
}
],
"item": {
"description": "4 x 2.0 GHz or higher Cores",
"id": 859,
Expand Down
1 change: 1 addition & 0 deletions SoftLayer/fixtures/SoftLayer_Product_Package_Preset.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"id": 209595,
"recurringFee": "118.26",
"item": {
"capacity": 8,
"description": "8 x 2.0 GHz or higher Cores",
"id": 11307,
"keyName": "GUEST_CORE_8",
Expand Down
27 changes: 23 additions & 4 deletions SoftLayer/managers/ordering.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ def get_preset_by_key(self, package_keyname, preset_keyname, mask=None):

return presets[0]

def get_price_id_list(self, package_keyname, item_keynames):
def get_price_id_list(self, package_keyname, item_keynames, core=None):
"""Converts a list of item keynames to a list of price IDs.
This function is used to convert a list of item keynames into
Expand All @@ -331,6 +331,7 @@ def get_price_id_list(self, package_keyname, item_keynames):
:param str package_keyname: The package associated with the prices
:param list item_keynames: A list of item keyname strings
:param str core: preset guest core capacity.
:returns: A list of price IDs associated with the given item
keynames in the given package
Expand All @@ -356,8 +357,7 @@ def get_price_id_list(self, package_keyname, item_keynames):
# can take that ID and create the proper price for us in the location
# in which the order is made
if matching_item['itemCategory']['categoryCode'] != "gpu0":
price_id = [p['id'] for p in matching_item['prices']
if not p['locationGroupId']][0]
price_id = self.get_item_price_id(core, matching_item['prices'])
else:
# GPU items has two generic prices and they are added to the list
# according to the number of gpu items added in the order.
Expand All @@ -370,6 +370,20 @@ def get_price_id_list(self, package_keyname, item_keynames):

return prices

@staticmethod
def get_item_price_id(core, prices):
"""get item price id"""
price_id = None
for price in prices:
if not price['locationGroupId']:
capacity_min = int(price.get('capacityRestrictionMinimum', -1))
capacity_max = int(price.get('capacityRestrictionMaximum', -1))
if capacity_min == -1:
price_id = price['id']
elif capacity_min <= int(core) <= capacity_max:
price_id = price['id']
return price_id

def get_preset_prices(self, preset):
"""Get preset item prices.
Expand Down Expand Up @@ -534,15 +548,20 @@ def generate_order(self, package_keyname, location, item_keynames, complex_type=
order['quantity'] = quantity
order['useHourlyPricing'] = hourly

preset_core = None
if preset_keyname:
preset_id = self.get_preset_by_key(package_keyname, preset_keyname)['id']
preset_items = self.get_preset_prices(preset_id)
for item in preset_items['prices']:
if item['item']['itemCategory']['categoryCode'] == "guest_core":
preset_core = item['item']['capacity']
order['presetId'] = preset_id

if not complex_type:
raise exceptions.SoftLayerError("A complex type must be specified with the order")
order['complexType'] = complex_type

price_ids = self.get_price_id_list(package_keyname, item_keynames)
price_ids = self.get_price_id_list(package_keyname, item_keynames, preset_core)
order['prices'] = [{'id': price_id} for price_id in price_ids]

container['orderContainers'] = [order]
Expand Down
38 changes: 30 additions & 8 deletions tests/managers/ordering_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,8 @@ def test_get_preset_by_key_preset_not_found(self):

def test_get_price_id_list(self):
category1 = {'categoryCode': 'cat1'}
price1 = {'id': 1234, 'locationGroupId': None, 'itemCategory': [category1]}
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]}
Expand All @@ -305,7 +306,7 @@ def test_get_price_id_list(self):
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'])
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]')
self.assertEqual([price1['id'], price2['id']], prices)
Expand All @@ -320,7 +321,7 @@ def test_get_price_id_list_item_not_found(self):

exc = self.assertRaises(exceptions.SoftLayerError,
self.ordering.get_price_id_list,
'PACKAGE_KEYNAME', ['ITEM2'])
'PACKAGE_KEYNAME', ['ITEM2'], "8")
list_mock.assert_called_once_with('PACKAGE_KEYNAME', mask='id, itemCategory, keyName, prices[categories]')
self.assertEqual("Item ITEM2 does not exist for package PACKAGE_KEYNAME", str(exc))

Expand All @@ -333,7 +334,7 @@ def test_get_price_id_list_gpu_items_with_two_categories(self):
with mock.patch.object(self.ordering, 'list_items') as list_mock:
list_mock.return_value = [item1, item1]

prices = self.ordering.get_price_id_list('PACKAGE_KEYNAME', ['ITEM1', 'ITEM1'])
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]')
self.assertEqual([price2['id'], price1['id']], prices)
Expand Down Expand Up @@ -366,7 +367,7 @@ def test_generate_order_with_preset(self):

mock_pkg.assert_called_once_with(pkg, mask='id')
mock_preset.assert_called_once_with(pkg, preset)
mock_get_ids.assert_called_once_with(pkg, items)
mock_get_ids.assert_called_once_with(pkg, items, 8)
self.assertEqual(expected_order, order)

def test_generate_order(self):
Expand All @@ -388,7 +389,7 @@ def test_generate_order(self):

mock_pkg.assert_called_once_with(pkg, mask='id')
mock_preset.assert_not_called()
mock_get_ids.assert_called_once_with(pkg, items)
mock_get_ids.assert_called_once_with(pkg, items, None)
self.assertEqual(expected_order, order)

def test_verify_order(self):
Expand Down Expand Up @@ -526,7 +527,7 @@ def test_location_group_id_none(self):
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'])
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]')
self.assertEqual([price1['id'], price2['id']], prices)
Expand All @@ -543,7 +544,28 @@ def test_location_groud_id_empty(self):
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'])
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]')
self.assertEqual([price1['id'], price2['id']], prices)

def test_get_item_price_id_without_capacity_restriction(self):
category1 = {'categoryCode': 'cat1'}
category2 = {'categoryCode': 'cat2'}
prices = [{'id': 1234, 'locationGroupId': '', 'categories': [category1]},
{'id': 2222, 'locationGroupId': 509, 'categories': [category2]}]

price_id = self.ordering.get_item_price_id("8", prices)

self.assertEqual(1234, price_id)

def test_get_item_price_id_with_capacity_restriction(self):
category1 = {'categoryCode': 'cat1'}
price1 = [{'id': 1234, 'locationGroupId': '', "capacityRestrictionMaximum": "16",
"capacityRestrictionMinimum": "1", 'categories': [category1]},
{'id': 2222, 'locationGroupId': '', "capacityRestrictionMaximum": "56",
"capacityRestrictionMinimum": "36", 'categories': [category1]}]

price_id = self.ordering.get_item_price_id("8", price1)

self.assertEqual(1234, price_id)

0 comments on commit 3a50f46

Please sign in to comment.