99import itertools
1010import socket
1111import time
12+ import warnings
1213
1314from SoftLayer import exceptions
1415from SoftLayer .managers import ordering
@@ -773,24 +774,24 @@ def upgrade(self, instance_id, cpus=None, memory=None,
773774 :param int instance_id: Instance id of the VS to be upgraded
774775 :param int cpus: The number of virtual CPUs to upgrade to
775776 of a VS instance.
776- :param bool public: CPU will be in Private/Public Node.
777777 :param int memory: RAM of the VS to be upgraded to.
778778 :param int nic_speed: The port speed to set
779+ :param bool public: CPU will be in Private/Public Node.
779780
780781 :returns: bool
781782 """
782- package_items = self ._get_package_items ( )
783+ upgrade_prices = self ._get_upgrade_prices ( instance_id )
783784 prices = []
784785
785786 for option , value in {'cpus' : cpus ,
786787 'memory' : memory ,
787788 'nic_speed' : nic_speed }.items ():
788789 if not value :
789790 continue
790- price_id = self ._get_price_id_for_upgrade ( package_items ,
791- option ,
792- value ,
793- public )
791+ price_id = self ._get_price_id_for_upgrade_option ( upgrade_prices ,
792+ option ,
793+ value ,
794+ public )
794795 if not price_id :
795796 # Every option provided is expected to have a price
796797 raise exceptions .SoftLayerError (
@@ -815,7 +816,12 @@ def upgrade(self, instance_id, cpus=None, memory=None,
815816 return False
816817
817818 def _get_package_items (self ):
818- """Following Method gets all the item ids related to VS."""
819+ """Following Method gets all the item ids related to VS.
820+
821+ Deprecated in favor of _get_upgrade_prices()
822+ """
823+ warnings .warn ("use _get_upgrade_prices() instead" ,
824+ DeprecationWarning )
819825 mask = [
820826 'description' ,
821827 'capacity' ,
@@ -833,15 +839,76 @@ def _get_package_items(self):
833839 package_service = self .client ['Product_Package' ]
834840 return package_service .getItems (id = package ['id' ], mask = mask )
835841
842+ def _get_upgrade_prices (self , instance_id , include_downgrade_options = True ):
843+ """Following Method gets all the price ids related to upgrading a VS.
844+
845+ :param int instance_id: Instance id of the VS to be upgraded
846+
847+ :returns: list
848+ """
849+ mask = [
850+ 'id' ,
851+ 'locationGroupId' ,
852+ 'categories[name,id,categoryCode]' ,
853+ 'item[description,capacity,units]'
854+ ]
855+ mask = "mask[%s]" % ',' .join (mask )
856+ return self .guest .getUpgradeItemPrices (include_downgrade_options , id = instance_id , mask = mask )
857+
858+ def _get_price_id_for_upgrade_option (self , upgrade_prices , option , value ,
859+ public = True ):
860+ """Find the price id for the option and value to upgrade. This
861+
862+ :param list upgrade_prices: Contains all the prices related to a VS upgrade
863+ :param string option: Describes type of parameter to be upgraded
864+ :param int value: The value of the parameter to be upgraded
865+ :param bool public: CPU will be in Private/Public Node.
866+ """
867+ option_category = {
868+ 'memory' : 'ram' ,
869+ 'cpus' : 'guest_core' ,
870+ 'nic_speed' : 'port_speed'
871+ }
872+ category_code = option_category .get (option )
873+ for price in upgrade_prices :
874+ if price .get ('categories' ) is None or price .get ('item' ) is None :
875+ continue
876+
877+ product = price .get ('item' )
878+ is_private = (product .get ('units' ) == 'PRIVATE_CORE'
879+ or product .get ('units' ) == 'DEDICATED_CORE' )
880+
881+ for category in price .get ('categories' ):
882+ if not (category .get ('categoryCode' ) == category_code
883+ and str (product .get ('capacity' )) == str (value )):
884+ continue
885+
886+ if option == 'cpus' :
887+ # Public upgrade and public guest_core price
888+ if public and not is_private :
889+ return price .get ('id' )
890+ # Private upgrade and private guest_core price
891+ elif not public and is_private :
892+ return price .get ('id' )
893+ elif option == 'nic_speed' :
894+ if 'Public' in product .get ('description' ):
895+ return price .get ('id' )
896+ else :
897+ return price .get ('id' )
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
843908 :param bool public: CPU will be in Private/Public Node.
844909 """
910+ warnings .warn ("use _get_price_id_for_upgrade_option() instead" ,
911+ DeprecationWarning )
845912 option_category = {
846913 'memory' : 'ram' ,
847914 'cpus' : 'guest_core' ,
0 commit comments