Skip to content

Commit

Permalink
Fixes #8691: Convert activation keys to scoped search.
Browse files Browse the repository at this point in the history
  • Loading branch information
ehelms committed Aug 10, 2015
1 parent aedbfac commit 6c6e3e9
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 97 deletions.
35 changes: 14 additions & 21 deletions app/controllers/katello/api/v2/activation_keys_controller.rb
@@ -1,13 +1,14 @@
module Katello
class Api::V2::ActivationKeysController < Api::V2::ApiController
include Katello::Concerns::FilteredAutoCompleteSearch

before_filter :verify_presence_of_organization_or_environment, :only => [:index]
before_filter :find_environment, :only => [:index, :create, :update]
before_filter :find_optional_organization, :only => [:index, :create, :show]
before_filter :find_activation_key, :only => [:show, :update, :destroy, :available_releases, :copy, :product_content,
:available_host_collections, :add_host_collections, :remove_host_collections,
:content_override, :add_subscriptions, :remove_subscriptions]
before_filter :authorize
before_filter :load_search_service, :only => [:index, :available_host_collections]

wrap_parameters :include => (ActivationKey.attribute_names + %w(host_collection_ids service_level auto_attach content_view_environment))

Expand All @@ -20,7 +21,8 @@ class Api::V2::ActivationKeysController < Api::V2::ApiController
param :name, String, :desc => N_("activation key name to filter by")
param_group :search, Api::V2::ApiController
def index
respond_for_index(:collection => search_for_activation_keys)
activation_key_includes = [:content_view, :environment, :host_collections, :organization]
respond(:collection => scoped_search(index_relation.uniq, :name, :desc, :includes => activation_key_includes))
end

api :POST, "/activation_keys", N_("Create an activation key")
Expand Down Expand Up @@ -203,6 +205,15 @@ def content_override
respond_for_show(:resource => @activation_key)
end

def index_relation
activation_keys = ActivationKey.readable
activation_keys = activation_keys.where(:name => params[:name]) if params[:name]
activation_keys = activation_keys.where(:organization_id => @organization) if @organization
activation_keys = activation_keys.where(:environment_id => @environment) if @environment
activation_keys = activation_keys.where(:content_view_id => @content_view) if @content_view
activation_keys
end

private

def validate_content_overrides(content_params)
Expand All @@ -217,7 +228,7 @@ def validate_content_overrides(content_params)
fail HttpErrors::BadRequest, _("Value must be 0/1, or 'default'")
end

unless @activation_key.available_content.map(&:content).any? { |content| content.label == content_params[:content_label] }
unless @activation_key.valid_content_label?(content_params[:content_label])
fail HttpErrors::BadRequest, _("Invalid content label: %s") % content_params[:content_label]
end
content_params
Expand Down Expand Up @@ -273,24 +284,6 @@ def verify_presence_of_organization_or_environment
fail HttpErrors::BadRequest, _("Either organization ID or environment ID needs to be specified")
end

def search_for_activation_keys
ids = ActivationKey.readable.pluck(:id)
ids &= [params[:id].to_i] if params[:id]
filters = [:terms => { :id => ids }]
filters << { :term => {:organization_id => @organization.id} } if params[:organization_id]
filters << { :terms => {:environment_id => [params[:environment_id]] } } if params[:environment_id]
filters << { :terms => {:content_view_id => [params[:content_view_id]] } } if params[:content_view_id]
filters << { :term => { :name => params[:name] } } if params[:name]

options = {
:filters => filters,
:load_records? => true
}

activation_key_includes = [:content_view, :environment, :host_collections, :organization]
item_search(ActivationKey.includes(activation_key_includes), params, options)
end

def activation_key_params
key_params = params.require(:activation_key).permit(:name,
:description,
Expand Down
3 changes: 0 additions & 3 deletions app/lib/actions/katello/activation_key/create.rb
Expand Up @@ -3,7 +3,6 @@ module Katello
module ActivationKey
class Create < Actions::EntryAction
def plan(activation_key)
activation_key.disable_auto_reindex!
activation_key.save!
if ::Katello.config.use_cp
cp_create = plan_action(Candlepin::ActivationKey::Create,
Expand All @@ -13,7 +12,6 @@ def plan(activation_key)
end
action_subject(activation_key, :cp_id => cp_id)
plan_self
plan_action ElasticSearch::Reindex, activation_key if ::Katello.config.use_elasticsearch
end

def humanized_name
Expand All @@ -22,7 +20,6 @@ def humanized_name

def finalize
activation_key = ::Katello::ActivationKey.find(input[:activation_key][:id])
activation_key.disable_auto_reindex!
activation_key.cp_id = input[:cp_id]
activation_key.save!
end
Expand Down
2 changes: 1 addition & 1 deletion app/lib/actions/katello/system/create.rb
Expand Up @@ -27,7 +27,7 @@ def plan(system, activation_keys = [])
uuid: system.uuid,
capabilities: system.capabilities,
guest_ids: system.guestIds,
activation_keys: activation_keys }
activation_keys: activation_keys.map(&:cp_name) }
system.save!
action_subject system

