mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
Add metadata models support. Also, include metadata attribute in server and image models, to manage metadata.
This commit is contained in:
parent
c99fcc871c
commit
01008d2c46
6 changed files with 176 additions and 5 deletions
|
@ -17,8 +17,8 @@ module Fog
|
|||
collection :images
|
||||
model :key_pair
|
||||
collection :key_pairs
|
||||
#model :meta
|
||||
#collection :metadata
|
||||
model :meta
|
||||
collection :metadata
|
||||
model :security_group
|
||||
collection :security_groups
|
||||
model :server
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
require 'fog/core/model'
|
||||
require 'fog/openstack/models/compute/metadata'
|
||||
|
||||
module Fog
|
||||
module Compute
|
||||
|
@ -15,10 +16,30 @@ module Fog
|
|||
attribute :status
|
||||
attribute :minDisk, :aliases => 'min_disk'
|
||||
attribute :minRam, :aliases => 'min_ram'
|
||||
attribute :server, :aliases => 'server'
|
||||
#attribute :metadata #TODO: Need to add it back when Metadata API is done
|
||||
attribute :server
|
||||
attribute :metadata
|
||||
attribute :links
|
||||
|
||||
def initialize(attributes)
|
||||
@connection = attributes[:connection]
|
||||
super
|
||||
end
|
||||
|
||||
def metadata
|
||||
@metadata ||= begin
|
||||
Fog::Compute::HP::Metadata.new({
|
||||
:connection => connection,
|
||||
:parent => self
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
def metadata=(new_metadata={})
|
||||
metas = []
|
||||
new_metadata.each_pair {|k,v| metas << {"key" => k, "value" => v} }
|
||||
metadata.load(metas)
|
||||
end
|
||||
|
||||
def destroy
|
||||
requires :id
|
||||
|
||||
|
|
29
lib/fog/hp/models/compute/meta.rb
Normal file
29
lib/fog/hp/models/compute/meta.rb
Normal file
|
@ -0,0 +1,29 @@
|
|||
require 'fog/core/model'
|
||||
require 'fog/hp/models/meta_parent'
|
||||
|
||||
module Fog
|
||||
module Compute
|
||||
class HP
|
||||
class Meta < Fog::Model
|
||||
|
||||
include Fog::Compute::HP::MetaParent
|
||||
|
||||
identity :key
|
||||
attribute :value
|
||||
|
||||
def destroy
|
||||
requires :identity
|
||||
connection.delete_meta(collection_name, @parent.id, key)
|
||||
true
|
||||
end
|
||||
|
||||
def save
|
||||
requires :identity, :value
|
||||
connection.update_meta(collection_name, @parent.id, key, value)
|
||||
true
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
69
lib/fog/hp/models/compute/metadata.rb
Normal file
69
lib/fog/hp/models/compute/metadata.rb
Normal file
|
@ -0,0 +1,69 @@
|
|||
require 'fog/core/collection'
|
||||
require 'fog/hp/models/meta_parent'
|
||||
require 'fog/hp/models/compute/meta'
|
||||
require 'fog/hp/models/compute/image'
|
||||
require 'fog/hp/models/compute/server'
|
||||
|
||||
module Fog
|
||||
module Compute
|
||||
class HP
|
||||
|
||||
class Metadata < Fog::Collection
|
||||
|
||||
model Fog::Compute::HP::Meta
|
||||
|
||||
include Fog::Compute::HP::MetaParent
|
||||
|
||||
def all
|
||||
requires :parent
|
||||
metadata = connection.list_metadata(collection_name, @parent.id).body['metadata']
|
||||
metas = []
|
||||
metadata.each_pair {|k,v| metas << {"key" => k, "value" => v} }
|
||||
load(metas)
|
||||
end
|
||||
|
||||
def get(key)
|
||||
requires :parent
|
||||
data = connection.get_meta(collection_name, @parent.id, key).body["meta"]
|
||||
metas = []
|
||||
data.each_pair {|k,v| metas << {"key" => k, "value" => v} }
|
||||
new(metas[0])
|
||||
rescue Fog::Compute::HP::NotFound
|
||||
nil
|
||||
end
|
||||
|
||||
def update(data=nil)
|
||||
requires :parent
|
||||
connection.update_metadata(collection_name, @parent.id, meta_hash(data))
|
||||
end
|
||||
|
||||
def set(data=nil)
|
||||
requires :parent
|
||||
connection.set_metadata(collection_name, @parent.id, meta_hash(data))
|
||||
end
|
||||
|
||||
def new(attributes = {})
|
||||
requires :parent
|
||||
super({ :parent => @parent }.merge!(attributes))
|
||||
end
|
||||
|
||||
private
|
||||
def meta_hash(data=nil)
|
||||
if data.nil?
|
||||
data={}
|
||||
self.each do |meta|
|
||||
if meta.is_a?(Fog::Compute::HP::Meta) then
|
||||
data.store(meta.key, meta.value)
|
||||
else
|
||||
data.store(meta["key"], meta["value"])
|
||||
end
|
||||
end
|
||||
end
|
||||
data
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,4 +1,5 @@
|
|||
require 'fog/compute/models/server'
|
||||
require 'fog/hp/models/compute/metadata'
|
||||
|
||||
module Fog
|
||||
module Compute
|
||||
|
@ -38,6 +39,7 @@ module Fog
|
|||
# assign these attributes first to prevent race condition with new_record?
|
||||
self.min_count = attributes.delete(:min_count)
|
||||
self.max_count = attributes.delete(:max_count)
|
||||
@connection = attributes[:connection]
|
||||
super
|
||||
end
|
||||
|
||||
|
@ -46,6 +48,21 @@ module Fog
|
|||
connection.get_console_output(id, num_lines)
|
||||
end
|
||||
|
||||
def metadata
|
||||
@metadata ||= begin
|
||||
Fog::Compute::HP::Metadata.new({
|
||||
:connection => connection,
|
||||
:parent => self
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
def metadata=(new_metadata={})
|
||||
metas = []
|
||||
new_metadata.each_pair {|k,v| metas << {"key" => k, "value" => v} }
|
||||
metadata.load(metas)
|
||||
end
|
||||
|
||||
def destroy
|
||||
requires :id
|
||||
connection.delete_server(id)
|
||||
|
@ -178,8 +195,10 @@ module Fog
|
|||
def save
|
||||
raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if identity
|
||||
requires :flavor_id, :image_id, :name
|
||||
meta_hash = {}
|
||||
metadata.each { |meta| meta_hash.store(meta.key, meta.value) }
|
||||
options = {
|
||||
'metadata' => metadata,
|
||||
'metadata' => meta_hash,
|
||||
'personality' => personality,
|
||||
'accessIPv4' => accessIPv4,
|
||||
'accessIPv6' => accessIPv6,
|
||||
|
|
33
lib/fog/hp/models/meta_parent.rb
Normal file
33
lib/fog/hp/models/meta_parent.rb
Normal file
|
@ -0,0 +1,33 @@
|
|||
module Fog
|
||||
module Compute
|
||||
class HP
|
||||
module MetaParent
|
||||
|
||||
def parent
|
||||
@parent
|
||||
end
|
||||
|
||||
def parent=(new_parent)
|
||||
@parent = new_parent
|
||||
end
|
||||
|
||||
def collection_name
|
||||
if @parent.class == Fog::Compute::HP::Image
|
||||
return "images"
|
||||
elsif @parent.class == Fog::Compute::HP::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.store(meta.key, meta.value) }
|
||||
hash
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue