From 42ac27032176b7dfca8fe73d818443e191dc9c73 Mon Sep 17 00:00:00 2001 From: Hector Sanjuan Date: Thu, 10 Nov 2011 23:04:11 +0100 Subject: [PATCH] Feature #969: Remove dependency from Configuration.rb (cherry picked from commit a54ce0af0109082a672db7549894df83c12e3410) --- src/ozones/Client/lib/zona.rb | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/src/ozones/Client/lib/zona.rb b/src/ozones/Client/lib/zona.rb index abc562c84a..b94e889c7f 100644 --- a/src/ozones/Client/lib/zona.rb +++ b/src/ozones/Client/lib/zona.rb @@ -18,7 +18,6 @@ require 'rubygems' require 'uri' require 'net/https' -require 'OpenNebula/Configuration' require 'zona/OZonesJSON' @@ -122,7 +121,7 @@ EOT # @param [String] tmpl_str OpenNebula template string # @return [String, Zona::Error] Response string or Error def post_resource_str(kind, tmpl_str) - tmpl_json = Zona.to_body(kind, tmpl_str) + tmpl_json = Zona.parse_template(kind, tmpl_str) post_resource(kind, tmpl_json) end @@ -150,7 +149,7 @@ EOT # @param [String] tmpl_str OpenNebula template string # @return [String, Zona::Error] Response string or Error def put_resource_str(kind, id, tmpl_str) - tmpl_json = Client.to_body(kind, tmpl_str) + tmpl_json = Zona.parse_template(kind, tmpl_str) put_resource(kind, id, tmpl_json) end @@ -261,7 +260,7 @@ EOT else if Zona.is_http_error?(value) str = "Operating with #{kind} failed with HTTP error" - str = " " + str + "code: #{value.code}\n" + str += " code: #{value.code}\n" if value.body # Try to extract error message begin @@ -281,13 +280,24 @@ EOT end - # Turns a OpenNebula template string into a JSON string + # Parses a OpenNebula template string and turns it into a JSON string # @param [String] kind element kind # @param [String] tmpl_str template # @return [String, Zona::Error] JSON string or Error - def self.to_body(kind, tmpl_str) - tmpl = OpenNebula::Configuration.new(tmpl_str) - res = { "#{kind.upcase}" => tmpl.conf } + def self.parse_template(kind, tmpl_str) + name_reg =/[\w\d_-]+/ + variable_reg =/\s*(#{name_reg})\s*=\s*/ + single_variable_reg =/^#{variable_reg}([^\[]+?)(#.*)?$/ + + tmpl = Hash.new + + tmpl_str.scan(single_variable_reg) do | m | + key = m[0].strip.upcase + value = m[1].strip + tmpl[key] = value + end + + res = { "#{kind.upcase}" => tmpl } return OZonesJSON.to_json(res) end