@@ -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
0 commit comments