deltacloud-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfoj...@apache.org
Subject [7/8] git commit: CIMI: Fix the case when stored attributes are XML
Date Fri, 30 Nov 2012 08:48:25 GMT
CIMI: Fix the case when stored attributes are XML

In XmlSimple the values of elements are actually Array.
In that case we need to do an extra step to extract the correct
String value that is being saved to the database.


Project: http://git-wip-us.apache.org/repos/asf/deltacloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/deltacloud/commit/106f62b7
Tree: http://git-wip-us.apache.org/repos/asf/deltacloud/tree/106f62b7
Diff: http://git-wip-us.apache.org/repos/asf/deltacloud/diff/106f62b7

Branch: refs/heads/master
Commit: 106f62b72797b39d0a49b9c28b5227ab844f42d1
Parents: a2165d9
Author: Michal Fojtik <mfojtik@redhat.com>
Authored: Wed Nov 28 14:56:57 2012 +0100
Committer: Michal fojtik <mfojtik@redhat.com>
Committed: Fri Nov 30 09:42:20 2012 +0100

----------------------------------------------------------------------
 server/lib/cimi/dependencies.rb  |    1 -
 server/lib/cimi/models/base.rb   |   15 ++++++++---
 server/lib/cimi/models/volume.rb |   44 +++++++++++++++++++++++++++++---
 3 files changed, 50 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltacloud/blob/106f62b7/server/lib/cimi/dependencies.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/dependencies.rb b/server/lib/cimi/dependencies.rb
index d02ff30..6f0000c 100644
--- a/server/lib/cimi/dependencies.rb
+++ b/server/lib/cimi/dependencies.rb
@@ -39,7 +39,6 @@ require 'deltacloud/models/load_balancer'
 require 'deltacloud/models/firewall'
 require 'deltacloud/models/firewall_rule'
 
-require 'json'
 require 'sinatra/rack_accept'
 require 'sinatra/rack_cimi'
 require 'sinatra/static_assets'

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/106f62b7/server/lib/cimi/models/base.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/models/base.rb b/server/lib/cimi/models/base.rb
index a5d8f34..2b13367 100644
--- a/server/lib/cimi/models/base.rb
+++ b/server/lib/cimi/models/base.rb
@@ -14,7 +14,6 @@
 # under the License.
 
 require 'xmlsimple'
-require 'json'
 
 # The base class for any CIMI object that we either read from a request or
 # write as a response. This class handles serializing/deserializing XML and
@@ -289,10 +288,18 @@ class CIMI::Model::Base < CIMI::Model::Resource
   class << self
     def store_attributes_for(context, entity, attrs={})
       stored_attributes = {}
-      stored_attributes[:description] = attrs['description'] if attrs['description']
-      stored_attributes[:name] = attrs['name'] if attrs['name']
-      stored_attributes[:ent_properties] = attrs['properties'].to_json if attrs['properties']
+      stored_attributes[:description] = extract_attribute_value('description', attrs) if
attrs['description']
+      stored_attributes[:name] = extract_attribute_value('name', attrs) if attrs['name']
+      stored_attributes[:ent_properties] = extract_attribute_value('properties', attrs).to_json
if attrs['properties']
       context.store_attributes_for(entity, stored_attributes)
     end
+
+    # In XML serialization the values stored in attrs are arrays, dues to
+    # XmlSimple. This method will help extract values from them
+    #
+    def extract_attribute_value(name, attrs={})
+      return unless attrs[name]
+      attrs[name].is_a?(Array) ? attrs[name].first : attrs[name]
+    end
   end
 end

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/106f62b7/server/lib/cimi/models/volume.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/models/volume.rb b/server/lib/cimi/models/volume.rb
index 6bae401..7951a99 100644
--- a/server/lib/cimi/models/volume.rb
+++ b/server/lib/cimi/models/volume.rb
@@ -53,7 +53,7 @@ class CIMI::Model::Volume < CIMI::Model::Base
     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)
+    create_volume({:volume_config_id=>volume_config_id, :volume_image_id=>volume_image_id},
json, context)
   end
 
   def self.create_from_xml(xml_in, context)
@@ -61,25 +61,45 @@ class CIMI::Model::Volume < CIMI::Model::Base
     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)
+    create_volume({:volume_config_id=>volume_config_id, :volume_image_id=>volume_image_id},
xml, context)
   end
 
   def self.delete!(id, context)
     context.driver.destroy_storage_volume(context.credentials, {:id=>id} )
+    delete_attributes_for(Volume.new(: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
+
+  def to_entity
+    'volume'
   end
 
   private
 
-  def self.create_volume(params, context)
+  def self.create_volume(params, data, context)
     volume_config = CIMI::Model::VolumeConfiguration.find(params[:volume_config_id], context)
     opts = {:capacity=>context.from_kibibyte(volume_config.capacity, "GB"), :snapshot_id=>params[:volume_image_id]
}
     storage_volume = context.driver.create_storage_volume(context.credentials, opts)
+    store_attributes_for(context, storage_volume, data)
     from_storage_volume(storage_volume, context)
   end
 
   def self.from_storage_volume(volume, context)
-    self.new( { :name => volume.id,
-                :description => volume.id,
+    stored_attributes = context.load_attributes_for(volume)
+    self.new( { :name => stored_attributes[:name] || volume.id,
+                :description => stored_attributes[:description] || 'Description of Volume',
+                :property => stored_attributes[:property],
                 :created => volume.created.nil? ? nil : Time.parse(volume.created).xmlschema,
                 :id => context.volume_url(volume.id),
                 :capacity => context.to_kibibyte(volume.capacity, 'GB'),
@@ -92,4 +112,18 @@ 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


Mime
View raw message