deltacloud-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfoj...@apache.org
Subject [4/5] git commit: CIMI: Code cleanup in way how we handle select in base model
Date Tue, 29 Jan 2013 13:40:43 GMT
CIMI: Code cleanup in way how we handle select in base model


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

Branch: refs/heads/master
Commit: ac31cba1ed466935c380f8d1134c92efe204916c
Parents: 32bdcc2
Author: Michal Fojtik <mfojtik@redhat.com>
Authored: Mon Jan 28 11:16:41 2013 +0100
Committer: Michal fojtik <mfojtik@redhat.com>
Committed: Tue Jan 29 14:38:53 2013 +0100

----------------------------------------------------------------------
 server/lib/cimi/collections/address_templates.rb   |    2 +-
 server/lib/cimi/collections/addresses.rb           |    2 +-
 server/lib/cimi/collections/credentials.rb         |    2 +-
 .../cimi/collections/forwarding_group_templates.rb |    2 +-
 server/lib/cimi/collections/forwarding_groups.rb   |    2 +-
 .../lib/cimi/collections/machine_configurations.rb |    2 +-
 server/lib/cimi/collections/machine_images.rb      |    2 +-
 server/lib/cimi/collections/machine_templates.rb   |    2 +-
 server/lib/cimi/collections/machines.rb            |    2 +-
 .../lib/cimi/collections/network_configurations.rb |    2 +-
 .../collections/network_port_configurations.rb     |    2 +-
 .../lib/cimi/collections/network_port_templates.rb |    2 +-
 server/lib/cimi/collections/network_ports.rb       |    2 +-
 server/lib/cimi/collections/network_templates.rb   |    2 +-
 server/lib/cimi/collections/networks.rb            |    2 +-
 .../lib/cimi/collections/volume_configurations.rb  |    2 +-
 server/lib/cimi/collections/volume_images.rb       |    2 +-
 server/lib/cimi/collections/volume_templates.rb    |    2 +-
 server/lib/cimi/collections/volumes.rb             |    2 +-
 server/lib/cimi/models/base.rb                     |   31 ++++++++-------
 server/lib/cimi/models/collection.rb               |    4 +-
 server/lib/cimi/models/schema.rb                   |    1 +
 22 files changed, 38 insertions(+), 36 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltacloud/blob/ac31cba1/server/lib/cimi/collections/address_templates.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/collections/address_templates.rb b/server/lib/cimi/collections/address_templates.rb
index 6e9c28f..fea3ca1 100644
--- a/server/lib/cimi/collections/address_templates.rb
+++ b/server/lib/cimi/collections/address_templates.rb
@@ -23,7 +23,7 @@ module CIMI::Collections
       operation :index do
         description 'List all AddressTemplates in the AddressTemplateCollection'
         control do
-          address_templates = AddressTemplate.list(self).filter_by(params['$select'])
+          address_templates = AddressTemplate.list(self).select_by(params['$select'])
           respond_to do |format|
             format.xml {address_templates.to_xml}
             format.json {address_templates.to_json}

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/ac31cba1/server/lib/cimi/collections/addresses.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/collections/addresses.rb b/server/lib/cimi/collections/addresses.rb
index 6f016b2..9ea1764 100644
--- a/server/lib/cimi/collections/addresses.rb
+++ b/server/lib/cimi/collections/addresses.rb
@@ -25,7 +25,7 @@ module CIMI::Collections
       operation :index, :with_capability => :addresses do
         description 'List all Addresses in the AddressCollection'
         control do
-          addresses = Address.list(self).filter_by(params['$select'])
+          addresses = Address.list(self).select_by(params['$select'])
           respond_to do |format|
             format.xml {addresses.to_xml}
             format.json {addresses.to_json}

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/ac31cba1/server/lib/cimi/collections/credentials.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/collections/credentials.rb b/server/lib/cimi/collections/credentials.rb
index 28d497d..dd0fbda 100644
--- a/server/lib/cimi/collections/credentials.rb
+++ b/server/lib/cimi/collections/credentials.rb
@@ -24,7 +24,7 @@ module CIMI::Collections
       operation :index, :with_capability => :keys do
         description "List all machine admins"
         control do
