From d989dfd18b950d1261311e0901c48614b7b936a8 Mon Sep 17 00:00:00 2001 From: Fernando Ojeda Date: Thu, 11 Oct 2018 10:34:50 -0400 Subject: [PATCH] Refactored suspend cloud server order --- SoftLayer/managers/ordering.py | 27 +++++++++++---------------- tests/managers/ordering_tests.py | 14 +++++++++----- 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/SoftLayer/managers/ordering.py b/SoftLayer/managers/ordering.py index 361ce0102..5b6927369 100644 --- a/SoftLayer/managers/ordering.py +++ b/SoftLayer/managers/ordering.py @@ -357,10 +357,7 @@ def get_price_id_list(self, package_keyname, item_keynames, core=None): # 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 = None - for price in matching_item['prices']: - if not price['locationGroupId']: - price_id = self.get_item_price_id(core, price, price_id) + 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. @@ -374,19 +371,17 @@ def get_price_id_list(self, package_keyname, item_keynames, core=None): return prices @staticmethod - def get_item_price_id(core, price, price_id): + def get_item_price_id(core, prices): """get item price id""" - category_code = [] - capacity_min = int(price.get('capacityRestrictionMinimum', -1)) - capacity_max = int(price.get('capacityRestrictionMaximum', -1)) - if capacity_min == -1: - if price['categories'][0]['categoryCode'] not in category_code: - category_code.append(price['categories'][0]['categoryCode']) - price_id = price['id'] - elif capacity_min <= int(core) <= capacity_max: - if price['categories'][0]['categoryCode'] not in category_code: - category_code.append(price['categories'][0]['categoryCode']) - price_id = 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): diff --git a/tests/managers/ordering_tests.py b/tests/managers/ordering_tests.py index 4319ff149..a0a253a9f 100644 --- a/tests/managers/ordering_tests.py +++ b/tests/managers/ordering_tests.py @@ -551,17 +551,21 @@ def test_location_groud_id_empty(self): def test_get_item_price_id_without_capacity_restriction(self): category1 = {'categoryCode': 'cat1'} - price1 = {'id': 1234, 'locationGroupId': '', 'categories': [category1]} + category2 = {'categoryCode': 'cat2'} + prices = [{'id': 1234, 'locationGroupId': '', 'categories': [category1]}, + {'id': 2222, 'locationGroupId': 509, 'categories': [category2]}] - price_id = self.ordering.get_item_price_id("8", price1, None) + 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]} + 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, None) + price_id = self.ordering.get_item_price_id("8", price1) self.assertEqual(1234, price_id)