deltacloud-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mar...@apache.org
Subject svn commit: r1221834 - in /deltacloud/trunk/server/lib/cimi: model/machine.rb model/volume.rb server.rb
Date Wed, 21 Dec 2011 18:33:08 GMT
Author: marios
Date: Wed Dec 21 18:33:08 2011
New Revision: 1221834

URL: http://svn.apache.org/viewvc?rev=1221834&view=rev
Log:
Attach/Detach CIMI::Volume(s) to/from a CIMI::Machine

Modified:
    deltacloud/trunk/server/lib/cimi/model/machine.rb
    deltacloud/trunk/server/lib/cimi/model/volume.rb
    deltacloud/trunk/server/lib/cimi/server.rb

Modified: deltacloud/trunk/server/lib/cimi/model/machine.rb
URL: http://svn.apache.org/viewvc/deltacloud/trunk/server/lib/cimi/model/machine.rb?rev=1221834&r1=1221833&r2=1221834&view=diff
==============================================================================
--- deltacloud/trunk/server/lib/cimi/model/machine.rb (original)
+++ deltacloud/trunk/server/lib/cimi/model/machine.rb Wed Dec 21 18:33:08 2011
@@ -117,6 +117,21 @@ class CIMI::Model::Machine < CIMI::Model
     metadata
   end
 
+  def self.attach_volumes(volumes, context)
+    volumes.each do |vol|
+      context.driver.attach_storage_volume(context.credentials,
+      {:id=>vol[:volume].name, :instance_id=>context.params[:id], :device=>vol[:attachment_point]})
+    end
+    self.find(context.params[:id], context)
+  end
+
+  def self.detach_volumes(volumes, context)
+    volumes.each do |vol|
+      context.driver.detach_storage_volume(context.credentials, {:id=>vol[:volume].name,
:instance_id => context.params[:id]})
+    end
+    self.find(context.params[:id], context)
+  end
+
   private
 
   def self.from_instance(instance, context)
@@ -131,6 +146,7 @@ class CIMI::Model::Machine < CIMI::Model
       :disks => convert_instance_storage(instance.instance_profile, context),
       :network_interfaces => convert_instance_addresses(instance),
       :operations => convert_instance_actions(instance, context),
+      :volumes=>convert_storage_volumes(instance, context),
       :property => convert_instance_properties(instance, context)
     )
   end
@@ -202,4 +218,9 @@ class CIMI::Model::Machine < CIMI::Model
     end
   end
 
+  def self.convert_storage_volumes(instance, context)
+    instance.storage_volumes.map{|vol| {:href=>context.volume_url(vol.values.first),
+                                       :attachment_point=>vol.keys.first} }
+  end
+
 end

Modified: deltacloud/trunk/server/lib/cimi/model/volume.rb
URL: http://svn.apache.org/viewvc/deltacloud/trunk/server/lib/cimi/model/volume.rb?rev=1221834&r1=1221833&r2=1221834&view=diff
==============================================================================
--- deltacloud/trunk/server/lib/cimi/model/volume.rb (original)
+++ deltacloud/trunk/server/lib/cimi/model/volume.rb Wed Dec 21 18:33:08 2011
@@ -64,6 +64,18 @@ class CIMI::Model::Volume < CIMI::Model:
     context.driver.destroy_storage_volume(context.credentials, {:id=>id} )
   end
 
+  def self.find_to_attach_from_json(json_in, context)
+    json = JSON.parse(json_in)
+    volumes = json["volumes"].map{|v| {:volume=>self.find(v["volume"]["href"].split("/volumes/").last,
context),
+                                       :attachment_point=>v["attachmentPoint"]  }}
+  end
+
+  def self.find_to_attach_from_xml(xml_in, context)
+    xml = XmlSimple.xml_in(xml_in)
+    volumes = xml["volume"].map{|v| {:volume => self.find(v["href"].split("/volumes/").last,
context),
+                                      :attachment_point=>v["attachmentPoint"] }}
+  end
+
   private
 
   def self.create_volume(params, context)

Modified: deltacloud/trunk/server/lib/cimi/server.rb
URL: http://svn.apache.org/viewvc/deltacloud/trunk/server/lib/cimi/server.rb?rev=1221834&r1=1221833&r2=1221834&view=diff
==============================================================================
--- deltacloud/trunk/server/lib/cimi/server.rb (original)
+++ deltacloud/trunk/server/lib/cimi/server.rb Wed Dec 21 18:33:08 2011
@@ -309,6 +309,42 @@ global_collection :machines do
     end
   end
 
+#NOTE: The routes for attach/detach used here are NOT as specified by CIMI
+#will likely move later. CIMI specifies PUT of the whole Machine description
+#with inclusion/ommission of the volumes you want [att|det]ached
+  operation :attach_volume, :method => :put, :member => true do
+    description "Attach CIMI Volume(s) to a machine."
+    param :id, :string, :required
+    control do
+      if request.content_type.end_with?("+json")
+        volumes_to_attach = Volume.find_to_attach_from_json(request.body.read, self)
+      else
+        volumes_to_attach = Volume.find_to_attach_from_xml(request.body.read, self)
+      end
+      machine = Machine.attach_volumes(volumes_to_attach, self)
+      respond_to do |format|
+        format.json{ machine.to_json}
+        format.xml{machine.to_xml}
+      end
+    end
+  end
+
+  operation :detach_volume, :method => :put, :member => true do
+    description "Detach CIMI Volume(s) from a machine."
+    param :id, :string, :required
+    control do
+      if request.content_type.end_with?("+json")
+        volumes_to_detach = Volume.find_to_attach_from_json(request.body.read, self)
+      else
+        volumes_to_detach = Volume.find_to_attach_from_xml(request.body.read, self)
+      end
+      machine = Machine.detach_volumes(volumes_to_detach, self)
+      respond_to do |format|
+        format.json{ machine.to_json}
+        format.xml{machine.to_xml}
+      end
+    end
+  end
 end
 
 global_collection :volumes do



Mime
View raw message