1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-01-10 01:17:40 +03:00

feature #200: XMLUtil class hierarchy

This commit is contained in:
Ruben S. Montero 2010-07-20 16:52:41 +02:00
parent fe0e6927e9
commit 923294c236
12 changed files with 92 additions and 82 deletions

View File

@ -58,7 +58,7 @@ class ImageOCCI < Image
@image_info = nil
if xml_info != nil
xmldoc = XMLUtilsElement.initialize_xml(xml_info, 'STORAGE')
xmldoc = XMLElement.build_xml(xml_info, 'STORAGE')
@image_info = XMLElement.new(xmldoc) if xmldoc != nil
end
end

View File

@ -89,7 +89,7 @@ class OCCIServer < CloudServer
# --- Get User's VMs ---
user_flag = -1
vmpool = VirtualMachinePoolOCCI.new(
get_client(request.env),
get_client(request.env),
user_flag)
# --- Prepare XML Response ---
@ -108,7 +108,7 @@ class OCCIServer < CloudServer
# --- Get User's VNETs ---
user_flag = -1
network_pool = VirtualNetworkPoolOCCI.new(
get_client(request.env),
get_client(request.env),
user_flag)
# --- Prepare XML Response ---
@ -126,9 +126,9 @@ class OCCIServer < CloudServer
# --- Get User's Images ---
user_flag = -1
image_pool = ImagePoolOCCI.new(
get_client(request.env),
get_client(request.env),
user_flag)
# --- Prepare XML Response ---
rc = image_pool.info
return rc, 404 if OpenNebula.is_error?(rc)
@ -179,7 +179,7 @@ class OCCIServer < CloudServer
vm = VirtualMachineOCCI.new(
VirtualMachine.build_xml(params[:id]),
get_client(request.env))
# --- Prepare XML Response ---
rc = vm.info
return rc, 404 if OpenNebula::is_error?(rc)
@ -210,7 +210,7 @@ class OCCIServer < CloudServer
# [return] _String_,_Integer_ Update confirmation msg or error,
# status code
def put_compute(request, params)
xmldoc = XMLUtilsElement.initialize_xml(request.body, 'COMPUTE')
xmldoc = XMLElement.build_xml(request.body, 'COMPUTE')
vm_info = XMLElement.new(xmldoc) if xmldoc != nil
# --- Get the VM and Action on it ---

View File

@ -65,7 +65,7 @@ class VirtualMachineOCCI < VirtualMachine
@template = nil
if xml_info != nil
xmldoc = XMLUtilsElement.initialize_xml(xml_info, 'COMPUTE')
xmldoc = XMLElement.build_xml(xml_info, 'COMPUTE')
@vm_info = XMLElement.new(xmldoc) if xmldoc != nil
end

View File

@ -47,7 +47,7 @@ class VirtualNetworkOCCI < VirtualNetwork
@vnet_info = nil
if xml_info != nil
xmldoc = XMLUtilsElement.initialize_xml(xml_info, 'NETWORK')
xmldoc = XMLElement.build_xml(xml_info, 'NETWORK')
@vnet_info = XMLElement.new(xmldoc) if xmldoc != nil
end
end

View File

@ -27,7 +27,7 @@ module OpenNebula
user_xml = "<CLUSTER></CLUSTER>"
end
XMLUtilsElement.initialize_xml(user_xml, 'CLUSTER')
XMLElement.build_xml(user_xml,'CLUSTER')
end
# ---------------------------------------------------------------------

View File

@ -36,7 +36,7 @@ module OpenNebula
host_xml = "<HOST></HOST>"
end
XMLUtilsElement.initialize_xml(host_xml, 'HOST')
XMLElement.build_xml(host_xml, 'HOST')
end
#######################################################################
@ -91,7 +91,7 @@ module OpenNebula
SHORT_HOST_STATES[state_str]
end
# Returns the cluster of the Host
# Returns the cluster of the Host
def cluster
self['CLUSTER']
end
@ -103,7 +103,7 @@ module OpenNebula
rc = @client.call(HOST_METHODS[:enable], @pe_id, enabled)
rc = nil if !OpenNebula.is_error?(rc)
return rc
end
end

View File

@ -48,7 +48,7 @@ module OpenNebula
image_xml = "<IMAGE></IMAGE>"
end
XMLUtilsElement.initialize_xml(image_xml, 'IMAGE')
XMLElement.build_xml(image_xml,'IMAGE')
end
# Class constructor

View File

