deltacloud-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfoj...@redhat.com
Subject [PATCH core 1/2] CIMI: Wrapped all collections to Collection entity with entries
Date Mon, 11 Jun 2012 12:33:03 GMT
From: Michal Fojtik <mfojtik@redhat.com>


Signed-off-by: Michal fojtik <mfojtik@redhat.com>
---
 server/lib/cimi/models/address_collection.rb       |    2 +-
 .../lib/cimi/models/address_template_collection.rb |    2 +-
 server/lib/cimi/models/base.rb                     |   12 +++--
 server/lib/cimi/models/cloud_entry_point.rb        |    1 +
 .../lib/cimi/models/entity_metadata_collection.rb  |    2 +-
 server/lib/cimi/models/machine_admin_collection.rb |    2 +-
 server/lib/cimi/models/machine_collection.rb       |    2 +-
 .../models/machine_configuration_collection.rb     |    2 +-
 server/lib/cimi/models/machine_image_collection.rb |    2 +-
 .../lib/cimi/models/machine_template_collection.rb |    2 +-
 server/lib/cimi/models/network_collection.rb       |    2 +-
 .../models/network_configuration_collection.rb     |    2 +-
 .../lib/cimi/models/network_template_collection.rb |    2 +-
 server/lib/cimi/models/routing_group_collection.rb |    2 +-
 .../models/routing_group_template_collection.rb    |    2 +-
 server/lib/cimi/models/schema.rb                   |   47 ++++++++++++++++++++
 server/lib/cimi/models/volume_collection.rb        |    2 +-
 .../cimi/models/volume_configuration_collection.rb |    2 +-
 server/lib/cimi/models/volume_image_collection.rb  |    2 +-
 .../lib/cimi/models/volume_template_collection.rb  |    2 +-
 server/lib/cimi/models/vsp_collection.rb           |    2 +-
 .../cimi/models/vsp_configuration_collection.rb    |    2 +-
 server/lib/cimi/models/vsp_template_collection.rb  |    2 +-
 23 files changed, 77 insertions(+), 23 deletions(-)

diff --git a/server/lib/cimi/models/address_collection.rb b/server/lib/cimi/models/address_collection.rb
index eef6c51..141ab16 100644
--- a/server/lib/cimi/models/address_collection.rb
+++ b/server/lib/cimi/models/address_collection.rb
@@ -17,7 +17,7 @@ class CIMI::Model::AddressCollection < CIMI::Model::Base
 
   act_as_root_entity :address
 
-  array :addresses do
+  collection :addresses do
     scalar :href
   end
 
diff --git a/server/lib/cimi/models/address_template_collection.rb b/server/lib/cimi/models/address_template_collection.rb
index 3bb95cd..0de997d 100644
--- a/server/lib/cimi/models/address_template_collection.rb
+++ b/server/lib/cimi/models/address_template_collection.rb
@@ -17,7 +17,7 @@ class CIMI::Model::AddressTemplateCollection < CIMI::Model::Base
 
   act_as_root_entity :address_template
 
-  array :address_templates do
+  collection :address_templates do
     scalar :href
   end
 
diff --git a/server/lib/cimi/models/base.rb b/server/lib/cimi/models/base.rb
index 16812ac..d929253 100644
--- a/server/lib/cimi/models/base.rb
+++ b/server/lib/cimi/models/base.rb
@@ -173,7 +173,12 @@ class CIMI::Model::Base
   #
 
   def self.xml_tag_name
-    self.name.split("::").last
+    tag = self.name.split("::").last
+    if tag =~ /Collection$/
+      tag = 'Collection'
+    else
+      tag
+    end
   end
 
   def self.to_json(model)
@@ -182,7 +187,8 @@ class CIMI::Model::Base
 
   def self.to_xml(model)
     xml = @schema.to_xml(model)
-    xml["xmlns"] = "http://www.dmtf.org/cimi"
+    xml["xmlns"] = "http://schemas.dmtf.org/cimi/1"
+    xml["resourceURI"] = "http://schemas.dmtf.org/cimi/1/#{self.name.split('::').last}"
     XmlSimple.xml_out(xml, :root_name => xml_tag_name)
   end
 
@@ -197,7 +203,7 @@ class CIMI::Model::Base
   #
   # Common attributes for all resources
   #
-  text :id, :name, :description, :created
+  text :id, :name, :description, :created, :updated
 
   # FIXME: this doesn't match with JSON
   hash :property, :content => :value do