-          credentials = Credential.list(self).filter_by(params['$select'])
+          credentials = Credential.list(self).select_by(params['$select'])
           respond_to do |format|
             format.xml { credentials.to_xml }
             format.json { credentials.to_json }

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/ac31cba1/server/lib/cimi/collections/forwarding_group_templates.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/collections/forwarding_group_templates.rb b/server/lib/cimi/collections/forwarding_group_templates.rb
index 0791276..98c19d5 100644
--- a/server/lib/cimi/collections/forwarding_group_templates.rb
+++ b/server/lib/cimi/collections/forwarding_group_templates.rb
@@ -22,7 +22,7 @@ module CIMI::Collections
       operation :index, :with_capability => :forwarding_groups do
         description 'List all ForwardingGroupTemplates in the ForwardingGroupTemplateCollection'
         control do
-          forwarding_group_templates = ForwardingGroupTemplate.list(self).filter_by(params['$select'])
+          forwarding_group_templates = ForwardingGroupTemplate.list(self).select_by(params['$select'])
           respond_to do |format|
             format.xml {forwarding_group_templates.to_xml}
             format.json {forwarding_group_templates.to_json}

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/ac31cba1/server/lib/cimi/collections/forwarding_groups.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/collections/forwarding_groups.rb b/server/lib/cimi/collections/forwarding_groups.rb
index 3de1bd5..6422b50 100644
--- a/server/lib/cimi/collections/forwarding_groups.rb
+++ b/server/lib/cimi/collections/forwarding_groups.rb
@@ -22,7 +22,7 @@ module CIMI::Collections
       operation :index, :with_capability => :forwarding_groups do
         description 'List all ForwardingGroups in the ForwardingGroupsCollection'
         control do
-          forwarding_groups = ForwardingGroup.list(self).filter_by(params['$select'])
+          forwarding_groups = ForwardingGroup.list(self).select_by(params['$select'])
           respond_to do |format|
             format.xml {forwarding_groups.to_xml}
             format.json {forwarding_groups.to_json}

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/ac31cba1/server/lib/cimi/collections/machine_configurations.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/collections/machine_configurations.rb b/server/lib/cimi/collections/machine_configurations.rb
index eb7833a..2d75559 100644
--- a/server/lib/cimi/collections/machine_configurations.rb
+++ b/server/lib/cimi/collections/machine_configurations.rb
@@ -24,7 +24,7 @@ module CIMI::Collections
       operation :index, :with_capability => :hardware_profiles do
         description "List all machine configurations"
         control do
-          machine_configs = MachineConfiguration.list(self).filter_by(params['$select'])
+          machine_configs = MachineConfiguration.list(self).select_by(params['$select'])
           respond_to do |format|
             format.xml { machine_configs.to_xml }
             format.json { machine_configs.to_json }

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/ac31cba1/server/lib/cimi/collections/machine_images.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/collections/machine_images.rb b/server/lib/cimi/collections/machine_images.rb
index bafea80..663c651 100644
--- a/server/lib/cimi/collections/machine_images.rb
+++ b/server/lib/cimi/collections/machine_images.rb
@@ -24,7 +24,7 @@ module CIMI::Collections
       operation :index, :with_capability => :images do
         description "List all machine configurations"
         control do
-          machine_images = MachineImage.list(self).filter_by(params['$select'])
+          machine_images = MachineImage.list(self).select_by(params['$select'])
           respond_to do |format|
             format.xml { machine_images.to_xml }
             format.json { machine_images.to_json }

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/ac31cba1/server/lib/cimi/collections/machine_templates.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/collections/machine_templates.rb b/server/lib/cimi/collections/machine_templates.rb
index 9a12915..99ce5cf 100644
--- a/server/lib/cimi/collections/machine_templates.rb
+++ b/server/lib/cimi/collections/machine_templates.rb
@@ -23,7 +23,7 @@ module CIMI::Collections
       operation :index do
         description "List all machine templates"
         control do
-          machine_templates = CIMI::Model::MachineTemplate.list(self).filter_by(params['$select'])
+          machine_templates = CIMI::Model::MachineTemplate.list(self).select_by(params['$select'])
           respond_to do |format|
             format.xml { machine_templates.to_xml }
             format.json { machine_templates.to_json }

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/ac31cba1/server/lib/cimi/collections/machines.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/collections/machines.rb b/server/lib/cimi/collections/machines.rb
index b18fb1c..9c0e931 100644
--- a/server/lib/cimi/collections/machines.rb
+++ b/server/lib/cimi/collections/machines.rb
@@ -24,7 +24,7 @@ module CIMI::Collections
       operation :index, :with_capability => :instances do
         description "List all machines"
         control do
