1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

Merge branch 'master' of github.com:fog/fog into file_metadata

This commit is contained in:
Kyle Rames 2013-02-27 08:25:31 -06:00
commit 280a26ece6
6 changed files with 175 additions and 29 deletions

View file

@ -100,6 +100,10 @@ module Fog
request :update_metadata
request :delete_metadata
# Metadatam
request :delete_meta
request :update_meta
# Address
request :list_addresses
request :list_address_pools

View file

@ -46,7 +46,6 @@ module Fog
def initialize(attributes={})
# Old 'connection' is renamed as service and should be used instead
prepare_service_value(attributes)
attributes[:metadata] = {}
self.security_groups = attributes.delete(:security_groups)
self.min_count = attributes.delete(:min_count)
@ -66,9 +65,10 @@ module Fog
end
def metadata=(new_metadata={})
return unless new_metadata
metas = []
new_metadata.each_pair {|k,v| metas << {"key" => k, "value" => v} }
metadata.load(metas)
@metadata = metadata.load(metas)
end
def user_data=(ascii_userdata)
@ -86,24 +86,41 @@ module Fog
service.images(:server => self)
end
def private_ip_address
if addresses['private']
#assume only a single private
return addresses['private'].first
elsif addresses['internet']
#assume no private IP means private cloud
return addresses['internet'].first
end
def all_addresses
# currently openstack API does not tell us what is a floating ip vs a fixed ip for the vm listing,
# we fall back to get all addresses and filter sadly.
@all_addresses ||= service.list_all_addresses.body["floating_ips"].select{|data| data['instance_id'] == id}
end
def public_ip_address
if addresses['public']
#assume last is either original or assigned from floating IPs
return addresses['public'].last
elsif addresses['internet']
#assume no public IP means private cloud
return addresses['internet'].first
end
def reload
@all_addresses = nil
super
end
# returns all ip_addresses for a given instance
# this includes both the fixed ip(s) and the floating ip(s)
def ip_addresses
addresses.values.flatten.map{|x| x['addr']}
end
def floating_ip_addresses
all_addresses.map{|addr| addr["ip"]}
end
alias_method :public_ip_addresses, :floating_ip_addresses
def floating_ip_address
floating_ip_addresses.first
end
alias_method :public_ip_address, :floating_ip_address
def private_ip_addresses
ip_addresses - floating_ip_addresses
end
def private_ip_address
private_ip_addresses.first
end
def image_ref
@ -229,10 +246,7 @@ module Fog
def save
raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if persisted?
requires :flavor_ref, :image_ref, :name
meta_hash = {}
metadata.each { |meta| meta_hash.store(meta.key, meta.value) }
options = {
'metadata' => meta_hash,
'personality' => personality,
'accessIPv4' => accessIPv4,
'accessIPv6' => accessIPv6,
@ -244,6 +258,7 @@ module Fog
'max_count' => @max_count,
'os:scheduler_hints' => @os_scheduler_hints
}
options['metadata'] = metadata.to_hash unless @metadata.nil?
options = options.reject {|key, value| value.nil?}
data = service.create_server(name, image_ref, flavor_ref, options)
merge_attributes(data.body['server'])

View file

@ -0,0 +1,43 @@
module Fog
module Compute
class OpenStack
class Real
def delete_meta(collection_name, parent_id, key)
request(
:expects => 204,
:method => 'DELETE',
:path => "#{collection_name}/#{parent_id}/metadata/#{key}"
)
end
end
class Mock
def delete_meta(collection_name, parent_id, key)
if collection_name == "images" then
if not list_images_detail.body['images'].detect {|_| _['id'] == parent_id}
raise Fog::Compute::OpenStack::NotFound
end
end
if collection_name == "servers" then
if not list_servers_detail.body['servers'].detect {|_| _['id'] == parent_id}
raise Fog::Compute::OpenStack::NotFound
end
end
response = Excon::Response.new
response.status = 204
response
end
end
end
end
end

View file

@ -0,0 +1,46 @@
module Fog
module Compute
class OpenStack
class Real
def update_meta(collection_name, parent_id, key, value)
request(
:body => Fog::JSON.encode({ 'meta' => {key => value}}),
:expects => 200,
:method => 'PUT',
:path => "#{collection_name}/#{parent_id}/metadata/#{key}"
)
end
end
class Mock
def update_meta(collection_name, parent_id, key, value)
if collection_name == "images" then
if not list_images_detail.body['images'].detect {|_| _['id'] == parent_id}
raise Fog::Compute::OpenStack::NotFound
end
end
if collection_name == "servers" then
if not list_servers_detail.body['servers'].detect {|_| _['id'] == parent_id}
raise Fog::Compute::OpenStack::NotFound
end
end
#FIXME join w/ existing metadata here
response = Excon::Response.new
response.body = { "metadata" => {key => value} }
response.status = 200
response
end
end
end
end
end

View file

@ -22,14 +22,11 @@ module Fog
# @note This method is incompatible with Cloud Servers utlizing RackConnect. RackConnect users
# should use server personalization to install keys. Please see Server#personality for more information.
# @example
# service = Fog::Compute.new(:provider => 'rackspace',
# :version => :v2,
# service.servers.bootstrap :name => 'bootstrap-server',
# :flavor_id => service.flavors.first.id,
# :image_id => service.images.find {|img| img.name =~ /Ubuntu/}.id,
# :public_key_path => '~/.ssh/fog_rsa.pub',
# :private_key_path => '~/.ssh/fog_rsa')
#
# service.servers.bootstrap :name => 'bootstap-server',
# :flavor_id => service.flavors.first.id
# :image_id => service.servers.first.id
# :private_key_path => '~/.ssh/fog_rsa'
#
def bootstrap(new_attributes = {})
server = create(new_attributes)

View file

@ -1,6 +1,7 @@
Shindo.tests("Fog::Compute[:openstack] | server", ['openstack']) do
tests('success') do
tests('#security_groups').succeeds do
fog = Fog::Compute[:openstack]
@ -20,7 +21,6 @@ Shindo.tests("Fog::Compute[:openstack] | server", ['openstack']) do
server.save
found_groups = server.security_groups
returns(1) { found_groups.length }
group = found_groups.first
@ -40,6 +40,47 @@ Shindo.tests("Fog::Compute[:openstack] | server", ['openstack']) do
my_group.destroy if my_group
end
end
tests('#metadata').succeeds do
fog = Fog::Compute[:openstack]
begin
flavor = fog.flavors.first.id
image = fog.images.first.id
server = fog.servers.new(:name => 'test server',
:metadata => {"foo" => "bar"},
:flavor_ref => flavor,
:image_ref => image)
server.save
returns(1) { server.metadata.length }
server.metadata.each do |datum|
datum.value = 'foo'
datum.save
datum.destroy
end
ensure
unless Fog.mocking? then
server.destroy if server
begin
fog.servers.get(server.id).wait_for do false end
rescue Fog::Errors::Error
# ignore, server went away
end
end
end
end
end
end