From ed9f730dce6a71535d0c3408256b9d2402fad722 Mon Sep 17 00:00:00 2001
From: Daniel Molina <danmolin@fdi.ucm.es>
Date: Wed, 17 Feb 2010 11:41:16 +0100
Subject: [PATCH] OpenNebula driver: Changing Crack to REXML

---
 .../deltacloud/drivers/opennebula/cloud_client.rb  |    7 -
 .../deltacloud/drivers/opennebula/occi_client.rb   |  175 +-------------------
 .../drivers/opennebula/opennebula_driver.rb        |    4 +-
 3 files changed, 5 insertions(+), 181 deletions(-)

diff --git a/server/libexec/lib/deltacloud/drivers/opennebula/cloud_client.rb b/server/libexec/lib/deltacloud/drivers/opennebula/cloud_client.rb
index 19f989d..c21d079 100644
--- a/server/libexec/lib/deltacloud/drivers/opennebula/cloud_client.rb
+++ b/server/libexec/lib/deltacloud/drivers/opennebula/cloud_client.rb
@@ -23,13 +23,6 @@ require 'uri'
 require 'net/https'
 
 begin
-    require 'curb'
-    CURL_LOADED=true
-rescue LoadError
-    CURL_LOADED=false
-end
-
-begin
     require 'net/http/post/multipart'
 rescue LoadError
 end
diff --git a/server/libexec/lib/deltacloud/drivers/opennebula/occi_client.rb b/server/libexec/lib/deltacloud/drivers/opennebula/occi_client.rb
index f3ceb52..e34deb8 100644
--- a/server/libexec/lib/deltacloud/drivers/opennebula/occi_client.rb
+++ b/server/libexec/lib/deltacloud/drivers/opennebula/occi_client.rb
@@ -18,8 +18,8 @@
 #--------------------------------------------------------------------------- #
 
 require 'rubygems'
-require 'crack'
 require 'uri'
+require 'rexml/document'
 
 require 'deltacloud/drivers/opennebula/cloud_client'
 
@@ -111,114 +111,6 @@ module OCCIClient
         end
 
         ######################################################################
-        # Post a new Network to the VN Pool
-        # :xmlfile xml description of the Virtual Network
-        ######################################################################
-        def post_network(xmlfile)
-            xml=File.read(xmlfile)
-
-            url = URI.parse(@endpoint+"/network")
-
-            req = Net::HTTP::Post.new(url.path)
-            req.body=xml
-
-            req.basic_auth @occiauth[0], @occiauth[1]
-
-            res = CloudClient::http_start(url) do |http|
-                http.request(req)
-            end
-
-            if CloudClient::is_error?(res)
-                return res
-            else
-                return res.body
-            end
-        end
-
-        ######################################################################
-        # Retieves the pool of Virtual Networks
-        ######################################################################
-        def get_networks
-            url = URI.parse(@endpoint+"/network")
-            req = Net::HTTP::Get.new(url.path)
-
-            req.basic_auth @occiauth[0], @occiauth[1]
-
-            res = CloudClient::http_start(url) {|http|
-                http.request(req)
-            }
-
-            if CloudClient::is_error?(res)
-                return res
-            else
-                return res.body
-            end
-        end
-
-        ######################################################################
-        # Post a new Image to the Image Pool
-        # :xmlfile
-        ######################################################################
-        def post_image(xmlfile, curb=true)
-            xml=File.read(xmlfile)
-            image_info=Crack::XML.parse(xml)
-
-            file_path = image_info['DISK']['URL']
-
-            m=file_path.match(/^\w+:\/\/(.*)$/)
-
-            if m
-                file_path="/"+m[1]
-            end
-
-            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.multipart_form_post = true
-
-                begin
-                    curl.http_post(
-                      Curl::PostField.content('occixml', xml),
-                      Curl::PostField.file('file', file_path)
-                    )
-                rescue Exception => e
-                    return CloudClient::Error.new(e.message)
-                end
-
-                return curl.body_str
-            else
-                file=File.open(file_path)
-
-                params=Hash.new
-                params["file"]=UploadIO.new(file,
-                    'application/octet-stream', file_path)
-
-                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) do |http|
-                    http.request(req)
-                end
-
-                file.close
-
-                if CloudClient::is_error?(res)
-                    return res
-                else
-                    return res.body
-                end
-            end
-        end
-
-        ######################################################################
         # Retieves the pool of Images owned by the user
         ######################################################################
         def get_images
