Hi,
This patch will replace REXML with Nokogiri. Converter was reworked in way which will allow us in future easy 'rewriting' methods / adding new methods. Nokogiri is already packaged for Fedora, so there is no new dependency.
Question:
Why we have convert_to_xml defined twice ? (conversion_helper.rb / driver_helper.rb)
- Michal
--- server/lib/converters/nokogiri_converter.rb | 141 ++++++++++++++++++++ server/lib/deltacloud/helpers/conversion_helper.rb | 10 +- server/lib/deltacloud/helpers/driver_helper.rb | 6 +- 3 files changed, 151 insertions(+), 6 deletions(-) create mode 100644 server/lib/converters/nokogiri_converter.rb
diff --git a/server/lib/converters/nokogiri_converter.rb b/server/lib/converters/nokogiri_converter.rb new file mode 100644 index 0000000..3ab165b --- /dev/null +++ b/server/lib/converters/nokogiri_converter.rb @@ -0,0 +1,141 @@ +require 'nokogiri' + +module Converters + class NokogiriConverter + + attr_accessor :data_type, :link_builder, :builder + + def initialize(link_builder, data_type) + @link_builder, @data_type = link_builder, data_type + end + + def convert(data) + if data.class.eql?(Array) + convert_array(data).to_xml + else + convert_single(data).to_xml + end + end + + def convert_flavor(xml, data) + lambda { + xml.flavor(:href => link_builder.send( :flavor_url, data.id )) { + xml.id_ data.id + xml.architecture data.architecture + xml.memory data.memory + xml.storage data.storage + } + } + end + + def convert_image(xml, data) + lambda { + xml.image(:href => link_builder.send( :image_url, data.id )) { + xml.id_ data.id + xml.name data.name + xml.architecture data.architecture + xml.owner_id data.owner_id + xml.description data.description + } + } + end + + def convert_realm(xml, data) + lambda { + xml.realm(:href => link_builder.send( :realm_url, data.id )) { + xml.id_ data.id + xml.name data.name + xml.state data.state + xml.limit data.limit.eql?(:unlimited) ? nil : data.limit + } + } + end + + def convert_instance(xml, data) + lambda { + xml.instance(:href => link_builder.send( :instance_url, data.id )) { + xml.id_ data.id + xml.name data.name + xml.owner_id data.owner_id + xml.state data.state + xml.image(:href => link_builder.send( :image_url, data.image_id)) + xml.flavor(:href => link_builder.send( :flavor_url, data.flavor_id)) if data.flavor_id + xml.realm(:href => link_builder.send( :realm_url, data.realm_id)) if data.realm_id + xml.actions { + data.actions.each do |action| + xml.link(:red => action, :href => link_builder.send("#{action}_instance_url", data.id)) + end if data.actions and not data.actions.empty? + } + xml.send('public-addresses') { + data.public_addresses.each do |address| + xml.address address + end + } + xml.send('private-addresses') { + data.private_addresses.each do |address| + xml.address address + end + } + } + } + end + + def convert_storage_volume(xml, data) + lambda { + xml.send('storage-volume', :href => link_builder.send(:storage_volume_url, data.id)) { + xml.id_ data.id + xml.created data.created + xml.state data.state + xml.capacity data.capacity + xml.device data.device + if data.instance_id + xml.instance(:href => link_builder.send(:instance_url, data.instance_id)) + else + xml.instance + end + } + } + end + + def convert_storage_snapshot(xml, data) + lambda { + xml.send('storage-snapshot', :href => link_builder.send(:storage_snapshot_url, data.id)) { + xml.id_ data.id + xml.created data.created + xml.state data.state + if data.storage_volume_id + xml.send('storage-volume', :href => link_builder.send(:storage_snapshot_url, data.storage_volume_id)) + else + xml.send('storage-volume') + end + } + } + end + + private + + def convert_array(data) + Nokogiri::XML::Builder.new do |xml| + xml.send(:"#{data_type.to_s.pluralize.tr('_', '-')}") { + data.each do |item| + self.send(:"convert_#{data_type}", xml, item).call + end + } + end + end + + def convert_single(data) + Nokogiri::XML::Builder.new do |xml| + self.send(:"convert_#{data_type}", xml, data).call + end + end + + end + + class String + def pluralize + "#{self}s" + end + end +end + diff --git a/server/lib/deltacloud/helpers/conversion_helper.rb b/server/lib/deltacloud/helpers/conversion_helper.rb index 6ba85d5..b4cef49 100644 --- a/server/lib/deltacloud/helpers/conversion_helper.rb +++ b/server/lib/deltacloud/helpers/conversion_helper.rb @@ -16,13 +16,17 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
-load 'converters/xml_converter.rb' +#load 'converters/xml_converter.rb' +load 'converters/nokogiri_converter.rb'
module ConversionHelper
def convert_to_xml(type, obj) - if ( [ :flavor, :account, :image, :realm, :instance, :storage_volume, :storage_snapshot ].include?( type ) ) - Converters::XMLConverter.new( self, type ).convert(obj) + #if ( [ :account, :image, :realm, :instance, :storage_volume, :storage_snapshot ].include?( type ) ) + # Converters::XMLConverter.new( self, type ).convert(obj) + #end + if [ :flavor, :execute].include?(type) + Converters::NokogiriConverter.new(self, type).convert(obj) end end
diff --git a/server/lib/deltacloud/helpers/driver_helper.rb b/server/lib/deltacloud/helpers/driver_helper.rb index 352253f..296d650 100644 --- a/server/lib/deltacloud/helpers/driver_helper.rb +++ b/server/lib/deltacloud/helpers/driver_helper.rb @@ -17,13 +17,13 @@
require 'deltacloud/base_driver' -require 'converters/xml_converter' +require 'converters/nokogiri_converter'
module DriverHelper
def convert_to_xml(type, obj) - if ( [ :flavor, :account, :image, :realm, :instance, :storage_volume, :storage_snapshot ].include?( type ) ) - Converters::XMLConverter.new( self, type ).convert(obj) + if [ :flavor, :realm, :image, :instance, :storage_snapshot, :storage_volume, :account ].include?(type) + Converters::NokogiriConverter.new( self, type ).convert(obj) end end end
On 30/03/10 15:19 +0200, mfojtik@redhat.com wrote:
module ConversionHelper
def convert_to_xml(type, obj)
- if ( [ :flavor, :account, :image, :realm, :instance, :storage_volume, :storage_snapshot ].include?( type ) )
Converters::XMLConverter.new( self, type ).convert(obj)
- #if ( [ :account, :image, :realm, :instance, :storage_volume, :storage_snapshot ].include?( type ) )
- # Converters::XMLConverter.new( self, type ).convert(obj)
- #end
- if [ :flavor, :execute].include?(type)
end endConverters::NokogiriConverter.new(self, type).convert(obj)
This is weird, maybe I commit too early. Code should looks like:
def convert_to_xml(type, obj) if [ :flavor, :realm, :image, :instance, :storage_snapshot, :storage_volume, :account ].include?(type) Converters::NokogiriConverter.new( self, type ).convert(obj) end end
Btw. this is code is not executed at all, so it doesn't break this patch testing at all.
- Michal
On Tue, 2010-03-30 at 15:19 +0200, mfojtik@redhat.com wrote:
This patch will replace REXML with Nokogiri. Converter was reworked in way which will allow us in future easy 'rewriting' methods / adding new methods. Nokogiri is already packaged for Fedora, so there is no new dependency.
Shouldn't we eventually turn all this into HAML templates ? I find them more readable than straightup Ruby code (but please don't do this right now, since I have a bunch of patches that I'd need to rework)
Why we have convert_to_xml defined twice ? (conversion_helper.rb / driver_helper.rb)
The one in driver_helper just exposes the XMLConverter in a helper method.
David
deltacloud-devel@lists.fedorahosted.org