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

changed metadata implementation for Fog::Compute::RackspaceV2::Server and Fog::Compute::RackspaceV2::Image

This commit is contained in:
Kyle Rames 2013-01-07 11:10:17 -06:00
parent 7ccdc04050
commit 4116970d66
6 changed files with 219 additions and 4 deletions

View file

@ -21,9 +21,28 @@ module Fog
attribute :progress
attribute :minDisk
attribute :minRam
attribute :metadata
attribute :disk_config, :aliases => 'OS-DCF:diskConfig'
attribute :links
def initialize(attributes={})
@connection = attributes[:connection]
super
end
def metadata
raise "Please save image before accessing metadata" unless identity
@metadata ||= begin
Fog::Compute::RackspaceV2::Metadata.new({
:connection => connection,
:parent => self
})
end
end
def metadata=(hash={})
raise "Please save image before accessing metadata" unless identity
metadata.from_hash(hash)
end
def ready?
state == ACTIVE

View file

@ -0,0 +1,33 @@
module Fog
module Compute
class RackspaceV2
module MetaParent
def parent
@parent
end
def parent=(new_parent)
@parent = new_parent
end
def collection_name
if parent.class == Fog::Compute::RackspaceV2::Image
return "images"
elsif parent.class == Fog::Compute::RackspaceV2::Server
return "servers"
else
raise "Metadata is not supported for this model type."
end
end
def metas_to_hash(metas)
hash = {}
metas.each { |meta| hash[meta.key] = meta.value }
hash
end
end
end
end
end

View file

@ -0,0 +1,75 @@
require 'fog/core/collection'
require 'fog/rackspace/models/compute_v2/meta_parent'
require 'fog/rackspace/models/compute_v2/metadatum'
require 'fog/rackspace/models/compute_v2/image'
require 'fog/rackspace/models/compute_v2/server'
module Fog
module Compute
class RackspaceV2
class Metadata < Fog::Collection
model Fog::Compute::RackspaceV2::Metadatum
include Fog::Compute::RackspaceV2::MetaParent
def all
requires :parent
data = connection.list_metadata(collection_name, parent.id).body['metadata']
from_hash(data)
end
def get(key)
requires :parent
data = connection.get_metadata_item(collection_name, parent.id, key).body["meta"]
datum = data.first
new(:key => datum[0], :value => datum[1])
rescue Fog::Compute::RackspaceV2::NotFound
nil
end
def [](key)
return nil unless key
datum = self.find {|datum| datum.key == key || datum.key == key.to_sym }
datum ? datum.value : nil
end
def []=(key, value)
return nil unless key
datum = self.find {|datum| datum.key == key || datum.key == key.to_sym }
if datum
data.value = value
else
self << Fog::Compute::RackspaceV2::Metadatum.new(:key => key, :value => value, :connection => connection, :parent => parent)
end
value
end
def save
requires :parent
connection.set_metadata(collection_name, parent.id, to_hash)
end
def new(attributes = {})
requires :parent
super({ :parent => parent }.merge!(attributes))
end
def from_hash(hash)
return unless hash
metas = []
hash.each_pair {|k,v| metas << {:key => k, :value => v} }
load(metas)
end
def to_hash
h = {}
self.each { |datum| h[datum.key] = datum.value }
h
end
end
end
end
end

View file

@ -0,0 +1,29 @@
require 'fog/core/model'
require 'fog/rackspace/models/compute_v2/meta_parent'
module Fog
module Compute
class RackspaceV2
class Metadatum < Fog::Model
include Fog::Compute::RackspaceV2::MetaParent
identity :key
attribute :value
def destroy
requires :identity
connection.delete_metadata_item(collection_name, parent.id, key)
true
end
def save
requires :identity, :value
connection.set_metadata_item(collection_name, parent.id, key, value)
true
end
end
end
end
end

View file

@ -1,4 +1,5 @@
require 'fog/compute/models/server'
require 'fog/rackspace/models/compute_v2/metadata'
module Fog
module Compute
@ -32,7 +33,6 @@ module Fog
attribute :user_id
attribute :tenant_id
attribute :links
attribute :metadata
attribute :personality
attribute :ipv4_address, :aliases => 'accessIPv4'
attribute :ipv6_address, :aliases => 'accessIPv6'
@ -42,7 +42,26 @@ module Fog
attribute :flavor_id, :aliases => 'flavor', :squash => 'id'
attribute :image_id, :aliases => 'image', :squash => 'id'
attr_reader :password
attr_reader :password
def initialize(attributes={})
@connection = attributes[:connection]
super
end
def metadata
raise "Please save server before accessing metadata" unless identity
@metadata ||= begin
Fog::Compute::RackspaceV2::Metadata.new({
:connection => connection,
:parent => self
})
end
end
def metadata=(hash={})
raise "Please save server before accessing metadata" unless identity
metadata.from_hash(hash)
end
def save
if identity
@ -58,7 +77,7 @@ module Fog
options = {}
options[:disk_config] = disk_config unless disk_config.nil?
options[:metadata] = metadata unless metadata.nil?
options[:metadata] = metadata unless @metadata.nil?
options[:personality] = personality unless personality.nil?
data = connection.create_server(name, image_id, flavor_id, 1, 1, options)

View file

@ -0,0 +1,40 @@
Shindo.tests('Fog::Compute::RackspaceV2 | metadata', ['rackspace']) do
pending if Fog.mocking?
service = Fog::Compute::RackspaceV2.new
test_time = Time.now.to_i.to_s
tests('success') do
begin
@server = service.servers.create(:name => "fog_server_#{test_time}", :flavor_id => 2, :image_id => "3afe97b2-26dc-49c5-a2cc-a2fc8d80c001")
@server.wait_for(timeout=1500) { ready? }
@server = service.servers.get "2ee0e1b5-3350-40ae-873a-fff4941cc400"
tests('server') do
collection_tests(@server.metadata, {:key => 'my_key', :value => 'my_value'}) do
@server.wait_for { ready? }
end
end
tests('image') do
image_id = @server.create_image("fog_image_#{test_time}", :metadata => {:my_key => 'my_value'})
@image = service.images.get image_id
@image.wait_for(timeout = 1500) { ready? }
tests("#all").succeeds do
pending if Fog.mocking? && !mocks_implemented
@image.metadata.all
end
tests("#get('my_key')").succeeds do
pending if Fog.mocking? && !mocks_implemented
@image.metadata.get('my_key')
end
end
ensure
@image.destroy if @image
@server.destroy if @server
end
end
end