Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
16 changes: 13 additions & 3 deletions SoftLayer/managers/ordering.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,10 +334,11 @@ def get_price_id_list(self, package_keyname, item_keynames):
keynames in the given package

"""
mask = 'id, keyName, prices'
mask = 'id, itemCategory, keyName, prices[categories]'
items = self.list_items(package_keyname, mask=mask)

prices = []
gpu_number = -1
for item_keyname in item_keynames:
try:
# Need to find the item in the package that has a matching
Expand All @@ -353,8 +354,17 @@ def get_price_id_list(self, package_keyname, item_keynames):
# because that is the most generic price. verifyOrder/placeOrder
# can take that ID and create the proper price for us in the location
# in which the order is made
price_id = [p['id'] for p in matching_item['prices']
if not p['locationGroupId']][0]
if matching_item['itemCategory']['categoryCode'] != "gpu0":
price_id = [p['id'] for p in matching_item['prices']
if not p['locationGroupId']][0]
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.
gpu_number += 1
price_id = [p['id'] for p in matching_item['prices']
if not p['locationGroupId']
and p['categories'][0]['categoryCode'] == "gpu" + str(gpu_number)][0]

prices.append(price_id)

return prices
Expand Down
8 changes: 6 additions & 2 deletions tests/CLI/modules/order_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,13 @@ def test_location_list(self):

def _get_order_items(self):
item1 = {'keyName': 'ITEM1', 'description': 'description1',
'prices': [{'id': 1111, 'locationGroupId': None}]}
'itemCategory': {'categoryCode': 'cat1'},
'prices': [{'id': 1111, 'locationGroupId': None,
'categories': [{'categoryCode': 'cat1'}]}]}
item2 = {'keyName': 'ITEM2', 'description': 'description2',
'prices': [{'id': 2222, 'locationGroupId': None}]}
'itemCategory': {'categoryCode': 'cat2'},
'prices': [{'id': 2222, 'locationGroupId': None,
'categories': [{'categoryCode': 'cat2'}]}]}

return [item1, item2]

Expand Down
57 changes: 39 additions & 18 deletions tests/managers/ordering_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,32 +283,49 @@ def test_get_preset_by_key_preset_not_found(self):
self.assertEqual('Preset {} does not exist in package {}'.format(keyname, 'PACKAGE_KEYNAME'), str(exc))

def test_get_price_id_list(self):
price1 = {'id': 1234, 'locationGroupId': None}
item1 = {'id': 1111, 'keyName': 'ITEM1', 'prices': [price1]}
price2 = {'id': 5678, 'locationGroupId': None}
item2 = {'id': 2222, 'keyName': 'ITEM2', 'prices': [price2]}
category1 = {'categoryCode': 'cat1'}
price1 = {'id': 1234, 'locationGroupId': None, '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'])

list_mock.assert_called_once_with('PACKAGE_KEYNAME', mask='id, keyName, prices')
list_mock.assert_called_once_with('PACKAGE_KEYNAME', mask='id, itemCategory, keyName, prices[categories]')
self.assertEqual([price1['id'], price2['id']], prices)

def test_get_price_id_list_item_not_found(self):
price1 = {'id': 1234, 'locationGroupId': ''}
item1 = {'id': 1111, 'keyName': 'ITEM1', 'prices': [price1]}
category1 = {'categoryCode': 'cat1'}
price1 = {'id': 1234, 'locationGroupId': '', 'categories': [category1]}
item1 = {'id': 1111, 'keyName': 'ITEM1', 'itemCategory': category1, 'prices': [price1]}

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'])
list_mock.assert_called_once_with('PACKAGE_KEYNAME', mask='id, keyName, prices')
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))

def test_get_price_id_list_gpu_items_with_two_categories(self):
# Specific for GPU prices which are differentiated by their category (gpu0, gpu1)
price1 = {'id': 1234, 'locationGroupId': None, 'categories': [{'categoryCode': 'gpu1'}]}
price2 = {'id': 5678, 'locationGroupId': None, 'categories': [{'categoryCode': 'gpu0'}]}
item1 = {'id': 1111, 'keyName': 'ITEM1', 'itemCategory': {'categoryCode': 'gpu0'}, 'prices': [price1, price2]}

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'])

list_mock.assert_called_once_with('PACKAGE_KEYNAME', mask='id, itemCategory, keyName, prices[categories]')
self.assertEqual([price2['id'], price1['id']], prices)

def test_generate_no_complex_type(self):
pkg = 'PACKAGE_KEYNAME'
items = ['ITEM1', 'ITEM2']
Expand Down Expand Up @@ -460,30 +477,34 @@ def test_get_location_id_int(self):

def test_location_group_id_none(self):
# RestTransport uses None for empty locationGroupId
price1 = {'id': 1234, 'locationGroupId': None}
item1 = {'id': 1111, 'keyName': 'ITEM1', 'prices': [price1]}
price2 = {'id': 5678, 'locationGroupId': None}
item2 = {'id': 2222, 'keyName': 'ITEM2', 'prices': [price2]}
category1 = {'categoryCode': 'cat1'}
price1 = {'id': 1234, 'locationGroupId': None, 'categories': [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'])

list_mock.assert_called_once_with('PACKAGE_KEYNAME', mask='id, keyName, prices')
list_mock.assert_called_once_with('PACKAGE_KEYNAME', mask='id, itemCategory, keyName, prices[categories]')
self.assertEqual([price1['id'], price2['id']], prices)

def test_location_groud_id_empty(self):
# XMLRPCtransport uses '' for empty locationGroupId
price1 = {'id': 1234, 'locationGroupId': ''}
item1 = {'id': 1111, 'keyName': 'ITEM1', 'prices': [price1]}
price2 = {'id': 5678, 'locationGroupId': ""}
item2 = {'id': 2222, 'keyName': 'ITEM2', 'prices': [price2]}
category1 = {'categoryCode': 'cat1'}
price1 = {'id': 1234, 'locationGroupId': '', 'categories': [category1]}
item1 = {'id': 1111, 'keyName': 'ITEM1', 'itemCategory': category1, 'prices': [price1]}
category2 = {'categoryCode': 'cat2'}
price2 = {'id': 5678, 'locationGroupId': "", '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'])

list_mock.assert_called_once_with('PACKAGE_KEYNAME', mask='id, keyName, prices')
list_mock.assert_called_once_with('PACKAGE_KEYNAME', mask='id, itemCategory, keyName, prices[categories]')
self.assertEqual([price1['id'], price2['id']], prices)