app/models/ec2_adapter.rb
# also used by AsAdapter to process info about AS Group's instances
also used by AsAdapter to process info about AS Group's instances
also used by AsAdapter to process info about AS Group ' s instances
def parse_instance_info(reservation, attributes, account_instances=nil, account_zones=nil, account_security_groups=nil, account_instance_vm_types=nil, cpu_profiles=nil, account_storage_types=nil)
        as_lifecycle_state_to_ec2_state = {}
        as_lifecycle_state_to_ec2_state['Pending'] = 'pending'
        as_lifecycle_state_to_ec2_state['InService'] = 'running'
        as_lifecycle_state_to_ec2_state['Terminating'] = 'shutting-down'
        as_lifecycle_state_to_ec2_state['Terminated'] = 'terminated'
        account_instances ||= @account.instances
        account_zones ||= @account.zones
        account_security_groups ||= @account.security_groups
        account_instance_vm_types ||= @account.instance_vm_types
        cpu_profiles ||= CpuProfile.all
        account_storage_types ||= @account.storage_types
        attributes[:instance_id] = attributes[:id]
        attributes.delete(:id)
        vm_type = account_instance_vm_types.detect{ |t| t.api_name == attributes[:type] }
        attributes[:instance_vm_type_id] = vm_type.id unless vm_type.nil?
        attributes.delete(:type)
        if attributes[:architecture]
          cpu_profile = cpu_profiles.detect{ |p| p.api_name == attributes[:architecture] }
          cpu_profile ||= CpuProfile.create( { :api_name => attributes[:architecture], :name => attributes[:architecture] } )
          attributes[:cpu_profile_id] = cpu_profile.id
          attributes.delete(:architecture)
        end
        if attributes[:root_device_type]
            storage_type = account_storage_types.detect{|t| t.api_name == attributes[:root_device_type]}
            storage_type ||= StorageType.create({
              :provider_id => @account.provider_id,
              :api_name    => attributes[:root_device_type],
              :name        => attributes[:root_device_type],
              :desc        => 'Created by Ec2Adapter during the Image import',
            })
            attributes[:storage_type_id] = storage_type.id
            attributes.delete(:root_device_type)
        end
        if attributes[:lifecycle_state].blank?
            zone_name = attributes[:zone]
            attributes.delete(:zone)
        else
            attributes[:state] = as_lifecycle_state_to_ec2_state[attributes[:lifecycle_state]]
            attributes.delete(:lifecycle_state)
            attributes[:state] ||= 'unknown'
            zone_name = attributes[:availability_zone]
            attributes.delete(:availability_zone)
        end
        attributes[:state].gsub!('-','_')
        instance = account_instances.detect{ |s| s.instance_id == attributes[:instance_id] }
        if instance.nil?
            instance = @account.instances.build(attributes)
        else
            instance.attributes = attributes
        end
        security_groups = []
        if reservation and reservation[:groups]
            security_groups = (account_security_groups.select{ |g| reservation[:groups].include?(g.api_name) })
        end
        instance.security_groups = []
        instance.security_groups = security_groups
        unless zone_name.blank?
            zone = account_zones.detect{ |z| z.name == zone_name }
            if zone and !zone.instances.include?(instance)
                zone.instances << instance
            end
        end
        instance.should_destroy = 0
        return instance    
    end
def parse_instance_info ( reservation , attributes , account_instances = nil , account_zones = nil , account_security_groups = nil , account_instance_vm_types = nil , cpu_profiles = nil , account_storage_types = nil ) as_lifecycle_state_to_ec2_state = { } as_lifecycle_state_to_ec2_state [ 'Pending' ] = 'pending' as_lifecycle_state_to_ec2_state [ 'InService' ] = 'running' as_lifecycle_state_to_ec2_state [ 'Terminating' ] = 'shutting-down' as_lifecycle_state_to_ec2_state [ 'Terminated' ] = 'terminated' account_instances ||= @account . instances account_zones ||= @account . zones account_security_groups ||= @account . security_groups account_instance_vm_types ||= @account . instance_vm_types cpu_profiles ||= CpuProfile . all account_storage_types ||= @account . storage_types attributes [ :instance_id ] = attributes [ :id ] attributes . delete ( :id ) vm_type = account_instance_vm_types . detect { | t | t . api_name == attributes [ :type ] } attributes [ :instance_vm_type_id ] = vm_type . id unless vm_type . nil? attributes . delete ( :type ) if attributes [ :architecture ] cpu_profile = cpu_profiles . detect { | p | p . api_name == attributes [ :architecture ] } cpu_profile ||= CpuProfile . create ( { :api_name => attributes [ :architecture ] , :name => attributes [ :architecture ] } ) attributes [ :cpu_profile_id ] = cpu_profile . id attributes . delete ( :architecture ) end if attributes [ :root_device_type ] storage_type = account_storage_types . detect { | t | t . api_name == attributes [ :root_device_type ] } storage_type ||= StorageType . create ( { :provider_id => @account . provider_id , :api_name => attributes [ :root_device_type ] , :name => attributes [ :root_device_type ] , :desc => 'Created by Ec2Adapter during the Image import' , } ) attributes [ :storage_type_id ] = storage_type . id attributes . delete ( :root_device_type ) end if attributes [ :lifecycle_state ] . blank? zone_name = attributes [ :zone ] attributes . delete ( :zone ) else attributes [ :state ] = as_lifecycle_state_to_ec2_state [ attributes [ :lifecycle_state ] ] attributes . delete ( :lifecycle_state ) attributes [ :state ] ||= 'unknown' zone_name = attributes [ :availability_zone ] attributes . delete ( :availability_zone ) end attributes [ :state ] . gsub! ( '-' , '_' ) instance = account_instances . detect { | s | s . instance_id == attributes [ :instance_id ] } if instance . nil? instance = @account . instances . build ( attributes ) else instance . attributes = attributes end security_groups = [ ] if reservation and reservation [ :groups ] security_groups = ( account_security_groups . select { | g | reservation [ :groups ] . include? ( g . api_name ) } ) end instance . security_groups = [ ] instance . security_groups = security_groups unless zone_name . blank? zone = account_zones . detect { | z | z . name == zone_name } if zone and ! zone . instances . include? ( instance ) zone . instances << instance end end instance . should_destroy = 0 return instance end
also used by AsAdapter to process info about AS Group's instances
also used by AsAdapter to process info about AS Group ' s instances
# delete :id attribute before building a instance record - :id is a special rails attribute # store :type attribute in the instance_type - :type is a special rails attribute # map architecture to cpu profile # map rootDeviceType element to StorageType # process ec2 instance info # process as instance info # replace states like shutting-down with states like shutting_down # get / build the instance model # get security groups # get zone information
reservation
attributes
account_instances
account_zones
account_security_groups
account_instance_vm_types
cpu_profiles
account_storage_types