Skip to content

Commit

Permalink
Merge pull request #9272 from h-kataria/service_retirement_fix
Browse files Browse the repository at this point in the history
Check if record supports retirement task only for VM or Template records
(cherry picked from commit 75d9a62)
  • Loading branch information
Dan Clarizio authored and chessbyte committed Jul 12, 2016
1 parent 7fa56db commit 6ff4157
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 4 deletions.
8 changes: 5 additions & 3 deletions app/controllers/application_controller/ci_processing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ 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.find(vms).any? { |vm| !vm.supports_retirement? }
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 @@ -1616,7 +1617,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.find(vms).any? { |vm| !vm.supports_retirement? }
add_flash(_("Retire does not apply to selected %{model}") %
{:model => ui_lookup(:table => "miq_template")}, :error)
render_flash_and_scroll
Expand Down Expand Up @@ -1663,7 +1666,6 @@ def vm_button_operation(method, display_name, partial_after_single_selection = n
end
end
end

vms.count
end

Expand Down
2 changes: 2 additions & 0 deletions app/models/vm/operations.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
module Vm::Operations
extend ActiveSupport::Concern

include_concern 'Guest'
include_concern 'Power'
include_concern 'Lifecycle'
Expand Down
10 changes: 10 additions & 0 deletions app/models/vm/operations/lifecycle.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
module Vm::Operations::Lifecycle
extend ActiveSupport::Concern

included do
supports :retirement do
if orphaned? || archived?
unsupported_reason_add :retirement, "VM orphaned or archived already"
end
end
end

def validate_clone
{:available => self.cloneable? && !(self.blank? || self.orphaned? || self.archived?), :message => nil}
end
Expand Down
2 changes: 2 additions & 0 deletions app/models/vm_or_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class VmOrTemplate < ApplicationRecord

include AvailabilityMixin

supports_not :retirement

has_many :ems_custom_attributes, -> { where(:source => 'VC') }, :as => :resource, :dependent => :destroy,
:class_name => "CustomAttribute"
has_many :counterparts, :as => :counterpart, :class_name => "ConfiguredSystem", :dependent => :nullify
Expand Down
58 changes: 58 additions & 0 deletions spec/controllers/application_controller/ci_processing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -329,3 +329,61 @@
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.to_s)
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.to_s)
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
5 changes: 4 additions & 1 deletion spec/controllers/ems_common_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,11 @@ def set_up_controller_for_show_method(controller, ems_id)

it "when Retire Button is pressed for a Cloud provider Instance" do
allow(controller).to receive(:role_allows).and_return(true)
vm = FactoryGirl.create(:vm_vmware)
ems = FactoryGirl.create("ems_vmware")
vm = FactoryGirl.create(:vm_vmware,
:ext_management_system => ems,
:storage => FactoryGirl.create(:storage)
)
post :button, :params => { :pressed => "instance_retire", "check_#{vm.id}" => "1", :format => :js, :id => ems.id, :display => 'instances' }
expect(response.status).to eq 200
expect(response.body).to include('vm/retire')
Expand Down

0 comments on commit 6ff4157

Please sign in to comment.