Skip to content

Commit

Permalink
Add object retirement_requester
Browse files Browse the repository at this point in the history
  • Loading branch information
d-m-u committed Oct 22, 2018
1 parent 6fa29c2 commit 2168b96
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 10 deletions.
11 changes: 11 additions & 0 deletions app/models/mixins/retirement_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 7 additions & 6 deletions spec/models/service/retirement_management_spec.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions spec/models/vm/retirement_management_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 2168b96

Please sign in to comment.