Skip to content

Commit

Permalink
Merge pull request #18620 from d-m-u/hammer_version_of_ansible_retire…
Browse files Browse the repository at this point in the history
…able_check

[HAMMER] Add create_subtasks method to service_retire_task for ansible playbooks
  • Loading branch information
simaishi authored Apr 4, 2019
2 parents 97919fa + 890e4ac commit ba60e64
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 2 deletions.
4 changes: 4 additions & 0 deletions app/models/service_ansible_playbook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 8 additions & 2 deletions app/models/service_retire_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
36 changes: 36 additions & 0 deletions spec/models/service_ansible_playbook_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
52 changes: 52 additions & 0 deletions spec/models/service_retire_task_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit ba60e64

Please sign in to comment.