@@ -51,6 +51,7 @@ def __init__(self, client, ordering_manager=None):
5151 self .client = client
5252 self .account = client ['Account' ]
5353 self .guest = client ['Virtual_Guest' ]
54+ self .package_svc = client ['Product_Package' ]
5455 self .resolvers = [self ._get_ids_from_ip , self ._get_ids_from_hostname ]
5556 if ordering_manager is None :
5657 self .ordering_manager = ordering .OrderingManager (client )
@@ -803,7 +804,7 @@ def capture(self, instance_id, name, additional_disks=False, notes=None):
803804 name , disks_to_capture , notes , id = instance_id )
804805
805806 def upgrade (self , instance_id , cpus = None , memory = None ,
806- nic_speed = None , public = True ):
807+ nic_speed = None , public = True , preset = None ):
807808 """Upgrades a VS instance.
808809
809810 Example::
@@ -817,6 +818,7 @@ def upgrade(self, instance_id, cpus=None, memory=None,
817818 :param int instance_id: Instance id of the VS to be upgraded
818819 :param int cpus: The number of virtual CPUs to upgrade to
819820 of a VS instance.
821+ :param string preset: preset assigned to the vsi
820822 :param int memory: RAM of the VS to be upgraded to.
821823 :param int nic_speed: The port speed to set
822824 :param bool public: CPU will be in Private/Public Node.
@@ -826,9 +828,30 @@ def upgrade(self, instance_id, cpus=None, memory=None,
826828 upgrade_prices = self ._get_upgrade_prices (instance_id )
827829 prices = []
828830
829- for option , value in {'cpus' : cpus ,
830- 'memory' : memory ,
831- 'nic_speed' : nic_speed }.items ():
831+ data = {'nic_speed' : nic_speed }
832+
833+ if cpus is not None and preset is not None :
834+ raise exceptions .SoftLayerError ("Do not use cpu, private and memory if you are using flavors" )
835+ else :
836+ data ['cpus' ] = cpus
837+
838+ if memory is not None and preset is not None :
839+ raise exceptions .SoftLayerError ("Do not use memory, private or cpu if you are using flavors" )
840+ else :
841+ data ['memory' ] = memory
842+
843+ maintenance_window = datetime .datetime .now (utils .UTC ())
844+ order = {
845+ 'complexType' : 'SoftLayer_Container_Product_Order_Virtual_Guest_'
846+ 'Upgrade' ,
847+ 'properties' : [{
848+ 'name' : 'MAINTENANCE_WINDOW' ,
849+ 'value' : maintenance_window .strftime ("%Y-%m-%d %H:%M:%S%z" )
850+ }],
851+ 'virtualGuests' : [{'id' : int (instance_id )}],
852+ }
853+
854+ for option , value in data .items ():
832855 if not value :
833856 continue
834857 price_id = self ._get_price_id_for_upgrade_option (upgrade_prices ,
@@ -841,23 +864,47 @@ def upgrade(self, instance_id, cpus=None, memory=None,
841864 "Unable to find %s option with value %s" % (option , value ))
842865
843866 prices .append ({'id' : price_id })
867+ order ['prices' ] = prices
844868
845- maintenance_window = datetime .datetime .now (utils .UTC ())
846- order = {
847- 'complexType' : 'SoftLayer_Container_Product_Order_Virtual_Guest_'
848- 'Upgrade' ,
849- 'prices' : prices ,
850- 'properties' : [{
851- 'name' : 'MAINTENANCE_WINDOW' ,
852- 'value' : maintenance_window .strftime ("%Y-%m-%d %H:%M:%S%z" )
853- }],
854- 'virtualGuests' : [{'id' : int (instance_id )}],
855- }
856- if prices :
869+ if preset is not None :
870+ presetId = self ._get_active_presets (preset )
871+ order ['presetId' ] = presetId
872+
873+ if prices or preset :
857874 self .client ['Product_Order' ].placeOrder (order )
858875 return True
859876 return False
860877
878+ def _get_active_presets (self , preset ):
879+ """Following Method gets the active presets.
880+ """
881+ packageId = 835
882+
883+ _filter = {
884+ 'activePresets' : {
885+ 'keyName' : {
886+ 'operation' : preset
887+ }
888+ },
889+ 'accountRestrictedActivePresets' : {
890+ 'keyName' : {
891+ 'operation' : preset
892+ }
893+ }
894+ }
895+
896+ mask = 'mask[id]'
897+ active_presets = self .package_svc .getActivePresets (id = packageId , mask = mask , filter = _filter )
898+
899+ if len (active_presets ) == 0 :
900+ raise exceptions .SoftLayerError (
901+ "Preset {} does not exist in package {}" .format (preset ,
902+ packageId ))
903+
904+ for presetId in active_presets :
905+ id = presetId ['id' ]
906+ return id
907+
861908 def _get_package_items (self ):
862909 """Following Method gets all the item ids related to VS.
863910
0 commit comments