From 890e4ac6307f330025e2f59e09442b54d7686753 Mon Sep 17 00:00:00 2001 From: d-m-u Date: Wed, 3 Apr 2019 09:43:12 -0400 Subject: [PATCH] Add create_subtasks? method to service_retire_task for ansible playbook services --- app/models/service_ansible_playbook.rb | 4 ++ app/models/service_retire_task.rb | 10 +++- spec/models/service_ansible_playbook_spec.rb | 36 ++++++++++++++ spec/models/service_retire_task_spec.rb | 52 ++++++++++++++++++++ 4 files changed, 100 insertions(+), 2 deletions(-) diff --git a/app/models/service_ansible_playbook.rb b/app/models/service_ansible_playbook.rb index 3dce885cd6e..d93c90ee54b 100644 --- a/app/models/service_ansible_playbook.rb +++ b/app/models/service_ansible_playbook.rb @@ -59,6 +59,10 @@ def on_error(action) postprocess(action) end + def retain_resources_on_retirement? + options.fetch_path(:config_info, :retirement, :remove_resources).to_s.start_with?("no_") + end + private def manageiq_extra_vars(action) diff --git a/app/models/service_retire_task.rb b/app/models/service_retire_task.rb index 4699ff5f40c..1ce69626cf9 100644 --- a/app/models/service_retire_task.rb +++ b/app/models/service_retire_task.rb @@ -28,8 +28,10 @@ def task_active def after_request_task_create update_attributes(:description => get_description) parent_svc = Service.find_by(:id => options[:src_ids]) - _log.info("- creating service subtasks for service task <#{self.class.name}:#{id}>, service <#{parent_svc.id}>") - create_retire_subtasks(parent_svc, self) + if create_subtasks?(parent_svc) + _log.info("- creating service subtasks for service task <#{self.class.name}:#{id}>, service <#{parent_svc.id}>") + create_retire_subtasks(parent_svc, self) + end end def create_retire_subtasks(parent_service, parent_task) @@ -68,6 +70,10 @@ def create_task(svc_rsc, parent_service, nh, parent_task) private + def create_subtasks?(parent_svc) + !parent_svc.try(:retain_resources_on_retirement?) + end + def retire_task_type(resource_type) (resource_type.base_class.name + "RetireTask").safe_constantize || (resource_type.name.demodulize + "RetireTask").safe_constantize end diff --git a/spec/models/service_ansible_playbook_spec.rb b/spec/models/service_ansible_playbook_spec.rb index 94c2fd09ccf..d40eed0ab8b 100644 --- a/spec/models/service_ansible_playbook_spec.rb +++ b/spec/models/service_ansible_playbook_spec.rb @@ -140,6 +140,42 @@ end end + shared_examples_for "#retain_resources_on_retirement" do + it "has config_info retirement options" do + service_options = service.options + service_options[:config_info][:retirement] = service_options[:config_info][:provision] + service_options[:config_info][:retirement][:remove_resources] = remove_resources + service.update_attributes(:options => service_options) + expect(service.retain_resources_on_retirement?).to eq(!can_children_be_retired?) + end + end + + describe '#retain_resources_on_retirement?' do + context "no_with_playbook returns true" do + let(:remove_resources) { 'no_with_playbook' } + let(:can_children_be_retired?) { false } + it_behaves_like "#retain_resources_on_retirement" + end + + context "no_without_playbook returns true" do + let(:remove_resources) { 'no_without_playbook' } + let(:can_children_be_retired?) { false } + it_behaves_like "#retain_resources_on_retirement" + end + + context "yes_with_playbook returns false" do + let(:remove_resources) { 'yes_with_playbook' } + let(:can_children_be_retired?) { true } + it_behaves_like "#retain_resources_on_retirement" + end + + context "yes_without_playbook returns false" do + let(:remove_resources) { 'yes_without_playbook' } + let(:can_children_be_retired?) { true } + it_behaves_like "#retain_resources_on_retirement" + end + end + describe '#execute' do let(:control_extras) { {'a' => 'A', 'b' => 'B', 'c' => 'C'} } before do diff --git a/spec/models/service_retire_task_spec.rb b/spec/models/service_retire_task_spec.rb index c75c5e77dee..99e052be259 100644 --- a/spec/models/service_retire_task_spec.rb +++ b/spec/models/service_retire_task_spec.rb @@ -30,6 +30,28 @@ end end + shared_examples_for "no_remove_resource" do + it "creates 1 service retire subtask" do + ap_service.add_resource!(FactoryBot.create(:vm_vmware)) + ap_service_retire_task.after_request_task_create + + expect(ap_service_retire_task.description).to eq("Service Retire for: #{ap_service.name} - ") + expect(ServiceRetireTask.count).to eq(1) + expect(VmRetireTask.count).to eq(0) + end + end + + shared_examples_for "yes_remove_resource" do + it "creates 1 service retire subtask and 1 vm retire subtask" do + ap_service.add_resource!(FactoryBot.create(:vm_vmware)) + ap_service_retire_task.after_request_task_create + + expect(ap_service_retire_task.description).to eq("Service Retire for: #{ap_service.name} - ") + expect(ServiceRetireTask.count).to eq(1) + expect(VmRetireTask.count).to eq(1) + end + end + describe "#after_request_task_create" do context "sans resource" do it "doesn't create subtask" do @@ -55,6 +77,36 @@ expect(ServiceRetireTask.count).to eq(2) end + context "ansible playbook service" do + context "no_with_playbook" do + let(:ap_service) { FactoryBot.create(:service_ansible_playbook, :options => {:config_info => {:retirement => {:remove_resources => "no_with_playbook"} }}) } + let(:ap_service_retire_task) { FactoryBot.create(:service_retire_task, :source => ap_service, :miq_request => miq_request, :options => {:src_ids => [ap_service.id] }) } + + it_behaves_like "no_remove_resource" + end + + context "no_without_playbook" do + let(:ap_service) { FactoryBot.create(:service_ansible_playbook, :options => {:config_info => {:retirement => {:remove_resources => "no_without_playbook"} }}) } + let(:ap_service_retire_task) { FactoryBot.create(:service_retire_task, :source => ap_service, :miq_request => miq_request, :options => {:src_ids => [ap_service.id] }) } + + it_behaves_like "no_remove_resource" + end + + context "yes_with_playbook" do + let(:ap_service) { FactoryBot.create(:service_ansible_playbook, :options => {:config_info => {:retirement => {:remove_resources => "yes_with_playbook"} }}) } + let(:ap_service_retire_task) { FactoryBot.create(:service_retire_task, :source => ap_service, :miq_request => miq_request, :options => {:src_ids => [ap_service.id] }) } + + it_behaves_like "yes_remove_resource" + end + + context "yes_without_playbook" do + let(:ap_service) { FactoryBot.create(:service_ansible_playbook, :options => {:config_info => {:retirement => {:remove_resources => "yes_without_playbook"} }}) } + let(:ap_service_retire_task) { FactoryBot.create(:service_retire_task, :source => ap_service, :miq_request => miq_request, :options => {:src_ids => [ap_service.id] }) } + + it_behaves_like "yes_remove_resource" + end + end + it "creates service retire subtask" do service.add_resource!(FactoryBot.create(:service)) service_retire_task.after_request_task_create