mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
tags implemented
This commit is contained in:
parent
28eb4db402
commit
1371c15daf
10 changed files with 339 additions and 0 deletions
|
@ -74,6 +74,8 @@ module Fog
|
|||
collection :disks
|
||||
model :vm_network
|
||||
collection :vm_networks
|
||||
model :tag # this is called metadata in vcloud
|
||||
collection :tags
|
||||
|
||||
request_path 'fog/vcloudng/requests/compute'
|
||||
request :get_organizations
|
||||
|
@ -98,6 +100,10 @@ module Fog
|
|||
request :put_vm_disks
|
||||
request :get_vm_network
|
||||
request :put_vm_network
|
||||
request :get_vm_metadata
|
||||
request :post_vm_metadata
|
||||
request :put_vm_metadata_value
|
||||
request :delete_vm_metadata
|
||||
request :get_request
|
||||
request :get_href
|
||||
|
||||
|
|
73
lib/fog/vcloudng/generators/compute/metadata.rb
Normal file
73
lib/fog/vcloudng/generators/compute/metadata.rb
Normal file
|
@ -0,0 +1,73 @@
|
|||
#
|
||||
#
|
||||
# {:metadata=>{"buenas si"=>"no tanto ya", "hola"=>"adios"},
|
||||
# :type=>"application/vnd.vmware.vcloud.metadata+xml",
|
||||
# :href=>
|
||||
# "https://devlab.mdsol.com/api/vApp/vm-18545e82-d919-4071-ae7e-d1300d9d8112/metadata",
|
||||
# :id=>"vm-18545e82-d919-4071-ae7e-d1300d9d8112"}
|
||||
#
|
||||
# <Metadata xmlns="http://www.vmware.com/vcloud/v1.5" type="application/vnd.vmware.vcloud.metadata+xml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.vmware.com/vcloud/v1.5 http://10.194.1.65/api/v1.5/schema/master.xsd">
|
||||
# <MetadataEntry>
|
||||
# <Key>buenas si</Key>
|
||||
# <Value>no tanto ya</Value>
|
||||
# </MetadataEntry>
|
||||
# <MetadataEntry">
|
||||
# <Key>hola</Key>
|
||||
# <Value>adios</Value>
|
||||
# </MetadataEntry>
|
||||
# </Metadata>
|
||||
|
||||
module Fog
|
||||
module Generators
|
||||
module Compute
|
||||
module Vcloudng
|
||||
|
||||
class Metadata
|
||||
|
||||
attr_reader :attrs
|
||||
|
||||
def initialize(attrs={})
|
||||
@attrs = attrs
|
||||
end
|
||||
|
||||
def generate_xml
|
||||
output = ""
|
||||
output << header
|
||||
attrs[:metadata].each_pair do |k,v|
|
||||
output << metadata_entry(k,v)
|
||||
end
|
||||
output << tail
|
||||
output
|
||||
end
|
||||
|
||||
def add_item(k,v)
|
||||
@attrs[:metadata].merge!(Hash[k,v])
|
||||
end
|
||||
|
||||
def header
|
||||
'<Metadata xmlns="http://www.vmware.com/vcloud/v1.5"
|
||||
type="application/vnd.vmware.vcloud.metadata+xml"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.vmware.com/vcloud/v1.5 http://10.194.1.65/api/v1.5/schema/master.xsd">
|
||||
'
|
||||
end
|
||||
|
||||
|
||||
def metadata_entry(key,value)
|
||||
body = <<EOF
|
||||
<MetadataEntry>
|
||||
<Key>#{key}</Key>
|
||||
<Value>#{value}</Value>
|
||||
</MetadataEntry>
|
||||
EOF
|
||||
end
|
||||
|
||||
def tail
|
||||
'</Metadata>'
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
49
lib/fog/vcloudng/models/compute/tag.rb
Normal file
49
lib/fog/vcloudng/models/compute/tag.rb
Normal file
|
@ -0,0 +1,49 @@
|
|||
require 'fog/core/model'
|
||||
|
||||
module Fog
|
||||
module Compute
|
||||
class Vcloudng
|
||||
|
||||
class Tag < Fog::Model
|
||||
|
||||
|
||||
identity :id, :aliases => :key
|
||||
attribute :value
|
||||
|
||||
|
||||
def save
|
||||
if value_changed?
|
||||
puts "Debug: change the value from #{attributes[:old_value]} to #{attributes[:value]}"
|
||||
set_value(value)
|
||||
attributes[:value_task].wait_for { :ready? }
|
||||
end
|
||||
end
|
||||
|
||||
def value=(new_value)
|
||||
attributes[:old_value] ||= attributes[:value]
|
||||
attributes[:value] = new_value
|
||||
end
|
||||
|
||||
def value_changed?
|
||||
return false unless attributes[:old_value]
|
||||
attributes[:value] != attributes[:old_value]
|
||||
end
|
||||
|
||||
def set_value(new_value)
|
||||
response = service.put_vm_metadata_value(attributes[:vm_id], id, value)
|
||||
task = response.body
|
||||
task[:id] = task[:href].split('/').last
|
||||
attributes[:value_task] = service.tasks.new(task)
|
||||
end
|
||||
|
||||
def destroy
|
||||
response = service.delete_vm_metadata(attributes[:vm_id], id)
|
||||
task = response.body
|
||||
task[:id] = task[:href].split('/').last
|
||||
attributes[:destroy_tag_task] = service.tasks.new(task)
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
47
lib/fog/vcloudng/models/compute/tags.rb
Normal file
47
lib/fog/vcloudng/models/compute/tags.rb
Normal file
|
@ -0,0 +1,47 @@
|
|||
require 'fog/core/collection'
|
||||
require 'fog/vcloudng/models/compute/tag'
|
||||
|
||||
module Fog
|
||||
module Compute
|
||||
class Vcloudng
|
||||
|
||||
class Tags < Fog::Collection
|
||||
model Fog::Compute::Vcloudng::Tag
|
||||
|
||||
attribute :vm_id
|
||||
|
||||
def index
|
||||
tags.keys.map{ |key| new({key: key, value: tags[key] }.merge(vm_id: vm_id))}
|
||||
end
|
||||
|
||||
def all
|
||||
index
|
||||
end
|
||||
|
||||
def get(tag_id)
|
||||
tag = tags.detect{ |tag| tag.keys.first == tag_id }
|
||||
return nil unless tag
|
||||
new(tag.merge(vm_id: vm_id))
|
||||
end
|
||||
|
||||
def create(key,value)
|
||||
tags unless @tags
|
||||
data = Fog::Generators::Compute::Vcloudng::Metadata.new(@tags)
|
||||
data.add_item(key,value)
|
||||
response = service.post_vm_metadata(vm_id, data.attrs)
|
||||
task = response.body
|
||||
task[:id] = task[:href].split('/').last
|
||||
attributes[:crate_tag_task] = service.tasks.new(task)
|
||||
end
|
||||
|
||||
# private
|
||||
|
||||
def tags
|
||||
@tags = service.get_vm_metadata(vm_id).body
|
||||
@tags[:metadata]
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -35,6 +35,11 @@ module Fog
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
def tags
|
||||
requires :id
|
||||
service.tags(:vm_id => id)
|
||||
end
|
||||
|
||||
def customization
|
||||
data = service.get_vm_customization(id).body
|
||||
|
|
66
lib/fog/vcloudng/parsers/compute/metadata.rb
Normal file
66
lib/fog/vcloudng/parsers/compute/metadata.rb
Normal file
|
@ -0,0 +1,66 @@
|
|||
#
|
||||
# <Metadata xmlns="http://www.vmware.com/vcloud/v1.5" type="application/vnd.vmware.vcloud.metadata+xml" href="https://devlab.mdsol.com/api/vApp/vm-18545e82-d919-4071-ae7e-d1300d9d8112/metadata" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.vmware.com/vcloud/v1.5 http://10.194.1.65/api/v1.5/schema/master.xsd">
|
||||
# <Link rel="up" type="application/vnd.vmware.vcloud.vm+xml" href="https://devlab.mdsol.com/api/vApp/vm-18545e82-d919-4071-ae7e-d1300d9d8112"/>
|
||||
# <Link rel="add" type="application/vnd.vmware.vcloud.metadata+xml" href="https://devlab.mdsol.com/api/vApp/vm-18545e82-d919-4071-ae7e-d1300d9d8112/metadata"/>
|
||||
# <MetadataEntry type="application/vnd.vmware.vcloud.metadata.value+xml" href="https://devlab.mdsol.com/api/vApp/vm-18545e82-d919-4071-ae7e-d1300d9d8112/metadata/buenas%20si">
|
||||
# <Link rel="up" type="application/vnd.vmware.vcloud.metadata+xml" href="https://devlab.mdsol.com/api/vApp/vm-18545e82-d919-4071-ae7e-d1300d9d8112/metadata"/>
|
||||
# <Link rel="edit" type="application/vnd.vmware.vcloud.metadata.value+xml" href="https://devlab.mdsol.com/api/vApp/vm-18545e82-d919-4071-ae7e-d1300d9d8112/metadata/buenas%20si"/>
|
||||
# <Link rel="remove" href="https://devlab.mdsol.com/api/vApp/vm-18545e82-d919-4071-ae7e-d1300d9d8112/metadata/buenas%20si"/>
|
||||
# <Key>buenas si</Key>
|
||||
# <Value>no tanto ya</Value>
|
||||
# </MetadataEntry>
|
||||
# <MetadataEntry type="application/vnd.vmware.vcloud.metadata.value+xml" href="https://devlab.mdsol.com/api/vApp/vm-18545e82-d919-4071-ae7e-d1300d9d8112/metadata/hola">
|
||||
# <Link rel="up" type="application/vnd.vmware.vcloud.metadata+xml" href="https://devlab.mdsol.com/api/vApp/vm-18545e82-d919-4071-ae7e-d1300d9d8112/metadata"/>
|
||||
# <Link rel="edit" type="application/vnd.vmware.vcloud.metadata.value+xml" href="https://devlab.mdsol.com/api/vApp/vm-18545e82-d919-4071-ae7e-d1300d9d8112/metadata/hola"/>
|
||||
# <Link rel="remove" href="https://devlab.mdsol.com/api/vApp/vm-18545e82-d919-4071-ae7e-d1300d9d8112/metadata/hola"/>
|
||||
# <Key>hola</Key>
|
||||
# <Value>adios</Value>
|
||||
# </MetadataEntry>
|
||||
# </Metadata>
|
||||
#
|
||||
#
|
||||
# {:metadata=>{"buenas si"=>"no tanto ya", "hola"=>"adios"},
|
||||
# :type=>"application/vnd.vmware.vcloud.metadata+xml",
|
||||
# :href=>
|
||||
# "https://devlab.mdsol.com/api/vApp/vm-18545e82-d919-4071-ae7e-d1300d9d8112/metadata",
|
||||
# :id=>"vm-18545e82-d919-4071-ae7e-d1300d9d8112"}
|
||||
#
|
||||
module Fog
|
||||
module Parsers
|
||||
module Compute
|
||||
module Vcloudng
|
||||
|
||||
class Metadata < VcloudngParser
|
||||
|
||||
def reset
|
||||
@response = { :metadata => {} }
|
||||
end
|
||||
|
||||
def start_element(name, attributes)
|
||||
super
|
||||
case name
|
||||
when 'Metadata'
|
||||
metadata = extract_attributes(attributes)
|
||||
@response[:type] = metadata['type']
|
||||
@response[:href] = metadata['href']
|
||||
@response[:id] = @response[:href].split('/')[-2]
|
||||
end
|
||||
end
|
||||
|
||||
def end_element(name)
|
||||
case name
|
||||
when 'Key'
|
||||
@key = value
|
||||
when 'Value'
|
||||
@val = value
|
||||
when 'MetadataEntry'
|
||||
@response[:metadata].merge!(Hash[@key, @val])
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
21
lib/fog/vcloudng/requests/compute/delete_vm_metadata.rb
Normal file
21
lib/fog/vcloudng/requests/compute/delete_vm_metadata.rb
Normal file
|
@ -0,0 +1,21 @@
|
|||
module Fog
|
||||
module Compute
|
||||
class Vcloudng
|
||||
class Real
|
||||
|
||||
def delete_vm_metadata(vm_id, metadata_key)
|
||||
require 'fog/vcloudng/parsers/compute/metadata'
|
||||
|
||||
request(
|
||||
:expects => 202,
|
||||
:headers => { 'Accept' => 'application/*+xml;version=1.5' },
|
||||
:method => 'DELETE',
|
||||
:parser => Fog::ToHashDocument.new,
|
||||
:path => "vApp/#{vm_id}/metadata/#{URI.escape(metadata_key)}"
|
||||
)
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
21
lib/fog/vcloudng/requests/compute/get_vm_metadata.rb
Normal file
21
lib/fog/vcloudng/requests/compute/get_vm_metadata.rb
Normal file
|
@ -0,0 +1,21 @@
|
|||
module Fog
|
||||
module Compute
|
||||
class Vcloudng
|
||||
class Real
|
||||
|
||||
def get_vm_metadata(vm_id)
|
||||
require 'fog/vcloudng/parsers/compute/metadata'
|
||||
|
||||
request(
|
||||
:expects => 200,
|
||||
:headers => { 'Accept' => 'application/*+xml;version=1.5' },
|
||||
:method => 'GET',
|
||||
:parser => Fog::Parsers::Compute::Vcloudng::Metadata.new,
|
||||
:path => "vApp/#{vm_id}/metadata/"
|
||||
)
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
25
lib/fog/vcloudng/requests/compute/post_vm_metadata.rb
Normal file
25
lib/fog/vcloudng/requests/compute/post_vm_metadata.rb
Normal file
|
@ -0,0 +1,25 @@
|
|||
module Fog
|
||||
module Compute
|
||||
class Vcloudng
|
||||
class Real
|
||||
|
||||
require 'fog/vcloudng/generators/compute/metadata'
|
||||
|
||||
def post_vm_metadata(vm_id, metadata={})
|
||||
|
||||
data = Fog::Generators::Compute::Vcloudng::Metadata.new(metadata)
|
||||
|
||||
request(
|
||||
:body => data.generate_xml,
|
||||
:expects => 202,
|
||||
:headers => { 'Content-Type' => "application/vnd.vmware.vcloud.metadata+xml",
|
||||
'Accept' => 'application/*+xml;version=1.5' },
|
||||
:method => 'POST',
|
||||
:parser => Fog::ToHashDocument.new,
|
||||
:path => "vApp/#{vm_id}/metadata/"
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
26
lib/fog/vcloudng/requests/compute/put_vm_metadata_value.rb
Normal file
26
lib/fog/vcloudng/requests/compute/put_vm_metadata_value.rb
Normal file
|
@ -0,0 +1,26 @@
|
|||
module Fog
|
||||
module Compute
|
||||
class Vcloudng
|
||||
class Real
|
||||
|
||||
def put_vm_metadata_value(vm_id, metadata_key, metadata_value)
|
||||
body="
|
||||
<MetadataValue xmlns=\"http://www.vmware.com/vcloud/v1.5\">
|
||||
<Value>#{metadata_value}</Value>
|
||||
</MetadataValue>"
|
||||
|
||||
|
||||
request(
|
||||
:body => body,
|
||||
:expects => 202,
|
||||
:headers => { 'Content-Type' => "application/vnd.vmware.vcloud.metadata.value+xml",
|
||||
'Accept' => 'application/*+xml;version=1.5' },
|
||||
:method => 'PUT',
|
||||
:parser => Fog::ToHashDocument.new,
|
||||
:path => "vApp/#{vm_id}/metadata/#{URI.escape(metadata_key)}"
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Reference in a new issue