From afeb766096ab4f3c91e0cdeeb079fc58c53232fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20S=2E=20Montero?= Date: Fri, 23 Oct 2009 11:54:13 +0000 Subject: [PATCH] OCCI uses new Error system, improved return for http_start git-svn-id: http://svn.opennebula.org/one/trunk@885 3034c82b-c49b-4eb3-8279-a7acafdc01c0 --- src/cloud/common/CloudClient.rb | 7 +- src/cloud/occi/bin/occi-compute | 124 ++++++++++++++----------------- src/cloud/occi/bin/occi-network | 70 ++++++++--------- src/cloud/occi/bin/occi-storage | 61 +++++++-------- src/cloud/occi/lib/OCCIClient.rb | 94 ++++++++++++++++++----- 5 files changed, 203 insertions(+), 153 deletions(-) diff --git a/src/cloud/common/CloudClient.rb b/src/cloud/common/CloudClient.rb index f00183052c..69c7845e4f 100644 --- a/src/cloud/common/CloudClient.rb +++ b/src/cloud/common/CloudClient.rb @@ -79,9 +79,10 @@ module CloudClient block.call(connection) end rescue Errno::ECONNREFUSED => e - puts "Error connecting to server (" + e.to_s + ")." - puts "Server: #{url.host}:#{url.port}" - exit -1 + str = "Error connecting to server (#{e.to_s})." + str << "Server: #{url.host}:#{url.port}" + + return CloudClient::Error.new(str) end end diff --git a/src/cloud/occi/bin/occi-compute b/src/cloud/occi/bin/occi-compute index 639286f0b0..cbd5e065d1 100755 --- a/src/cloud/occi/bin/occi-compute +++ b/src/cloud/occi/bin/occi-compute @@ -116,96 +116,82 @@ begin end end rescue Exception => e - exit -1 + exit(-1) end begin occi_client = OCCIClient::Client.new(url,username,password,debug) rescue Exception => e puts "#{cmd_name}: #{e.message}" - exit -1 + exit(-1) +end + + +begin + occi_client = OCCIClient::Client.new(url,username,password,debug) +rescue Exception => e + puts "#{cmd_name}: #{e.message}" + exit(-1) end if !ARGV[0] puts "#{cmd_name}: [COMMAND] not present" puts "#{cmd_name}: Execute #{cmd_name} -h for help." - exit -1 + exit(-1) end case ARGV[0].downcase -when 'list' - occi_client.get_vms + when 'list' + rc = occi_client.get_vms -when 'create' - vm_xml = ARGV[1] + when 'create' + vm_xml = ARGV[1] - if !vm_xml || !File.exists?(vm_xml) - puts "#{cmd_name} create: missing OCCI-XML parameter or file not found" - exit -1 - end + if !vm_xml || !File.exists?(vm_xml) + puts "#{cmd_name} create: missing OCCI-XML or file not found" + exit(-1) + end - begin - occi_client = OCCIClient::Client.new(url,username,password,debug) - rescue Exception => e - puts "#{cmd_name} create: #{e.message}" - exit -1 - end + rc = occi_client.post_vms(vm_xml) + + when 'show' + vm_id = ARGV[1] - occi_client.post_vms(vm_xml) - -when 'show' - vm_id = ARGV[1] + if !vm_id + puts "#{cmd_name} show: missing VM-ID parameter" + exit(-1) + end - if !vm_id - puts "#{cmd_name} show: missing VM-ID parameter" - exit -1 - end + rc = occi_client.get_vm(vm_id) + + when 'update' + vm_xml = ARGV[1] - begin - occi_client = OCCIClient::Client.new(url,username,password,debug) - rescue Exception => e - puts "#{cmd_name} show: #{e.message}" - exit -1 - end + if !vm_xml || !File.exists?(vm_xml) + puts "#{cmd_name} update: missing OCCI-XML or file not found" + exit -1 + end - occi_client.get_vm(vm_id) - -when 'update' - vm_xml = ARGV[1] + rc = occi_client.put_vm(vm_xml) + + when 'delete' + vm_id = ARGV[1] - if !vm_xml || !File.exists?(vm_xml) - puts "#{cmd_name} update: missing OCCI-XML parameter or file not found" - exit -1 - end + if !vm_id + puts "#{cmd_name} delete: missing VM-ID parameter" + exit -1 + end - begin - occi_client = OCCIClient::Client.new(url,username,password,debug) - rescue Exception => e - puts "#{cmd_name} update: #{e.message}" - exit -1 - end - - occi_client.put_vm(vm_xml) - -when 'delete' - vm_id = ARGV[1] - - if !vm_id - puts "#{cmd_name} delete: missing VM-ID parameter" - exit -1 - end - - begin - occi_client = OCCIClient::Client.new(url,username,password,debug) - rescue Exception => e - puts "#{cmd_name} delete: #{e.message}" - exit -1 - end - - occi_client.delete_vm(vm_id) - -else - puts "Command #{ARGV[0]} not valid." - exit -1 - + rc = occi_client.delete_vm(vm_id) + + else + puts "Command #{ARGV[0]} not valid." + exit(-1) end + +if CloudClient::is_error?(rc) + puts rc.to_s() +else + puts rc +end + diff --git a/src/cloud/occi/bin/occi-network b/src/cloud/occi/bin/occi-network index 7b736c8a67..a833f178a1 100755 --- a/src/cloud/occi/bin/occi-network +++ b/src/cloud/occi/bin/occi-network @@ -116,7 +116,7 @@ begin end end rescue Exception => e - exit -1 + exit(-1) end @@ -124,56 +124,58 @@ begin occi_client = OCCIClient::Client.new(url,username,password,debug) rescue Exception => e puts "#{cmd_name}: #{e.message}" - exit -1 + exit(-1) end if !ARGV[0] puts "#{cmd_name}: [COMMAND] not present" puts "#{cmd_name}: Execute #{cmd_name} -h for help." - exit -1 + exit(-1) end case ARGV[0].downcase -when 'create' - network_xml = ARGV[1] + when 'create' + network_xml = ARGV[1] - if !network_xml || !File.exists?(network_xml) - puts "#{cmd_name} create: missing OCCI-XML parameter or file not found" - exit -1 - end + if !network_xml || !File.exists?(network_xml) + puts "#{cmd_name} create: missing OCCI-XML or file not found" + exit(-1) + end - occi_client.post_network(network_xml) + rc = occi_client.post_network(network_xml) -when 'list' - occi_client.get_networks + when 'list' + rc = occi_client.get_networks -when 'show' - network_id = ARGV[1] + when 'show' + network_id = ARGV[1] - if !network_id - puts "#{cmd_name} show: missing NETWORK-ID parameter or file not found" - exit -1 - end - - occi_client.get_network(network_id) + if !network_id + puts "#{cmd_name} show: missing NETWORK-ID or file not found" + exit(-1) + end + + rc = occi_client.get_network(network_id) + pp "KKKK" -when 'delete' - network_id = ARGV[1] + when 'delete' + network_id = ARGV[1] - if !network_id - puts "#{cmd_name} delete: missing NETWORK-ID parameter" - exit -1 - end + if !network_id + puts "#{cmd_name} delete: missing NETWORK-ID parameter" + exit(-1) + end - occi_client.delete_network(network_id) + rc = occi_client.delete_network(network_id) + else + puts "Command #{ARGV[0]} not valid." + exit(-1) +end + +if CloudClient::is_error?(rc) + puts rc.to_s() else - puts "Command #{ARGV[0]} not valid." - exit -1 - + puts rc end - - - - diff --git a/src/cloud/occi/bin/occi-storage b/src/cloud/occi/bin/occi-storage index 8c5dcbaa9a..e717cf8e70 100755 --- a/src/cloud/occi/bin/occi-storage +++ b/src/cloud/occi/bin/occi-storage @@ -118,13 +118,13 @@ begin end end rescue Exception => e - exit -1 + exit(-1) end if !ARGV[0] puts "#{cmd_name}: [COMMAND] not present" puts "#{cmd_name}: Execute #{cmd_name} -h for help." - exit -1 + exit(-1) end @@ -132,43 +132,44 @@ begin occi_client = OCCIClient::Client.new(url,username,password,debug) rescue Exception => e puts "#{cmd_name}: #{e.message}" - exit -1 + exit(-1) end case ARGV[0].downcase -when 'create' - image_xml = ARGV[1] + when 'create' + image_xml = ARGV[1] - if !image_xml || !File.exists?(image_xml) - puts "#{cmd_name} create: missing occi xml parameter or file not found" - exit -1 - end + if !image_xml || !File.exists?(image_xml) + puts "#{cmd_name} create: missing occi xml or file not found" + exit(-1) + end - occi_client.post_image(image_xml, curb) + rc = occi_client.post_image(image_xml, curb) -when 'list' - occi_client.get_images + when 'list' + rc = occi_client.get_images -when 'show' - image_id = ARGV[1] + when 'show' + image_id = ARGV[1] - if !image_id - puts "#{cmd_name} show: missing storage id parameter or file not found" - exit -1 - end - - occi_client.get_image(image_id) + if !image_id + puts "#{cmd_name} show: missing storage id parameter" + exit(-1) + end + + rc = occi_client.get_image(image_id) -when 'delete' - puts 'Delete not yet implemented' - exit -1 + when 'delete' + puts 'Delete not yet implemented' + exit(-1) -else - puts "Command #{ARGV[0]} not valid." - exit -1 - + else + puts "Command #{ARGV[0]} not valid." + exit(-1) end - - - +if CloudClient::is_error?(rc) + puts rc.to_s() +else + puts rc +end diff --git a/src/cloud/occi/lib/OCCIClient.rb b/src/cloud/occi/lib/OCCIClient.rb index 4c4a80a822..6b30a2b58a 100755 --- a/src/cloud/occi/lib/OCCIClient.rb +++ b/src/cloud/occi/lib/OCCIClient.rb @@ -18,6 +18,7 @@ #--------------------------------------------------------------------------- # require 'rubygems' +require 'crack' require 'uri' require 'CloudClient' @@ -30,7 +31,6 @@ module OCCIClient ##################################################################### class Client - include CloudClient ###################################################################### # Initialize client library ###################################################################### @@ -82,8 +82,12 @@ module OCCIClient res = CloudClient::http_start(url) do |http| http.request(req) end - - puts res.body + + if CloudClient::is_error?(res) + return res + else + return res.body + end end ###################################################################### @@ -98,7 +102,12 @@ module OCCIClient res = CloudClient::http_start(url) {|http| http.request(req) } - puts res.body + + if CloudClient::is_error?(res) + return res + else + return res.body + end end ###################################################################### @@ -119,7 +128,11 @@ module OCCIClient http.request(req) end - puts res.body + if CloudClient::is_error?(res) + return res + else + return res.body + end end ###################################################################### @@ -134,7 +147,12 @@ module OCCIClient res = CloudClient::http_start(url) {|http| http.request(req) } - puts res.body + + if CloudClient::is_error?(res) + return res + else + return res.body + end end ###################################################################### @@ -155,9 +173,10 @@ module OCCIClient if curb and CURL_LOADED curl=Curl::Easy.new(@endpoint+"/storage") - curl.http_auth_types=Curl::CURLAUTH_BASIC - curl.userpwd="#{@occiauth[0]}:#{@occiauth[1]}" - curl.verbose=true if @debug + + curl.http_auth_types = Curl::CURLAUTH_BASIC + curl.userpwd = "#{@occiauth[0]}:#{@occiauth[1]}" + curl.verbose = true if @debug curl.multipart_form_post = true begin @@ -166,10 +185,10 @@ module OCCIClient Curl::PostField.file('file', file_path) ) rescue Exception => e - pp e.message + return CloudClient::Error.new(e.message) end - puts curl.body_str + return curl.body_str else file=File.open(file_path) @@ -188,9 +207,14 @@ module OCCIClient res = CloudClient::http_start(url) do |http| http.request(req) end + file.close - puts res.body + if CloudClient::is_error?(res) + return res + else + return res.body + end end end @@ -206,7 +230,12 @@ module OCCIClient res = CloudClient::http_start(url) {|http| http.request(req) } - puts res.body + + if CloudClient::is_error?(res) + return res + else + return res.body + end end #################################### @@ -225,7 +254,12 @@ module OCCIClient res = CloudClient::http_start(url) {|http| http.request(req) } - puts res.body + + if CloudClient::is_error?(res) + return res + else + return res.body + end end ###################################################################### @@ -247,7 +281,11 @@ module OCCIClient http.request(req) end - puts res.body + if CloudClient::is_error?(res) + return res + else + return res.body + end end #################################################################### @@ -262,6 +300,12 @@ module OCCIClient res = CloudClient::http_start(url) {|http| http.request(req) } + + if CloudClient::is_error?(res) + return res + else + return res.body + end end ###################################################################### @@ -277,7 +321,12 @@ module OCCIClient res = CloudClient::http_start(url) {|http| http.request(req) } - puts res.body + + if CloudClient::is_error?(res) + return res + else + return res.body + end end ###################################################################### @@ -292,6 +341,12 @@ module OCCIClient res = CloudClient::http_start(url) {|http| http.request(req) } + + if CloudClient::is_error?(res) + return res + else + return res.body + end end ####################################################################### @@ -307,7 +362,12 @@ module OCCIClient res = CloudClient::http_start(url) {|http| http.request(req) } - puts res.body + + if CloudClient::is_error?(res) + return res + else + return res.body + end end end end