From 2168b96c9b227159a7f29e65ff4cd3742fc18955 Mon Sep 17 00:00:00 2001 From: d-m-u Date: Thu, 18 Oct 2018 17:08:36 -0400 Subject: [PATCH] Add object retirement_requester --- app/models/mixins/retirement_mixin.rb | 11 +++++++++++ .../retirement_management_spec.rb | 1 + spec/models/service/retirement_management_spec.rb | 13 +++++++------ spec/models/vm/retirement_management_spec.rb | 10 ++++++---- 4 files changed, 25 insertions(+), 10 deletions(-) diff --git a/app/models/mixins/retirement_mixin.rb b/app/models/mixins/retirement_mixin.rb index 4ebdfd8d088..bf3ec5ffcbd 100644 --- a/app/models/mixins/retirement_mixin.rb +++ b/app/models/mixins/retirement_mixin.rb @@ -13,6 +13,7 @@ module ClassMethods def make_retire_request(*src_ids, requester) klass = (name.demodulize + "RetireRequest").constantize options = {:src_ids => src_ids.presence, :__request_type__ => klass.request_types.first} + set_retirement_requester(name, options[:src_ids], requester) klass.make_request(nil, options, requester, true) end @@ -26,6 +27,16 @@ def retire(ids, options = {}) q_options.merge!(:user_id => user.id, :group_id => user.current_group.id, :tenant_id => user.current_tenant.id) if user MiqQueue.put(q_options) end + + def set_retirement_requester(klass, obj_ids, requester) + existing_objects = klass.where(:id => obj_ids) + updated_count = existing_objects.update_all(:retirement_requester => requester.userid) + if updated_count != obj_ids.count + _log.info("Retirement requester for #{klass}.pluralize #{(obj_ids - existing_objects.pluck(:id))} not set because objects not found") + else + _log.info("Retirement requester for #{klass}.pluralize #{obj_ids} being set to #{requester.userid}") + end + end end def retirement_warn=(days) diff --git a/spec/models/orchestration_stack/retirement_management_spec.rb b/spec/models/orchestration_stack/retirement_management_spec.rb index be71e50205b..e1b0d263a6b 100644 --- a/spec/models/orchestration_stack/retirement_management_spec.rb +++ b/spec/models/orchestration_stack/retirement_management_spec.rb @@ -15,6 +15,7 @@ @stack.retirement_check @stack.reload expect(@stack.retirement_last_warn).not_to be_nil + expect(@stack.retirement_requester).to eq(user.userid) end it "#start_retirement" do diff --git a/spec/models/service/retirement_management_spec.rb b/spec/models/service/retirement_management_spec.rb index 8cefda8f357..3d35dfacc0b 100644 --- a/spec/models/service/retirement_management_spec.rb +++ b/spec/models/service/retirement_management_spec.rb @@ -1,6 +1,7 @@ describe "Service Retirement Management" do let!(:user) { FactoryGirl.create(:user_miq_request_approver, :userid => 'admin') } let(:service_without_owner) { FactoryGirl.create(:service) } + let(:service3) { FactoryGirl.create(:service) } before do @server = EvmSpecHelper.local_miq_server @service = FactoryGirl.create(:service, :evm_owner_id => user.id) @@ -16,6 +17,7 @@ @service.retirement_check @service.reload expect(@service.retirement_last_warn).not_to be_nil + expect(@service.retirement_requester).to eq(user.userid) end end end @@ -28,6 +30,7 @@ service_without_owner.retirement_check service_without_owner.reload expect(service_without_owner.retirement_last_warn).not_to be_nil + expect(service_without_owner.retirement_requester).to eq(user.userid) expect(MiqRequest.first.userid).to eq("admin") end end @@ -96,15 +99,13 @@ end it "with one src_id" do - User.current_user = user - expect(ServiceRetireRequest).to receive(:make_request).with(nil, {:src_ids => ['yabadabadoo'], :__request_type__ => "service_retire"}, User.current_user, true) - @service.class.to_s.demodulize.constantize.make_retire_request('yabadabadoo', User.current_user) + expect(ServiceRetireRequest).to receive(:make_request).with(nil, {:src_ids => [service3.id], :__request_type__ => "service_retire"}, user, true) + @service.class.to_s.demodulize.constantize.make_retire_request(service3.id, user) end it "with many src_ids" do - User.current_user = user - expect(ServiceRetireRequest).to receive(:make_request).with(nil, {:src_ids => [1, 2, 3], :__request_type__ => "service_retire"}, User.current_user, true) - @service.class.to_s.demodulize.constantize.make_retire_request(1, 2, 3, User.current_user) + expect(ServiceRetireRequest).to receive(:make_request).with(nil, {:src_ids => [@service.id, service3.id, service_without_owner.id], :__request_type__ => "service_retire"}, user, true) + @service.class.to_s.demodulize.constantize.make_retire_request(@service.id, service3.id, service_without_owner.id, user) end it "#retire date" do diff --git a/spec/models/vm/retirement_management_spec.rb b/spec/models/vm/retirement_management_spec.rb index 27587740326..e771dacc1fa 100644 --- a/spec/models/vm/retirement_management_spec.rb +++ b/spec/models/vm/retirement_management_spec.rb @@ -1,6 +1,7 @@ describe "VM Retirement Management" do let!(:user) { FactoryGirl.create(:user_miq_request_approver) } let(:region) { FactoryGirl.create(:miq_region, :region => ApplicationRecord.my_region_number) } + let(:vm2) { FactoryGirl.create(:vm) } before do @zone = EvmSpecHelper.local_miq_server.zone @@ -15,6 +16,7 @@ @vm.retirement_check @vm.reload expect(@vm.retirement_last_warn).not_to be_nil + expect(@vm.retirement_requester).to eq(user.userid) end it "#start_retirement" do @@ -80,13 +82,13 @@ describe "retire request" do it "with one src_id" do - expect(VmRetireRequest).to receive(:make_request).with(nil, {:src_ids => ['yabadabadoo'], :__request_type__ => "vm_retire"}, User.current_user, true) - @vm.class.to_s.demodulize.constantize.make_retire_request('yabadabadoo', User.current_user) + expect(VmRetireRequest).to receive(:make_request).with(nil, {:src_ids => [@vm.id], :__request_type__ => "vm_retire"}, user, true) + Vm.make_retire_request(@vm.id, user) end it "with many src_ids" do - expect(VmRetireRequest).to receive(:make_request).with(nil, {:src_ids => [1, 2, 3], :__request_type__ => "vm_retire"}, User.current_user, true) - @vm.class.to_s.demodulize.constantize.make_retire_request(1, 2, 3, User.current_user) + expect(VmRetireRequest).to receive(:make_request).with(nil, {:src_ids => [@vm.id, vm2.id], :__request_type__ => "vm_retire"}, user, true) + Vm.make_retire_request(@vm.id, vm2.id, user) end end