deltacloud-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfoj...@redhat.com
Subject [PATCH core 3/3] CIMI: Fix the case when stored attributes are XML
Date Wed, 28 Nov 2012 13:58:53 GMT
From: Michal Fojtik <mfojtik@redhat.com>

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.

Signed-off-by: Michal fojtik <mfojtik@redhat.com>
---
 server/lib/cimi/dependencies.rb  |  1 -
 server/lib/cimi/models/base.rb   | 15 +++++++++++----
 server/lib/cimi/models/volume.rb | 20 +++++++++++++++-----
 3 files changed, 26 insertions(+), 10 deletions(-)

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'
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
diff --git a/server/lib/cimi/models/volume.rb b/server/lib/cimi/models/volume.rb
index c89835c..d3fbd37 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,11 +61,12 @@ 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)
@@ -80,18 +81,25 @@ class CIMI::Model::Volume < CIMI::Model::Base
                            :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 => Time.parse(volume.created).xmlschema,
                 :id => context.volume_url(volume.id),
                 :capacity => context.to_kibibyte(volume.capacity, 'GB'),
@@ -117,4 +125,6 @@ class CIMI::Model::Volume < CIMI::Model::Base
     )
   end
 
+
+
 end
-- 
1.8.0


Mime
View raw message