From 98950cc9772c1d62ae9833b2b06240420b97ac84 Mon Sep 17 00:00:00 2001 From: d-m-u Date: Thu, 28 Feb 2019 13:26:19 -0500 Subject: [PATCH] Fix the dynamic task naming generation for subclasses for subclasses like ServiceOrchestration we need the base class name for Vms we just need the class name --- app/models/service_retire_task.rb | 12 ++++-- spec/models/service_retire_task_spec.rb | 56 ++++++++++++++++++------- 2 files changed, 49 insertions(+), 19 deletions(-) diff --git a/app/models/service_retire_task.rb b/app/models/service_retire_task.rb index 09b9e28f3c0..4699ff5f40c 100644 --- a/app/models/service_retire_task.rb +++ b/app/models/service_retire_task.rb @@ -51,18 +51,24 @@ def create_retire_subtasks(parent_service, parent_task) end def create_task(svc_rsc, parent_service, nh, parent_task) - resource_type = svc_rsc.resource.type.presence || "Service" - (resource_type.demodulize + "RetireTask").constantize.new(nh).tap do |task| + task_type = retire_task_type(svc_rsc.resource.class) + task_type.new(nh).tap do |task| task.options.merge!( :src_ids => [svc_rsc.resource.id], :service_resource_id => svc_rsc.id, :parent_service_id => parent_service.id, :parent_task_id => parent_task.id, ) - task.request_type = resource_type.demodulize.underscore.downcase + "_retire" + task.request_type = task_type.name.underscore[0..-6] task.source = svc_rsc.resource parent_task.miq_request_tasks << task task.save! end end + + private + + def retire_task_type(resource_type) + (resource_type.base_class.name + "RetireTask").safe_constantize || (resource_type.name.demodulize + "RetireTask").safe_constantize + end end diff --git a/spec/models/service_retire_task_spec.rb b/spec/models/service_retire_task_spec.rb index cd2f2be93cc..5958c68295c 100644 --- a/spec/models/service_retire_task_spec.rb +++ b/spec/models/service_retire_task_spec.rb @@ -37,6 +37,7 @@ expect(service_retire_task.description).to eq("Service Retire for: #{service.name} - ") expect(VmRetireTask.count).to eq(0) + expect(ServiceRetireTask.count).to eq(1) end end @@ -47,26 +48,49 @@ miq_request.approve(approver, reason) end - context "resource lacks type" do - it "creates service retire subtask" do - resource = FactoryBot.create(:service_resource, :resource_type => nil, :service_id => service.id, :resource_id => vm.id) - service.service_resources << resource - service_retire_task.after_request_task_create + it "creates service retire subtask" do + service.add_resource!(FactoryBot.create(:service_orchestration)) + service_retire_task.after_request_task_create - expect(service_retire_task.description).to eq("Service Retire for: #{service.name} - ") - expect(ServiceRetireTask.count).to eq(1) - end + expect(service_retire_task.description).to eq("Service Retire for: #{service.name} - ") + expect(ServiceRetireTask.count).to eq(2) end - context "resource has type" do - it "creates vm retire subtask" do - resource = FactoryBot.create(:service_resource, :resource_type => "VmOrTemplate", :service_id => service.id, :resource_id => vm.id) - service.service_resources << resource - service_retire_task.after_request_task_create + it "creates service retire subtask" do + service.add_resource!(FactoryBot.create(:service)) + service_retire_task.after_request_task_create - expect(service_retire_task.description).to eq("Service Retire for: #{service.name} - ") - expect(VmRetireTask.count).to eq(1) - end + expect(service_retire_task.description).to eq("Service Retire for: #{service.name} - ") + expect(ServiceRetireTask.count).to eq(2) + end + + it "creates stack retire subtask" do + service.add_resource!(FactoryBot.create(:orchestration_stack)) + service_retire_task.after_request_task_create + + expect(service_retire_task.description).to eq("Service Retire for: #{service.name} - ") + expect(OrchestrationStackRetireTask.count).to eq(1) + expect(ServiceRetireTask.count).to eq(1) + end + + it "doesn't create subtask for miq_provision_request_template" do + admin = FactoryBot.create(:user_admin) + vm_template = FactoryBot.create(:vm_openstack, :ext_management_system => FactoryBot.create(:ext_management_system)) + service.add_resource!(FactoryBot.create(:miq_provision_request_template, :requester => admin, :src_vm_id => vm_template.id)) + service_retire_task.after_request_task_create + + expect(service_retire_task.description).to eq("Service Retire for: #{service.name} - ") + expect(MiqRetireTask.count).to eq(1) + expect(ServiceRetireTask.count).to eq(1) + end + + it "creates vm retire subtask" do + service.add_resource!(FactoryBot.create(:vm_openstack)) + service_retire_task.after_request_task_create + + expect(service_retire_task.description).to eq("Service Retire for: #{service.name} - ") + expect(VmRetireTask.count).to eq(1) + expect(ServiceRetireTask.count).to eq(1) end end