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

[vcloud_director] New & refactored XML generators.

This commit is contained in:
Nick Osborn 2013-10-03 19:07:58 +01:00
parent 788fef9d15
commit 2b4ea80024
19 changed files with 438 additions and 65 deletions

View file

@ -4,32 +4,51 @@ module Fog
module VcloudDirector
class Base
attr_reader :options
attr_reader :builder, :data, :root_attributes
def initialize(options)
@options = options
# @param [Hash] data
def initialize(data={})
@data = data
@root_attributes = {}
end
# @return [String]
def to_xml
builder.to_xml(:ident => 0)
build
builder.doc.root.to_xml(:ident => 0)
end
# @return [String]
def pretty_xml
builder.to_xml
build
builder.doc.root.to_xml
end
# @api private
# @return [Nokogiri::XML::Document]
def doc
build
builder.doc
end
protected
# @api private
def builder
def build
@root_attributes.delete_if {|k, v| v.nil?}
@builder ||= Nokogiri::XML::Builder.new do |xml|
xml.send(self.class.to_s.split('::').last, @root_attributes) do |x|
inner_build(x)
end
end
end
def with(root)
@root_attributes.delete_if {|k, v| v.nil?}
@builder ||= Nokogiri::XML::Builder.with(root) do |xml|
xml.send(self.class.to_s.split('::').last, @root_attributes) do |x|
inner_build(x)
end
end
end
end

View file

@ -2,33 +2,30 @@ module Fog
module Generators
module Compute
module VcloudDirector
require 'fog/vcloud_director/generators/compute/base'
require 'fog/vcloud_director/generators/compute/params_type'
attr_reader :source
# Parameters for a captureVapp request.
# @see http://pubs.vmware.com/vcd-51/topic/com.vmware.vcloud.api.reference.doc_51/doc/types/CaptureVAppParamsType.html
# vCloud API Documentation
class CaptureVappParams < Base
class CaptureVAppParams < ParamsType
attr_reader :name, :source_href
def initialize(name, source_href, options={})
super options
@name = name
@source_href = source_href
# @params [Hash] data
def initialize(source_href, data={})
data[:source] = {:href => source_href}
super data
@root_attributes[:'xmlns:ovf'] = 'http://schemas.dmtf.org/ovf/envelope/1'
@source = {:href => source_href}
end
# @api private
# @return [Nokogiri::XML::Builder]
def builder
@builder ||= Nokogiri::XML::Builder.new do |xml|
attrs = {:xmlns => 'http://www.vmware.com/vcloud/v1.5', :name => name}
xml.CaptureVAppParams(attrs) {
xml.Description options[:Description] || ''
xml.Source(:href => source_href)
}
end
end
protected
def inner_build(xml)
super
xml.Source(:href => data.delete(:source)[:href])
#xml.VdcStorageProfile(:href => source_href) # since 5.1
end
end
end
end

View file

@ -0,0 +1,32 @@
module Fog
module Generators
module Compute
module VcloudDirector
require 'fog/vcloud_director/generators/compute/entity'
# Contains a reference to a VappTemplate or Media object and related metadata.
# @see http://pubs.vmware.com/vcd-51/topic/com.vmware.vcloud.api.reference.doc_51/doc/types/CatalogItemType.html
# vCloud API Documentation
class CatalogItem < Entity
attr_reader :entity
# @param [String] name The name of the entity.
# @param [String] entity_href Contains the URI to the entity.
# @param [Hash] data
def initialize(name, entity_href, data={})
super name, data
@root_attributes[:status] = data.delete(:status)
@entity = {:href => entity_href}
end
protected
def inner_build(xml)
xml.Entity(entity)
end
end
end
end
end
end

View file

@ -0,0 +1,31 @@
module Fog
module Generators
module Compute
module VcloudDirector
require 'fog/vcloud_director/generators/compute/identifiable_resource_type'
# Basic entity type in the vCloud object model. Includes a name, an
# optional description, and an optional list of links.
# @see http://pubs.vmware.com/vcd-51/topic/com.vmware.vcloud.api.reference.doc_51/doc/types/EntityType.html
# vCloud API Documentation
class Entity < IdentifiableResourceType
# @param [String] name The name of the entity.
# @param [Hash] data
def initialize(name, data={})
super data
@root_attributes[:name] = name
end
protected
def inner_build(xml)
super
xml.Description data.delete(:Description)
end
end
end
end
end
end

View file

