server/lib/deltacloud/drivers/opennebula/occi_client.rb
######################################################################
# Post a new Image to the Image Pool
# :xmlfile
######################################################################
Post a new Image to the Image Pool
:xmlfile
Post a new Image to the Image Pool : xmlfile
def post_image(xmlfile, curb=true)
      xml        = File.read(xmlfile)
      begin
        image_info = REXML::Document.new(xml).root
      rescue Exception => e
        error = CloudClient::Error.new(e.message)
        return error
      end
      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
      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
        curl.userpwd             = "#{@occiauth[0]}:#{@occiauth[1]}"
        curl.verbose             = true if @debug
        curl.multipart_form_post = true
        begin
          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
        return curl.body_str
      else
        if !MULTIPART_LOADED
          error_msg = "multipart-post gem not loaded"
          error = CloudClient::Error.new(error_msg)
          return error
        end
        params=Hash.new
        if file_path
          file=File.open(file_path)
          params["file"]=UploadIO.new(file,
            'application/octet-stream', file_path)
        end
        params['occixml'] = xml
        url = URI.parse(@endpoint+"/storage")
        req = Net::HTTP::Post::Multipart.new(url.path, params)
        req.basic_auth @occiauth[0], @occiauth[1]
        res = CloudClient::http_start(url, @timeout) do |http|
          http.request(req)
        end
        file.close if file_path
        if CloudClient::is_error?(res)
          return res
        else
          return res.body
        end
      end
    end
def post_image ( xmlfile , curb = true ) xml = File . read ( xmlfile ) begin image_info = REXML :: Document . new ( xml ) . root rescue Exception => e error = CloudClient :: Error . new ( e . message ) return error end 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 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 curl . userpwd = "#{@occiauth[0]}:#{@occiauth[1]}" curl . verbose = true if @debug curl . multipart_form_post = true begin 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 return curl . body_str else if ! MULTIPART_LOADED error_msg = "multipart-post gem not loaded" error = CloudClient :: Error . new ( error_msg ) return error end params = Hash . new if file_path file = File . open ( file_path ) params [ "file" ] = UploadIO . new ( file , 'application/octet-stream' , file_path ) end params [ 'occixml' ] = xml url = URI . parse ( @endpoint + "/storage" ) req = Net :: HTTP :: Post :: Multipart . new ( url . path , params ) req . basic_auth @occiauth [ 0 ] , @occiauth [ 1 ] res = CloudClient :: http_start ( url , @timeout ) do | http | http . request ( req ) end file . close if file_path if CloudClient :: is_error? ( res ) return res else return res . body end end end
Post a new Image to the Image Pool
:xmlfile
Post a new Image to the Image Pool : xmlfile
xmlfile
<not_specific>
curb
<not_specific>