Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed suspend cloud server order. #1054

Merged
merged 6 commits into from
Oct 11, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
31 changes: 27 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):
FernandoOjeda marked this conversation as resolved.
Show resolved Hide resolved
"""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,10 @@ 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 = None
FernandoOjeda marked this conversation as resolved.
Show resolved Hide resolved
for price in matching_item['prices']:
if not price['locationGroupId']:
price_id = self.get_item_price_id(core, price, price_id)
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 +373,21 @@ def get_price_id_list(self, package_keyname, item_keynames):

return prices

@staticmethod
def get_item_price_id(core, price, price_id):
"""get item price id"""
category_code = []
FernandoOjeda marked this conversation as resolved.
Show resolved Hide resolved
allmightyspiff marked this conversation as resolved.
Show resolved Hide resolved
if 'capacityRestrictionMinimum' not in price:
if price['categories'][0]['categoryCode'] not in category_code:
category_code.append(price['categories'][0]['categoryCode'])
price_id = price['id']
elif int(price['capacityRestrictionMinimum']) <= int(core) <= int(
price['capacityRestrictionMaximum']):
if price['categories'][0]['categoryCode'] not in category_code:
category_code.append(price['categories'][0]['categoryCode'])
price_id = price['id']
return price_id

def get_preset_prices(self, preset):
"""Get preset item prices.

Expand Down Expand Up @@ -534,15 +552,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
34 changes: 26 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,24 @@ 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'}
price1 = {'id': 1234, 'locationGroupId': '', 'categories': [category1]}

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

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]}

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

self.assertEqual(1234, price_id)