diff --git a/server/lib/cimi/models/cloud_entry_point.rb b/server/lib/cimi/models/cloud_entry_point.rb
index ffe1ef5..14ee5d3 100644
--- a/server/lib/cimi/models/cloud_entry_point.rb
+++ b/server/lib/cimi/models/cloud_entry_point.rb
@@ -25,6 +25,7 @@ class CIMI::Model::CloudEntryPoint < CIMI::Model::Base
       :description => "Cloud Entry Point for the Deltacloud #{context.driver.name} driver",
       :id => context.cloudEntryPoint_url,
       :created => Time.now,
+      :updated => Time.now,
       :entity_metadata => CIMI::Model::EntityMetadata.all_uri(context)
     }))
   end
diff --git a/server/lib/cimi/models/entity_metadata_collection.rb b/server/lib/cimi/models/entity_metadata_collection.rb
index 595b502..26a2fb0 100644
--- a/server/lib/cimi/models/entity_metadata_collection.rb
+++ b/server/lib/cimi/models/entity_metadata_collection.rb
@@ -15,7 +15,7 @@
 
 class CIMI::Model::EntityMetadataCollection < CIMI::Model::Base
 
-  array :entity_metadata do
+  collection :entity_metadata do
     scalar :href
   end
 
diff --git a/server/lib/cimi/models/machine_admin_collection.rb b/server/lib/cimi/models/machine_admin_collection.rb
index 08c1559..90b9f07 100644
--- a/server/lib/cimi/models/machine_admin_collection.rb
+++ b/server/lib/cimi/models/machine_admin_collection.rb
@@ -17,7 +17,7 @@ class CIMI::Model::MachineAdminCollection < CIMI::Model::Base
 
   act_as_root_entity :machine_admin
 
-  array :machine_admins do
+  collection :machine_admins do
     scalar :href
   end
 
diff --git a/server/lib/cimi/models/machine_collection.rb b/server/lib/cimi/models/machine_collection.rb
index 9154aa9..d9d517d 100644
--- a/server/lib/cimi/models/machine_collection.rb
+++ b/server/lib/cimi/models/machine_collection.rb
@@ -17,7 +17,7 @@ class CIMI::Model::MachineCollection < CIMI::Model::Base
 
   act_as_root_entity :machine
 
-  array :machines do
+  collection :machines do
     scalar :href
   end
 
diff --git a/server/lib/cimi/models/machine_configuration_collection.rb b/server/lib/cimi/models/machine_configuration_collection.rb
index 6d7ebad..51dbaa6 100644
--- a/server/lib/cimi/models/machine_configuration_collection.rb
+++ b/server/lib/cimi/models/machine_configuration_collection.rb
@@ -17,7 +17,7 @@ class CIMI::Model::MachineConfigurationCollection < CIMI::Model::Base
 
   act_as_root_entity :machine_configuration
 
-  array :machine_configurations do
+  collection :machine_configurations do
     scalar :href
   end
 
diff --git a/server/lib/cimi/models/machine_image_collection.rb b/server/lib/cimi/models/machine_image_collection.rb
index 70bdc7b..50d8ea5 100644
--- a/server/lib/cimi/models/machine_image_collection.rb
+++ b/server/lib/cimi/models/machine_image_collection.rb
@@ -17,7 +17,7 @@ class CIMI::Model::MachineImageCollection < CIMI::Model::Base
 
   act_as_root_entity :machine_image
 
-  array :machine_images do
+  collection :machine_images do
     scalar :href
   end
 
diff --git a/server/lib/cimi/models/machine_template_collection.rb b/server/lib/cimi/models/machine_template_collection.rb
index d2a3f23..a7c415c 100644
--- a/server/lib/cimi/models/machine_template_collection.rb
+++ b/server/lib/cimi/models/machine_template_collection.rb
@@ -17,7 +17,7 @@ class CIMI::Model::MachineTemplateCollection < CIMI::Model::Base
 
   act_as_root_entity :machine_template
 
-  array :machine_templates do
+  collection :machine_templates do
     scalar :href
   end
 
diff --git a/server/lib/cimi/models/network_collection.rb b/server/lib/cimi/models/network_collection.rb
index d76a3dd..240554d 100644
--- a/server/lib/cimi/models/network_collection.rb
+++ b/server/lib/cimi/models/network_collection.rb
@@ -17,7 +17,7 @@ class CIMI::Model::NetworkCollection < CIMI::Model::Base
 
   act_as_root_entity :network
 
