1010import socket
1111import time
1212
13+ from SoftLayer import exceptions
1314from SoftLayer .managers import ordering
1415from SoftLayer import utils
1516# pylint: disable=no-self-use
@@ -745,22 +746,22 @@ def upgrade(self, instance_id, cpus=None, memory=None,
745746 """
746747 package_items = self ._get_package_items ()
747748 prices = []
748- if cpus :
749- prices . append ({
750- 'id ' : self . _get_item_id_for_upgrade ( package_items ,
751- 'cpus' ,
752- cpus ,
753- public )})
754- if memory :
755- prices . append ({
756- 'id' : self . _get_item_id_for_upgrade ( package_items ,
757- 'memory' ,
758- memory )})
759- if nic_speed :
760- prices . append ({
761- 'id' : self . _get_item_id_for_upgrade ( package_items ,
762- 'nic_speed' ,
763- nic_speed ) })
749+
750+ for option , value in { 'cpus' : cpus ,
751+ 'memory ' : memory ,
752+ 'nic_speed' : nic_speed }. items ():
753+ if not value :
754+ continue
755+ price_id = self . _get_price_id_for_upgrade ( package_items ,
756+ option ,
757+ value ,
758+ public )
759+ if not price_id :
760+ # Every option provided is expected to have a price
761+ raise exceptions . SoftLayerError (
762+ "Unable to find %s option with value %s" % ( option , value ))
763+
764+ prices . append ({ 'id' : price_id })
764765
765766 maintenance_window = datetime .datetime .now (utils .UTC ())
766767 order = {
@@ -783,7 +784,7 @@ def _get_package_items(self):
783784 mask = [
784785 'description' ,
785786 'capacity' ,
786- 'prices[id,categories[name,id,categoryCode]]'
787+ 'prices[id,locationGroupId, categories[name,id,categoryCode]]'
787788 ]
788789 mask = "mask[%s]" % ',' .join (mask )
789790
@@ -793,9 +794,9 @@ def _get_package_items(self):
793794
794795 return package_service .getItems (id = package_id , mask = mask )
795796
796- def _get_item_id_for_upgrade (self , package_items , option , value ,
797- public = True ):
798- """Find the item ids for the parameters you want to upgrade to .
797+ def _get_price_id_for_upgrade (self , package_items , option , value ,
798+ public = True ):
799+ """Find the price id for the option and value to upgrade.
799800
800801 :param list package_items: Contains all the items related to an VS
801802 :param string option: Describes type of parameter to be upgraded
@@ -807,20 +808,29 @@ def _get_item_id_for_upgrade(self, package_items, option, value,
807808 'cpus' : 'guest_core' ,
808809 'nic_speed' : 'port_speed'
809810 }
810-
811+ category_code = option_category [ option ]
811812 for item in package_items :
812- categories = item ['prices' ][0 ]['categories' ]
813- for category in categories :
814- if not (category ['categoryCode' ] == option_category [option ] and
815- str (item ['capacity' ]) == str (value )):
813+ is_private = str (item ['description' ]).startswith ('Private' )
814+ for price in item ['prices' ]:
815+ if 'locationGroupId' in price and price ['locationGroupId' ]:
816+ # Skip location based prices
817+ continue
818+
819+ if 'categories' not in price :
816820 continue
817- if option == 'cpus' :
818- if public and ('Private' not in item ['description' ]):
819- return item ['prices' ][0 ]['id' ]
820- elif not public and ('Private' in item ['description' ]):
821- return item ['prices' ][0 ]['id' ]
822- elif option == 'nic_speed' :
823- if 'Public' in item ['description' ]:
824- return item ['prices' ][0 ]['id' ]
825- else :
826- return item ['prices' ][0 ]['id' ]
821+
822+ categories = price ['categories' ]
823+ for category in categories :
824+ if not (category ['categoryCode' ] == category_code
825+ and str (item ['capacity' ]) == str (value )):
826+ continue
827+ if option == 'cpus' :
828+ if public and not is_private :
829+ return price ['id' ]
830+ elif not public and is_private :
831+ return price ['id' ]
832+ elif option == 'nic_speed' :
833+ if 'Public' in item ['description' ]:
834+ return price ['id' ]
835+ else :
836+ return price ['id' ]
0 commit comments