Skip to content

Commit

Permalink
Refactor resource_pool controller, make Edit VM thru Resource Pool work
Browse files Browse the repository at this point in the history
  • Loading branch information
Hilda Stastna committed Jan 21, 2020
1 parent 4b1bc48 commit b419bd2
Show file tree
Hide file tree
Showing 3 changed files with 257 additions and 53 deletions.
7 changes: 4 additions & 3 deletions app/controllers/mixins/generic_button_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def handle_vm_buttons(pressed)
return [:finished, pfx] if vm_button_redirected?(pfx, pressed)

unless ["#{pfx}_edit", "#{pfx}_miq_request_new", "#{pfx}_clone",
"#{pfx}_migrate", "#{pfx}_publish"].include?(pressed)
"#{pfx}_migrate", "#{pfx}_publish", 'vm_rename'].include?(pressed)
@refresh_div = "main_div"
@refresh_partial = "layouts/gtl"
show # Handle VMs buttons
Expand Down Expand Up @@ -79,8 +79,9 @@ def button
return
end

if params[:pressed].ends_with?("_edit") || ["#{pfx}_miq_request_new", "#{pfx}_clone",
"#{pfx}_migrate", "#{pfx}_publish"].include?(params[:pressed])
if params[:pressed].ends_with?("_edit") ||
["#{pfx}_miq_request_new", "#{pfx}_clone", "#{pfx}_migrate", "#{pfx}_publish"].include?(params[:pressed]) ||
params[:pressed] == 'vm_rename' && @flash_array.nil?
render_or_redirect_partial(pfx)
elsif @refresh_div == "main_div" && @lastaction == "show_list"
replace_gtl_main_div
Expand Down
51 changes: 12 additions & 39 deletions app/controllers/resource_pool_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class ResourcePoolController < ApplicationController
after_action :cleanup_action
after_action :set_session_data

include Mixins::GenericButtonMixin
include Mixins::GenericListMixin
include Mixins::GenericSessionMixin
include Mixins::GenericShowMixin
Expand All @@ -17,48 +18,20 @@ def self.display_methods
def button
@edit = session[:edit] # Restore @edit for adv search box
params[:display] = @display if %w[all_vms vms resource_pools].include?(@display) # Were we displaying sub-items
if %w[all_vms vms resource_pools].include?(@display) # Need to check, since RPs contain RPs

if params[:pressed].starts_with?("vm_", # Handle buttons from sub-items screen
"miq_template_",
"guest_")

pfx = pfx_for_vm_button_pressed(params[:pressed])
process_vm_buttons(pfx)

return if ["#{pfx}_policy_sim", "#{pfx}_compare", "#{pfx}_tag", "#{pfx}_protect",
"#{pfx}_retire", "#{pfx}_right_size", "#{pfx}_ownership",
"#{pfx}_reconfigure"].include?(params[:pressed]) &&
@flash_array.nil? # Some other screen is showing, so return

unless ["#{pfx}_edit", "#{pfx}_miq_request_new", "#{pfx}_clone",
"#{pfx}_migrate", "#{pfx}_publish", 'vm_rename'].include?(params[:pressed])
@refresh_div = "main_div"
@refresh_partial = "layouts/gtl"
show
end
@refresh_div = 'main_div' unless @display # Default div for button.rjs to refresh
case params[:pressed]
when 'resource_pool_delete'
deleteresourcepools
if @refresh_div == 'main_div' && @lastaction == 'show_list'
replace_gtl_main_div
else
render_flash unless performed?
end
when 'resource_pool_protect'
assign_policies(ResourcePool)
else
@refresh_div = "main_div" # Default div for button.rjs to refresh
tag(ResourcePool) if params[:pressed] == "resource_pool_tag"
deleteresourcepools if params[:pressed] == "resource_pool_delete"
assign_policies(ResourcePool) if params[:pressed] == "resource_pool_protect"
end

return if %w[resource_pool_tag resource_pool_protect].include?(params[:pressed]) && @flash_array.nil? # Tag screen showing, so return

check_if_button_is_implemented