-  array :networks do
+  collection :networks do
     scalar :href
   end
 
diff --git a/server/lib/cimi/models/network_configuration_collection.rb b/server/lib/cimi/models/network_configuration_collection.rb
index fc14592..7331874 100644
--- a/server/lib/cimi/models/network_configuration_collection.rb
+++ b/server/lib/cimi/models/network_configuration_collection.rb
@@ -17,7 +17,7 @@ class CIMI::Model::NetworkConfigurationCollection < CIMI::Model::Base
 
   act_as_root_entity :network_configuration
 
-  array :network_configurations do
+  collection :network_configurations do
     scalar :href
   end
 
diff --git a/server/lib/cimi/models/network_template_collection.rb b/server/lib/cimi/models/network_template_collection.rb
index b2ac537..7b8ea68 100644
--- a/server/lib/cimi/models/network_template_collection.rb
+++ b/server/lib/cimi/models/network_template_collection.rb
@@ -18,7 +18,7 @@ class CIMI::Model::NetworkTemplateCollection < CIMI::Model::Base
 
   act_as_root_entity :network_template
 
-  array :network_templates do
+  collection :network_templates do
     scalar :href
   end
 
diff --git a/server/lib/cimi/models/routing_group_collection.rb b/server/lib/cimi/models/routing_group_collection.rb
index 285e164..bd4d543 100644
--- a/server/lib/cimi/models/routing_group_collection.rb
+++ b/server/lib/cimi/models/routing_group_collection.rb
@@ -17,7 +17,7 @@ class CIMI::Model::RoutingGroupCollection < CIMI::Model::Base
 
   act_as_root_entity :routing_group
 
-  array :routing_groups do
+  collection :routing_groups do
     scalar :href
   end
 
diff --git a/server/lib/cimi/models/routing_group_template_collection.rb b/server/lib/cimi/models/routing_group_template_collection.rb
index 61a4a68..d3ef92a 100644
--- a/server/lib/cimi/models/routing_group_template_collection.rb
+++ b/server/lib/cimi/models/routing_group_template_collection.rb
@@ -18,7 +18,7 @@ class CIMI::Model::RoutingGroupTemplateCollection < CIMI::Model::Base
 
   act_as_root_entity :routing_group_template
 
-  array :routing_group_templates do
+  collection :routing_group_templates do
     scalar :href
   end
 
diff --git a/server/lib/cimi/models/schema.rb b/server/lib/cimi/models/schema.rb
index f301b69..22b92fa 100644
--- a/server/lib/cimi/models/schema.rb
+++ b/server/lib/cimi/models/schema.rb
@@ -171,6 +171,49 @@ class CIMI::Model::Schema
     end
   end
 
+  class Collection < Array
+
+    def from_xml(xml, model)
+      model[name] = xml['Entry'].inject([]) do |result, entry|
+        result << entry[xml_name].map { |elt| @struct.convert_from_xml(elt) }
+        result
+      end.flatten
+    end
+
+    def from_json(xml, model)
+      model[name] = json['entries'].inject([]) do |result, entry|
+        result << entry[json_name].map { |elt| @struct.convert_from_json(elt) }
+        result
+      end.flatten
+    end
+
+    def from_json(json, model)
+      model[name] = (json[json_name] || []).map { |elt| @struct.convert_from_json(elt) }
+    end
+
+    def to_xml(model, xml)
+      xml['Entry'] = []
+      (model[name] || []).each do |elt|
+        xml['Entry'] << {
+          "resourceURI" => "http://schemas.dmtf.org/cimi/1/#{model.class.name.split('::').last}Entry",
+          :id => elt[:href].scan(/\/([\w\-\_]+)$/).first,
+          xml_name => @struct.convert_to_xml(elt),
+        }
+      end
+    end
+
+    def to_json(model, json)
+      json['entries'] = []
+      (model[name] || []).each do |elt|
+        json['entries'] << {
+          :id => elt[:href].scan(/\/(\w+)$/).first,
+          json_name => @struct.convert_to_json(elt),
+        }
+      end
+    end
+
+  end
+
   class Hash < Attribute
 
     def initialize(name, opts = {}, &block)
