Skip to content

Commit

Permalink
Merge pull request #1974 from parthaa/filter-rule-cli
Browse files Browse the repository at this point in the history
Implementation for add/remove filter rules via cli
  • Loading branch information
parthaa committed Apr 19, 2013
2 parents 736ae62 + 7aa73a8 commit f5b2d45
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 4 deletions.
87 changes: 87 additions & 0 deletions app/controllers/api/filter_rules_controller.rb
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
2 changes: 1 addition & 1 deletion app/controllers/filter_rules_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def destroy_parameters
@rule.save!

notify.success(_("Rule parameters successfully deleted for rule type '%{type}'. Parameters deleted: %{parameters}.") %
{:type => FilterRule::CONTENT_OPTIONS.index(@rule.content_type),
{:type => FilterRule::CONTENT_OPTIONS.key(@rule.content_type),
:parameters => params[:units].join(', ')})

render :nothing => true
Expand Down
11 changes: 11 additions & 0 deletions app/models/erratum_rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,15 @@ def generate_clauses(repo)
end
end
end

def as_json(options = {})
params = Util::Support.deep_copy(parameters).with_indifferent_access
from_date = start_date
to_date = end_date
params[:date_range][:start] = from_date if from_date
params[:date_range][:end] = to_date if to_date
json_val = super(options).update("rule" => params)
json_val.delete("parameters")
json_val
end
end
3 changes: 2 additions & 1 deletion app/models/filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ def as_json(options = {})
super(options).update("content_view_definition_label" => content_view_definition.label,
"organization" => content_view_definition.organization.label,
"products" => products.collect(&:name),
"repos" => repositories.collect(&:name))
"repos" => repositories.collect(&:name),
"rules" => rules)
end

def validate_filter_products_and_repos(errors, cvd)
Expand Down
14 changes: 12 additions & 2 deletions app/models/filter_rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ def self.class_for( content_type)
when ERRATA
ErratumRule
else
params = {:content_type => content_type, :content_types => CONTENT_TYPES.join(",")}
raise (_("Invalid content type (%{content_type}) provided. Content types can be one of %{content_types}") % params)
params = {:content_type => content_type, :content_types => CONTENT_TYPES.join(", ")}
raise (_("Invalid content type '%{content_type}' provided. Content types can be one of %{content_types}") % params)
end
end

Expand All @@ -58,4 +58,14 @@ def self.create_for( content_type, options)
clazz = class_for(content_type)
clazz.create!(options)
end

def as_json(options = {})
json_val = super(options).update("id" => id,
"content" => content_type,
"type" => inclusion ? _("includes"): _("excludes"),
"rule" => parameters)
json_val.delete("parameters")
json_val
end

end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,7 @@ def matches?(request)
put :index, :action => :update_repositories
end
end
resources :rules, :controller => :filter_rules, :only => [:create, :destroy]
end
end
end
Expand Down
83 changes: 83 additions & 0 deletions test/controllers/api/filter_rules_controller_test.rb
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

0 comments on commit f5b2d45

Please sign in to comment.