diff --git a/app/models/service_ansible_playbook.rb b/app/models/service_ansible_playbook.rb index 4497e5f4f46..46519218574 100644 --- a/app/models/service_ansible_playbook.rb +++ b/app/models/service_ansible_playbook.rb @@ -5,7 +5,7 @@ class ServiceAnsiblePlaybook < ServiceGeneric # A chance for taking options from automate script to override options from a service dialog def preprocess(action, add_options = {}) - unless add_options.blank? + if add_options.present? _log.info("Override with new options:") $log.log_hashes(add_options) end @@ -66,6 +66,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 service_manageiq_env(action) @@ -73,7 +77,7 @@ def service_manageiq_env(action) 'service' => href_slug, 'action' => action }.merge(manageiq_env(evm_owner, miq_group, miq_request_task)) - .merge(request_options_extra_vars) + .merge(request_options_extra_vars) end def request_options_extra_vars @@ -92,7 +96,7 @@ def save_job_options(action, overrides) job_options.deep_merge!(parse_dialog_options) unless action == ResourceAction::RETIREMENT job_options.deep_merge!(overrides) - %i(credential vault_credential).each do |cred| + %i[credential vault_credential].each do |cred| cred_sym = "#{cred}_id".to_sym credential_id = job_options.delete(cred_sym) job_options[cred] = Authentication.find(credential_id).native_ref if credential_id.present? 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 d3a4d0e3785..7aa4da7bc20 100644 --- a/spec/models/service_ansible_playbook_spec.rb +++ b/spec/models/service_ansible_playbook_spec.rb @@ -78,6 +78,42 @@ } 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 '#preprocess' do context 'basic service' do it 'prepares job options from service template' do diff --git a/spec/models/service_retire_task_spec.rb b/spec/models/service_retire_task_spec.rb index 5958c68295c..9ff4cbb9fb0 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 @@ -56,6 +78,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