def to_hash(context)
r = {
:id => self.id,
:href => context.instance_url(self.id),
:name => name,
:state => state,
:owner => owner_id,
:image => { :href => context.image_url(image_id), :id => image_id, :rel => :image },
:realm => { :href => context.realm_url(realm_id), :id => realm_id, :rel => :realm },
:actions => actions.compact.map { |a|
{
:href => context.send("#{a}_instance_url", self.id),
:rel => "#{a}",
:method => context.instance_action_method(a)
}
},
:hardware_profile => {
:id => instance_profile.id,
:href => context.hardware_profile_url(instance_profile.id),
:rel => :hardware_profile,
:properties => instance_profile.overrides
},
:public_addresses => public_addresses.map { |addr| addr.to_hash(context) },
:private_addresses => private_addresses.map { |addr| addr.to_hash(context) }
}
if context.driver.respond_to? :run_on_instance
r[:actions] << { :href => "#{context.run_instance_url(self.id)};id=#{self.id}", :rel => 'run', :method => 'post'}
end
if can_create_image?
r[:actions] << { :href => "#{context.create_image_url};instance_id=#{self.id}", :rel => 'create_image', :method => 'post'}
end
r.merge!(:create_time => launch_time) if launch_time
r.merge!(:create_image => create_image) if create_image
r.merge!(:firewalls => firewalls.map { |f| { :id => f, :href => context.firewall_url(f), :rel => :firewall }}) if firewalls
if storage_volumes
r.merge!(:storage_volumes => storage_volumes.map { |v| { :id => v.keys.first, :href => context.storage_volume_url(v.keys.first), :rel => :storage_volume }})
end
if context.driver.class.has_feature?(:instances, :authentication_key)
r.merge!(:authentication_type => 'key' )
r.merge!(:authentication => { :keyname => keyname }) if keyname
end
if context.driver.class.has_feature?(:instances, :authentication_password)
r[:authentication] && username ? r[:authentication].merge!({ :user => username, :password => password }) :
(username ? r.merge!(:authentication => { :user => username, :password => password }) : nil)
end
r
end