-          machines = Machine.list(self).filter_by(params['$select'])
+          machines = Machine.list(self).select_by(params['$select'])
           respond_to do |format|
             format.xml { machines.to_xml }
             format.json { machines.to_json }

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/ac31cba1/server/lib/cimi/collections/network_configurations.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/collections/network_configurations.rb b/server/lib/cimi/collections/network_configurations.rb
index 54c8fff..5dcaa9b 100644
--- a/server/lib/cimi/collections/network_configurations.rb
+++ b/server/lib/cimi/collections/network_configurations.rb
@@ -23,7 +23,7 @@ module CIMI::Collections
       operation :index, :with_capability => :network_configurations do
         description 'List all NetworkConfigurations'
         control do
-          network_configurations = NetworkConfiguration.list(self).filter_by(params['$select'])
+          network_configurations = NetworkConfiguration.list(self).select_by(params['$select'])
           respond_to do |format|
             format.xml { network_configurations.to_xml  }
             format.json { network_configurations.to_json }

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/ac31cba1/server/lib/cimi/collections/network_port_configurations.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/collections/network_port_configurations.rb b/server/lib/cimi/collections/network_port_configurations.rb
index 59bc194..52a8b86 100644
--- a/server/lib/cimi/collections/network_port_configurations.rb
+++ b/server/lib/cimi/collections/network_port_configurations.rb
@@ -23,7 +23,7 @@ module CIMI::Collections
       operation :index, :with_capability => :network_port_configurations do
         description 'List all NetworkPortConfigurations in the NetworkPortConfigurationCollection'
         control do
-          net_port_configs = NetworkPortConfiguration.list(self).filter_by(params['$select'])
+          net_port_configs = NetworkPortConfiguration.list(self).select_by(params['$select'])
           respond_to do |format|
             format.xml {net_port_configs.to_xml}
             format.json {net_port_configs.to_json}

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/ac31cba1/server/lib/cimi/collections/network_port_templates.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/collections/network_port_templates.rb b/server/lib/cimi/collections/network_port_templates.rb
index 4c70fe9..94c6744 100644
--- a/server/lib/cimi/collections/network_port_templates.rb
+++ b/server/lib/cimi/collections/network_port_templates.rb
@@ -25,7 +25,7 @@ module CIMI::Collections
       operation :index, :with_capability => :network_port_templates do
         description 'List all NetworkPortTemplates in the NetworkPortTemplateCollection'
         control do
-          network_port_templates = NetworkPortTemplate.list(self).filter_by(params['$select'])
+          network_port_templates = NetworkPortTemplate.list(self).select_by(params['$select'])
           respond_to do |format|
             format.xml {network_port_templates.to_xml}
             format.json {network_port_templates.to_json}

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/ac31cba1/server/lib/cimi/collections/network_ports.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/collections/network_ports.rb b/server/lib/cimi/collections/network_ports.rb
index 9a009d6..ee7b83a 100644
--- a/server/lib/cimi/collections/network_ports.rb
+++ b/server/lib/cimi/collections/network_ports.rb
@@ -25,7 +25,7 @@ module CIMI::Collections
       operation :index, :with_capability => :network_ports do
         description 'List all NetworkPorts in the NetworkPortCollection'
         control do
-          network_ports = NetworkPort.list(self).filter_by(params['$select'])
+          network_ports = NetworkPort.list(self).select_by(params['$select'])
           respond_to do |format|
             format.xml {network_ports.to_xml}
             format.json {network_ports.to_json}

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/ac31cba1/server/lib/cimi/collections/network_templates.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/collections/network_templates.rb b/server/lib/cimi/collections/network_templates.rb
index 4157875..541d607 100644
--- a/server/lib/cimi/collections/network_templates.rb
+++ b/server/lib/cimi/collections/network_templates.rb
@@ -23,7 +23,7 @@ module CIMI::Collections
       operation :index, :with_capability => :network_templates do
         description 'List all Network Templates in the NetworkTemplateCollection'
         control do
