diff --git a/app/models/service_retire_task.rb b/app/models/service_retire_task.rb index 5081056122aa..38f7a1c634ec 100644 --- a/app/models/service_retire_task.rb +++ b/app/models/service_retire_task.rb @@ -32,7 +32,11 @@ def after_request_task_create def create_retire_subtasks(parent_service, parent_task) parent_service.service_resources.collect do |svc_rsc| - next unless svc_rsc.resource.try(:retireable?) + next unless svc_rsc.resource.present? && svc_rsc.resource.try(:retireable?) + next if svc_rsc.resource_type == "ServiceTemplate" && + !self.class.include_service_template?(self, + svc_rsc.resource.id, + parent_service) # TODO: the next line deals with the filtering for provisioning # (https://github.com/ManageIQ/manageiq/blob/3921e87915b5a69937b9d4a70bb24ab71b97c165/app/models/service_template/filter.rb#L5) # which should be extended to retirement as part of later work diff --git a/spec/models/service_retire_task_spec.rb b/spec/models/service_retire_task_spec.rb index b760f49ad98d..692585dc7caa 100644 --- a/spec/models/service_retire_task_spec.rb +++ b/spec/models/service_retire_task_spec.rb @@ -85,6 +85,7 @@ service.service_resources << FactoryGirl.create(:service_resource, :resource_type => "VmOrTemplate", :service_id => service_c1.id, :resource_id => vm.id) service.service_resources << FactoryGirl.create(:service_resource, :resource_type => "VmOrTemplate", :service_id => service_c1.id, :resource_id => vm1.id) service.service_resources << FactoryGirl.create(:service_resource, :resource_type => "Service", :service_id => service_c1.id, :resource_id => service_c1.id) + service.service_resources << FactoryGirl.create(:service_resource, :resource_type => "ServiceTemplate", :service_id => service_c1.id, :resource_id => service_c1.id) @service_retire_task.after_request_task_create expect(VmRetireTask.count).to eq(2) @@ -92,6 +93,24 @@ expect(ServiceRetireTask.count).to eq(1) expect(ServiceRetireRequest.count).to eq(1) end + + it "doesn't creates subtask for ServiceTemplates" do + @service_retire_task = FactoryGirl.create(:service_retire_task, :source => service, :miq_request_task_id => nil, :miq_request_id => @miq_request.id, :options => {:src_ids => [service.id] }) + service.service_resources << FactoryGirl.create(:service_resource, :resource_type => "ServiceTemplate", :service_id => service_c1.id, :resource_id => service_c1.id) + + @service_retire_task.after_request_task_create + expect(ServiceRetireTask.count).to eq(1) + expect(ServiceRetireRequest.count).to eq(1) + end + + it "doesn't creates subtask for service resources whose resources are nil" do + @service_retire_task = FactoryGirl.create(:service_retire_task, :source => service, :miq_request_task_id => nil, :miq_request_id => @miq_request.id, :options => {:src_ids => [service.id] }) + service.service_resources << FactoryGirl.create(:service_resource, :resource_type => "ServiceTemplate", :service_id => service_c1.id, :resource => nil) + + @service_retire_task.after_request_task_create + expect(ServiceRetireTask.count).to eq(1) + expect(ServiceRetireRequest.count).to eq(1) + end end end