deltacloud-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfoj...@apache.org
Subject svn commit: r1211932 - in /deltacloud/trunk/server/lib/cimi: model/base.rb server.rb
Date Thu, 08 Dec 2011 15:26:43 GMT
Author: mfojtik
Date: Thu Dec  8 15:26:43 2011
New Revision: 1211932

URL: http://svn.apache.org/viewvc?rev=1211932&view=rev
Log:
CIMI: Added CIMISelect query param to filter Collection output

Modified:
    deltacloud/trunk/server/lib/cimi/model/base.rb
    deltacloud/trunk/server/lib/cimi/server.rb

Modified: deltacloud/trunk/server/lib/cimi/model/base.rb
URL: http://svn.apache.org/viewvc/deltacloud/trunk/server/lib/cimi/model/base.rb?rev=1211932&r1=1211931&r2=1211932&view=diff
==============================================================================
--- deltacloud/trunk/server/lib/cimi/model/base.rb (original)
+++ deltacloud/trunk/server/lib/cimi/model/base.rb Thu Dec  8 15:26:43 2011
@@ -209,4 +209,36 @@ class CIMI::Model::Base
   end
 
   def self.all(_self); find(:all, _self); end
+
+  def filter_by(filter_opts)
+    return self if filter_opts.nil?
+    return filter_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)
+      else self
+    end
+  end
+
+  private
+
+  def filter_attributes(attr_list)
+    attrs = attr_list.inject({}) do |result, attr|
+      result[attr] = self.send(attr) if self.respond_to?(attr)
+      result
+    end
+    self.class.new(attrs)
+  end
+
+  def filter_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)
+    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])
+  end
 end

Modified: deltacloud/trunk/server/lib/cimi/server.rb
URL: http://svn.apache.org/viewvc/deltacloud/trunk/server/lib/cimi/server.rb?rev=1211932&r1=1211931&r2=1211932&view=diff
==============================================================================
--- deltacloud/trunk/server/lib/cimi/server.rb (original)
+++ deltacloud/trunk/server/lib/cimi/server.rb Thu Dec  8 15:26:43 2011
@@ -92,9 +92,10 @@ global_collection :machine_configuration
   description 'List all machine configurations'
 
   operation :index do
+    param :CIMISelect,  :string,  :optional
     description "List all machine configurations"
     control do
-      machine_configs = MachineConfigurationCollection.default(self)
+      machine_configs = MachineConfigurationCollection.default(self).filter_by(params[:CIMISelect])
       respond_to do |format|
         format.xml { machine_configs.to_xml }
         format.json { machine_configs.to_json }
@@ -125,8 +126,9 @@ global_collection :machine_images do
 
   operation :index do
     description "List all machine configurations"
+    param :CIMISelect,  :string,  :optional
     control do
-      machine_images = MachineImageCollection.default(self)
+      machine_images = MachineImageCollection.default(self).filter_by(params[:CIMISelect])
       respond_to do |format|
         format.xml { machine_images.to_xml }
         format.json { machine_images.to_json }
@@ -153,9 +155,10 @@ global_collection :machine_admins do
 
   operation :index do
     description "List all machine admins"
+    param :CIMISelect,  :string,  :optional
     with_capability :keys
     control do
-      machine_admins = MachineAdminCollection.default(self)
+      machine_admins = MachineAdminCollection.default(self).filter_by(params[:CIMISelect])
       respond_to do |format|
         format.xml { machine_admins.to_xml }
         format.json { machine_admins.to_json }
@@ -208,9 +211,10 @@ global_collection :machines do
   description 'List all machine'
 
   operation :index do
+    param :CIMISelect,  :string,  :optional
     description "List all machines"
     control do
-      machines = MachineCollection.default(self)
+      machines = MachineCollection.default(self).filter_by(params[:CIMISelect])
       respond_to do |format|
         format.xml { machines.to_xml }
         format.json { machines.to_json }
@@ -313,8 +317,9 @@ global_collection :volumes do
 
   operation :index do
     description "List all volumes"
+    param :CIMISelect,  :string,  :optional
     control do
-      volumes = VolumeCollection.default(self)
+      volumes = VolumeCollection.default(self).filter_by(params[:CIMISelect])
       respond_to do |format|
         format.xml { volumes.to_xml }
         format.json { volumes.to_json }
@@ -334,7 +339,6 @@ global_collection :volumes do
     end
   end
 
-
   operation :create do
     description "Create a new Volume."
     control do
@@ -361,9 +365,6 @@ global_collection :volumes do
     end
   end
 
-
-
-
 end
 
 global_collection :volume_configurations do
@@ -371,8 +372,9 @@ global_collection :volume_configurations
 
   operation :index do
     description "Get list all VolumeConfigurations"
+    param :CIMISelect,  :string,  :optional
     control do
-      volume_configuration = VolumeConfigurationCollection.default(self)
+      volume_configuration = VolumeConfigurationCollection.default(self).filter_by(params[:CIMISelect])
       respond_to do |format|
         format.xml { volume_configuration.to_xml }
         format.json { volume_configuration.to_json }
@@ -397,8 +399,9 @@ global_collection :volume_images do
 
   operation :index do
     description "List all volumes images"
+    param :CIMISelect,  :string,  :optional
     control do
-      volume_images = VolumeImageCollection.default(self)
+      volume_images = VolumeImageCollection.default(self).filter_by(params[:CIMISelect])
       respond_to do |format|
         format.xml { volume_images.to_xml }
         format.json { volume_images.to_json }



Mime
View raw message