From: marios <[email protected]> * attach/detach operations on Machine model * correct generation of MachineVolume collection
Signed-off-by: marios <[email protected]> --- server/lib/cimi/models/machine.rb | 26 ++++++++-------- server/lib/cimi/models/machine_volume.rb | 52 ++++++++++++++++++++++++++++++-- server/lib/cimi/models/volume.rb | 26 ---------------- 3 files changed, 62 insertions(+), 42 deletions(-) diff --git a/server/lib/cimi/models/machine.rb b/server/lib/cimi/models/machine.rb index 199a168..21e0081 100644 --- a/server/lib/cimi/models/machine.rb +++ b/server/lib/cimi/models/machine.rb @@ -102,20 +102,18 @@ class CIMI::Model::Machine < CIMI::Model::Base end 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) + #returns the newly attach machine_volume + def self.attach_volume(volume, location, context) + context.driver.attach_storage_volume(context.credentials, + {:id=>volume, :instance_id=>context.params[:id], :device=>location}) + CIMI::Model::MachineVolume.find(context.params[:id], context, volume) + end + + #returns the machine_volume_collection for the given machine + def self.detach_volume(volume, location, context) + context.driver.detach_storage_volume(context.credentials, + {:id=>volume, :instance_id=>context.params[:id], :device=>location}) + CIMI::Model::MachineVolume.collection_for_instance(context.params[:id], context) end private diff --git a/server/lib/cimi/models/machine_volume.rb b/server/lib/cimi/models/machine_volume.rb index f5fa966..bb6a8a1 100644 --- a/server/lib/cimi/models/machine_volume.rb +++ b/server/lib/cimi/models/machine_volume.rb @@ -26,17 +26,65 @@ class CIMI::Model::MachineVolume < CIMI::Model::Base if id == :all volumes = context.driver.storage_volumes(context.credentials) volumes.inject([]) do |attached, vol| + id = context.machine_url(instance_id)+"/volumes/#{vol.id}" attached << self.new( - :id => context.machine_url(instance_id)+"/volumes/#{vol.id}", + :id => id, :name => vol.id, :description => "MachineVolume #{vol.id} for Machine #{instance_id}", :created => Time.parse(vol.created).xmlschema, :initial_location => vol.device, - :volume => {:href=>context.volume_url(vol.id)} + :volume => {:href=>context.volume_url(vol.id)}, + :operations => [{:href=>id, :rel => "delete" }] ) if vol.instance_id == instance_id attached end else + vol = context.driver.storage_volume(context.credentials, {:id=>id}) + id = context.machine_url(instance_id)+"/volumes/#{vol.id}" + raise CIMI::Model::NotFound unless vol.instance_id == instance_id + self.new( + :id => id, + :name => vol.id, + :description => "MachineVolume #{vol.id} for Machine #{instance_id}", + :created => Time.parse(vol.created).xmlschema, + :initial_location => vol.device, + :volume => {:href=>context.volume_url(vol.id)}, + :operations => [{:href=>id, :rel => "delete" }] + ) end end + + def self.find_to_attach_from_xml(xml_in, context) + xml = XmlSimple.xml_in(xml_in) + vol_id = xml["volume"].first["href"].split("/").last + location = xml["initialLocation"].first.strip + [vol_id, location] + end + + def self.find_to_attach_from_json(json_in, context) + json = JSON.parse(json_in) + vol_id = json["volume"]["href"].split("/").last + location = json["initialLocation"] + [vol_id, location] + end + + + def self.collection_for_instance(instance_id, context) + machine_volumes = self.find(instance_id, context) + volumes_url = context.url("/machines/#{instance_id}/volumes") + unless CIMI::Model.const_defined?('MachineVolumeCollection') + collection_class = CIMI::Model::Collection.generate(self) + else + collection_class = CIMI::Model::MachineVolumeCollection + end + collection_class.new( + :id => volumes_url, + :name => 'default', + :count => machine_volumes.size, + :description => "Volume collection for Machine #{instance_id}", + :entries => machine_volumes, + :operations => [{ :href => volumes_url.singularize+"_attach", :rel => "add" }] + ) + end + end diff --git a/server/lib/cimi/models/volume.rb b/server/lib/cimi/models/volume.rb index c89835c..9f998ab 100644 --- a/server/lib/cimi/models/volume.rb +++ b/server/lib/cimi/models/volume.rb @@ -68,18 +68,6 @@ 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) - 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) - 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) @@ -103,18 +91,4 @@ class CIMI::Model::Volume < CIMI::Model::Base } ) end - def self.collection_for_instance(instance_id, context) - instance = context.driver.instance(context.credentials, :id => instance_id) - volumes = instance.storage_volumes.map do |mappings| - mappings.keys.map { |volume_id| from_storage_volume(context.driver.storage_volume(context.credentials, :id => volume_id), context) } - end.flatten - CIMI::Model::VolumeCollection.new( - :id => context.url("/machines/#{instance_id}/volumes"), - :name => 'default', - :count => volumes.size, - :description => "Volume collection for Machine #{instance_id}", - :entries => volumes - ) - end - end -- 1.7.11.7