-          network_templates = NetworkTemplate.list(self).filter_by(params['$select'])
+          network_templates = NetworkTemplate.list(self).select_by(params['$select'])
           respond_to do |format|
             format.xml {network_templates.to_xml}
             format.json {network_templates.to_json}

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/ac31cba1/server/lib/cimi/collections/networks.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/collections/networks.rb b/server/lib/cimi/collections/networks.rb
index 6500c82..377a868 100644
--- a/server/lib/cimi/collections/networks.rb
+++ b/server/lib/cimi/collections/networks.rb
@@ -24,7 +24,7 @@ module CIMI::Collections
       operation :index, :with_capability => :networks do
         description "List all Networks"
         control do
-          networks = Network.list(self).filter_by(params['$select'])
+          networks = Network.list(self).select_by(params['$select'])
           respond_to do |format|
             format.xml { networks.to_xml }
             format.json { networks.to_json }

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/ac31cba1/server/lib/cimi/collections/volume_configurations.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/collections/volume_configurations.rb b/server/lib/cimi/collections/volume_configurations.rb
index 5895209..6a03dc5 100644
--- a/server/lib/cimi/collections/volume_configurations.rb
+++ b/server/lib/cimi/collections/volume_configurations.rb
@@ -23,7 +23,7 @@ module CIMI::Collections
       operation :index, :with_capability => :storage_volumes do
         description "Get list all VolumeConfigurations"
         control do
-          volume_configuration = VolumeConfiguration.list(self).filter_by(params['$select'])
+          volume_configuration = VolumeConfiguration.list(self).select_by(params['$select'])
           respond_to do |format|
             format.xml { volume_configuration.to_xml }
             format.json { volume_configuration.to_json }

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/ac31cba1/server/lib/cimi/collections/volume_images.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/collections/volume_images.rb b/server/lib/cimi/collections/volume_images.rb
index 259a25a..708f2f1 100644
--- a/server/lib/cimi/collections/volume_images.rb
+++ b/server/lib/cimi/collections/volume_images.rb
@@ -24,7 +24,7 @@ module CIMI::Collections
       operation :index, :with_capability => :storage_snapshots do
         description "List all volumes images"
         control do
-          volume_images = VolumeImage.list(self).filter_by(params['$select'])
+          volume_images = VolumeImage.list(self).select_by(params['$select'])
           respond_to do |format|
             format.xml { volume_images.to_xml }
             format.json { volume_images.to_json }

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/ac31cba1/server/lib/cimi/collections/volume_templates.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/collections/volume_templates.rb b/server/lib/cimi/collections/volume_templates.rb
index 6ba7a62..4e3dfdb 100644
--- a/server/lib/cimi/collections/volume_templates.rb
+++ b/server/lib/cimi/collections/volume_templates.rb
@@ -23,7 +23,7 @@ module CIMI::Collections
       operation :index, :with_capability => :storage_volumes do
         description "Retrieve the Volume Template Collection"
         control do
-          volume_template = VolumeTemplate.list(self).filter_by(params['$select'])
+          volume_template = VolumeTemplate.list(self).select_by(params['$select'])
           respond_to do |format|
             format.xml { volume_template.to_xml }
             format.json { volume_template.to_json }

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/ac31cba1/server/lib/cimi/collections/volumes.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/collections/volumes.rb b/server/lib/cimi/collections/volumes.rb
index c8edf56..bf1f5f0 100644
--- a/server/lib/cimi/collections/volumes.rb
+++ b/server/lib/cimi/collections/volumes.rb
@@ -23,7 +23,7 @@ module CIMI::Collections
       operation :index, :with_capability => :storage_volumes do
         description "List all volumes"
         control do
-          volumes = Volume.list(self).filter_by(params['$select'])
+          volumes = Volume.list(self).select_by(params['$select'])
           respond_to do |format|
             format.xml { volumes.to_xml }
             format.json { volumes.to_json }

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/ac31cba1/server/lib/cimi/models/base.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/models/base.rb b/server/lib/cimi/models/base.rb
index 7995e07..c1ed579 100644
--- a/server/lib/cimi/models/base.rb
+++ b/server/lib/cimi/models/base.rb
@@ -164,17 +164,18 @@ class CIMI::Model::Resource
   end
 
   def []=(a, v)
+    return @attribute_values.delete(a) if v.nil?
     @attribute_values[a] = self.class.schema.convert(a, v)
   end
 
   # Prepare to serialize
   def prepare
     self.class.schema.collections.map { |coll| coll.name }.each do |n|
-      if !@filter_attrs.empty? and !@filter_attrs.include?(n)
-        @attribute_values[n] = nil
-      else
+      if @select_attrs.empty? or @select_attrs.include?(n)
         self[n].href = "#{self.base_id}/#{n}" if !self[n].href
         self[n].id = "#{self.base_id}/#{n}" if !self[n].entries.empty?