if single_delete_test
single_delete_redirect
elsif ["#{pfx}_miq_request_new", "#{pfx}_migrate", "#{pfx}_clone",
"#{pfx}_migrate", "#{pfx}_publish"].include?(params[:pressed]) ||
params[:pressed] == 'vm_rename' && @flash_array.nil?
render_or_redirect_partial(pfx)
elsif @refresh_div == "main_div" && @lastaction == "show_list"
replace_gtl_main_div
else
render_flash unless performed?
super
end
end

Expand Down
252 changes: 241 additions & 11 deletions spec/controllers/resource_pool_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,6 @@
expect(controller.send(:flash_errors?)).not_to be_truthy
end

it "when VM Retire is pressed" do
controller.params = {:pressed => "vm_retire"}
expect(controller).to receive(:retirevms).once
controller.button
expect(controller.send(:flash_errors?)).not_to be_truthy
end

it "when VM Manage Policies is pressed" do
controller.params = {:pressed => "vm_protect"}
expect(controller).to receive(:assign_policies).with(VmOrTemplate)
Expand Down Expand Up @@ -98,19 +91,256 @@
expect(controller.instance_variable_get(:@flash_array)).to eq([{:message => 'Check Compliance initiated for 1 VM and Instance from the ManageIQ Database', :level => :success}])
end
end

it 'calls check_compliance_vms' do
expect(controller).to receive(:check_compliance_vms)
controller.send(:button)
end
end

context 'reconfigure VMs' do
before do
controller.instance_variable_set(:@display, 'vms')
controller.params = {:pressed => 'vm_reconfigure'}
end
before { controller.params = {:pressed => 'vm_reconfigure'} }

it 'calls vm_reconfigure' do
expect(controller).to receive(:vm_reconfigure)
controller.send(:button)
end
end

context 'Extract Running Processes for selected VMs' do
let(:vm) { FactoryBot.create(:vm_vmware) }

before do
allow(controller).to receive(:assert_privileges)
allow(controller).to receive(:show)
allow(controller).to receive(:performed?).and_return(true)
allow(controller).to receive(:render)
controller.params = {:pressed => 'vm_collect_running_processes', :miq_grid_checks => vm.id.to_s}
end

it 'calls getprocessesvms' do
expect(controller).to receive(:getprocessesvms)
controller.send(:button)
end

it 'sets error flash message' do
controller.send(:button)
expect(controller.instance_variable_get(:@flash_array)).to eq([{:message => 'Collect Running Processes action does not apply to selected items', :level => :error}])
end
end

context 'Compare selected VMs' do
before { controller.params = {:pressed => 'vm_compare'} }

it 'calls comparemiq' do
expect(controller).to receive(:comparemiq)
controller.send(:button)
end
end

context 'Edit selected VM' do
before do
allow(controller).to receive(:render_or_redirect_partial)
controller.params = {:pressed => 'vm_edit'}
end

it 'calls edit_record' do
expect(controller).to receive(:edit_record)
controller.send(:button)
end
end

context 'Rename selected VM' do
before do
allow(controller).to receive(:performed?).and_return(true)
controller.params = {:pressed => 'vm_rename'}
end

it 'calls vm_rename' do
expect(controller).to receive(:vm_rename)
controller.send(:button)
end
end

context 'Set ownership for selected VMs' do
before { controller.params = {:pressed => 'vm_ownership'} }

it 'calls set_ownership' do
expect(controller).to receive(:set_ownership)
controller.send(:button)
end
end

context 'Policy Simulation for selected VMs' do
before { controller.params = {:pressed => 'vm_policy_sim'} }

it 'calls polsimvms' do
expect(controller).to receive(:polsimvms)
controller.send(:button)
end
end

context 'Provision VMs' do
before do
allow(controller).to receive(:render_or_redirect_partial)
controller.params = {:pressed => 'vm_miq_request_new'}
end

it 'calls prov_redirect' do
expect(controller).to receive(:prov_redirect).with(no_args)
controller.send(:button)
end
end

