From: Michal Fojtik <[email protected]> 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 <[email protected]> --- 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 deletion(-) create mode 100644 server/lib/cimi/helpers/filter_helper.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 } 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 diff --git a/server/lib/cimi/models/resource.rb b/server/lib/cimi/models/resource.rb index e99e1a2..3596828 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 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 -- 1.8.1