Expand Down
9 changes: 3 additions & 6 deletions app/lib/katello/resources/candlepin.rb
Expand Up @@ -105,14 +105,10 @@ def get(params)

# rubocop:disable ParameterLists
def create(env_id, _key, name, type, facts, installed_products = [], autoheal = true, release_ver = nil,
service_level = "", uuid = "", capabilities = nil, activation_keys = [], guest_ids = [],
service_level = "", uuid = "", capabilities = nil, activation_key_cp_ids = [], guest_ids = [],
last_checkin = nil)
# rubocop:enable ParameterLists

activation_key_ids = activation_keys.collect do |activation_key|
activation_key.cp_name
end

installed_products ||= []

url = "/candlepin/environments/#{url_encode(env_id)}/consumers/"
Expand All @@ -127,7 +123,8 @@ def create(env_id, _key, name, type, facts, installed_products = [], autoheal =
:capabilities => capabilities,
:guestIds => guest_ids,
:lastCheckin => last_checkin}
url += "?activation_keys=" + activation_key_ids.join(",") if activation_key_ids.length > 0

url += "?activation_keys=" + activation_key_cp_ids.join(",") if activation_key_cp_ids.length > 0
response = self.post(url, attrs.to_json, self.default_headers).body
JSON.parse(response).with_indifferent_access
end
Expand Down
8 changes: 7 additions & 1 deletion app/models/katello/activation_key.rb
Expand Up @@ -3,7 +3,6 @@ class ActivationKey < Katello::Model
self.include_root_in_json = false

include Glue::Candlepin::ActivationKey if Katello.config.use_cp
include Glue::ElasticSearch::ActivationKey if Katello.config.use_elasticsearch
include Glue if Katello.config.use_cp
include Katello::Authorization::ActivationKey
include ForemanTasks::Concerns::ActionSubject
Expand Down Expand Up @@ -49,6 +48,9 @@ class ActivationKey < Katello::Model

scoped_search :on => :name, :complete_value => true
scoped_search :on => :organization_id, :complete_value => true
scoped_search :rename => :environment, :on => :name, :in => :environment, :complete_value => true
scoped_search :rename => :content_view, :on => :name, :in => :content_view, :complete_value => true
scoped_search :on => :content_view_id, :complete_value => true

def environment_exists
if environment_id && environment.nil?
Expand Down Expand Up @@ -122,6 +124,10 @@ def available_content
self.products.map(&:available_content).flatten
end

def valid_content_label?(content_label)
self.available_content.map(&:content).any? { |content| content.label == content_label }
end

# sets up system when registering with this activation key - must be executed in a transaction
def apply_to_system(system)
if !max_content_hosts.nil? && !self.unlimited_content_hosts && usage_count >= max_content_hosts
Expand Down
25 changes: 0 additions & 25 deletions app/models/katello/glue/elastic_search/activation_key.rb

This file was deleted.

1 change: 1 addition & 0 deletions config/routes/api/v2.rb
Expand Up @@ -28,6 +28,7 @@ class ActionDispatch::Routing::Mapper
end

api_resources :activation_keys, :only => [:index, :create, :show, :update, :destroy] do
get :auto_complete_search, :on => :collection
member do
match '/product_content' => 'activation_keys#product_content', :via => :get
match '/content_override' => 'activation_keys#content_override', :via => :put
Expand Down
Expand Up @@ -25,7 +25,8 @@ angular.module('Bastion.activation-keys').factory('ActivationKey',
availableHostCollections: {method: 'GET', params: {action: 'host_collections', action2: 'available'}},
removeHostCollections: {method: 'PUT', isArray: false, params: {action: 'host_collections'}},
addHostCollections: {method: 'POST', isArray: false, params: {action: 'host_collections'}},
contentOverride: {method: 'PUT', isArray: false, params: {action: 'content_override'}}
contentOverride: {method: 'PUT', isArray: false, params: {action: 'content_override'}},
autocomplete: {method: 'GET', isArray: true, params: {id: 'auto_complete_search'}}
});
}]
);
1 change: 0 additions & 1 deletion test/actions/katello/activation_key_test.rb
Expand Up @@ -22,7 +22,6 @@ class CreateTest < TestBase
::Actions::Candlepin::ActivationKey::Create,
:organization_label => activation_key.organization.label,
:auto_attach => true)
assert_action_planed action, ::Actions::ElasticSearch::Reindex
end

it 'raises error when validation fails' do
Expand Down
57 changes: 20 additions & 37 deletions test/controllers/api/v2/activation_keys_controller_test.rb
Expand Up @@ -7,20 +7,14 @@ class Api::V2::ActivationKeysControllerTest < ActionController::TestCase
include Support::ForemanTasks::Task

def models
ActivationKey.any_instance.stubs(:products).returns([])
ActivationKey.any_instance.stubs(:valid_content_label?).returns(true)
ActivationKey.any_instance.stubs(:content_overrides).returns([])
ActivationKey.any_instance.stubs(:products).returns([])