context 'Clone VMs' do
before do
allow(controller).to receive(:render_or_redirect_partial)
controller.params = {:pressed => 'vm_clone'}
end

it 'calls prov_redirect with appropriate argument' do
expect(controller).to receive(:prov_redirect).with('clone')
controller.send(:button)
end
end

context 'Publish VM to a Template' do
before do
allow(controller).to receive(:render_or_redirect_partial)
controller.params = {:pressed => 'vm_publish'}
end

it 'calls prov_redirect with appropriate argument' do
expect(controller).to receive(:prov_redirect).with('publish')
controller.send(:button)
end
end

context 'Retire VMs' do
before do
allow(controller).to receive(:performed?).and_return(true)
allow(controller).to receive(:show)
controller.params = {:pressed => 'vm_retire_now'}
end

it 'calls retirevms_now' do
expect(controller).to receive(:retirevms_now)
controller.send(:button)
end
end

context 'Shutdown Guest of a VM' do
before do
allow(controller).to receive(:performed?).and_return(true)
allow(controller).to receive(:show)
controller.params = {:pressed => 'vm_guest_shutdown'}
end

it 'calls guestshutdown' do
expect(controller).to receive(:guestshutdown)
controller.send(:button)
end
end

context 'Restart Guest of a VM' do
before do
allow(controller).to receive(:performed?).and_return(true)
allow(controller).to receive(:show)
controller.params = {:pressed => 'vm_guest_restart'}
end

it 'calls guestshutdown' do
expect(controller).to receive(:guestreboot)
controller.send(:button)
end
end

%w[delete refresh reset retire scan start stop suspend].each do |action|
context "#{action} for selected VMs displayed in a nested list" do
before { controller.params = {:pressed => "vm_#{action}"} }

it "calls #{action + 'vms'} method" do
allow(controller).to receive(:show)
allow(controller).to receive(:performed?).and_return(true)
expect(controller).to receive((action + 'vms').to_sym)
controller.send(:button)
expect(controller.send(:flash_errors?)).not_to be_truthy
end
end
end

context 'deleting Resource Pool' do
before do
allow(controller).to receive(:performed?).and_return(true)
controller.instance_variable_set(:@lastaction, 'show_list')
controller.instance_variable_set(:@display, nil)
controller.params = {:pressed => 'resource_pool_delete'}
end

it 'calls deleteresourcepools and replace_gtl_main_div' do
expect(controller).to receive(:deleteresourcepools)
expect(controller).to receive(:replace_gtl_main_div)
controller.send(:button)
end

context 'default div to refresh' do
before do
allow(controller).to receive(:deleteresourcepools)
allow(controller).to receive(:replace_gtl_main_div)
end

it 'sets @refresh_div to main div' do
controller.send(:button)
expect(controller.instance_variable_get(:@refresh_div)).to eq('main_div')
end
end

context 'nested list of Resource Pools' do
before do
allow(controller).to receive(:performed?).and_return(false)
controller.instance_variable_set(:@display, 'resource_pools')
end

it 'calls deleteresourcepools and render_flash' do
expect(controller).to receive(:deleteresourcepools)
expect(controller).to receive(:render_flash)
controller.send(:button)
end
end
end

context 'managing policies of Resource Pools' do
before do
controller.instance_variable_set(:@display, nil)
controller.params = {:pressed => 'resource_pool_protect'}
end

it 'calls assign_policies' do
expect(controller).to receive(:assign_policies).with(ResourcePool)
controller.send(:button)
end

context 'default div to refresh' do
before { allow(controller).to receive(:assign_policies) }

it 'sets @refresh_div to main div' do
controller.send(:button)
expect(controller.instance_variable_get(:@refresh_div)).to eq('main_div')
end
end
end

context 'tagging Resource Pool' do
before do
controller.instance_variable_set(:@display, nil)
controller.params = {:pressed => 'resource_pool_tag'}
end

it 'calls tag method' do
expect(controller).to receive(:tag).with(ResourcePool)
controller.send(:button)
end
end
end

describe "#show" do
Expand Down

0 comments on commit b419bd2

Please sign in to comment.