Skip to content

Commit 6898e69

Browse files
committed
move getting upgrade prices to the getUpgradeItemPrices method, handle DEDICATED_CORE units
1 parent 42a275e commit 6898e69

3 files changed

Lines changed: 164 additions & 10 deletions

File tree

SoftLayer/fixtures/SoftLayer_Virtual_Guest.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,3 +252,85 @@
252252
setTags = True
253253
createArchiveTransaction = {}
254254
executeRescueLayer = True
255+
256+
getUpgradeItemPrices = [
257+
{
258+
'id': 1007,
259+
'categories': [{'id': 80,
260+
'name': 'Computing Instance',
261+
'categoryCode': 'guest_core'}],
262+
'item': {
263+
'capacity': '4',
264+
'units': 'PRIVATE_CORE',
265+
'description': 'Computing Instance (Dedicated)',
266+
}
267+
},
268+
{
269+
'id': 1144,
270+
'locationGroupId': None,
271+
'categories': [{'id': 80,
272+
'name': 'Computing Instance',
273+
'categoryCode': 'guest_core'}],
274+
'item': {
275+
'capacity': '4',
276+
'units': 'CORE',
277+
'description': 'Computing Instance',
278+
}
279+
},
280+
{
281+
'id': 332211,
282+
'locationGroupId': 1,
283+
'categories': [{'id': 80,
284+
'name': 'Computing Instance',
285+
'categoryCode': 'guest_core'}],
286+
'item': {
287+
'capacity': '4',
288+
'units': 'CORE',
289+
'description': 'Computing Instance',
290+
}
291+
},
292+
{
293+
'id': 1122,
294+
'categories': [{'id': 26,
295+
'name': 'Uplink Port Speeds',
296+
'categoryCode': 'port_speed'}],
297+
'item': {
298+
'capacity': '1000',
299+
'description': 'Public & Private Networks',
300+
}
301+
},
302+
{
303+
'id': 1144,
304+
'categories': [{'id': 26,
305+
'name': 'Uplink Port Speeds',
306+
'categoryCode': 'port_speed'}],
307+
'item': {
308+
'capacity': '1000',
309+
'description': 'Private Networks',
310+
}
311+
},
312+
{
313+
'id': 1133,
314+
'categories': [{'id': 3,
315+
'name': 'RAM',
316+
'categoryCode': 'ram'}],
317+
'item': {
318+
'capacity': '2',
319+
'description': 'RAM',
320+
}
321+
},
322+
]
323+
324+
DEDICATED_GET_UPGRADE_ITEM_PRICES = [
325+
{
326+
'id': 115566,
327+
'categories': [{'id': 80,
328+
'name': 'Computing Instance',
329+
'categoryCode': 'guest_core'}],
330+
'item': {
331+
'capacity': '4',
332+
'units': 'DEDICATED_CORE',
333+
'description': 'Computing Instance (Dedicated Host)',
334+
}
335+
},
336+
]