+      else
+        self[n] = nil
       end
     end
   end
@@ -184,11 +185,11 @@ class CIMI::Model::Resource
   #
   def initialize(values = {})
     names = self.class.schema.attribute_names
-    @filter_attrs = values[:filter_attr_list] || []
+    @select_attrs = values[:select_attr_list] || []
     # Make sure we always have the :id of the entity even
     # the $select parameter is used and :id is filtered out
     #
-    @_base_id = values[:base_id] || values[:id]
+    @base_id = values[:base_id] || values[:id]
     @attribute_values = names.inject(OrderedHash.new) do |hash, name|
       hash[name] = self.class.schema.convert(name, values[name])
       hash
@@ -196,7 +197,7 @@ class CIMI::Model::Resource
   end
 
   def base_id
-    self.id || @_base_id
+    self.id || @base_id
   end
 
   # Construct a new object from the XML representation +xml+
@@ -258,23 +259,23 @@ class CIMI::Model::Resource
     self.class.to_xml(self)
   end
 
-  def filter_by(filter_opts)
+  def select_by(filter_opts)
     return self if filter_opts.nil?
-    return filter_attributes(filter_opts.split(',').map{ |a| a.intern }) if filter_opts.include?
','
+    return select_attributes(filter_opts.split(',').map{ |a| a.intern }) if filter_opts.include?
','
     case filter_opts
-      when /^([\w\_]+)$/ then filter_attributes([$1.intern])
-      when /^([\w\_]+)\[(\d+\-\d+)\]$/ then filter_by_arr_range($1.intern, $2)
-      when /^([\w\_]+)\[(\d+)\]$/ then filter_by_arr_index($1.intern, $2)
+      when /^([\w\_]+)$/ then select_attributes([$1.intern])
+      when /^([\w\_]+)\[(\d+\-\d+)\]$/ then select_by_arr_range($1.intern, $2)
+      when /^([\w\_]+)\[(\d+)\]$/ then select_by_arr_index($1.intern, $2)
       else self
     end
   end
 
-  def filter_by_arr_index(attr, filter)
+  def select_by_arr_index(attr, filter)
     return self unless self.respond_to?(attr)
     self.class.new(attr => [self.send(attr)[filter.to_i]])
   end
 
-  def filter_by_arr_range(attr, filter)
+  def select_by_arr_range(attr, filter)
     return self unless self.respond_to?(attr)
     filter = filter.split('-').inject { |s,e| s.to_i..e.to_i }
     self.class.new(attr => self.send(attr)[filter])
@@ -292,14 +293,14 @@ class CIMI::Model::Base < CIMI::Model::Resource
 
   hash :property
 
-  def filter_attributes(attr_list)
+  def select_attributes(attr_list)
     attrs = attr_list.inject({}) do |result, attr|
       attr = attr.to_s.underscore
       result[attr.to_sym] = self.send(attr) if self.respond_to?(attr)
       result
     end
     self.class.new(attrs.merge(
-      :filter_attr_list => attr_list,
+      :select_attr_list => attr_list,
       :base_id => self.send(:id)
     ))
   end

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/ac31cba1/server/lib/cimi/models/collection.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/models/collection.rb b/server/lib/cimi/models/collection.rb
index a4faaa6..931adcc 100644
--- a/server/lib/cimi/models/collection.rb
+++ b/server/lib/cimi/models/collection.rb
@@ -70,9 +70,9 @@ module CIMI::Model
       super(a, v)
     end
 
-    def filter_attributes(attr_list)
+    def select_attributes(attr_list)
       self[self.class.entry_name] = entries.map do |e|
-        e.filter_attributes(attr_list)
+        e.select_attributes(attr_list)
       end
       self
     end

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/ac31cba1/server/lib/cimi/models/schema.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/models/schema.rb b/server/lib/cimi/models/schema.rb
index 24a5a0e..4026c69 100644
--- a/server/lib/cimi/models/schema.rb
+++ b/server/lib/cimi/models/schema.rb
@@ -251,6 +251,7 @@ class CIMI::Model::Schema
     end
 
     def to_json(model, json)
+      return if model[name].nil?
       model[name].prepare
       if model[name].entries.empty?
         json[json_name] = { "href" => model[name].href }


Mime
View raw message