deltacloud-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfoj...@redhat.com
Subject [PATCH core 2/2] CIMI: Added syntax sugar for collection members
Date Thu, 06 Sep 2012 12:39:41 GMT
From: Michal Fojtik <mfojtik@redhat.com>


Signed-off-by: Michal fojtik <mfojtik@redhat.com>
---
 server/lib/cimi/models/base.rb                           |   14 ++++++++++++++
 server/lib/cimi/models/disk_collection.rb                |    3 +--
 server/lib/cimi/models/machine_collection.rb             |    2 +-
 .../lib/cimi/models/machine_configuration_collection.rb  |    4 +---
 server/lib/cimi/models/machine_image_collection.rb       |    2 +-
 server/lib/cimi/models/machine_template_collection.rb    |    4 +---
 server/lib/cimi/models/machine_volume_collection.rb      |    2 +-
 server/lib/cimi/models/network_collection.rb             |    4 ++--
 server/lib/cimi/models/volume_collection.rb              |    2 +-
 .../lib/cimi/models/volume_configuration_collection.rb   |    2 +-
 server/lib/cimi/models/volume_image_collection.rb        |    4 ++--
 server/lib/cimi/models/volume_template_collection.rb     |    2 +-
 12 files changed, 27 insertions(+), 18 deletions(-)

diff --git a/server/lib/cimi/models/base.rb b/server/lib/cimi/models/base.rb
index b375ce0..b627b82 100644
--- a/server/lib/cimi/models/base.rb
+++ b/server/lib/cimi/models/base.rb
@@ -94,6 +94,20 @@ class CIMI::Model::Base
   # attribute, we also define a getter and a setter to access/change the
   # value for that attribute
   class << self
+
+    def <<(model)
+      clone_base_schema unless base_schema_cloned?
+      member_name = model.name.split("::").last
+      if ::Struct.const_defined?("CIMI_#{member_name}")
+        puts "Removing struct"
+        ::Struct.send(:remove_const, "CIMI_#{member_name}")
+      end
+      member_symbol = member_name.underscore.pluralize.to_sym
+      members = CIMI::Model::Schema::Array.new(member_symbol)
+      members.struct.schema.attributes = model.schema.attributes
+      base_schema.attributes << members
+    end
+
     def base_schema
       @schema ||= CIMI::Model::Schema.new
     end
diff --git a/server/lib/cimi/models/disk_collection.rb b/server/lib/cimi/models/disk_collection.rb
index b86a3c9..7dbff39 100644
--- a/server/lib/cimi/models/disk_collection.rb
+++ b/server/lib/cimi/models/disk_collection.rb
@@ -16,7 +16,7 @@ class CIMI::Model::DiskCollection < CIMI::Model::Base
   text :count
 
   #add disks array:
-  self.schema.add_collection_member_array(CIMI::Model::Disk)
+  self << CIMI::Model::Disk
 
   array :operations do
     scalar :rel, :href
@@ -26,7 +26,6 @@ class CIMI::Model::DiskCollection < CIMI::Model::Base
     instance = context.driver.instance(context.credentials, :id=>instance_id)
     machine_conf = CIMI::Model::MachineConfiguration.find(instance.instance_profile.name,
context)
     disks = CIMI::Model::Disk.find(instance, machine_conf, context, :all)
