Skip to content

Commit 3a50f46

Browse files
Merge pull request #1054 from FernandoOjeda/fo_suspend_cloud_server
Fixed ordering to use correct priceId when an item has a capacity restriction (like windows server).
2 parents d63f92a + d989dfd commit 3a50f46

File tree

4 files changed

+59
-12
lines changed

4 files changed

+59
-12
lines changed

SoftLayer/fixtures/SoftLayer_Product_Package.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,6 +1346,11 @@
13461346
"hourlyRecurringFee": ".093",
13471347
"id": 204015,
13481348
"recurringFee": "62",
1349+
"categories": [
1350+
{
1351+
"categoryCode": "guest_core"
1352+
}
1353+
],
13491354
"item": {
13501355
"description": "4 x 2.0 GHz or higher Cores",
13511356
"id": 859,

SoftLayer/fixtures/SoftLayer_Product_Package_Preset.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"id": 209595,
5050
"recurringFee": "118.26",
5151
"item": {
52+
"capacity": 8,
5253
"description": "8 x 2.0 GHz or higher Cores",
5354
"id": 11307,
5455
"keyName": "GUEST_CORE_8",

SoftLayer/managers/ordering.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ def get_preset_by_key(self, package_keyname, preset_keyname, mask=None):
322322

323323
return presets[0]
324324

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

371371
return prices
372372

373+
@staticmethod
374+
def get_item_price_id(core, prices):
375+
"""get item price id"""
376+
price_id = None
377+
for price in prices:
378+
if not price['locationGroupId']:
379+
capacity_min = int(price.get('capacityRestrictionMinimum', -1))
380+
capacity_max = int(price.get('capacityRestrictionMaximum', -1))
381+
if capacity_min == -1:
382+
price_id = price['id']
383+
elif capacity_min <= int(core) <= capacity_max:
384+
price_id = price['id']
385+
return price_id
386+
373387
def get_preset_prices(self, preset):
374388
"""Get preset item prices.
375389
@@ -534,15 +548,20 @@ def generate_order(self, package_keyname, location, item_keynames, complex_type=
534548
order['quantity'] = quantity
535549
order['useHourlyPricing'] = hourly
536550

551+
preset_core = None
537552
if preset_keyname:
538553
preset_id = self.get_preset_by_key(package_keyname, preset_keyname)['id']
554+
preset_items = self.get_preset_prices(preset_id)
555+
for item in preset_items['prices']:
556+
if item['item']['itemCategory']['categoryCode'] == "guest_core":
557+
preset_core = item['item']['capacity']
539558
order['presetId'] = preset_id
540559

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

545-
price_ids = self.get_price_id_list(package_keyname, item_keynames)
564+
price_ids = self.get_price_id_list(package_keyname, item_keynames, preset_core)
546565
order['prices'] = [{'id': price_id} for price_id in price_ids]
547566

548567
container['orderContainers'] = [order]

tests/managers/ordering_tests.py

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,8 @@ def test_get_preset_by_key_preset_not_found(self):
296296

297297
def test_get_price_id_list(self):
298298
category1 = {'categoryCode': 'cat1'}
299-
price1 = {'id': 1234, 'locationGroupId': None, 'itemCategory': [category1]}
299+
price1 = {'id': 1234, 'locationGroupId': None, 'categories': [{"categoryCode": "guest_core"}],
300+
'itemCategory': [category1]}
300301
item1 = {'id': 1111, 'keyName': 'ITEM1', 'itemCategory': category1, 'prices': [price1]}
301302
category2 = {'categoryCode': 'cat2'}
302303
price2 = {'id': 5678, 'locationGroupId': None, 'categories': [category2]}
@@ -305,7 +306,7 @@ def test_get_price_id_list(self):
305306
with mock.patch.object(self.ordering, 'list_items') as list_mock:
306307
list_mock.return_value = [item1, item2]
307308

308-
prices = self.ordering.get_price_id_list('PACKAGE_KEYNAME', ['ITEM1', 'ITEM2'])
309+
prices = self.ordering.get_price_id_list('PACKAGE_KEYNAME', ['ITEM1', 'ITEM2'], "8")
309310

310311
list_mock.assert_called_once_with('PACKAGE_KEYNAME', mask='id, itemCategory, keyName, prices[categories]')
311312
self.assertEqual([price1['id'], price2['id']], prices)
@@ -320,7 +321,7 @@ def test_get_price_id_list_item_not_found(self):
320321

321322
exc = self.assertRaises(exceptions.SoftLayerError,
322323
self.ordering.get_price_id_list,
323-
'PACKAGE_KEYNAME', ['ITEM2'])
324+
'PACKAGE_KEYNAME', ['ITEM2'], "8")
324325
list_mock.assert_called_once_with('PACKAGE_KEYNAME', mask='id, itemCategory, keyName, prices[categories]')
325326
self.assertEqual("Item ITEM2 does not exist for package PACKAGE_KEYNAME", str(exc))
326327

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

336-
prices = self.ordering.get_price_id_list('PACKAGE_KEYNAME', ['ITEM1', 'ITEM1'])
337+
prices = self.ordering.get_price_id_list('PACKAGE_KEYNAME', ['ITEM1', 'ITEM1'], "8")
337338

338339
list_mock.assert_called_once_with('PACKAGE_KEYNAME', mask='id, itemCategory, keyName, prices[categories]')
339340
self.assertEqual([price2['id'], price1['id']], prices)
@@ -366,7 +367,7 @@ def test_generate_order_with_preset(self):
366367

367368
mock_pkg.assert_called_once_with(pkg, mask='id')
368369
mock_preset.assert_called_once_with(pkg, preset)
369-
mock_get_ids.assert_called_once_with(pkg, items)
370+
mock_get_ids.assert_called_once_with(pkg, items, 8)
370371
self.assertEqual(expected_order, order)
371372

372373
def test_generate_order(self):
@@ -388,7 +389,7 @@ def test_generate_order(self):
388389

389390
mock_pkg.assert_called_once_with(pkg, mask='id')
390391
mock_preset.assert_not_called()
391-
mock_get_ids.assert_called_once_with(pkg, items)
392+
mock_get_ids.assert_called_once_with(pkg, items, None)
392393
self.assertEqual(expected_order, order)
393394

394395
def test_verify_order(self):
@@ -526,7 +527,7 @@ def test_location_group_id_none(self):
526527
with mock.patch.object(self.ordering, 'list_items') as list_mock:
527528
list_mock.return_value = [item1, item2]
528529

529-
prices = self.ordering.get_price_id_list('PACKAGE_KEYNAME', ['ITEM1', 'ITEM2'])
530+
prices = self.ordering.get_price_id_list('PACKAGE_KEYNAME', ['ITEM1', 'ITEM2'], "8")
530531

531532
list_mock.assert_called_once_with('PACKAGE_KEYNAME', mask='id, itemCategory, keyName, prices[categories]')
532533
self.assertEqual([price1['id'], price2['id']], prices)
@@ -543,7 +544,28 @@ def test_location_groud_id_empty(self):
543544
with mock.patch.object(self.ordering, 'list_items') as list_mock:
544545
list_mock.return_value = [item1, item2]
545546

546-
prices = self.ordering.get_price_id_list('PACKAGE_KEYNAME', ['ITEM1', 'ITEM2'])
547+
prices = self.ordering.get_price_id_list('PACKAGE_KEYNAME', ['ITEM1', 'ITEM2'], "8")
547548

548549
list_mock.assert_called_once_with('PACKAGE_KEYNAME', mask='id, itemCategory, keyName, prices[categories]')
549550
self.assertEqual([price1['id'], price2['id']], prices)
551+
552+
def test_get_item_price_id_without_capacity_restriction(self):
553+
category1 = {'categoryCode': 'cat1'}
554+
category2 = {'categoryCode': 'cat2'}
555+
prices = [{'id': 1234, 'locationGroupId': '', 'categories': [category1]},
556+
{'id': 2222, 'locationGroupId': 509, 'categories': [category2]}]
557+
558+
price_id = self.ordering.get_item_price_id("8", prices)
559+
560+
self.assertEqual(1234, price_id)
561+
562+
def test_get_item_price_id_with_capacity_restriction(self):
563+
category1 = {'categoryCode': 'cat1'}
564+
price1 = [{'id': 1234, 'locationGroupId': '', "capacityRestrictionMaximum": "16",
565+
"capacityRestrictionMinimum": "1", 'categories': [category1]},
566+
{'id': 2222, 'locationGroupId': '', "capacityRestrictionMaximum": "56",
567+
"capacityRestrictionMinimum": "36", 'categories': [category1]}]
568+
569+
price_id = self.ordering.get_item_price_id("8", price1)
570+
571+
self.assertEqual(1234, price_id)

0 commit comments

Comments
 (0)