@ -0,0 +1,25 @@
module Fog
module Generators
module Compute
module VcloudDirector
require 'fog/vcloud_director/generators/compute/resource_type'
# The base type for all resource types which contain an id attribute.
# @see http://pubs.vmware.com/vcd-51/topic/com.vmware.vcloud.api.reference.doc_51/doc/types/IdentifiableResourceType.html
# vCloud API Documentation
class IdentifiableResourceType < ResourceType
# @param [Hash] data
# @option data [String] :href The URI of the entity.
# @option data [String] :type The MIME type of the entity.
# @option data [String] :operationKey Optional unique identifier to
# support idempotent semantics for create and delete operations.
def initialize(data={})
super
@root_attributes[:operationKey] = data.delete(:operationKey) # since 5.1
end
end
end
end
end
end

View file

@ -0,0 +1,27 @@
module Fog
module Generators
module Compute
module VcloudDirector
require 'fog/vcloud_director/generators/compute/resource_entity'
# Represents a Media object.
# @see http://pubs.vmware.com/vcd-51/topic/com.vmware.vcloud.api.reference.doc_51/doc/types/MediaType.html
# vCloud API Documentation
class Media < ResourceEntity
# @param [String] name
# @param [String] image_type
# @param [Integer] size
# @param [Hash] data
def initialize(name, image_type, size, data={})
super name, data
@root_attributes[:name] = name
@root_attributes[:imageType] = image_type
@root_attributes[:size] = size
end
end
end
end
end
end

View file

@ -0,0 +1,33 @@
module Fog
module Generators
module Compute
module VcloudDirector
require 'fog/vcloud_director/generators/compute/base'
# A basic type used to specify request parameters.
# @see http://pubs.vmware.com/vcd-51/topic/com.vmware.vcloud.api.reference.doc_51/doc/types/ParamsType.html
# vCloud API Documentation
class ParamsType < Base
# @param [Hash] data
# @option data [String] :name Typically used to name or identify the
# subject of the request. For example, the name of the object being
# created or modified.
# @option data [String] :Description Optional description.
def initialize(data={})
super
@root_attributes[:xmlns] = 'http://www.vmware.com/vcloud/v1.5'
@root_attributes[:name] = data.delete(:name)
end
protected
def inner_build(xml)
xml.Description data.delete(:Description)
end
end
end
end
end
end

View file

@ -0,0 +1,16 @@
module Fog
module Generators
module Compute
module VcloudDirector
require 'fog/vcloud_director/generators/compute/entity'
# Base type that represents a resource entity such as a vApp template
# or virtual media.
# @see http://pubs.vmware.com/vcd-51/topic/com.vmware.vcloud.api.reference.doc_51/doc/types/ResourceEntityType.html
# vCloud API Documentation
class ResourceEntity < Entity
end
end
end
end
end

View file

@ -0,0 +1,31 @@
module Fog
module Generators
module Compute
module VcloudDirector
require 'fog/vcloud_director/generators/compute/base'
# The base type for all objects in the vCloud model. Has an optional
# list of links and href and type attributes.
# @see http://pubs.vmware.com/vcd-51/topic/com.vmware.vcloud.api.reference.doc_51/doc/types/ResourceType.html
# vCloud API Documentation
class ResourceType < Base
# @param [Hash] data
# @option data [String] :href The URI of the entity.
# @option data [String] :data The MIME type of the entity.
def initialize(data={})
super
@root_attributes[:xmlns] = 'http://www.vmware.com/vcloud/v1.5'
@root_attributes[:href] = data.delete(:href)
@root_attributes[:type] = data.delete(:type)
end
protected
def inner_build(xml)
end
end
end
end
end
end

View file

@ -2,22 +2,18 @@ module Fog
module Generators
module Compute
module VcloudDirector
require 'fog/vcloud_director/generators/compute/base'
require 'fog/vcloud_director/generators/compute/params_type'
# Parameters to an undeploy vApp request.
# @see http://pubs.vmware.com/vcd-51/topic/com.vmware.vcloud.api.reference.doc_51/doc/types/UndeployVAppParamsType.html
# vCloud API Documentation
class UndeployVappParams < Base
class UndeployVAppParams < ParamsType
# @api private
# @return [Nokogiri::XML::Builder]
def builder
@builder ||= Nokogiri::XML::Builder.new do |xml|
xml.UndeployVAppParams(:xmlns => 'http://www.vmware.com/vcloud/v1.5') {
if options.has_key?(:UndeployPowerAction)
xml.UndeployPowerAction options[:UndeployPowerAction]
end
}
protected
def inner_build(xml)
if data.has_key?(:UndeployPowerAction)
xml.UndeployPowerAction data.delete(:UndeployPowerAction)
end
end

View file

