From 4f2a7cd86977436a6fd389e08dbe508c9cefb112 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Tue, 20 Jul 2010 20:05:59 +0200 Subject: [PATCH] bug #213: Check if multipart post gem is loaded --- src/cloud/common/CloudClient.rb | 2 ++ src/cloud/ec2/lib/EC2QueryClient.rb | 55 ++++++++++++++++++----------- src/cloud/occi/lib/OCCIClient.rb | 14 +++++++- 3 files changed, 49 insertions(+), 22 deletions(-) diff --git a/src/cloud/common/CloudClient.rb b/src/cloud/common/CloudClient.rb index 6990641739..abbafd27ec 100644 --- a/src/cloud/common/CloudClient.rb +++ b/src/cloud/common/CloudClient.rb @@ -29,7 +29,9 @@ end begin require 'net/http/post/multipart' + MULTIPART_LOADED=true rescue LoadError + MULTIPART_LOADED=false end ############################################################################### diff --git a/src/cloud/ec2/lib/EC2QueryClient.rb b/src/cloud/ec2/lib/EC2QueryClient.rb index aa08866075..358e7a2958 100644 --- a/src/cloud/ec2/lib/EC2QueryClient.rb +++ b/src/cloud/ec2/lib/EC2QueryClient.rb @@ -30,18 +30,19 @@ module EC2QueryClient # # ########################################################################## - class Client + class Client API_VERSION = '2008-12-01' - + ###################################################################### # # ###################################################################### - def initialize(secret=nil, endpoint=nil) + def initialize(secret=nil, endpoint=nil, timeout=nil) # Autentication - ec2auth=nil - + ec2auth = nil + @timeout = nil + if secret ec2auth = secret.split(':') elsif ENV["EC2_ACCESS_KEY"] and ENV["EC2_SECRET_KEY"] @@ -49,16 +50,16 @@ module EC2QueryClient else ec2auth=CloudClient::get_one_auth end - + if !ec2auth raise "No authorization data present" end - + @access_key_id = ec2auth[0] @access_key_secret = Digest::SHA1.hexdigest(ec2auth[1]) - + # Server location - + if !endpoint if $ec2url endpoint = $ec2url @@ -66,9 +67,9 @@ module EC2QueryClient endpoint = "http://localhost:4567" end end - + @uri = URI.parse(endpoint) - + @ec2_connection = AWS::EC2::Base.new( :access_key_id => @access_key_id, :secret_access_key => @access_key_secret, @@ -89,7 +90,7 @@ module EC2QueryClient error = CloudClient::Error.new(e.message) return error end - + return response end @@ -106,12 +107,12 @@ module EC2QueryClient :instance_type => type, :user_data => user_data, :base64_encoded => true - ) + ) rescue Exception => e error = CloudClient::Error.new(e.message) return error end - + return response end @@ -128,13 +129,13 @@ module EC2QueryClient error = CloudClient::Error.new(e.message) return error end - + return response end ###################################################################### # - # Returns true if HTTP code is 200, + # Returns true if HTTP code is 200, ###################################################################### def upload_image(file_name, curb=true) params = { "Action" => "UploadImage", @@ -146,10 +147,16 @@ module EC2QueryClient str = AWS.canonical_string(params, @uri.host) sig = AWS.encode(@access_key_secret, str, false) - + post_fields = Array.new; - if curb and CURL_LOADED + if curb + if !CURL_LOADED + error_msg = "curb gem not loaded" + error = CloudClient::Error.new(error_msg) + return error + end + params.each { |k,v| post_fields << Curl::PostField.content(k,v) } @@ -168,6 +175,12 @@ module EC2QueryClient return CloudClient::Error.new(connection.body_str) end else + if !MULTIPART_LOADED + error_msg = "multipart-post gem not loaded" + error = CloudClient::Error.new(error_msg) + return error + end + params["Signature"]=sig file=File.open(file_name) @@ -175,7 +188,7 @@ module EC2QueryClient 'application/octet-stream', file_name) req = Net::HTTP::Post::Multipart.new('/', params) - res = CloudClient.http_start(@uri) do |http| + res = CloudClient.http_start(@uri,@timeout) do |http| http.request(req) end @@ -202,7 +215,7 @@ module EC2QueryClient error = CloudClient::Error.new(e.message) return error end - + return response end @@ -218,7 +231,7 @@ module EC2QueryClient error = CloudClient::Error.new(e.message) return error end - + return response end end diff --git a/src/cloud/occi/lib/OCCIClient.rb b/src/cloud/occi/lib/OCCIClient.rb index 5ca53f22a1..3d8b1ba065 100755 --- a/src/cloud/occi/lib/OCCIClient.rb +++ b/src/cloud/occi/lib/OCCIClient.rb @@ -176,7 +176,13 @@ module OCCIClient file_path="/"+m[1] end - if curb and CURL_LOADED + if curb + if !CURL_LOADED + error_msg = "curb gem not loaded" + error = CloudClient::Error.new(error_msg) + return error + end + curl=Curl::Easy.new(@endpoint+"/storage") curl.http_auth_types = Curl::CURLAUTH_BASIC @@ -195,6 +201,12 @@ module OCCIClient return curl.body_str else + if !MULTIPART_LOADED + error_msg = "multipart-post gem not loaded" + error = CloudClient::Error.new(error_msg) + return error + end + file=File.open(file_path) params=Hash.new