You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltacloud.apache.org by lu...@apache.org on 2013/02/06 00:04:26 UTC
[1/2] git commit: CIMI: Initial implementation of $filter parameter
CIMI: Initial implementation of $filter parameter
This parameter should be able to filter all collections
using given key/value pair:
/cimi/machines?$filter=name="Mock1"
should return only those Machines with the 'name' attribute
set to 'Mock1'.
For now only '=' and '!=' operators are supported and only
one filter is supported in URL.
Signed-off-by: Michal fojtik <mf...@redhat.com>
TrackedAt: http://tracker.deltacloud.org/patch/9b2e338b9179d6fcf168790f9e27ece6373530b2
Project: http://git-wip-us.apache.org/repos/asf/deltacloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/deltacloud/commit/b2c15bc4
Tree: http://git-wip-us.apache.org/repos/asf/deltacloud/tree/b2c15bc4
Diff: http://git-wip-us.apache.org/repos/asf/deltacloud/diff/b2c15bc4
Branch: refs/heads/master
Commit: b2c15bc47d5157f180d4a993b89203585302e123
Parents: ed4cc82
Author: Michal Fojtik <mf...@redhat.com>
Authored: Wed Jan 30 13:59:16 2013 +0100
Committer: David Lutterkort <lu...@redhat.com>
Committed: Tue Feb 5 14:56:08 2013 -0800
----------------------------------------------------------------------
server/lib/cimi/collections/machines.rb | 4 +-
server/lib/cimi/helpers/filter_helper.rb | 41 +++++++++++++++++++
server/lib/cimi/models/resource.rb | 2 +
server/tests/cimi/collections/machines_test.rb | 22 ++++++++++
4 files changed, 68 insertions(+), 1 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/deltacloud/blob/b2c15bc4/server/lib/cimi/collections/machines.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/collections/machines.rb b/server/lib/cimi/collections/machines.rb
index 9c0e931..f28cdd1 100644
--- a/server/lib/cimi/collections/machines.rb
+++ b/server/lib/cimi/collections/machines.rb
@@ -24,7 +24,9 @@ module CIMI::Collections
operation :index, :with_capability => :instances do
description "List all machines"
control do
- machines = Machine.list(self).select_by(params['$select'])
+ machines = Machine.list(self)
+ .select_by(params['$select'])
+ .filter_by(params['$filter'])
respond_to do |format|
format.xml { machines.to_xml }
format.json { machines.to_json }
http://git-wip-us.apache.org/repos/asf/deltacloud/blob/b2c15bc4/server/lib/cimi/helpers/filter_helper.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/helpers/filter_helper.rb b/server/lib/cimi/helpers/filter_helper.rb
new file mode 100644
index 0000000..c3c223c
--- /dev/null
+++ b/server/lib/cimi/helpers/filter_helper.rb
@@ -0,0 +1,41 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership. The
+# ASF licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the
+# License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+module CIMI
+ module Helpers
+ module FilterResourceMethods
+
+ def filter_by(filter_opts)
+ return self if filter_opts.nil?
+ return self unless kind_of? CIMI::Model::Collection
+ attribute, value = parse_filter_opts(filter_opts)
+ if attribute =~ /\!$/
+ attribute.chomp!('!')
+ self.entries.delete_if { |entry| entry[attribute.to_sym] == value }
+ else
+ self.entries.delete_if { |entry| entry[attribute.to_sym] != value }
+ end
+ self
+ end
+
+ def parse_filter_opts(opts)
+ attribute, value = opts.split('=')
+ value.gsub!(/\A("|')|("|')\Z/, '')
+ [attribute, value]
+ end
+
+ end
+ end
+end
http://git-wip-us.apache.org/repos/asf/deltacloud/blob/b2c15bc4/server/lib/cimi/models/resource.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/models/resource.rb b/server/lib/cimi/models/resource.rb
index 735aa6b..0586538 100644
--- a/server/lib/cimi/models/resource.rb
+++ b/server/lib/cimi/models/resource.rb
@@ -13,6 +13,7 @@
# License for the specific language governing permissions and limitations
# under the License.
+require_relative '../helpers/filter_helper'
require_relative '../helpers/select_helper'
module CIMI
@@ -21,6 +22,7 @@ module CIMI
extend CIMI::Model::Schema::DSL
include CIMI::Helpers::SelectResourceMethods
+ include CIMI::Helpers::FilterResourceMethods
#
# We keep the values of the attributes in a hash
http://git-wip-us.apache.org/repos/asf/deltacloud/blob/b2c15bc4/server/tests/cimi/collections/machines_test.rb
----------------------------------------------------------------------
diff --git a/server/tests/cimi/collections/machines_test.rb b/server/tests/cimi/collections/machines_test.rb
index 02f3863..f0d00ce 100644
--- a/server/tests/cimi/collections/machines_test.rb
+++ b/server/tests/cimi/collections/machines_test.rb
@@ -72,6 +72,28 @@ describe CIMI::Collections::Machines do
end
end
+ describe '$filter' do
+
+ it 'should filter collection by name attribute' do
+ get root_url("/machines?$filter=name='MockUserInstance'")
+ status.must_equal 200
+ (xml/'Collection/Machine').wont_be_empty
+ (xml/'Collection/Machine').size.must_equal 1
+ xml.at('Collection/count').text.must_equal '1'
+ xml.at('Collection/Machine/name').text.must_equal 'MockUserInstance'
+ end
+
+ it 'should filter collection by reverse name attribute' do
+ get root_url("/machines?$filter=name!='MockUserInstance'")
+ status.must_equal 200
+ (xml/'Collection/Machine').wont_be_empty
+ (xml/'Collection/Machine').size.must_equal 1
+ xml.at('Collection/count').text.must_equal '1'
+ xml.at('Collection/Machine/name').text.must_equal 'Mock Instance With Profile Change'
+ end
+
+ end
+
describe '$select' do
it 'should return only selected attribute' do