@ -2,6 +2,8 @@ module Fog
module Compute
class VcloudDirector
class Real
require 'fog/vcloud_director/generators/compute/media.rb'
# Upload a media image.
#
# The response includes an upload link for the media image.
@ -18,16 +20,10 @@ module Fog
# vCloud API Documentation
# @since vCloud API version 0.9
def post_upload_media(vdc_id, name, image_type, size, options={})
body = <<-END
<Media xmlns="http://www.vmware.com/vcloud/v1.5"
name="#{name}"
imageType="#{image_type}"
size="#{size}">
</Media>
END
body = Fog::Generators::Compute::VcloudDirector::Media.new(name, image_type, size, options)
request(
:body => body,
:body => body.to_xml,
:expects => 201,
:headers => {'Content-Type' => 'application/vnd.vmware.vcloud.media+xml'},
:method => 'POST',

View file

@ -1,17 +1,58 @@
Shindo.tests('Compute::VcloudDirector | CaptureVAppParams generator', ['vclouddirector', 'xsd']) do
Shindo.tests('Compute::VcloudDirector | generator | CaptureVAppParams', ['vclouddirector', 'xsd']) do
require 'fog/vcloud_director/generators/compute/capture_vapp_params'
tests('#to_xml').returns(String) do
@capture_vapp_params = Fog::Generators::Compute::VcloudDirector::CaptureVappParams.new(
'name', 'http://example.com/api/vApp/0'
)
@capture_vapp_params.to_xml.class
SOURCE_HREF = 'SOURCE:HREF'
NAME = 'NAME'
tests('without :Description') do
tests('#to_xml').returns(String) do
@element = Fog::Generators::Compute::VcloudDirector::CaptureVAppParams.new(
SOURCE_HREF, :name => NAME
)
@element.to_xml.class
end
tests('#data.empty?').returns(true) { @element.data.empty? }
tests('#validate').returns([]) do
pending unless VcloudDirector::Generators::Helpers.have_xsd?
VcloudDirector::Generators::Helpers.validate(@element.doc)
end
tests('#attributes') do
attributes = @element.doc.root.attributes
tests('#name').returns(NAME) { attributes['name'].value }
end
tests('#elements') do
pending
end
end
tests('#validate').returns([]) do
pending unless VcloudDirector::Generators::Helpers.have_xsd?
VcloudDirector::Generators::Helpers.validate(@capture_vapp_params.doc)
tests('with :Description') do
DESCRIPTION = 'DESCRIPTION'
tests('#to_xml').returns(String) do
@element = Fog::Generators::Compute::VcloudDirector::CaptureVAppParams.new(
SOURCE_HREF, :name => NAME, :Description => DESCRIPTION
)
@element.to_xml.class
end
tests('#data.empty?').returns(true) { @element.data.empty? }
tests('#validate').returns([]) do
pending unless VcloudDirector::Generators::Helpers.have_xsd?
VcloudDirector::Generators::Helpers.validate(@element.doc)
end
tests('#attributes') do
attributes = @element.doc.root.attributes
tests('#name').returns(NAME) { attributes['name'].value }
end
tests('#elements') do
pending
end
end
end

View file

@ -0,0 +1,19 @@
Shindo.tests('Compute::VcloudDirector | generator | CatalogItem', ['vclouddirector', 'xsd']) do
require 'fog/vcloud_director/generators/compute/catalog_item'
NAME = 'name'
SOURCE_HREF = 'SOURCE:HREF'
tests('#to_xml').returns(String) do
@element = Fog::Generators::Compute::VcloudDirector::CatalogItem.new(NAME, SOURCE_HREF)
@element.to_xml.class
end
tests('#data.empty?').returns(true) { @element.data.empty? }
tests('#validate').returns([]) do
pending unless VcloudDirector::Generators::Helpers.have_xsd?
VcloudDirector::Generators::Helpers.validate(@element.doc)
end
end

View file

@ -0,0 +1,33 @@
Shindo.tests('Compute::VcloudDirector | generator | Entity', ['vclouddirector', 'xsd']) do
require 'fog/vcloud_director/generators/compute/entity'
NAME = 'NAME'
HREF = 'HREF'
TYPE = 'TYPE'
OPERATION_KEY = 'OPERATION_KEY' # since 5.1
DESCRIPTION = 'DESCRIPTION'
tests('#to_xml').returns(String) do
@element = Fog::Generators::Compute::VcloudDirector::Entity.new(
NAME,
:href => HREF, :type => TYPE, :operationKey => OPERATION_KEY,
:Description => DESCRIPTION)
@element.to_xml.class
end
tests('#data.empty?').returns(true) { $stderr.puts @element.data.inspect; @element.data.empty? }
tests('#validate').returns([]) do
pending unless VcloudDirector::Generators::Helpers.have_xsd?
VcloudDirector::Generators::Helpers.validate(@element.doc)
end
tests('#attributes') do
attributes = @element.doc.root.attributes
tests('#name').returns(NAME) { attributes['name'].value }
tests('#href').returns(HREF) { attributes['href'].value }
tests('#type').returns(TYPE) { attributes['type'].value }
tests('#operationKey').returns(OPERATION_KEY) { attributes['operationKey'].value }
end
end

View file

@ -0,0 +1,23 @@
Shindo.tests('Compute::VcloudDirector | generator | IdentifiableResourceType', ['vclouddirector', 'xsd']) do
require 'fog/vcloud_director/generators/compute/identifiable_resource_type'
tests('#to_xml').returns(String) do
data = {
:href => 'HREF',
:type => 'TYPE',
:operationKey => 'OPERATION_KEY', # since 5.1
}
@type = Fog::Generators::Compute::VcloudDirector::IdentifiableResourceType.new(data)
@type.to_xml.class
end
tests('#data.empty?').returns(true) { @type.data.empty? }
tests('#attributes') do
attributes = @type.doc.root.attributes
tests('#href').returns('HREF') { attributes['href'].value }
tests('#type').returns('TYPE') { attributes['type'].value }
tests('#operationKey').returns('OPERATION_KEY') { attributes['operationKey'].value }
end
end

View file

@ -0,0 +1,16 @@
Shindo.tests('Compute::VcloudDirector | generator | Media', ['vclouddirector', 'xsd']) do
require 'fog/vcloud_director/generators/compute/media'
tests('#to_xml').returns(String) do
@element = Fog::Generators::Compute::VcloudDirector::Media.new('NAME', 'iso', '0')
@element.to_xml.class
end
tests('#data.empty?').returns(true) { @element.data.empty? }
tests('#validate').returns([]) do
pending unless VcloudDirector::Generators::Helpers.have_xsd?
VcloudDirector::Generators::Helpers.validate(@element.doc)
end
end

View file

@ -0,0 +1,20 @@
Shindo.tests('Compute::VcloudDirector | generator | ParamsType', ['vclouddirector', 'xsd']) do
require 'fog/vcloud_director/generators/compute/params_type'
NAME = 'NAME'
tests('#to_xml').returns(String) do
@type = Fog::Generators::Compute::VcloudDirector::ParamsType.new(
:name => NAME
)
@type.to_xml.class
end
tests('#data.empty?').returns(true) { @type.data.empty? }
tests('#attributes') do
attributes = @type.doc.root.attributes
tests('#name').returns(NAME) { attributes['name'].value }
end
end

View file

@ -0,0 +1,16 @@
Shindo.tests('Compute::VcloudDirector | generator | ResourceType', ['vclouddirector', 'xsd']) do
require 'fog/vcloud_director/generators/compute/resource_type'
HREF = 'HREF'
TYPE = 'TYPE'
tests('#to_xml').returns(String) do
@type = Fog::Generators::Compute::VcloudDirector::ResourceType.new(
:href => HREF, :type => TYPE
)
@type.to_xml.class
end
tests('#data.empty?').returns(true) { @type.data.empty? }
end

View file

@ -1,30 +1,32 @@
Shindo.tests('Compute::VcloudDirector | UndeployVAppParams generator', ['vclouddirector', 'xsd']) do
Shindo.tests('Compute::VcloudDirector | generator | UndeployVAppParams', ['vclouddirector', 'xsd']) do
require 'fog/vcloud_director/generators/compute/undeploy_vapp_params'
tests('with :UndeployPowerAction option') do
tests('without :UndeployPowerAction option') do
tests('#to_xml').returns(String) do
attrs = {:UndeployPowerAction => 'default'}
@undeploy_vapp_params = Fog::Generators::Compute::VcloudDirector::UndeployVappParams.new(attrs)
@undeploy_vapp_params.to_xml.class
@element = Fog::Generators::Compute::VcloudDirector::UndeployVAppParams.new
@element.to_xml.class
end
tests('#data.empty?').returns(true) { @element.data.empty? }
tests('#validate').returns([]) do
pending unless VcloudDirector::Generators::Helpers.have_xsd?
VcloudDirector::Generators::Helpers.validate(@undeploy_vapp_params.doc)
VcloudDirector::Generators::Helpers.validate(@element.doc)
end
end
tests('without :UndeployPowerAction option') do
tests('with :UndeployPowerAction option') do
tests('#to_xml').returns(String) do
attrs = {}
@undeploy_vapp_params = Fog::Generators::Compute::VcloudDirector::UndeployVappParams.new(attrs)
@undeploy_vapp_params.to_xml.class
@element = Fog::Generators::Compute::VcloudDirector::UndeployVAppParams.new(
:UndeployPowerAction => 'default'
)
@element.to_xml.class
end
tests('#data.empty?').returns(true) { @element.data.empty? }
tests('#validate').returns([]) do
pending unless VcloudDirector::Generators::Helpers.have_xsd?
VcloudDirector::Generators::Helpers.validate(@undeploy_vapp_params.doc)
VcloudDirector::Generators::Helpers.validate(@element.doc)
end
end