@activation_key = ActivationKey.find(katello_activation_keys(:simple_key))
@organization = get_organization
@activation_key = ActivationKey.find(katello_activation_keys(:simple_key))
@view = katello_content_views(:library_view)
@library = @organization.library

@activation_key.stubs(:get_key_pools).returns([])
@activation_key.stubs(:auto_attach).returns(nil)

::Katello::ActivationKey.stubs(:find).returns(@activation_key)

stub_find_organization(@organization)
end

def permissions
Expand All @@ -32,41 +26,31 @@ def permissions

def setup
setup_controller_defaults_api
@request.env['HTTP_ACCEPT'] = 'application/json'
@request.env['CONTENT_TYPE'] = 'application/json'
login_user(User.find(users(:admin)))
@fake_search_service = @controller.load_search_service(Support::SearchService::FakeSearchService.new)

models
permissions
end

def test_index
@fake_search_service.stubs(:retrieve).returns([[@activation_key], 1])
@fake_search_service.stubs(:total_items).returns(1)

results = JSON.parse(get(:index, :organization_id => @organization.id).body)

assert_response :success
assert_template 'api/v2/activation_keys/index'

assert_equal results.keys.sort, ['page', 'per_page', 'results', 'search', 'sort', 'subtotal', 'total']
assert_equal results['results'].size, 1
assert_equal results['results'][0]['id'], @activation_key.id
assert_equal results['results'].size, 6
assert_includes results['results'].collect { |item| item['id'] }, @activation_key.id
end

def test_index_protected
allowed_perms = [@view_permission]
denied_perms = [@create_permission, @update_permission, @destroy_permission]

assert_protected_action(:index, allowed_perms, denied_perms) do
get :index, :organization_id => @organization.label
get :index, :organization_id => @organization.id
end
end

def test_show
@fake_search_service.stubs(:retrieve).returns([[@activation_key], 1])
@fake_search_service.stubs(:total_items).returns(1)
results = JSON.parse(get(:show, :id => @activation_key.id).body)

assert_equal results['name'], 'Simple Activation Key'
Expand All @@ -76,9 +60,6 @@ def test_show
end

def test_show_protected
@fake_search_service.stubs(:retrieve).returns([[@activation_key], 1])
@fake_search_service.stubs(:total_items).returns(1)

allowed_perms = [@view_permission]
denied_perms = [@create_permission, @update_permission, @destroy_permission]

Expand All @@ -98,6 +79,7 @@ def test_create_protected
end

def test_create_unlimited
ActivationKey.any_instance.expects(:reload)
assert_sync_task(::Actions::Katello::ActivationKey::Create) do |activation_key|
activation_key.max_content_hosts.must_be_nil
end
Expand All @@ -110,13 +92,18 @@ def test_create_unlimited
end

def test_update
assert_sync_task(::Actions::Katello::ActivationKey::Update) do |activation_key, activation_key_params|
assert_equal activation_key.id, @activation_key.id
assert_equal activation_key_params[:name], 'New Name'
assert_equal activation_key_params[:max_content_hosts], "2"
assert_equal activation_key_params[:unlimited_content_hosts], false
end

put :update, :id => @activation_key.id, :organization_id => @organization.id,
:activation_key => {:name => 'New Name', :max_content_hosts => 2}
:activation_key => {:name => 'New Name', :max_content_hosts => 2}

assert_response :success
assert_template 'api/v2/activation_keys/show'
assert_equal assigns[:activation_key].name, 'New Name'
assert_equal assigns[:activation_key].max_content_hosts, 2
end

def test_update_protected
Expand Down Expand Up @@ -194,23 +181,19 @@ def test_content_override_protected
end

def test_content_override
results = JSON.parse(put(:update, :id => @activation_key.id, :content_label => 'some-content',
:name => 'enabled', :value => 1).body)
ActivationKey.any_instance.expects(:set_content_override).returns(true)

assert_equal results['name'], 'enabled'
put(:content_override, :id => @activation_key.id, :content_override => {:content_label => 'some-content',
:name => 'enabled', :value => 1})

assert_response :success
assert_template 'api/v2/activation_keys/show'
end

def test_content_override_empty
results = JSON.parse(put(:update, :id => @activation_key.id, :content_label => 'some-content',
:name => 'enabled').body)
put(:content_override, :id => @activation_key.id, :content_override => {:content_label => 'some-content', :name => 'enabled'})

assert_equal results['name'], 'enabled'

assert_response :success
assert_template 'api/v2/activation_keys/show'
assert_response 400
end

def test_add_subscriptions_protected
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/models/katello_activation_keys.yml
Expand Up @@ -44,4 +44,4 @@ library_dev_staging_view_key:
organization_id: <%= ActiveRecord::Fixtures.identify(:empty_organization) %>
environment_id: <%= ActiveRecord::Fixtures.identify(:dev) %>
content_view_id: <%= ActiveRecord::Fixtures.identify(:library_dev_staging_view) %>
auto_attach: true
auto_attach: true

0 comments on commit 6c6e3e9

Please sign in to comment.