-
Notifications
You must be signed in to change notification settings - Fork 71
Fixes #39532 - All hosts - add action - bulk add capsule #616
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Lukshio
wants to merge
3
commits into
theforeman:master
Choose a base branch
from
Lukshio:addBulkAction
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
app/controllers/concerns/foreman_openscap/api/v2/hosts_bulk_actions_controller_extensions.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| module ForemanOpenscap | ||
| module Api | ||
| module V2 | ||
| module HostsBulkActionsControllerExtensions | ||
| extend ActiveSupport::Concern | ||
|
|
||
| included do | ||
| before_action :find_editable_hosts, only: [:change_openscap_proxy] | ||
| end | ||
|
|
||
| def change_openscap_proxy | ||
| openscap_proxy_id = params[:openscap_proxy_id] | ||
|
|
||
| if openscap_proxy_id.blank? | ||
| return render json: { | ||
| error: { | ||
| message: _("No OpenSCAP Proxy selected."), | ||
| }, | ||
| }, status: :unprocessable_entity | ||
| end | ||
|
|
||
| smart_proxy = begin | ||
| ::SmartProxy.find(openscap_proxy_id) | ||
| rescue ActiveRecord::RecordNotFound | ||
| nil | ||
| end | ||
|
|
||
| if smart_proxy.nil? | ||
| return render json: { | ||
| error: { | ||
| message: _("OpenSCAP proxy with id %s not found") % openscap_proxy_id, | ||
| }, | ||
| }, status: :unprocessable_entity | ||
| end | ||
|
|
||
| unless smart_proxy.has_feature?('Openscap') | ||
| return render json: { | ||
| error: { | ||
| message: _("The selected proxy does not have the OpenSCAP feature enabled."), | ||
| }, | ||
| }, status: :unprocessable_entity | ||
| end | ||
|
|
||
| failed_hosts = [] | ||
| @hosts.each do |host| | ||
| host.openscap_proxy = smart_proxy | ||
| failed_hosts << host unless host.save | ||
| end | ||
|
|
||
| if failed_hosts.empty? | ||
| process_response(true, { | ||
| message: _("OpenSCAP Proxy set to %s") % smart_proxy.name, | ||
| }) | ||
| else | ||
| render_error(:bulk_hosts_error, status: :unprocessable_entity, | ||
| locals: { | ||
| message: n_("Failed to assign OpenSCAP Proxy to %s host", | ||
| "Failed to assign OpenSCAP Proxy to %s hosts", | ||
| failed_hosts.count) % failed_hosts.count, | ||
|
Comment on lines
+57
to
+59
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could the message include also how many were successful/not done? (if I try 60, and see that 55 failed its unclear if the other 5 were skipped or successful) |
||
| failed_host_ids: failed_hosts.map(&:id), | ||
| }) | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def action_permission | ||
| case params[:action] | ||
| when 'change_openscap_proxy' | ||
| 'edit' | ||
| else | ||
| super | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
test/functional/api/v2/hosts_bulk_actions_controller_test.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| require 'test_plugin_helper' | ||
|
|
||
| class Api::V2::HostsBulkActionsControllerTest < ActionController::TestCase | ||
| def setup | ||
| as_admin do | ||
| @organization = FactoryBot.create(:organization) | ||
| @location = FactoryBot.create(:location) | ||
| @proxy = FactoryBot.create(:openscap_proxy, | ||
| :organizations => [@organization], | ||
| :locations => [@location]) | ||
| @host1 = FactoryBot.create(:host, :managed, | ||
| :organization => @organization, | ||
| :location => @location) | ||
| @host2 = FactoryBot.create(:host, :managed, | ||
| :organization => @organization, | ||
| :location => @location) | ||
| @host_ids = [@host1.id, @host2.id] | ||
| end | ||
| end | ||
|
|
||
| def valid_bulk_params(host_ids = @host_ids) | ||
| { | ||
| :organization_id => @organization.id, | ||
| :location_id => @location.id, | ||
| :included => { | ||
| :ids => host_ids, | ||
| }, | ||
| :excluded => { | ||
| :ids => [], | ||
| }, | ||
| } | ||
| end | ||
|
|
||
| test "should assign openscap proxy to selected hosts" do | ||
| put :change_openscap_proxy, | ||
| params: valid_bulk_params.merge(:openscap_proxy_id => @proxy.id), | ||
| session: set_session_user | ||
|
|
||
| assert_response :success | ||
| response = ActiveSupport::JSON.decode(@response.body) | ||
| assert_match(/OpenSCAP Proxy set to/, response['message']) | ||
| assert_includes response['message'], @proxy.name | ||
|
|
||
| [@host1, @host2].each do |host| | ||
| host.reload | ||
| assert_equal @proxy.id, host.openscap_proxy_id | ||
| end | ||
| end | ||
|
|
||
| test "should require openscap_proxy_id" do | ||
| put :change_openscap_proxy, | ||
| params: valid_bulk_params, | ||
| session: set_session_user | ||
|
|
||
| assert_response :unprocessable_entity | ||
| response = ActiveSupport::JSON.decode(@response.body) | ||
| assert_match(/No OpenSCAP Proxy selected/, response['error']['message']) | ||
| end | ||
|
|
||
| test "should return error when proxy is not found" do | ||
| put :change_openscap_proxy, | ||
| params: valid_bulk_params.merge(:openscap_proxy_id => 0), | ||
| session: set_session_user | ||
|
|
||
| assert_response :unprocessable_entity | ||
| response = ActiveSupport::JSON.decode(@response.body) | ||
| assert_match(/not found/, response['error']['message']) | ||
| end | ||
|
|
||
| test "should return error when proxy lacks Openscap feature" do | ||
| other_proxy = FactoryBot.create(:smart_proxy, | ||
| :organizations => [@organization], | ||
| :locations => [@location]) | ||
| openscap_feature = Feature.find_by(:name => 'Openscap') | ||
| other_proxy.features.delete(openscap_feature) if openscap_feature | ||
| refute other_proxy.reload.has_feature?('Openscap') | ||
|
|
||
| put :change_openscap_proxy, | ||
| params: valid_bulk_params.merge(:openscap_proxy_id => other_proxy.id), | ||
| session: set_session_user | ||
|
|
||
| assert_response :unprocessable_entity | ||
| response = ActiveSupport::JSON.decode(@response.body) | ||
| assert_match(/OpenSCAP feature/, response['error']['message']) | ||
| end | ||
|
|
||
| test "should assign openscap proxy for a single host" do | ||
| put :change_openscap_proxy, | ||
| params: valid_bulk_params([@host1.id]).merge(:openscap_proxy_id => @proxy.id), | ||
| session: set_session_user | ||
|
|
||
| assert_response :success | ||
| @host1.reload | ||
| assert_equal @proxy.id, @host1.openscap_proxy_id | ||
| @host2.reload | ||
| assert_nil @host2.openscap_proxy_id | ||
| end | ||
| end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Success message doesn't include host count (inconsistent with core bulk actions)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried changing org and location but it does not show count either