Skip to content

Commit

Permalink
fixes #10569 - convert products to scoped search
Browse files Browse the repository at this point in the history
  • Loading branch information
jlsherrill committed May 27, 2015
1 parent 2bc486b commit 2e40ce1
Show file tree
Hide file tree
Showing 18 changed files with 73 additions and 129 deletions.
49 changes: 16 additions & 33 deletions app/controllers/katello/api/v2/products_controller.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
module Katello
class Api::V2::ProductsController < Api::V2::ApiController
include Katello::Concerns::FilteredAutoCompleteSearch

before_filter :find_activation_key, :only => [:index]
before_filter :find_system, :only => [:index]
before_filter :find_organization, :only => [:create, :index]
before_filter :find_organization, :only => [:create, :index, :auto_complete_search]
before_filter :find_product, :only => [:update, :destroy, :sync]
before_filter :find_organization_from_product, :only => [:update]
before_filter :authorize_gpg_key, :only => [:update, :create]
Expand All @@ -28,25 +30,19 @@ class Api::V2::ProductsController < Api::V2::ApiController
param :custom, :bool, :desc => N_("Filter products by custom")
param_group :search, Api::V2::ApiController
def index
options = {
:filters => [],
:load_records? => true
}
products = Product.readable.where(:organization_id => @organization.id)
products = products.where(:provider_id => @organization.anonymous_provider.id) if params[:custom]

ids = products.pluck(:id)
ids = filter_by_subscription(ids, params[:subscription_id]) if params[:subscription_id]
ids = filter_by_activation_key(ids, @activation_key) if @activation_key
ids = filter_by_system(ids, @system) if @system

options[:filters] << {:terms => {:id => ids}}
options[:filters] << {:term => {:name => params[:name]}} if params[:name]
options[:filters] << {:term => {:enabled => ::Foreman::Cast.to_bool(params[:enabled])}} if params[:enabled]
options.merge!(sort_params)
options[:includes] = [:sync_plan, :provider]

respond(:collection => item_search(Product, params, options))
options = {:includes => [:sync_plan, :provider]}
respond(:collection => scoped_search(index_relation.uniq, :name, :desc, options))
end

def index_relation
query = Product.readable.where(:organization_id => @organization.id)
query = query.where(:provider_id => @organization.anonymous_provider.id) if params[:custom]
query = query.where(:name => params[:name]) if params[:name]
query = query.enabled if params[:enabled]
query = query.where(:id => @activation_key.products) if @activation_key
query = query.where(:id => @system.products) if @system
query = query.where(:id => Pool.find_by_id!(params[:subscription_id]).products) if params[:subscription_id]
query
end

api :POST, "/products", N_("Create a product")
Expand Down Expand Up @@ -120,15 +116,6 @@ def find_system
end
end

def filter_by_subscription(ids, subscription_id)
@subscription = Pool.find_by_id!(subscription_id)
ids & @subscription.products.pluck("#{Product.table_name}.id")
end

def filter_by_activation_key(ids = [], activation_key)
ids & activation_key.products.map { |product| product.id }
end

def find_organization_from_product
@organization = @product.organization
end
Expand All @@ -141,10 +128,6 @@ def authorize_gpg_key
end
end

def filter_by_system(ids = [], system)
ids & system.products.map { |product| product.id }
end

def product_params
# only allow sync plan id to be updated if the product is a Red Hat product
if @product && @product.redhat?
Expand Down
21 changes: 3 additions & 18 deletions app/controllers/katello/products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,26 +47,11 @@ def toggle_repository
render :partial => 'katello/providers/redhat/enable_errors', :locals => { :error_message => e.message}, :status => 500
end

#used by content search
def auto_complete
query = "name_autocomplete:#{params[:term]}"
org = current_organization

readable_ids = []
readable_ids += Product.readable.pluck(:id) if Product.readable?
readable_ids += ContentView.readable_products.pluck("#{Katello::Product.table_name}.id")
readable_ids.uniq

products = Product.search do
query do
string query
end
filter :term, :organization_id => org.id
filter :terms, :id => readable_ids
end

products = Product.readable.enabled.where(:organization_id => current_organization).
where("#{Product.table_name}.name ILIKE ?", "#{params[:term]}%")
render :json => products.collect { |s| {:label => s.name, :value => s.name, :id => s.id} }
rescue Tire::Search::SearchRequestFailed
render :json => Util::Support.array_with_total
end

