Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion app/controllers/api/v2/hosts_bulk_actions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class HostsBulkActionsController < V2::BaseController
include Api::V2::BulkHostsExtension

before_action :find_deletable_hosts, :only => [:bulk_destroy]
before_action :find_editable_hosts, :only => [:build, :reassign_hostgroup]
before_action :find_editable_hosts, :only => [:build, :reassign_hostgroup, :change_owner]

def_param_group :bulk_host_ids do
param :organization_id, :number, :required => true, :desc => N_("ID of the organization")
Expand Down Expand Up @@ -78,6 +78,14 @@ def reassign_hostgroup
end
end

api :PUT, "/hosts/bulk/change_owner", N_("Change owner")
param_group :bulk_host_ids
param :owner_id, :number, :required => true, :desc => N_("ID of the owner to reassign the hosts to")
def change_owner
BulkHostsManager.new(hosts: @hosts).change_owner(params[:owner_id])
process_response(true, { :message => n_("Updated host: changed owner", "Updated hosts: changed owner", @hosts.count)})
end

protected

def action_permission
Expand Down
5 changes: 1 addition & 4 deletions app/controllers/hosts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -453,10 +453,7 @@ def update_multiple_owner
end

# update the hosts
@hosts.each do |host|
host.is_owned_by = id
host.save(:validate => false)
end
BulkHostsManager.new(hosts: @hosts).change_owner(id)

success _('Updated hosts: changed owner')
redirect_back_or_to helpers.current_hosts_path
Expand Down
7 changes: 7 additions & 0 deletions app/services/bulk_hosts_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,11 @@ def rebuild_configuration
end
all_fails
end

def change_owner(owner_id)
@hosts.each do |host|
host.is_owned_by = owner_id
host.save(:validate => false)
end
end
end
2 changes: 1 addition & 1 deletion config/initializers/f_foreman_permissions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@
:"api/v2/hosts" => [:update, :disassociate, :forget_status],
:"api/v2/interfaces" => [:create, :update, :destroy],
:"api/v2/compute_resources" => [:associate],
:"api/v2/hosts_bulk_actions" => [:build, :reassign_hostgroup],
:"api/v2/hosts_bulk_actions" => [:build, :reassign_hostgroup, :change_owner],
}
map.permission :destroy_hosts, {:hosts => [:destroy, :multiple_actions, :reset_multiple, :multiple_destroy, :submit_multiple_destroy],
:"api/v2/hosts" => [:destroy],
Expand Down
1 change: 1 addition & 0 deletions config/routes/api/v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
scope "(:apiv)", :module => :v2, :defaults => {:apiv => 'v2'}, :apiv => /v2/, :constraints => ApiConstraints.new(:version => 2, :default => true) do
match 'hosts/bulk', :to => 'hosts_bulk_actions#bulk_destroy', :via => [:delete]
match 'hosts/bulk/build', :to => 'hosts_bulk_actions#build', :via => [:put]
match 'hosts/bulk/change_owner', :to => 'hosts_bulk_actions#change_owner', :via => [:put]
Comment thread
chris1984 marked this conversation as resolved.
Outdated
match 'hosts/bulk/reassign_hostgroup', :to => 'hosts_bulk_actions#reassign_hostgroup', :via => [:put]

resources :architectures, :except => [:new, :edit] do
Expand Down
111 changes: 111 additions & 0 deletions test/controllers/api/v2/hosts_bulk_actions_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
require 'test_helper'

class Api::V2::HostsBulkActionsControllerTest < ActionController::TestCase
def setup
as_admin do
@organization = FactoryBot.create(:organization)
@location = FactoryBot.create(:location)
@host1 = FactoryBot.create(:host, :managed, :organization => @organization, :location => @location)
@host2 = FactoryBot.create(:host, :managed, :organization => @organization, :location => @location)
@host3 = FactoryBot.create(:host, :managed, :organization => @organization, :location => @location)
@user = FactoryBot.create(:user, :organizations => [@organization], :locations => [@location])
@usergroup = FactoryBot.create(:usergroup)
@host_ids = [@host1.id, @host2.id, @host3.id]
end
end

def valid_bulk_params(host_ids = @host_ids)
{
:organization_id => @organization.id,
:included => {
:ids => host_ids,
},
:excluded => {
:ids => [],
},
}
end

test "should change owner with user id" do
put :change_owner, params: valid_bulk_params.merge(:owner_id => @user.id_and_type)

assert_response :success
response = ActiveSupport::JSON.decode(@response.body)
assert_match(/Updated hosts: changed owner/, response['message'])

[@host1, @host2, @host3].each do |host|
host.reload
assert_equal @user.id_and_type, host.is_owned_by
end
end

test "should change owner with usergroup id" do
put :change_owner, params: valid_bulk_params.merge(:owner_id => @usergroup.id_and_type)

assert_response :success
response = ActiveSupport::JSON.decode(@response.body)
assert_match(/Updated hosts: changed owner/, response['message'])

[@host1, @host2, @host3].each do |host|
host.reload
assert_equal @usergroup.id_and_type, host.is_owned_by
end
end

test "should handle single host ownership change" do
single_host_params = valid_bulk_params([@host1.id])

put :change_owner, params: single_host_params.merge(:owner_id => @user.id_and_type)

assert_response :success
response = ActiveSupport::JSON.decode(@response.body)
assert_match(/Updated host: changed owner/, response['message'])

@host1.reload
assert_equal @user.id_and_type, @host1.is_owned_by
end

test "should require owner_id parameter" do
put :change_owner, params: valid_bulk_params

assert_response :success
end

test "should call BulkHostsManager with correct parameters" do
bulk_manager = mock('BulkHostsManager')
BulkHostsManager.expects(:new).with(hosts: anything).returns(bulk_manager)
bulk_manager.expects(:change_owner).with(@user.id_and_type)

put :change_owner, params: valid_bulk_params.merge(:owner_id => @user.id_and_type)

assert_response :success
end

context "with different host counts" do
test "should handle pluralization correctly for single host" do
single_host_params = valid_bulk_params([@host1.id])

put :change_owner, params: single_host_params.merge(:owner_id => @user.id_and_type)

assert_response :success
response = ActiveSupport::JSON.decode(@response.body)
# Should use singular form "host" not "hosts"
assert_match(/Updated host: changed owner/, response['message'])
end

test "should handle pluralization correctly for multiple hosts" do
put :change_owner, params: valid_bulk_params.merge(:owner_id => @user.id_and_type)

assert_response :success
response = ActiveSupport::JSON.decode(@response.body)
# Should use plural form "hosts"
assert_match(/Updated hosts: changed owner/, response['message'])
end
end

private

def set_session_user
{ :user => users(:admin).id }
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ const BulkBuildHostModal = ({
variant="link"
onClick={handleModalClose}
>
Cancel
{__('Cancel')}
</Button>,
];
return (
Expand Down
Loading
Loading