Skip to content

Commit

Permalink
fixes #8481 - adds content override to content host API, BZ1166891
Browse files Browse the repository at this point in the history
  • Loading branch information
cfouant committed Aug 10, 2015
1 parent aedbfac commit 6827d36
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 8 deletions.
53 changes: 48 additions & 5 deletions app/controllers/katello/api/v2/systems_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ class Api::V2::SystemsController < Api::V2::ApiController
:package_profile,
:pools, :enabled_repos, :releases,
:available_host_collections,
:refresh_subscriptions, :tasks, :content_override, :events]
:refresh_subscriptions, :tasks, :content_override,
:product_content, :events]
before_filter :find_environment, :only => [:index, :report]
before_filter :find_optional_organization, :only => [:create, :index, :report]
before_filter :find_host_collection, :only => [:index]
Expand Down Expand Up @@ -254,11 +255,35 @@ def releases
respond_for_index :collection => response
end

api :PUT, "/systems/:id/content_override", N_("Set content overrides for the content host")
param :id, String, :desc => N_("UUID of the content host"), :required => true
param :content_override, Hash, :desc => N_("Content override parameters") do
param :content_label, String, :desc => N_("Label of the content"), :required => true
param :value, [0, 1, "default"], :desc => N_("Override to 0/1, or 'default'"), :required => true
end
def content_override
content_override = params[:content_override]
@system.set_content_override(content_override[:content_label],
content_override[:name], content_override[:value]) if content_override
respond_for_show
content_override = validate_content_overrides(params[:content_override])
@system.set_content_override(content_override[:content_label], 'enabled', content_override[:value])

content = @system.available_content
response = {
:results => content,
:total => content.size,
:subtotal => content.size
}
respond_for_index :collection => response
end

api :GET, "/systems/:id/product_content", N_("Get content overrides for the content host")
param :id, String, :desc => N_("UUID of the content host"), :required => true
def product_content
content = @system.available_content
response = {
:results => content,
:total => content.size,
:subtotal => content.size
}
respond_for_index :collection => response
end

api :GET, "/systems/:id/events", N_("List Candlepin events for the content host")
Expand All @@ -270,6 +295,24 @@ def events

private

def validate_content_overrides(content_params)
case content_params[:value].to_s
when 'default'
content_params[:value] = nil
when '1'
content_params[:value] = 1
when '0'
content_params[:value] = 0
else
fail HttpErrors::BadRequest, _("Value must be 0/1, or 'default'")
end

unless @system.available_content.map(&:content).any? { |content| content.label == content_params[:content_label] }
fail HttpErrors::BadRequest, _("Invalid content label: %s") % content_params[:content_label]
end
content_params
end

def find_system
@system = System.where(:uuid => params[:id]).first
if @system.nil?
Expand Down
4 changes: 4 additions & 0 deletions app/models/katello/system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,10 @@ def import_applicability(partial = true)
end
end

def available_content
self.products.flat_map(&:available_content)
end

private

def insert_errata_applicability(uuids)
Expand Down
17 changes: 17 additions & 0 deletions app/views/katello/api/v2/systems/_content.json.rabl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
object false

extends "katello/api/v2/common/metadata"

child @collection[:results] => :results do
child :product => :product do
attributes :id, :name
end

child :content => :content do
attributes :id, :name, :label
end

node :override do |pc|
pc.content_override(@system)
end
end
3 changes: 3 additions & 0 deletions app/views/katello/api/v2/systems/content_override.json.rabl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
object false

extends "katello/api/v2/systems/content"
3 changes: 3 additions & 0 deletions app/views/katello/api/v2/systems/product_content.json.rabl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
object false

extends "katello/api/v2/systems/content"
1 change: 1 addition & 0 deletions config/routes/api/v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ class ActionDispatch::Routing::Mapper
get :releases
put :refresh_subscriptions
put :content_override
get :product_content
end
api_resources :activation_keys, :only => [:index]
api_resources :host_collections, :only => [:index]
Expand Down
4 changes: 2 additions & 2 deletions lib/katello/permissions/content_host_permissions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
permission :view_content_hosts,
{
'katello/content_hosts' => [:auto_complete_search],
'katello/api/v2/systems' => [:index, :show, :errata, :package_profile,
'katello/api/v2/systems' => [:index, :show, :errata, :package_profile, :product_content,
:report, :pools, :releases, :available_host_collections, :events],
'katello/api/v2/system_errata' => [:show],
'katello/api/v2/systems_bulk_actions' => [:applicable_errata],
Expand All @@ -20,7 +20,7 @@
:resource_type => 'Katello::System'
permission :edit_content_hosts,
{
'katello/api/v2/systems' => [:update, :refresh_subscriptions],
'katello/api/v2/systems' => [:update, :refresh_subscriptions, :content_override],
'katello/api/v2/system_packages' => [:install, :upgrade, :upgrade_all, :remove],
'katello/api/v2/system_errata' => [:apply],
'katello/api/v2/systems_bulk_actions' => [:install_content, :update_content,
Expand Down
44 changes: 43 additions & 1 deletion test/controllers/api/v2/systems_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def setup
System.any_instance.stubs(:katello_agent_installed?).returns(true)
System.any_instance.stubs(:refresh_subscriptions).returns(true)
System.any_instance.stubs(:content_overrides).returns([])
System.any_instance.stubs(:products).returns([{:id => 1, :name => 'product', :available_content => []}])
System.any_instance.stubs(:products).returns([])
@fake_search_service = @controller.load_search_service(Support::SearchService::FakeSearchService.new)

Katello::Package.stubs(:package_count).returns(0)
Expand Down Expand Up @@ -201,5 +201,47 @@ def test_update
post :update, input
assert_response :success
end

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

assert_protected_action(:content_override, allowed_perms, denied_perms) do
put(:content_override, :id => @system.uuid, :content_label => 'some-content',
:value => 1)
end
end

def test_content_override
System.any_instance.stubs(:available_content).returns([Candlepin::ProductContent.new(:content => {:label => 'some-content'})])
Resources::Candlepin::Consumer.expects(:update_content_override).with(@system.uuid, 'some-content', 'enabled', 1)
put :content_override, :id => @system.uuid, :content_override => {:content_label => 'some-content', :value => 1}

assert_response :success
assert_template 'api/v2/systems/content_override'
end

def test_invalid_content_fails
System.any_instance.stubs(:available_content).returns([Candlepin::ProductContent.new(:content => {:label => 'some-content'})])
put :content_override, :id => @system.uuid, :content_override => {:content_label => 'wrong-content', :value => 1}

assert_response 400
end

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

assert_protected_action(:product_content, allowed_perms, denied_perms) do
get(:product_content, :id => @system.uuid)
end
end

def test_product_content
get :product_content, :id => @system.uuid

assert_response :success
assert_template 'api/v2/systems/product_content'
end
end
end

0 comments on commit 6827d36

Please sign in to comment.