-
Notifications
You must be signed in to change notification settings - Fork 297
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1974 from parthaa/filter-rule-cli
Implementation for add/remove filter rules via cli
- Loading branch information
Showing
7 changed files
with
197 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
# | ||
# Katello Organization actions | ||
# Copyright (c) 2013 Red Hat, Inc. | ||
# | ||
# This software is licensed to you under the GNU General Public License, | ||
# version 2 (GPLv2). There is NO WARRANTY for this software, express or | ||
# implied, including the implied warranties of MERCHANTABILITY or FITNESS | ||
# FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 | ||
# along with this software; if not, see | ||
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. | ||
# | ||
# Red Hat trademarks are not licensed under GPLv2. No permission is | ||
# granted to use or replicate Red Hat trademarks that are incorporated | ||
# in this software or its documentation. | ||
# | ||
|
||
class Api::FilterRulesController < Api::ApiController | ||
respond_to :json | ||
before_filter :find_organization | ||
before_filter :find_definition | ||
before_filter :find_filter | ||
before_filter :find_filter_rule, :except => [:create] | ||
before_filter :authorize | ||
|
||
def rules | ||
definition_editable = lambda { @definition && @definition.editable? } | ||
{ | ||
:create => definition_editable, | ||
:destroy => definition_editable, | ||
} | ||
end | ||
|
||
api :POST, | ||
"/organizations/:organization_id/content_view_definitions/:content_view_definition_id/filters/:filter_id/rules", | ||
"Create a filter rule for a content filter" | ||
param :organization_id, :identifier, :desc => "organization identifier", :required => true | ||
param :content_view_definition_id, String, :desc => "id of the content view definition", :required => true | ||
param :filter_id, String, :desc => "name of the filter", :required => true | ||
param :rule, String, :required => true, :desc => "A specification of the rule in json format (required)." | ||
param :content, String, :desc => "content type of the rule", :required => true | ||
param :inclusion, String, :desc => "true if its an includes rule, false otherwise. Defauls to true", :required => false | ||
def create | ||
rule = JSON.parse(params[:rule]).with_indifferent_access | ||
inclusion = params[:inclusion].to_s.to_bool | ||
content_type = params[:content] | ||
if rule.has_key?(:date_range) | ||
date_range = rule[:date_range] | ||
date_range[:start]= date_range[:start].to_time.to_i if date_range.has_key?(:start) | ||
date_range[:end] = date_range[:end].to_time.to_i if date_range.has_key?(:end) | ||
end | ||
fr = FilterRule.create_for(content_type, :filter => @filter , :inclusion => inclusion, :parameters => rule) | ||
render :json => fr | ||
end | ||
|
||
api :DELETE, | ||
"/organizations/:organization_id/content_view_definitions/:content_view_definition_id/filters/:filter_id/rules/:id", | ||
"Delete a filter rule" | ||
param :organization_id, :identifier, :desc => "organization identifier", :required => true | ||
param :content_view_definition_id, String, :desc => "id of the content view definition", :required => true | ||
param :filter_id, String, :desc => "name of the filter", :required => true | ||
param :id, :String, :desc => "Id of the filter rule", :required => true | ||
def destroy | ||
@filter_rule.destroy | ||
render :json => @filter_rule | ||
end | ||
|
||
private | ||
|
||
def find_definition | ||
@definition = ContentViewDefinition.where(:organization_id => @organization.id).find(params[:content_view_definition_id]) | ||
end | ||
|
||
def find_filter | ||
id = params[:filter_id] | ||
@filter = Filter.where(:id => id, :content_view_definition_id => @definition).first | ||
raise HttpErrors::NotFound, _("Couldn't find filter '%s'") % params[:id] if @filter.nil? | ||
@filter | ||
end | ||
|
||
def find_filter_rule | ||
id = params[:id] | ||
@filter_rule = FilterRule.where(:filter_id => @filter.id, :id => id).first | ||
raise HttpErrors::NotFound, _("Couldn't find filter rule '%s'") % params[:id] if @filter_rule.nil? | ||
@filter_rule | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# encoding: utf-8 | ||
# | ||
# Copyright 2013 Red Hat, Inc. | ||
# | ||
# This software is licensed to you under the GNU General Public | ||
# License as published by the Free Software Foundation; either version | ||
# 2 of the License (GPLv2) or (at your option) any later version. | ||
# There is NO WARRANTY for this software, express or implied, | ||
# including the implied warranties of MERCHANTABILITY, | ||
# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should | ||
# have received a copy of GPLv2 along with this software; if not, see | ||
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. | ||
|
||
require "minitest_helper" | ||
|
||
class Api::FilterRulesControllerTest < MiniTest::Rails::ActionController::TestCase | ||
fixtures :all | ||
|
||
def setup | ||
models = ["Organization", "KTEnvironment", "User", "Filter", | ||
"FilterRule", "ErratumRule", "PackageRule", "PackageGroupRule", | ||
"ContentViewEnvironment", "ContentViewDefinition"] | ||
disable_glue_layers(["Candlepin", "Pulp", "ElasticSearch"], models) | ||
login_user(User.find(users(:admin))) | ||
@filter = filters(:simple_filter) | ||
end | ||
|
||
|
||
test "should delete a filter_rule" do | ||
populated_filter = filters(:populated_filter) | ||
rule_id = populated_filter.rules.first.id | ||
delete :destroy, :organization_id => populated_filter.content_view_definition.organization.label, | ||
:content_view_definition_id=> populated_filter.content_view_definition.id, | ||
:filter_id => populated_filter.id.to_s, :id => rule_id | ||
assert_response :success | ||
assert_empty FilterRule.where(:id => rule_id) | ||
end | ||
|
||
[ | ||
[FilterRule::PACKAGE, {:units => [{:name =>"w*", :version => "4.0"}]}], | ||
[FilterRule::PACKAGE_GROUP, {:units => [{:name =>["w*"]}]}], | ||
[FilterRule::ERRATA, {:units => [{:id =>["RH1", "RH2"]}]}], | ||
[FilterRule::ERRATA, {:errata_type => ["bugfix", "enhancement", "security"]}] | ||
].each do |content, rule| | ||
[true, false].each do |inclusion| | ||
test "should create a filter #{content} rule for inclusion = #{inclusion}" do | ||
rule = rule.with_indifferent_access | ||
post :create, :organization_id => @filter.content_view_definition.organization.label, | ||
:content_view_definition_id=> @filter.content_view_definition.id, | ||
:filter_id => @filter.id.to_s, :content => content, :inclusion => inclusion, | ||
:rule => rule.to_json | ||
assert_response :success | ||
response_hash = JSON.parse(response.body) | ||
assert_kind_of Hash, response_hash | ||
response_hash = response_hash.with_indifferent_access | ||
assert_equal content, response_hash[:content] | ||
assert_equal inclusion, response_hash[:inclusion] | ||
assert_equal rule, response_hash["rule"] | ||
refute_nil FilterRule.find(response_hash["id"]) | ||
end | ||
end | ||
end | ||
|
||
test "should create an errata rule based on date" do | ||
content = FilterRule::ERRATA | ||
inclusion = true | ||
rule = {:date_range => {:start => "2013-04-15T15:44:48-04:00", | ||
:end => "2013-05-15T15:44:48-04:00"}}.with_indifferent_access | ||
post :create, :organization_id => @filter.content_view_definition.organization.label, | ||
:content_view_definition_id=> @filter.content_view_definition.id, | ||
:filter_id => @filter.id.to_s, :content => content, :inclusion => inclusion, | ||
:rule => rule.to_json | ||
assert_response :success | ||
response_hash = JSON.parse(response.body) | ||
assert_kind_of Hash, response_hash | ||
response_hash = response_hash.with_indifferent_access | ||
assert_equal content, response_hash[:content] | ||
assert_equal inclusion, response_hash[:inclusion] | ||
assert_equal rule[:date_range][:start].to_date, response_hash[:rule][:date_range][:start].to_date | ||
assert_equal rule[:date_range][:end].to_date, response_hash[:rule][:date_range][:end].to_date | ||
refute_nil FilterRule.find(response_hash["id"]) | ||
end | ||
end |