private
Expand Down
3 changes: 0 additions & 3 deletions app/lib/actions/katello/product/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module Katello
module Product
class Create < Actions::EntryAction
def plan(product, organization)
product.disable_auto_reindex!
product.provider = organization.anonymous_provider
product.organization = organization

Expand All @@ -21,13 +20,11 @@ def plan(product, organization)
action_subject product, :cp_id => cp_id

plan_self
plan_action ElasticSearch::Reindex, product
plan_action ElasticSearch::Provider::ReindexSubscriptions, product.provider
end

def finalize
product = ::Katello::Product.find(input[:product][:id])
product.disable_auto_reindex!
product.cp_id = input[:cp_id]
product.save!
end
Expand Down
2 changes: 0 additions & 2 deletions app/lib/actions/katello/product/destroy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ def plan(product, options = {})
end

no_other_assignment = ::Katello::Product.where(["cp_id = ? AND id != ?", product.cp_id, product.id]).count == 0
product.disable_auto_reindex!
action_subject(product)

sequence do
Expand Down Expand Up @@ -46,7 +45,6 @@ def plan(product, options = {})
end

plan_self
plan_action(ElasticSearch::Reindex, product)
plan_action(ElasticSearch::Provider::ReindexSubscriptions, product.provider) unless organization_destroy
end
end
Expand Down
2 changes: 0 additions & 2 deletions app/lib/actions/katello/product/update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module Katello
module Product
class Update < Actions::EntryAction
def plan(product, product_params)
product.disable_auto_reindex!
action_subject product
product.update_attributes!(product_params)
if product.previous_changes.key?('gpg_key_id')
Expand All @@ -14,7 +13,6 @@ def plan(product, product_params)
plan_action(::Actions::Candlepin::Product::Update, product)
end
plan_action(::Actions::Pulp::Repos::Update, product) if ::Katello.config.use_pulp
plan_action(ElasticSearch::Reindex, product) if ::Katello.config.use_elasticsearch
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ def plan(product, content, options)
options,
options[:registry_name]).find_repository
action_subject(repository)
plan_action(ElasticSearch::Reindex, repository.product)
plan_action(Repository::Destroy, repository)
else
fail ::Katello::Errors::NotFound, _('Repository not found')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ def plan(product, content, options)
end
repository = mapper.build_repository
plan_action(Repository::Create, repository)
plan_action(ElasticSearch::Reindex, repository.product)
action_subject(repository)
end

Expand Down
9 changes: 0 additions & 9 deletions app/models/katello/glue/elastic_search/marketing_product.rb

This file was deleted.

41 changes: 0 additions & 41 deletions app/models/katello/glue/elastic_search/product.rb

This file was deleted.

2 changes: 0 additions & 2 deletions app/models/katello/marketing_product.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
module Katello
class MarketingProduct < Product
include Glue::ElasticSearch::MarketingProduct if Katello.config.use_elasticsearch

has_many :marketing_engineering_products, :dependent => :destroy
has_many :engineering_products, :through => :marketing_engineering_products
validates_lengths_from_database
Expand Down
17 changes: 16 additions & 1 deletion app/models/katello/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ class Product < Katello::Model
self.include_root_in_json = false

include ForemanTasks::Concerns::ActionSubject
include Glue::ElasticSearch::Product if Katello.config.use_elasticsearch
include Glue::Candlepin::Product if Katello.config.use_cp
include Glue::Pulp::Repos if Katello.config.use_pulp
include Glue if Katello.config.use_cp || Katello.config.use_pulp
Expand Down Expand Up @@ -34,6 +33,10 @@ class Product < Katello::Model

scoped_search :on => :name, :complete_value => true
scoped_search :on => :organization_id, :complete_value => true
scoped_search :on => :label, :complete_value => true
scoped_search :on => :description
scoped_search :in => :provider, :on => :provider_type, :rename => :redhat,
:complete_value => {:true => Provider::REDHAT, :false => Provider::ANONYMOUS }

def library_repositories
self.repositories.in_default_view
Expand Down Expand Up @@ -210,6 +213,18 @@ def cdn_resource
::Katello::Resources::CDN::CdnResource.new(provider.repository_url, certs)
end

