Skip to content

Commit

Permalink
Merge pull request #890 from lgalis/fix_find_checked_ids_with_rbac_fo…
Browse files Browse the repository at this point in the history
…r_retirement_call

Fix retirevms and vm_button_operations to account for  it being called for objects other than VMOrTemplate
  • Loading branch information
martinpovolny committed Apr 5, 2017
2 parents 7c372e5 + 0ab8562 commit 5523cfb
Showing 1 changed file with 23 additions and 25 deletions.
48 changes: 23 additions & 25 deletions app/controllers/application_controller/ci_processing.rb
Expand Up @@ -212,12 +212,13 @@ def ownership_update
end
end

# Retire 1 or more VMs
# Retire 1 or more items (vms, stacks, services)
def retirevms
assert_privileges(params[:pressed])
vms = find_checked_ids_with_rbac(VmOrTemplate)
klass = get_class_from_controller_param(params[:controller])
selected_items = find_checked_ids_with_rbac(klass)
if !%w(orchestration_stack service).include?(request.parameters["controller"]) && !%w(orchestration_stacks).include?(params[:display]) &&
VmOrTemplate.find(vms).any? { |vm| !vm.supports_retire? }
VmOrTemplate.find(selected_items).any? { |vm| !vm.supports_retire? }
add_flash(_("Set Retirement Date does not apply to selected %{model}") %
{:model => ui_lookup(:table => "miq_template")}, :error)
javascript_flash(:scroll_top => true)
Expand All @@ -234,18 +235,16 @@ def retirevms
rec_cls = "orchestration_stack"
bc_msg = _("Retire Orchestration Stack")
end
if vms.blank?
if selected_items.blank?
session[:retire_items] = [params[:id]]
else
if vms.length < 1
elsif selected_items.empty?
add_flash(_("At least one %{model} must be selected for tagging") %
{:model => ui_lookup(:model => "Vm")}, :error)
@refresh_div = "flash_msg_div"
@refresh_partial = "layouts/flash_msg"
return
else
session[:retire_items] = vms # Set the array of retire items
end
else
session[:retire_items] = selected_items # Set the array of retire items
end
session[:assigned_filters] = assigned_filters
if @explorer
Expand Down Expand Up @@ -1821,53 +1820,52 @@ def reconfigure_calculations(mbsize)
return humansize.to_s, fmt
end

# Common VM button handler routines
# Common item button handler routines
def vm_button_operation(method, display_name, partial_after_single_selection = nil)
vms = []

# Either a list or coming from a different controller (eg from host screen, go to its vms)
selected_items = []
klass = get_rec_cls
# Either a list or coming from a different controller (eg from host screen, go to its selected_items)
if @lastaction == "show_list" ||
!%w(orchestration_stack service vm_cloud vm_infra vm miq_template vm_or_template).include?(
request.parameters["controller"]) # showing a list

# FIXME retrieving vms from DB two times
vms = find_checked_ids_with_rbac(VmOrTemplate)
selected_items = find_checked_ids_with_rbac(klass)
if method == 'retire_now' &&
!%w(orchestration_stack service).include?(request.parameters["controller"]) &&
VmOrTemplate.find(vms).any? { |vm| !vm.supports_retire? }
VmOrTemplate.find(selected_items).any? { |vm| !vm.supports_retire? }
add_flash(_("Retire does not apply to selected %{model}") %
{:model => ui_lookup(:table => "miq_template")}, :error)
javascript_flash(:scroll_top => true)
return
end

if method == 'scan' && !VmOrTemplate.batch_operation_supported?('smartstate_analysis', vms)
if method == 'scan' && !VmOrTemplate.batch_operation_supported?('smartstate_analysis', selected_items)
render_flash_not_applicable_to_model('Smartstate Analysis', ui_lookup(:tables => "vm_or_template"))
return
end

if vms.empty?
if selected_items.empty?
add_flash(_("No %{model} were selected for %{task}") % {:model => ui_lookup(:tables => request.parameters["controller"]), :task => display_name}, :error)
else
process_objects(vms, method)
process_objects(selected_items, method)
end

if @lastaction == "show_list" # In vm controller, refresh show_list, else let the other controller handle it
if @lastaction == "show_list" # In the controller, refresh show_list, else let the other controller handle it
show_list unless @explorer
@refresh_partial = "layouts/gtl"
end

else # showing 1 vm
klass = get_rec_cls
else # showing 1 item
if params[:id].nil? || klass.find_by_id(params[:id]).nil?
add_flash(_("%{record} no longer exists") %
{:record => ui_lookup(:table => request.parameters["controller"])}, :error)
show_list unless @explorer
@refresh_partial = "layouts/gtl"
else

vms.push(find_id_with_rbac(klass, params[:id]))
process_objects(vms, method) unless vms.empty?
selected_items.push(find_id_with_rbac(klass, params[:id]))
process_objects(selected_items, method) unless selected_items.empty?

# TODO: tells callers to go back to show_list because this VM may be gone
# Should be refactored into calling show_list right here
Expand All @@ -1882,7 +1880,7 @@ def vm_button_operation(method, display_name, partial_after_single_selection = n
end
end
end
vms.count
selected_items.count
end

def process_cloud_object_storage_buttons(pressed)
Expand Down Expand Up @@ -2071,7 +2069,7 @@ def scanvms
alias_method :vm_scan, :scanvms
alias_method :miq_template_scan, :scanvms

# Immediately retire VMs
# Immediately retire items
def retirevms_now
assert_privileges(params[:pressed])
vm_button_operation('retire_now', 'retire')
Expand Down

0 comments on commit 5523cfb

Please sign in to comment.