Skip to content

Commit

Permalink
Check if record supports retirement task only for VM or Template records
Browse files Browse the repository at this point in the history
Made changes to code to not check for retirement task eligibility for Service & Orchestration template records. This check should only be for VmOrTemplate records. Added model methods to check if record type supports retirement. Having service record with a same id as a MiqTemple record in database was causing an issue while trying to retire the service and incorrectly going thru the code path trying to render a flash message that "Retire does not apply to selected xxxx"

https://bugzilla.redhat.com/show_bug.cgi?id=1344678
https://bugzilla.redhat.com/show_bug.cgi?id=1349989
  • Loading branch information
h-kataria committed Jul 8, 2016
1 parent c78c373 commit f4cb6eb
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 3 deletions.
7 changes: 4 additions & 3 deletions app/controllers/application_controller/ci_processing.rb
Expand Up @@ -192,7 +192,7 @@ def ownership_update
def retirevms
assert_privileges(params[:pressed])
vms = find_checked_items
if VmOrTemplate.includes_template?(vms.map(&:to_i).uniq)
if !%w(orchestration_template service).include?(request.parameters["controller"]) && !VmOrTemplate.supports_operation?("retirement", vms)
add_flash(_("Set Retirement Date does not apply to selected %{model}") %
{:model => ui_lookup(:table => "miq_template")}, :error)
render_flash_and_scroll
Expand Down Expand Up @@ -1619,7 +1619,9 @@ def vm_button_operation(method, display_name, partial_after_single_selection = n
request.parameters["controller"]) # showing a list

vms = find_checked_items
if method == 'retire_now' && VmOrTemplate.includes_template?(vms)
if method == 'retire_now' &&
!%w(orchestration_template service).include?(request.parameters["controller"]) &&
!VmOrTemplate.supports_operation?("retirement", vms)
add_flash(_("Retire does not apply to selected %{model}") %
{:model => ui_lookup(:table => "miq_template")}, :error)
render_flash_and_scroll
Expand Down Expand Up @@ -1666,7 +1668,6 @@ def vm_button_operation(method, display_name, partial_after_single_selection = n
end
end
end

vms.count
end

Expand Down
4 changes: 4 additions & 0 deletions app/models/miq_template.rb
Expand Up @@ -30,4 +30,8 @@ def self.eligible_for_provisioning
end

def active?; false; end

def supports_retirement?
false
end
end
4 changes: 4 additions & 0 deletions app/models/vm.rb
Expand Up @@ -88,4 +88,8 @@ def running_processes
end
pl
end

def supports_retirement?
true
end
end
4 changes: 4 additions & 0 deletions app/models/vm_or_template.rb
Expand Up @@ -1940,5 +1940,9 @@ def self.arel_coalesce(values)
Arel::Nodes::NamedFunction.new('COALESCE', values)
end

def self.supports_operation?(operation, ids)
VmOrTemplate.where(:id => ids).all? { |v| v.public_send("supports_#{operation}?") }
end

include DeprecatedCpuMethodsMixin
end
56 changes: 56 additions & 0 deletions spec/controllers/application_controller/ci_processing_spec.rb
Expand Up @@ -334,3 +334,59 @@
end
end
end

describe ServiceController do
context "#vm_button_operation" do
before do
_guid, @miq_server, @zone = EvmSpecHelper.remote_guid_miq_server_zone
allow(MiqServer).to receive(:my_zone).and_return("default")
controller.instance_variable_set(:@lastaction, "show_list")
end

it "should render flash message when trying to retire a template" do
controller.request.parameters["controller"] = "vm_or_template"
vm = FactoryGirl.create(:vm_vmware,
:ext_management_system => FactoryGirl.create(:ems_openstack_infra),
:storage => FactoryGirl.create(:storage)
)
template = FactoryGirl.create(:template,
:ext_management_system => FactoryGirl.create(:ems_openstack_infra),
:storage => FactoryGirl.create(:storage)
)
controller.instance_variable_set(:@_params, :miq_grid_checks => "#{vm.id}, #{template.id}")
expect(controller).to receive(:render_flash_and_scroll)
controller.send(:vm_button_operation, 'retire_now', "Retirement")
expect(response.status).to eq(200)
end

it "should continue to retire a vm" do
controller.request.parameters["controller"] = "vm_or_template"
vm = FactoryGirl.create(:vm_vmware,
:ext_management_system => FactoryGirl.create(:ems_openstack_infra),
:storage => FactoryGirl.create(:storage)
)

controller.instance_variable_set(:@_params, :miq_grid_checks => "#{vm.id}")
expect(controller).to receive(:show_list)
controller.send(:vm_button_operation, 'retire_now', "Retirement")
expect(response.status).to eq(200)
expect(assigns(:flash_array).first[:message]).to include("Retirement initiated for 1 VM and Instance from the CFME Database")
end

it "should continue to retire a service and does not render flash message 'xxx does not apply xxx' " do
controller.request.parameters["controller"] = "service"
service = FactoryGirl.create(:service)
template = FactoryGirl.create(:template,
:ext_management_system => FactoryGirl.create(:ems_openstack_infra),
:storage => FactoryGirl.create(:storage)
)
service.update_attribute(:id, template.id)
service.reload
controller.instance_variable_set(:@_params, :miq_grid_checks => "#{service.id}")
expect(controller).to receive(:show_list)
controller.send(:vm_button_operation, 'retire_now', "Retirement")
expect(response.status).to eq(200)
expect(assigns(:flash_array).first[:message]).to include("Retirement initiated for 1 Service from the CFME Database")
end
end
end

0 comments on commit f4cb6eb

Please sign in to comment.