-    storage_override = instance.instance_profile.overrides.find { |p, v| p == :storage }
     self.new(
       :id => context.machine_url(instance_id)+"/disks",
       :description => "DiskCollection for Machine #{instance_id}",
diff --git a/server/lib/cimi/models/machine_collection.rb b/server/lib/cimi/models/machine_collection.rb
index 27c721d..9c57301 100644
--- a/server/lib/cimi/models/machine_collection.rb
+++ b/server/lib/cimi/models/machine_collection.rb
@@ -20,7 +20,7 @@ class CIMI::Model::MachineCollection < CIMI::Model::Base
   text :count
 
   #add machines array:
-  self.schema.add_collection_member_array(CIMI::Model::Machine)
+  self << CIMI::Model::Machine
 
   def self.default(context)
     machines = CIMI::Model::Machine.all(context)
diff --git a/server/lib/cimi/models/machine_configuration_collection.rb b/server/lib/cimi/models/machine_configuration_collection.rb
index e75bbb4..051e1b9 100644
--- a/server/lib/cimi/models/machine_configuration_collection.rb
+++ b/server/lib/cimi/models/machine_configuration_collection.rb
@@ -15,11 +15,9 @@
 
 class CIMI::Model::MachineConfigurationCollection < CIMI::Model::Base
 
-  act_as_root_entity :machine_configuration
-
   text :count
 
-  self.schema.add_collection_member_array(CIMI::Model::MachineConfiguration)
+  self << CIMI::Model::MachineConfiguration
 
   def self.default(context)
     machine_configurations = CIMI::Model::MachineConfiguration.all(context)
diff --git a/server/lib/cimi/models/machine_image_collection.rb b/server/lib/cimi/models/machine_image_collection.rb
index 703e515..cd4b5b4 100644
--- a/server/lib/cimi/models/machine_image_collection.rb
+++ b/server/lib/cimi/models/machine_image_collection.rb
@@ -19,7 +19,7 @@ class CIMI::Model::MachineImageCollection < CIMI::Model::Base
 
   text :count
 
-  self.schema.add_collection_member_array(CIMI::Model::MachineImage)
+  self << CIMI::Model::MachineImage
 
   def self.default(context)
     machine_images = CIMI::Model::MachineImage.all(context)
diff --git a/server/lib/cimi/models/machine_template_collection.rb b/server/lib/cimi/models/machine_template_collection.rb
index bef9aaf..289c984 100644
--- a/server/lib/cimi/models/machine_template_collection.rb
+++ b/server/lib/cimi/models/machine_template_collection.rb
@@ -15,11 +15,9 @@
 
 class CIMI::Model::MachineTemplateCollection < CIMI::Model::Base
 
-  act_as_root_entity :machine_template
-
   text :count
 
-  self.schema.add_collection_member_array(CIMI::Model::MachineTemplate)
+  self << CIMI::Model::MachineTemplate
 
   def self.default(context)
     machine_templates = CIMI::Model::MachineTemplate.all(context)
diff --git a/server/lib/cimi/models/machine_volume_collection.rb b/server/lib/cimi/models/machine_volume_collection.rb
index 85483ff..b9dfcad 100644
--- a/server/lib/cimi/models/machine_volume_collection.rb
+++ b/server/lib/cimi/models/machine_volume_collection.rb
@@ -16,7 +16,7 @@ class CIMI::Model::MachineVolumeCollection < CIMI::Model::Base
 
   text :count
 
-  self.schema.add_collection_member_array(CIMI::Model::MachineVolume)
+  self << CIMI::Model::MachineVolume
 
   array :operations do
     scalar :rel, :href
diff --git a/server/lib/cimi/models/network_collection.rb b/server/lib/cimi/models/network_collection.rb
index c17c3f7..7c746b4 100644
--- a/server/lib/cimi/models/network_collection.rb
+++ b/server/lib/cimi/models/network_collection.rb
@@ -17,8 +17,8 @@ class CIMI::Model::NetworkCollection < CIMI::Model::Base
   act_as_root_entity :network
   text :count
 
-#add networks Array:
-  self.schema.add_collection_member_array(CIMI::Model::Network)
+  #add networks Array:
+  self << CIMI::Model::Network
 
   def self.default(context)
     networks = CIMI::Model::Network.all(context)
diff --git a/server/lib/cimi/models/volume_collection.rb b/server/lib/cimi/models/volume_collection.rb
index 6bec78f..e780845 100644
--- a/server/lib/cimi/models/volume_collection.rb
+++ b/server/lib/cimi/models/volume_collection.rb
@@ -19,7 +19,7 @@ class CIMI::Model::VolumeCollection < CIMI::Model::Base
 
   text :count
 
-  self.schema.add_collection_member_array(CIMI::Model::Volume)
+  self << CIMI::Model::Volume
 
   def self.default(context)
     volumes = CIMI::Model::Volume.all(context)
diff --git a/server/lib/cimi/models/volume_configuration_collection.rb b/server/lib/cimi/models/volume_configuration_collection.rb
index 8800e35..d8ac229 100644
--- a/server/lib/cimi/models/volume_configuration_collection.rb
+++ b/server/lib/cimi/models/volume_configuration_collection.rb
@@ -19,7 +19,7 @@ class CIMI::Model::VolumeConfigurationCollection < CIMI::Model::Base
 
   text :count
 
-  self.schema.add_collection_member_array(CIMI::Model::VolumeConfiguration)
+  self << CIMI::Model::VolumeConfiguration
 
   def self.default(context)
     volume_configurations = CIMI::Model::VolumeConfiguration.all(context)
diff --git a/server/lib/cimi/models/volume_image_collection.rb b/server/lib/cimi/models/volume_image_collection.rb
index 78881d5..d411431 100644
--- a/server/lib/cimi/models/volume_image_collection.rb
+++ b/server/lib/cimi/models/volume_image_collection.rb
@@ -17,9 +17,9 @@ class CIMI::Model::VolumeImageCollection < CIMI::Model::Base
 
   act_as_root_entity :volume_image
 
-  text :count 
+  text :count
 
-  self.schema.add_collection_member_array(CIMI::Model::VolumeImage)
+  self << CIMI::Model::VolumeImage
 
   def self.default(context)
       volume_images = CIMI::Model::VolumeImage.all(context)
diff --git a/server/lib/cimi/models/volume_template_collection.rb b/server/lib/cimi/models/volume_template_collection.rb
index 3f2ae02..90462a0 100644
--- a/server/lib/cimi/models/volume_template_collection.rb
+++ b/server/lib/cimi/models/volume_template_collection.rb
@@ -19,7 +19,7 @@ class CIMI::Model::VolumeTemplateCollection < CIMI::Model::Base
 
   text :count
 
-  self.schema.add_collection_member_array(CIMI::Model::VolumeTemplate)
+  self << CIMI::Model::VolumeTemplate
 
   def self.default(context)
     volume_templates = CIMI::Model::VolumeTemplate.all(context)
-- 
1.7.10.2


Mime
View raw message