You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@deltacloud.apache.org by ma...@redhat.com on 2011/12/19 17:03:10 UTC

[PATCH 2/2] Attach CIMI::Volume(s) to a CIMI::Machine

From: marios <ma...@redhat.com>


Signed-off-by: marios <ma...@redhat.com>
---
 server/lib/cimi/model/machine.rb |   14 ++++++++++++++
 server/lib/cimi/model/volume.rb  |   12 ++++++++++++
 server/lib/cimi/server.rb        |   17 +++++++++++++++++
 3 files changed, 43 insertions(+), 0 deletions(-)

diff --git a/server/lib/cimi/model/machine.rb b/server/lib/cimi/model/machine.rb
index d2673e7..6a0b81e 100644
--- a/server/lib/cimi/model/machine.rb
+++ b/server/lib/cimi/model/machine.rb
@@ -117,6 +117,14 @@ class CIMI::Model::Machine < CIMI::Model::Base
     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
+
   private
 
   def self.from_instance(instance, context)
@@ -131,6 +139,7 @@ class CIMI::Model::Machine < CIMI::Model::Base
       :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 +211,9 @@ class CIMI::Model::Machine < CIMI::Model::Base
     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
diff --git a/server/lib/cimi/model/volume.rb b/server/lib/cimi/model/volume.rb
index cb9a34f..773dc1f 100644
--- a/server/lib/cimi/model/volume.rb
+++ b/server/lib/cimi/model/volume.rb
@@ -64,6 +64,18 @@ class CIMI::Model::Volume < CIMI::Model::Base
     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)
diff --git a/server/lib/cimi/server.rb b/server/lib/cimi/server.rb
index 2d62c44..74cccfa 100644
--- a/server/lib/cimi/server.rb
+++ b/server/lib/cimi/server.rb
@@ -309,6 +309,23 @@ global_collection :machines do
     end
   end
 
+  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
+
 end
 
 global_collection :volumes do
-- 
1.7.6.4