@ -3,10 +3,8 @@ module OpenNebula
# The Pool class represents a generic OpenNebula Pool in XML format
# and provides the basic functionality to handle the Pool elements
require 'pp'
class Pool
class Pool < XMLPool
include Enumerable
include XMLUtilsElement
include XMLUtilsPool
protected
@ -14,18 +12,19 @@ module OpenNebula
#element:: _String_ XML name of the Pool elements
#client:: _Client_ represents a XML-RPC connection
def initialize(pool,element,client)
super(nil)
@pool_name = pool.upcase
@element_name = element.upcase
@client = client
@xml = nil
@client = client
@hash = nil
end
# Default Factory Method for the Pools. The factory method returns an
# suitable PoolElement object. Each Pool MUST implement the
# suitable PoolElement object. Each Pool MUST implement the
# corresponding factory method
# element_xml:: _XML_ XML element describing the pool element
# element_xml:: _XML_ XML element describing the pool element
# [return] a PoolElement object
def factory(element_xml)
OpenNebula::PoolElement.new(element_xml,client)
@ -39,15 +38,15 @@ module OpenNebula
# representation in XML format
# xml_method:: _String_ the name of the XML-RPC method
# args:: _Array_ with additional arguments for the info call
# [return] nil in case of success or an Error object
# [return] nil in case of success or an Error object
def info(xml_method,*args)
rc = @client.call(xml_method,*args)
if !OpenNebula.is_error?(rc)
@xml = XMLUtilsElement::initialize_xml(rc,@pool_name)
initialize_xml(rc,@pool_name)
rc = nil
end
return rc
end
@ -65,23 +64,22 @@ module OpenNebula
str = ""
REXML::Formatters::Pretty.new(1).write(@xml,str)
return str
return str
end
end
# The PoolElement Class represents a generic element of a Pool in
# XML format
class PoolElement
include XMLUtilsElement
class PoolElement < XMLElement
protected
# node:: _XML_is a XML element that represents the Pool element
# node:: _XML_is a XML element that represents the Pool element
# client:: _Client_ represents a XML-RPC connection
def initialize(node, client)
@xml = node
@client = client
@hash = nil
if self['ID']
@pe_id = self['ID'].to_i
else
@ -98,16 +96,16 @@ module OpenNebula
# detailed information in XML format
# xml_method:: _String_ the name of the XML-RPC method
# root_element:: _String_ Base XML element
# [return] nil in case of success or an Error object
# [return] nil in case of success or an Error object
def info(xml_method, root_element)
return Error.new('ID not defined') if !@pe_id
rc = @client.call(xml_method,@pe_id)
if !OpenNebula.is_error?(rc)
@xml = XMLUtilsElement::initialize_xml(rc, root_element)
initialize_xml(rc, root_element)
rc = nil
@pe_id = self['ID'].to_i if self['ID']
@name = self['NAME'] if self['NAME']
end
@ -116,11 +114,11 @@ module OpenNebula
end
# Calls to the corresponding allocate method to create a new element
# in the OpenNebula core
# in the OpenNebula core
# xml_method:: _String_ the name of the XML-RPC method
# args:: _Array_ additional arguments including the template for the
# new element
# [return] nil in case of success or an Error object
# new element
# [return] nil in case of success or an Error object
def allocate(xml_method, *args)
rc = @client.call(xml_method, *args)
@ -131,13 +129,13 @@ module OpenNebula
return rc
end
# Calls to the corresponding update method to modify
# the object's template
# xml_method:: _String_ the name of the XML-RPC method
# name:: _String_ the name of the property to be modified
# value:: _String_ the new value of the property to be modified
# [return] nil in case of success or an Error object
# [return] nil in case of success or an Error object
def update(xml_method, name, value)
return Error.new('ID not defined') if !@pe_id
@ -148,9 +146,9 @@ module OpenNebula
end
# Calls to the corresponding delete method to remove this element
# from the OpenNebula core
# from the OpenNebula core
# xml_method:: _String_ the name of the XML-RPC method
# [return] nil in case of success or an Error object
# [return] nil in case of success or an Error object
def delete(xml_method)
return Error.new('ID not defined') if !@pe_id
@ -161,20 +159,20 @@ module OpenNebula
end
public
# Creates new element specifying its id
# id:: identifyier of the element
# client:: initialized OpenNebula::Client object
def self.new_with_id(id, client=nil)
self.new(self.build_xml(id), client)
end
# Returns element identifier
# [return] _Integer_ the PoolElement ID
# [return] _Integer_ the PoolElement ID
def id
@pe_id
end
# Gets element name
# [return] _String_ the PoolElement name
def name
@ -185,8 +183,8 @@ module OpenNebula
def to_str
str = ""
REXML::Formatters::Pretty.new(1).write(@xml,str)
return str
return str
end
end
end

View File

@ -25,7 +25,7 @@ module OpenNebula
user_xml = "<USER></USER>"
end
XMLUtilsElement.initialize_xml(user_xml, 'USER')
XMLElement.build_xml(user_xml, 'USER')
end
# ---------------------------------------------------------------------

View File