SoftLayer/managers/vs.py

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -773,21 +773,21 @@ def upgrade(self, instance_id, cpus=None, memory=None,
773773
:param int instance_id: Instance id of the VS to be upgraded
774774
:param int cpus: The number of virtual CPUs to upgrade to
775775
of a VS instance.
776-
:param bool public: CPU will be in Private/Public Node.
777776
:param int memory: RAM of the VS to be upgraded to.
778777
:param int nic_speed: The port speed to set
778+
:param bool public: CPU will be in Private/Public Node.
779779
780780
:returns: bool
781781
"""
782-
package_items = self._get_package_items()
782+
upgrade_prices = self._get_upgrade_prices(instance_id)
783783
prices = []
784784

785785
for option, value in {'cpus': cpus,
786786
'memory': memory,
787787
'nic_speed': nic_speed}.items():
788788
if not value:
789789
continue
790-
price_id = self._get_price_id_for_upgrade(package_items,
790+
price_id = self._get_price_id_for_upgrade_option(upgrade_prices,
791791
option,
792792
value,
793793
public)
@@ -833,10 +833,75 @@ def _get_package_items(self):
833833
package_service = self.client['Product_Package']
834834
return package_service.getItems(id=package['id'], mask=mask)
835835

836+
def _get_upgrade_prices(self, instance_id):
837+
"""Following Method gets all the price ids related to upgrading a VS.
838+
839+
:param int instance_id: Instance id of the VS to be upgraded
840+
841+
:returns: list
842+
"""
843+
mask = [
844+
'id',
845+
'locationGroupId',
846+
'categories[name,id,categoryCode]',
847+
'item[description,capacity,units]'
848+
]
849+
mask = "mask[%s]" % ','.join(mask)
850+
return self.guest.getUpgradeItemPrices(id=instance_id, mask=mask)
851+
852+
def _get_price_id_for_upgrade_option(self, upgrade_prices, option, value,
853+
public=True):
854+
"""Find the price id for the option and value to upgrade. This
855+
856+
:param list upgrade_prices: Contains all the prices related to a VS upgrade
857+
:param string option: Describes type of parameter to be upgraded
858+
:param int value: The value of the parameter to be upgraded
859+
:param bool public: CPU will be in Private/Public Node.
860+
"""
861+
option_category = {
862+
'memory': 'ram',
863+
'cpus': 'guest_core',
864+
'nic_speed': 'port_speed'
865+
}
866+
category_code = option_category[option]
867+
for price in upgrade_prices:
868+
if 'locationGroupId' in price and price['locationGroupId']:
869+
# Skip location based prices
870+
continue
871+
872+
if 'categories' not in price:
873+
continue
874+
875+
if 'item' not in price:
876+
continue
877+
878+
product = price['item']
879+
is_private = (product.get('units') == 'PRIVATE_CORE'
880+
or product.get('units') == 'DEDICATED_CORE')
881+
882+
categories = price['categories']
883+
for category in categories:
884+
if not (category['categoryCode'] == category_code
885+
and str(product['capacity']) == str(value)):
886+
continue
887+
if option == 'cpus':
888+
if public and not is_private:
889+
return price['id']
890+
elif not public and is_private:
891+
return price['id']
892+
elif option == 'nic_speed':
893+
if 'Public' in product['description']:
894+
return price['id']
895+
else:
896+
return price['id']
897+
898+
836899
def _get_price_id_for_upgrade(self, package_items, option, value,
837900
public=True):
838901
"""Find the price id for the option and value to upgrade.
839902
903+
Deprecated in favor of _get_price_id_for_upgrade_option()
904+
840905
:param list package_items: Contains all the items related to an VS
841906
:param string option: Describes type of parameter to be upgraded
842907
:param int value: The value of the parameter to be upgraded

tests/managers/vs_tests.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -636,13 +636,6 @@ def test_capture_additional_disks(self):
636636
identifier=1)
637637

638638
def test_upgrade(self):
639-
mock = self.set_mock('SoftLayer_Product_Package', 'getAllObjects')
640-
mock.return_value = [
641-
{'id': 46, 'name': 'Virtual Servers',
642-
'description': 'Virtual Server Instances',
643-
'type': {'keyName': 'VIRTUAL_SERVER_INSTANCE'}, 'isActive': 1},
644-
]
645-
646639
# test single upgrade
647640
result = self.vs.upgrade(1, cpus=4, public=False)
648641

@@ -678,6 +671,20 @@ def test_upgrade_full(self):
678671
self.assertIn({'id': 1122}, order_container['prices'])
679672
self.assertEqual(order_container['virtualGuests'], [{'id': 1}])
680673

674+
def test_upgrade_dedicated_host_instance(self):
675+
mock = self.set_mock('SoftLayer_Virtual_Guest', 'getUpgradeItemPrices')
676+
mock.return_value = fixtures.SoftLayer_Virtual_Guest.DEDICATED_GET_UPGRADE_ITEM_PRICES
677+
678+
# test single upgrade
679+
result = self.vs.upgrade(1, cpus=4, public=False)
680+
681+
self.assertEqual(result, True)
682+
self.assert_called_with('SoftLayer_Product_Order', 'placeOrder')
683+
call = self.calls('SoftLayer_Product_Order', 'placeOrder')[0]
684+
order_container = call.args[0]
685+
self.assertEqual(order_container['prices'], [{'id': 115566}])
686+
self.assertEqual(order_container['virtualGuests'], [{'id': 1}])
687+
681688
def test_upgrade_skips_location_based_prices(self):
682689
# Test that no prices that have locationGroupId set are used
683690
self.assertRaises(exceptions.SoftLayerError,

0 commit comments

Comments
 (0)