From d28dd0b44574aa1abca1a96ac094e099bd90193e Mon Sep 17 00:00:00 2001 From: Hector Sanjuan Date: Thu, 13 Oct 2011 20:12:03 +0200 Subject: [PATCH] Task #908: Integrate OzonesClient in zona.rb api. This commit deletes OzonesClient.rb. All functionality has been merged to zona.rb and OzonesJSON.rb. All dependent files have been accordingly updated. Cli operations have been manually tested. API operations have not been tested yet. --- install.sh | 4 - src/ozones/Client/bin/onevdc | 3 +- src/ozones/Client/bin/onezone | 3 +- src/ozones/Client/lib/OZonesClient.rb | 286 ------------------ src/ozones/Client/lib/api/zona.rb | 249 ++++++++++++++- .../Client/lib/api/zona/OZonesElement.rb | 16 +- src/ozones/Client/lib/api/zona/OZonesJSON.rb | 51 +++- src/ozones/Client/lib/api/zona/OZonesPool.rb | 8 +- src/ozones/Client/lib/api/zona/VDCElement.rb | 12 +- src/ozones/Client/lib/api/zona/VDCPool.rb | 2 +- src/ozones/Client/lib/api/zona/ZoneElement.rb | 6 +- src/ozones/Client/lib/api/zona/ZonePool.rb | 4 +- src/ozones/Client/lib/cli/ozones_helper.rb | 18 +- .../lib/cli/ozones_helper/vdc_helper.rb | 14 +- src/ozones/Server/lib/OZones/Zones.rb | 2 +- 15 files changed, 329 insertions(+), 349 deletions(-) delete mode 100644 src/ozones/Client/lib/OZonesClient.rb diff --git a/install.sh b/install.sh index ad6eb4074c..4b79fbb55e 100755 --- a/install.sh +++ b/install.sh @@ -372,7 +372,6 @@ INSTALL_CLIENT_FILES=( CLI_LIB_FILES:$LIB_LOCATION/ruby/cli ONE_CLI_LIB_FILES:$LIB_LOCATION/ruby/cli/one_helper ETC_CLIENT_FILES:$ETC_LOCATION - OZONES_LIB_CLIENT_FILES:$LIB_LOCATION/ruby OZONES_BIN_CLIENT_FILES:$BIN_LOCATION OZONES_LIB_CLIENT_CLI_FILES:$LIB_LOCATION/ruby/cli OZONES_LIB_CLIENT_CLI_HELPER_FILES:$LIB_LOCATION/ruby/cli/ozones_helper @@ -432,7 +431,6 @@ INSTALL_OZONES_FILES=( OZONES_PUBLIC_IMAGES_FILES:$OZONES_LOCATION/public/images OZONES_PUBLIC_CSS_FILES:$OZONES_LOCATION/public/css OZONES_PUBLIC_JS_PLUGINS_FILES:$OZONES_LOCATION/public/js/plugins - OZONES_LIB_CLIENT_FILES:$LIB_LOCATION/ruby OZONES_BIN_CLIENT_FILES:$BIN_LOCATION OZONES_LIB_CLIENT_CLI_FILES:$LIB_LOCATION/ruby/cli OZONES_LIB_CLIENT_CLI_HELPER_FILES:$LIB_LOCATION/ruby/cli/ozones_helper @@ -1106,8 +1104,6 @@ OZONES_PUBLIC_JS_PLUGINS_FILES="src/ozones/Server/public/js/plugins/zones-tab.js src/ozones/Server/public/js/plugins/aggregated-tab.js \ src/ozones/Server/public/js/plugins/dashboard-tab.js" -OZONES_LIB_CLIENT_FILES="src/ozones/Client/lib/OZonesClient.rb" - OZONES_LIB_CLIENT_CLI_FILES="src/ozones/Client/lib/cli/ozones_helper.rb" OZONES_LIB_CLIENT_CLI_HELPER_FILES="\ diff --git a/src/ozones/Client/bin/onevdc b/src/ozones/Client/bin/onevdc index 246487f312..a181a1fb3d 100755 --- a/src/ozones/Client/bin/onevdc +++ b/src/ozones/Client/bin/onevdc @@ -49,8 +49,7 @@ cmd=CommandParser::CmdParser.new(ARGV) do helper.show_resource(args[0],options) end - command :list, 'Lists VDCs in the pool', - :options=>CLIHelper::OPTIONS+OpenNebulaHelper::OPTIONS do + command :list, 'Lists VDCs in the pool' do helper.list_pool(options) end diff --git a/src/ozones/Client/bin/onezone b/src/ozones/Client/bin/onezone index 33e9b96ad8..6ba31eb8a8 100755 --- a/src/ozones/Client/bin/onezone +++ b/src/ozones/Client/bin/onezone @@ -114,8 +114,7 @@ cmd=CommandParser::CmdParser.new(ARGV) do 0 end - command :list, 'Lists Zones in the pool', - :options=>CLIHelper::OPTIONS+OpenNebulaHelper::OPTIONS do + command :list, 'Lists Zones in the pool' do helper.list_pool(options) end diff --git a/src/ozones/Client/lib/OZonesClient.rb b/src/ozones/Client/lib/OZonesClient.rb deleted file mode 100644 index 2320bb8d07..0000000000 --- a/src/ozones/Client/lib/OZonesClient.rb +++ /dev/null @@ -1,286 +0,0 @@ -# -------------------------------------------------------------------------- # -# Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) # -# # -# Licensed under the Apache License, Version 2.0 (the "License"); you may # -# not use this file except in compliance with the License. You may obtain # -# a copy of the License at # -# # -# http://www.apache.org/licenses/LICENSE-2.0 # -# # -# Unless required by applicable law or agreed to in writing, software # -# distributed under the License is distributed on an "AS IS" BASIS, # -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # -# See the License for the specific language governing permissions and # -# limitations under the License. # -#--------------------------------------------------------------------------- # - -require 'rubygems' -require 'uri' -require 'net/https' -require 'json' -require 'OpenNebula/Configuration' - -module OZonesClient - class Client - - OZONES_VERSION = < e - str = "Error connecting to server (#{e.to_s}).\n" - str << "Server: #{url.host}:#{url.port}" - - return OZonesClient::Error.new(str) - rescue Errno::ETIMEDOUT => e - str = "Error timeout connecting to server (#{e.to_s}).\n" - str << "Server: #{url.host}:#{url.port}" - - return OZonesClient::Error.new(str) - rescue Timeout::Error => e - str = "Error timeout while connected to server (#{e.to_s}).\n" - str << "Server: #{url.host}:#{url.port}" - - return OZonesClient::Error.new(str) - end - end - - ########################################################################## - # JSON & Template utils - ########################################################################## - - def self.to_body(kind, tmpl_str) - tmpl = OpenNebula::Configuration.new(tmpl_str) - res = { "#{kind}" => tmpl.conf } - - return JSON::generate(res) - end - - def self.parse_json(json_str, root_element) - begin - hash = JSON.parse(json_str) - rescue Exception => e - return OZonesClient::Error.new(e.message) - end - - if hash.has_key?(root_element) - return hash[root_element] - else - return OZonesClient::Error.new("Error parsing JSON: Wrong resource type") - end - end - - def self.to_json(hash_to_convert) - begin - JSON.pretty_generate hash_to_convert - rescue Exception => e - OZonesClient::Error.new(e.message) - end - end -end diff --git a/src/ozones/Client/lib/api/zona.rb b/src/ozones/Client/lib/api/zona.rb index bdb392371e..f28008a4ea 100644 --- a/src/ozones/Client/lib/api/zona.rb +++ b/src/ozones/Client/lib/api/zona.rb @@ -15,7 +15,9 @@ #--------------------------------------------------------------------------- # require 'rubygems' -require 'OZonesClient.rb' +require 'uri' +require 'net/https' +require 'OpenNebula/Configuration' require 'zona/OZonesJSON' @@ -30,4 +32,249 @@ require 'zona/VDCElement' module Zona + class Client + + OZONES_VERSION = < e + str = "Error connecting to server (#{e.to_s}).\n" + str << "Server: #{url.host}:#{url.port}" + return Error.new(str) + rescue Errno::ETIMEDOUT => e + str = "Error timeout connecting to server (#{e.to_s}).\n" + str << "Server: #{url.host}:#{url.port}" + return Error.new(str) + rescue Timeout::Error => e + str = "Error timeout while connected to server (#{e.to_s}).\n" + str << "Server: #{url.host}:#{url.port}" + return Error.new(str) + rescue Errno::ENETUNREACH => e + str = "Error trying to reach network (#{e.to_s}).\n" + str << "Server: #{url.host}:#{url.port}" + return Error.new(str) + end + end + + def self.parse_error(value, kind) + if Zona.is_error?(value) + return value + else + if Zona.is_http_error?(value) + str = "Operating with #{kind.upcase} failed with HTTP error" + str = " " + str + "code: #{value.code}\n" + if value.body + # Try to extract error message + begin + str << "Body: " << + OZonesJSON.parse_json(value.body, + "error")["message"] + rescue + str.gsub!("\nBody:","") + end + end + return Error.new(str) + end + end + value # If it is not an error, return it as-is + end + + end + + # ############################################ + # Template helpers + # ############################################ + + def self.to_body(kind, tmpl_str) + tmpl = OpenNebula::Configuration.new(tmpl_str) + res = { "#{kind}" => tmpl.conf } + + return OZonesJSON.to_json(res) + end + + # ######################################################################### + # Error handling functions + # ######################################################################### + + def self.is_error?(value) + value.class==Zona::Error + end + + def self.is_http_error?(value) + value.class != Net::HTTPOK + end + + # ######################################################################### + # The Error Class represents a generic error in the Zona + # library. It contains a readable representation of the error. + # ######################################################################### + class Error + attr_reader :message + + # +message+ a description of the error + def initialize(message=nil) + @message=message + end + + def to_s() + @message + end + end + end diff --git a/src/ozones/Client/lib/api/zona/OZonesElement.rb b/src/ozones/Client/lib/api/zona/OZonesElement.rb index c375875bdc..2aefab0446 100644 --- a/src/ozones/Client/lib/api/zona/OZonesElement.rb +++ b/src/ozones/Client/lib/api/zona/OZonesElement.rb @@ -17,7 +17,7 @@ module Zona class OZonesElement < JSONElement - + protected def initialize(hash, client) @@ -30,16 +30,16 @@ module Zona def info(kind, root_element) return Error.new('ID not defined') if !@pe_id - + rc = @client.get_resource(kind,@pe_id) - if !OZonesClient.is_error?(rc) + if !Zona.is_error?(rc) initialize_json(rc.body,root_element) rc = nil - + @pe_id = self["id"] ? self["id"].to_i : nil @name = self["name"] ? self["name"] : nil - + end rc end @@ -50,8 +50,8 @@ module Zona def allocate(kind, tmpl_json) rc = @client.post_resource(kind, tmpl_json) - - if !OZonesClient.is_error?(rc) + + if !Zona.is_error?(rc) initialize_json(rc.body,kind.upcase) @pe_id = self["id"].to_i rc = nil @@ -63,7 +63,7 @@ module Zona return Error.new('ID not defined') if !@pe_id rc = @client.delete_resource(kind,@pe_id) - return rc if OZonesClient.is_error?(rc) + return rc if Zona.is_error?(rc) nil end diff --git a/src/ozones/Client/lib/api/zona/OZonesJSON.rb b/src/ozones/Client/lib/api/zona/OZonesJSON.rb index 0cfd1fbd17..f0a4e0a0f5 100644 --- a/src/ozones/Client/lib/api/zona/OZonesJSON.rb +++ b/src/ozones/Client/lib/api/zona/OZonesJSON.rb @@ -19,33 +19,58 @@ module Zona require 'json' + class OZonesJSON + + def self.build_json(json_str, root_element) + begin + parser = JSON.parser.new(json_str, {:symbolize_names => false}) + hash = parser.parse + + if hash.has_key?(root_element) + return hash[root_element] + end + + Error.new("Error parsing JSON:\ root element not present") + + rescue => e + Error.new(e.message) + end + end + + # Alias for compatibility + def self.parse_json(json_str, root_element) + OZonesJSON.build_json(json_str, root_element) + end + + + def self.to_json(hash_to_convert) + begin + JSON.pretty_generate(hash_to_convert) + rescue Exception => e + Error.new(e.message) + end + end + + end + class JSONElement def initialize(json_hash=nil) @json_hash=json_hash end def initialize_json(json_str, root_element) - rc = JSONElement.build_json(json_str,root_element) + rc = OZonesJSON.build_json(json_str,root_element) @json_hash = rc - if OZonesClient.is_error?(rc) || (rc.size == 0) + if Zona.is_error?(rc) || (rc.size == 0) @json_hash=nil end end - - def self.build_json(json_str, root_element) - begin - parser = JSON.parser.new(json_str, {:symbolize_names => false}) - hash = parser.parse - hash[root_element] - rescue => e - OZonesClient::Error.new(e.message) - end - end - def [](key) @json_hash[key] end + + end class JSONPool < JSONElement diff --git a/src/ozones/Client/lib/api/zona/OZonesPool.rb b/src/ozones/Client/lib/api/zona/OZonesPool.rb index 31952ef0ea..bb4a5ac6c8 100644 --- a/src/ozones/Client/lib/api/zona/OZonesPool.rb +++ b/src/ozones/Client/lib/api/zona/OZonesPool.rb @@ -19,7 +19,7 @@ module Zona class OZonesPool < JSONPool protected - + def initialize(pool,element,client) super(nil) @@ -29,13 +29,13 @@ module Zona end def factory(element_json) - Zona::OZonesPoolElement.new(element_json, @client) + OZonesPoolElement.new(element_json, @client) end def info(kind) rc = @client.get_pool(kind) - if !OZonesClient.is_error?(rc) + if !Zona.is_error?(rc) initialize_json(rc.body,@pool_name) rc=nil end @@ -44,7 +44,7 @@ module Zona end public - + def each(&block) each_element(block) if @json_hash end diff --git a/src/ozones/Client/lib/api/zona/VDCElement.rb b/src/ozones/Client/lib/api/zona/VDCElement.rb index 73f57b10b0..41faebc1c1 100644 --- a/src/ozones/Client/lib/api/zona/VDCElement.rb +++ b/src/ozones/Client/lib/api/zona/VDCElement.rb @@ -26,13 +26,13 @@ module Zona else json = '{"VDC":{}}' end - JSONElement.build_json(json,"VDC") + OZonesJSON.build_json(json,"VDC") end def initialize(hash, client) super(hash, client) end - + def info super(VDC_KIND,"VDC") end @@ -51,11 +51,11 @@ module Zona def addhosts(hosts_array,options={}) return Error.new('VDC not info-ed') if !@json_hash - + # array of hosts, integers hosts = self["hosts"].split(',').collect!{|x| x.to_i} hosts.concat(hosts_array).uniq! - + new_hosts = hosts.join(',') template = {:id => @pe_id, :hosts => new_hosts} template[:force] = "yes" if options[:force] @@ -63,7 +63,7 @@ module Zona template = {:vdc => template} rc = @client.put_resource(VDC_KIND,@pe_id,template.to_json) - return rc if OZonesClient.is_error?(rc) + return rc if Zona.is_error?(rc) nil end @@ -76,7 +76,7 @@ module Zona template = {:vdc => {:id => @pe_id, :hosts => new_hosts}} rc = @client.put_resource(VDC_KIND,@pe_id,template.to_json) - return rc if OZonesClient.is_error?(rc) + return rc if Zona.is_error?(rc) nil end diff --git a/src/ozones/Client/lib/api/zona/VDCPool.rb b/src/ozones/Client/lib/api/zona/VDCPool.rb index c019829ca5..0984ba584b 100644 --- a/src/ozones/Client/lib/api/zona/VDCPool.rb +++ b/src/ozones/Client/lib/api/zona/VDCPool.rb @@ -24,7 +24,7 @@ module Zona end def factory(element_json) - Zona::VDC.new(element_json,@client) + VDC.new(element_json,@client) end def info diff --git a/src/ozones/Client/lib/api/zona/ZoneElement.rb b/src/ozones/Client/lib/api/zona/ZoneElement.rb index 31b891a979..ce51cbeafa 100644 --- a/src/ozones/Client/lib/api/zona/ZoneElement.rb +++ b/src/ozones/Client/lib/api/zona/ZoneElement.rb @@ -17,7 +17,7 @@ module Zona class Zone < OZonesElement - + ZONE_KIND = "zone" def self.build_json(pe_id=nil) @@ -26,13 +26,13 @@ module Zona else json = '{"ZONE":{}}' end - JSONElement.build_json(json,"ZONE") + OZonesJSON.build_json(json,"ZONE") end def initialize(hash, client) super(hash, client) end - + def info super(ZONE_KIND,"ZONE") end diff --git a/src/ozones/Client/lib/api/zona/ZonePool.rb b/src/ozones/Client/lib/api/zona/ZonePool.rb index 1cfe72280d..663d313a36 100644 --- a/src/ozones/Client/lib/api/zona/ZonePool.rb +++ b/src/ozones/Client/lib/api/zona/ZonePool.rb @@ -17,7 +17,7 @@ module Zona class ZonePool < OZonesPool - + ZONE_POOL_KIND = "zone" def initialize(client) @@ -25,7 +25,7 @@ module Zona end def factory(element_json) - Zona::Zone.new(element_json,@client) + Zone.new(element_json,@client) end def info diff --git a/src/ozones/Client/lib/cli/ozones_helper.rb b/src/ozones/Client/lib/cli/ozones_helper.rb index 70115f8832..20c319d1d7 100644 --- a/src/ozones/Client/lib/cli/ozones_helper.rb +++ b/src/ozones/Client/lib/cli/ozones_helper.rb @@ -14,14 +14,14 @@ # limitations under the License. # #--------------------------------------------------------------------------- # -require 'OZonesClient' +require 'zona' module OZonesHelper class OZHelper def initialize(user=nil, pass=nil, endpoint_str=nil, timeout=nil, debug_flag=true) - @client = OZonesClient::Client.new(user, + @client = Zona::Client.new(user, pass, endpoint_str, timeout, @@ -31,7 +31,7 @@ module OZonesHelper def create_resource(kind, template) rc = @client.post_resource_file(kind, template) - if OZonesClient::is_error?(rc) + if Zona::is_error?(rc) [-1, rc.message] else id = get_id(rc) @@ -42,10 +42,10 @@ module OZonesHelper def list_pool(kind, options) rc = @client.get_pool(kind) - if OZonesClient::is_error?(rc) + if Zona::is_error?(rc) [-1, rc.message] else - pool=OZonesClient::parse_json(rc.body, kind.upcase + "_POOL") + pool=Zona::OZonesJSON.parse_json(rc.body, kind.upcase + "_POOL") format_pool(pool, options) end end @@ -53,10 +53,10 @@ module OZonesHelper def show_resource(kind, id, options) rc = @client.get_resource(kind, id) - if OZonesClient::is_error?(rc) + if Zona::is_error?(rc) [-1, rc.message] else - resource=OZonesClient::parse_json(rc.body, kind.upcase) + resource=Zona::OZonesJSON.parse_json(rc.body, kind.upcase) format_resource(resource, options) end end @@ -64,10 +64,10 @@ module OZonesHelper def delete_resource(kind, id, options) rc = @client.delete_resource(kind, id) - if OZonesClient::is_error?(rc) + if Zona::is_error?(rc) [-1, rc.message] else - message=OZonesClient::parse_json(rc.body, "message") + message=Zona::OZonesJSON.parse_json(rc.body, "message") [0, "#{message}"] end end diff --git a/src/ozones/Client/lib/cli/ozones_helper/vdc_helper.rb b/src/ozones/Client/lib/cli/ozones_helper/vdc_helper.rb index 6ddb48e412..58c0b57b60 100644 --- a/src/ozones/Client/lib/cli/ozones_helper/vdc_helper.rb +++ b/src/ozones/Client/lib/cli/ozones_helper/vdc_helper.rb @@ -33,7 +33,7 @@ class VDCHelper < OZonesHelper::OZHelper rc = @client.post_resource_str(@vdc_str, tmpl_str) - if OZonesClient::is_error?(rc) + if Zona::is_error?(rc) [-1, rc.message] else id = get_id(rc) @@ -56,10 +56,10 @@ class VDCHelper < OZonesHelper::OZHelper def addhost(id, host_array, options) rc = @client.get_resource(@vdc_str, id) - if OZonesClient::is_error?(rc) + if Zona::is_error?(rc) return [-1, rc.message] else - vdc = OZonesClient::parse_json(rc.body, @vdc_str.upcase) + vdc = Zona::OZonesJSON.parse_json(rc.body, @vdc_str.upcase) end hosts = vdc['hosts'].split(',').collect!{|x| x.to_i} @@ -74,7 +74,7 @@ class VDCHelper < OZonesHelper::OZHelper rc = @client.put_resource_str(@vdc_str, id, template) - if OZonesClient::is_error?(rc) + if Zona::is_error?(rc) return [-1, rc.message] end @@ -84,10 +84,10 @@ class VDCHelper < OZonesHelper::OZHelper def delhost(id, host_array, options) rc = @client.get_resource(@vdc_str, id) - if OZonesClient::is_error?(rc) + if Zona::is_error?(rc) return [-1, rc.message] else - vdc = OZonesClient::parse_json(rc.body, @vdc_str.upcase) + vdc = Zona::OZonesJSON.parse_json(rc.body, @vdc_str.upcase) end hosts = vdc['hosts'].split(',').collect!{|x| x.to_i} @@ -97,7 +97,7 @@ class VDCHelper < OZonesHelper::OZHelper rc = @client.put_resource_str(@vdc_str, id, template) - if OZonesClient::is_error?(rc) + if Zona.is_error?(rc) return [-1, rc.message] end diff --git a/src/ozones/Server/lib/OZones/Zones.rb b/src/ozones/Server/lib/OZones/Zones.rb index 6c1567aee6..3c40601f48 100644 --- a/src/ozones/Server/lib/OZones/Zones.rb +++ b/src/ozones/Server/lib/OZones/Zones.rb @@ -60,7 +60,7 @@ module OZones zone_attributes["ZONE"][:vdcs] = Array.new self.vdcs.all.each{|vdc| - zone_attributes["ZONE"][:vdcs]<