@@ -268,9 +160,9 @@ module OCCIClient
         ######################################################################
         def put_vm(xmlfile)
             xml=File.read(xmlfile)
-            vm_info=Crack::XML.parse(xml)
+            vm_info=REXML::Document.new(xml).root.elements
 
-            url = URI.parse(@endpoint+'/compute/' + vm_info['COMPUTE']['ID'])
+            url = URI.parse(@endpoint+'/compute/' + vm_info['ID'].text)
 
             req = Net::HTTP::Put.new(url.path)
             req.body = xml
@@ -288,67 +180,6 @@ module OCCIClient
             end
         end
 
-        ####################################################################
-        # :id Compute identifier
-        ####################################################################
-        def delete_vm(id)
-            url = URI.parse(@endpoint+"/compute/" + id.to_s)
-            req = Net::HTTP::Delete.new(url.path)
-
-            req.basic_auth @occiauth[0], @occiauth[1]
-
-            res = CloudClient::http_start(url) {|http|
-                http.request(req)
-            }
-
-            if CloudClient::is_error?(res)
-                return res
-            else
-                return res.body
-            end
-        end
-
-        ######################################################################
-        # Retrieves a Virtual Network
-        # :id Virtual Network identifier
-        ######################################################################
-        def get_network(id)
-            url = URI.parse(@endpoint+"/network/" + id.to_s)
-            req = Net::HTTP::Get.new(url.path)
-
-            req.basic_auth @occiauth[0], @occiauth[1]
-
-            res = CloudClient::http_start(url) {|http|
-                http.request(req)
-            }
-
-            if CloudClient::is_error?(res)
-                return res
-            else
-                return res.body
-            end
-        end
-
-        ######################################################################
-        # :id VM identifier
-        ######################################################################
-        def delete_network(id)
-            url = URI.parse(@endpoint+"/network/" + id.to_s)
-            req = Net::HTTP::Delete.new(url.path)
-
-            req.basic_auth @occiauth[0], @occiauth[1]
-
-            res = CloudClient::http_start(url) {|http|
-                http.request(req)
-            }
-
-            if CloudClient::is_error?(res)
-                return res
-            else
-                return res.body
-            end
-        end
-
        #######################################################################
         # Retieves an Image
         # :image_uuid Image identifier
diff --git a/server/libexec/lib/deltacloud/drivers/opennebula/opennebula_driver.rb b/server/libexec/lib/deltacloud/drivers/opennebula/opennebula_driver.rb
index dc97f87..e88131a 100644
--- a/server/libexec/lib/deltacloud/drivers/opennebula/opennebula_driver.rb
+++ b/server/libexec/lib/deltacloud/drivers/opennebula/opennebula_driver.rb
@@ -246,7 +246,7 @@ class OpennebulaDriver < Deltacloud::BaseDriver
 			<DISK image="<%= image_id %>" dev="sda1" />
 		</STORAGE>
 	</COMPUTE>
-  }.gsub(/^        /, '') ) unless defined?( DELTA_VM )
+  }.gsub(/^        /, '') ) unless defined?( OCCI_VM )
 
 
   (OCCI_ACTION = %q{
@@ -254,7 +254,7 @@ class OpennebulaDriver < Deltacloud::BaseDriver
 		<ID><%= id %></ID>
 		<STATE><%= strstate %></STATE>
 	</COMPUTE>
-  }.gsub(/^        /, '') ) unless defined?( DELTA_ACTION )
+  }.gsub(/^        /, '') ) unless defined?( OCCI_ACTION )
 
  end
 
-- 
1.6.5.1

