diff --git a/src/cloud/occi/lib/ImageOCCI.rb b/src/cloud/occi/lib/ImageOCCI.rb index 0ee1f97c33..77a6150349 100755 --- a/src/cloud/occi/lib/ImageOCCI.rb +++ b/src/cloud/occi/lib/ImageOCCI.rb @@ -46,6 +46,9 @@ class ImageOCCI < Image <% if @image_info['DESCRIPTION'] != nil %> DESCRIPTION = "<%= @image_info['DESCRIPTION'] %>" <% end %> + <% if @image_file != nil %> + PATH = "<%= @image_file %>" + <% end %> <% if @image_info['PUBLIC'] != nil %> PUBLIC = "<%= @image_info['PUBLIC'] %>" <% end %> @@ -64,9 +67,14 @@ class ImageOCCI < Image }.gsub(/^ /, '') # Class constructor - def initialize(xml, client, xml_info=nil) + def initialize(xml, client, xml_info=nil, file=nil) super(xml, client) @image_info = nil + @image_file = file + + if file && file[:tempfile] + @image_file = file[:tempfile].path + end if xml_info != nil xmldoc = XMLElement.build_xml(xml_info, 'STORAGE') diff --git a/src/cloud/occi/lib/OCCIClient.rb b/src/cloud/occi/lib/OCCIClient.rb index 29bf1f7279..6c7041f22a 100755 --- a/src/cloud/occi/lib/OCCIClient.rb +++ b/src/cloud/occi/lib/OCCIClient.rb @@ -170,18 +170,18 @@ module OCCIClient return error end - if image_info.elements['URL'] == nil + if image_info.elements['URL'] + file_path = image_info.elements['URL'].text + + m = file_path.match(/^\w+:\/\/(.*)$/) + + if m + file_path="/"+m[1] + end + elsif !image_info.elements['TYPE'] == "DATABLOCK" return CloudClient::Error.new("Can not find URL") end - file_path = image_info.elements['URL'].text - - m = file_path.match(/^\w+:\/\/(.*)$/) - - if m - file_path="/"+m[1] - end - if curb if !CURL_LOADED error_msg = "curb gem not loaded" @@ -197,10 +197,14 @@ module OCCIClient curl.multipart_form_post = true begin - curl.http_post( - Curl::PostField.content('occixml', xml), - Curl::PostField.file('file', file_path) - ) + postfields = Array.new + postfields << Curl::PostField.content('occixml', xml) + + if file_path + postfields << Curl::PostField.file('file', file_path) + end + + curl.http_post(*postfields) rescue Exception => e return CloudClient::Error.new(e.message) end @@ -213,11 +217,13 @@ module OCCIClient return error end - file=File.open(file_path) - params=Hash.new - params["file"]=UploadIO.new(file, - 'application/octet-stream', file_path) + + if file_path + file=File.open(file_path) + params["file"]=UploadIO.new(file, + 'application/octet-stream', file_path) + end params['occixml'] = xml @@ -231,8 +237,8 @@ module OCCIClient http.request(req) end - file.close - pp res + file.close if file_path + if CloudClient::is_error?(res) return res else diff --git a/src/cloud/occi/lib/OCCIServer.rb b/src/cloud/occi/lib/OCCIServer.rb index 1c3afb198a..da8db35353 100755 --- a/src/cloud/occi/lib/OCCIServer.rb +++ b/src/cloud/occi/lib/OCCIServer.rb @@ -401,16 +401,18 @@ class OCCIServer < CloudServer image = ImageOCCI.new( Image.build_xml, get_client(request.env), - occixml) + occixml, + request.params['file']) - rc = add_image(image, request.params['file']) - return rc, 500 if OpenNebula.is_error?(rc) + # --- Generate the template and Allocate the new Instance --- + template = image.to_one_template + return template, 500 if OpenNebula.is_error?(template) - # --- Enable the new Image --- - rc = image.enable + rc = image.allocate(template) return rc, 500 if OpenNebula.is_error?(rc) # --- Prepare XML Response --- + image.info return to_occi_xml(image, 201) end