From: marios <[email protected]>

Signed-off-by: marios <[email protected]>
---
 server/lib/cimi/model/volume.rb |   22 +++++++++++++++++++---
 server/lib/cimi/server.rb       |   12 ++----------
 2 files changed, 21 insertions(+), 13 deletions(-)

diff --git a/server/lib/cimi/model/volume.rb b/server/lib/cimi/model/volume.rb
index 4c19889..63dcf86 100644
--- a/server/lib/cimi/model/volume.rb
+++ b/server/lib/cimi/model/volume.rb
@@ -44,15 +44,31 @@ class CIMI::Model::Volume < CIMI::Model::Base
 
   def self.all(context); find(:all, context); end
 
-  def self.create(params, context)
+  def self.create_from_json(json_in, context)
+    json = JSON.parse(json_in)
+    volume_config_id = 
json["volumeTemplate"]["volumeConfig"]["href"].split("/").last
+    volume_image_id = (json["volumeTemplate"].has_key?("volumeImage") ?
+                json["volumeTemplate"]["volumeImage"]["href"].split("/").last  
: nil)
+    create_volume({:volume_config_id=>volume_config_id, 
:volume_image_id=>volume_image_id}, context)
+  end
+
+  def self.create_from_xml(xml_in, context)
+    xml = XmlSimple.xml_in(xml_in)
+    volume_config_id = 
xml["volumeTemplate"][0]["volumeConfig"][0]["href"].split("/").last
+    volume_image_id = (xml["volumeTemplate"][0].has_key?("volumeImage") ?
+             
xml["volumeTemplate"][0]["volumeImage"][0]["href"].split("/").last  : nil)
+    create_volume({:volume_config_id=>volume_config_id, 
:volume_image_id=>volume_image_id}, context)
+  end
+
+  private
+
+  def self.create_volume(params, context)
     volume_config = VolumeConfiguration.find(params[:volume_config_id], 
context)
     opts = {:capacity=>volume_config.capacity[:quantity], 
:snapshot_id=>params[:volume_image_id] }
     storage_volume = self.driver.create_storage_volume(context.credentials, 
opts)
     from_storage_volume(storage_volume, context)
   end
 
-  private
-
   def self.from_storage_volume(volume, context)
     self.new( { :name => volume.id,
                 :description => volume.id,
diff --git a/server/lib/cimi/server.rb b/server/lib/cimi/server.rb
index 9671df2..c0d2627 100644
--- a/server/lib/cimi/server.rb
+++ b/server/lib/cimi/server.rb
@@ -345,18 +345,10 @@ global_collection :volumes do
           #((request.content_type.end_with?("+xml")) ? :xml : 
report_error(415) ) FIXME
       case content_type
         when :json
-          json = JSON.parse(request.body.read)
-          volume_config_id = 
json["volumeTemplate"]["volumeConfig"]["href"].split("/").last
-          volume_image_id = (json["volumeTemplate"].has_key?("volumeImage") ?
-                      
json["volumeTemplate"]["volumeImage"]["href"].split("/").last  : nil)
+          new_volume = Volume.create_from_json(request.body.read, self)
         when :xml
-          xml = XmlSimple.xml_in(request.body.read)
-          volume_config_id = 
xml["volumeTemplate"][0]["volumeConfig"][0]["href"].split("/").last
-          volume_image_id = (xml["volumeTemplate"][0].has_key?("volumeImage") ?
-                      
xml["volumeTemplate"][0]["volumeImage"][0]["href"].split("/").last  : nil)
+          new_volume = Volume.create_from_xml(request.body.read, self)
       end
-      params.merge!( {:volume_config_id => volume_config_id, :volume_image_id 
=> volume_image_id} )
-      new_volume = Volume.create(params, self)
       respond_to do |format|
         format.json { new_volume.to_json }
         format.xml { new_volume.to_xml }
-- 
1.7.6.4

Reply via email to