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

Merge pull request #1597 from dprince/fix_openstack_metadata

OpenStack Compute: Fix server model metadata
This commit is contained in:
Dan Prince 2013-02-26 15:10:34 -08:00
commit bef47ce960
5 changed files with 138 additions and 6 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)
@ -241,10 +241,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,
@ -256,6 +253,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

@ -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