@@ -258,6 +301,10 @@ class CIMI::Model::Schema
       add_attributes!([name, opts], Array, &block)
     end
 
+    def collection(name, opts={}, &block)
+      add_attributes!([name, opts], Collection, &block)
+    end
+
     def struct(name, opts={}, &block)
       add_attributes!([name, opts], Struct, &block)
     end
diff --git a/server/lib/cimi/models/volume_collection.rb b/server/lib/cimi/models/volume_collection.rb
index 1f4152e..5c00cb6 100644
--- a/server/lib/cimi/models/volume_collection.rb
+++ b/server/lib/cimi/models/volume_collection.rb
@@ -17,7 +17,7 @@ class CIMI::Model::VolumeCollection < CIMI::Model::Base
 
   act_as_root_entity :volume
 
-  array :volumes do
+  collection :volumes do
     scalar :href
   end
 
diff --git a/server/lib/cimi/models/volume_configuration_collection.rb b/server/lib/cimi/models/volume_configuration_collection.rb
index 2120ae6..36940aa 100644
--- a/server/lib/cimi/models/volume_configuration_collection.rb
+++ b/server/lib/cimi/models/volume_configuration_collection.rb
@@ -17,7 +17,7 @@ class CIMI::Model::VolumeConfigurationCollection < CIMI::Model::Base
 
   act_as_root_entity :volume_configuration
 
-  array :volume_configurations do
+  collection :volume_configurations do
     scalar :href
   end
 
diff --git a/server/lib/cimi/models/volume_image_collection.rb b/server/lib/cimi/models/volume_image_collection.rb
index f3da877..b3b697b 100644
--- a/server/lib/cimi/models/volume_image_collection.rb
+++ b/server/lib/cimi/models/volume_image_collection.rb
@@ -17,7 +17,7 @@ class CIMI::Model::VolumeImageCollection < CIMI::Model::Base
 
   act_as_root_entity :volume_image
 
-  array :volume_images do
+  collection :volume_images do
     scalar :href
   end
 
diff --git a/server/lib/cimi/models/volume_template_collection.rb b/server/lib/cimi/models/volume_template_collection.rb
index f53547c..7b637c0 100644
--- a/server/lib/cimi/models/volume_template_collection.rb
+++ b/server/lib/cimi/models/volume_template_collection.rb
@@ -17,7 +17,7 @@ class CIMI::Model::VolumeTemplateCollection < CIMI::Model::Base
 
   act_as_root_entity :volume_template
 
-  array :volume_templates do
+  collection :volume_templates do
     scalar :href
   end
 
diff --git a/server/lib/cimi/models/vsp_collection.rb b/server/lib/cimi/models/vsp_collection.rb
index fc72024..c5ba51a 100644
--- a/server/lib/cimi/models/vsp_collection.rb
+++ b/server/lib/cimi/models/vsp_collection.rb
@@ -17,7 +17,7 @@ class CIMI::Model::VSPCollection < CIMI::Model::Base
 
   CIMI::Model.register_as_root_entity! "VSPs"
 
-  array :vsps do
+  collection :vsps do
     scalar :href
   end
 
diff --git a/server/lib/cimi/models/vsp_configuration_collection.rb b/server/lib/cimi/models/vsp_configuration_collection.rb
index d4927e7..f387469 100644
--- a/server/lib/cimi/models/vsp_configuration_collection.rb
+++ b/server/lib/cimi/models/vsp_configuration_collection.rb
@@ -17,7 +17,7 @@ class CIMI::Model::VSPConfigurationCollection < CIMI::Model::Base
 
   CIMI::Model.register_as_root_entity! "VSPConfigurations"
 
-  array :vsp_configurations do
+  collection :vsp_configurations do
     scalar :href
   end
 
diff --git a/server/lib/cimi/models/vsp_template_collection.rb b/server/lib/cimi/models/vsp_template_collection.rb
index 61d5311..403e4c0 100644
--- a/server/lib/cimi/models/vsp_template_collection.rb
+++ b/server/lib/cimi/models/vsp_template_collection.rb
@@ -17,7 +17,7 @@ class CIMI::Model::VSPTemplateCollection < CIMI::Model::Base
 
   CIMI::Model.register_as_root_entity! "VSPTemplates"
 
-  array :vsp_templates do
+  collection :vsp_templates do
     scalar :href
   end
 
-- 
1.7.10.2


Mime
View raw message