@ -12,9 +12,9 @@ module OpenNebula
:migrate => "vm.migrate",
:deploy => "vm.deploy"
}
VM_STATE=%w{INIT PENDING HOLD ACTIVE STOPPED SUSPENDED DONE FAILED}
LCM_STATE=%w{LCM_INIT PROLOG BOOT RUNNING MIGRATE SAVE_STOP SAVE_SUSPEND
SAVE_MIGRATE PROLOG_MIGRATE PROLOG_RESUME EPILOG_STOP EPILOG
SHUTDOWN CANCEL FAILURE DELETE UNKNOWN}
@ -72,8 +72,8 @@ module OpenNebula
else
vm_xml = "<VM></VM>"
end
XMLUtilsElement.initialize_xml(vm_xml, 'VM')
XMLElement.build_xml(vm_xml, 'VM')
end
def VirtualMachine.get_reason(reason)
@ -88,7 +88,7 @@ module OpenNebula
#######################################################################
def initialize(xml, client)
super(xml,client)
@element_name = "VM"
@client = client
end
@ -110,7 +110,7 @@ module OpenNebula
rc = @client.call(VM_METHODS[:deploy], @pe_id, host_id.to_i)
rc = nil if !OpenNebula.is_error?(rc)
return rc
end
@ -155,7 +155,7 @@ module OpenNebula
rc = @client.call(VM_METHODS[:migrate], @pe_id, host_id.to_i, false)
rc = nil if !OpenNebula.is_error?(rc)
return rc
end
@ -164,14 +164,14 @@ module OpenNebula
rc = @client.call(VM_METHODS[:migrate], @pe_id, host_id.to_i, true)
rc = nil if !OpenNebula.is_error?(rc)
return rc
end
#######################################################################
# Helpers to get VirtualMachine information
#######################################################################
# Returns the VM state of the VirtualMachine (numeric value)
def state
self['STATE'].to_i

View File

@ -26,7 +26,7 @@ module OpenNebula
vn_xml = "<VNET></VNET>"
end
XMLUtilsElement.initialize_xml(vn_xml, 'VNET')
XMLElement.build_xml(vn_xml, 'VNET')
end
# Class constructor
@ -39,7 +39,7 @@ module OpenNebula
#######################################################################
# XML-RPC Methods for the Virtual Network Object
#######################################################################
def info()
super(VN_METHODS[:info], 'VNET')
end
@ -47,11 +47,11 @@ module OpenNebula
def allocate(description)
super(VN_METHODS[:allocate],description)
end
def publish
set_publish(true)
end
def unpublish
set_publish(false)
end
@ -59,7 +59,7 @@ module OpenNebula
def delete()
super(VN_METHODS[:delete])
end
private
def set_publish(published)
return Error.new('ID not defined') if !@pe_id
@ -69,6 +69,6 @@ module OpenNebula
return rc
end
end
end

View File

@ -19,27 +19,42 @@ module OpenNebula
# The XMLUtilsElement module provides an abstraction of the underlying
# XML parser engine. It provides XML-related methods for the Pool Elements
###########################################################################
module XMLUtilsElement
class XMLElement
def initialize(xml=nil)
@xml = xml
end
# Initialize a XML document for the element
# xml:: _String_ the XML document of the object
# root_element:: _String_ Base xml element
# [return] _XML_ object for the underlying XML engine
def self.initialize_xml(xml, root_element)
def initialize_xml(xml, root_element)
if NOKOGIRI
xmldoc = Nokogiri::XML(xml).xpath("/#{root_element}")
if xmldoc.size == 0
xmldoc = nil
@xml = Nokogiri::XML(xml).xpath("/#{root_element}")
if @xml.size == 0
@xml = nil
end
else
xmldoc = REXML::Document.new(xml).root
if xmldoc.name != root_element
xmldoc = nil
@xml = REXML::Document.new(xml).root
if @xml.name != root_element
@xml = nil
end
end
return xmldoc
end
# Builds a XML document
# xml:: _String_ the XML document of the object
# root_element:: _String_ Base xml element
# [return] _XML_ object for the underlying XML engine
def self.build_xml(xml, root_element)
if NOKOGIRI
doc = Nokogiri::XML(xml).xpath("/#{root_element}")
else
doc = REXML::Document.new(xml).root
end
return doc
end
# Extract an element from the XML description of the PoolElement.
# key::_String_ The name of the element
# [return] _String_ the value of the element
@ -49,6 +64,7 @@ module OpenNebula
def [](key)
if NOKOGIRI
element=@xml.xpath(key.to_s)
if element.size == 0
return nil
end
@ -175,7 +191,11 @@ module OpenNebula
# The XMLUtilsPool module provides an abstraction of the underlying
# XML parser engine. It provides XML-related methods for the Pools
###########################################################################
module XMLUtilsPool
class XMLPool < XMLElement
def initialize(xml=nil)
super(xml)
end
#Executes the given block for each element of the Pool
#block:: _Block_
@ -193,7 +213,6 @@ module OpenNebula
end
end
def to_hash
if !@hash && @xml
@hash=Crack::XML.parse(to_xml)
@ -202,13 +221,6 @@ module OpenNebula
end
end
class XMLElement
include XMLUtilsElement
def initialize(xml)
@xml = xml
end
end
end