def total_package_count(env, view)
repo_ids = view.repos(env).in_product(self).collect { |r| r.pulp_id }
result = Katello::Package.legacy_search('*', 0, 1, repo_ids)
result.length > 0 ? result.total : 0
end

def total_puppet_module_count(env, view)
repo_ids = view.repos(env).in_product(self).collect { |r| r.pulp_id }
results = Katello::PuppetModule.legacy_search('', :page_size => 1, :repoids => repo_ids)
results.empty? ? 0 : results.total
end

def self.humanize_class_name(_name = nil)
_("Product and Repositories")
end
Expand Down
3 changes: 3 additions & 0 deletions config/routes/api/v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ class ActionDispatch::Routing::Mapper
member do
post :sync
end
collection do
get :auto_complete_search
end
api_resources :repository_sets, :only => [:index, :show] do
member do
get :available_repositories
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ angular.module('Bastion.products').factory('Product',
return BastionResource('/katello/api/products/:id/:action', {id: '@id'}, {
update: { method: 'PUT'},
sync: { method: 'POST', params: { action: 'sync' }},
updateSyncPlan: { method: 'POST', params: { action: 'sync_plan' }}
updateSyncPlan: { method: 'POST', params: { action: 'sync_plan' }},
autocomplete: {method: 'GET', isArray: true, params: {id: 'auto_complete_search'}}
});

}]
Expand Down
4 changes: 0 additions & 4 deletions spec/controllers/products_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ module Katello
end

describe "get auto_complete_product" do
before :each do
Product.expects(:search).once.returns([OpenStruct.new(:name => "a", :id => 100)])
end

it 'should succeed' do
get :auto_complete, :term => "a"
must_respond_with(:success)
Expand Down
7 changes: 0 additions & 7 deletions test/actions/katello/product_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ class CreateTest < TestBase
params[:cp_id].must_be_kind_of Dynflow::ExecutionPlan::OutputReference
params[:cp_id].subkeys.must_equal %w(response id)
end

product.expects(:disable_auto_reindex!).returns
product.expects(:save!).returns([])

plan_action(action, product, product.organization)
Expand All @@ -94,7 +92,6 @@ class CreateTest < TestBase

# TODO: figure out how to specify the candlepin id or a placeholder
assert_action_planed(action, ::Actions::Candlepin::Product::CreateUnlimitedSubscription)
assert_action_planed_with(action, ::Actions::ElasticSearch::Reindex, product)
end
end

Expand All @@ -113,7 +110,6 @@ class UpdateTest < TestBase
assert_action_planed_with(action,
::Actions::Pulp::Repos::Update,
product)
assert_action_planed action, ::Actions::ElasticSearch::Reindex
end

it 'raises error when validation fails' do
Expand All @@ -140,7 +136,6 @@ class DestroyTest < TestBase
default_view_repos = product.repositories.in_default_view.map(&:id)

action.expects(:plan_self)
product.expects(:disable_auto_reindex!)

plan_action(action, product)

Expand All @@ -156,8 +151,6 @@ class DestroyTest < TestBase

assert_action_planed_with(action, candlepin_delete_subscriptions_class,
cp_id: product.cp_id, organization_label: product.organization.label)

assert_action_planed_with(action, ::Actions::ElasticSearch::Reindex, product)
end

it 'fails' do
Expand Down
9 changes: 8 additions & 1 deletion test/controllers/api/v2/products_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def models
@product = Product.find(katello_products(:empty_product))
@product.stubs(:redhat?).returns(false)
Product.any_instance.stubs('productContent').returns([])
Product.any_instance.stubs('sync_status').returns(PulpSyncStatus.new)
end

def permissions
Expand All @@ -24,7 +25,6 @@ def permissions

def setup
setup_controller_defaults_api
@fake_search_service = @controller.load_search_service(Support::SearchService::FakeSearchService.new)
models
permissions
end
Expand All @@ -36,6 +36,13 @@ def test_index
assert_template 'api/v2/products/index'
end

def test_index_name
get :index, :organization_id => @organization.id, :name => @product.name

assert_response :success
assert_template 'api/v2/products/index'
end

def test_index_protected
allowed_perms = [@read_permission]
denied_perms = [@create_permission, @delete_permission, @update_permission]
Expand Down
Loading

0 comments on commit 2e40ce1

Please sign in to comment.