Skip to content

Commit

Permalink
Add retireable check to service ansible playbooks
Browse files Browse the repository at this point in the history
  • Loading branch information
d-m-u committed Apr 1, 2019
1 parent f73e7e6 commit b5599fa
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 5 deletions.
10 changes: 7 additions & 3 deletions app/models/service_ansible_playbook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -66,14 +66,18 @@ 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)
{
'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
Expand All @@ -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?
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 retire_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 retire_subtasks?(parent_svc)
parent_svc.kind_of?(ServiceAnsiblePlaybook) && parent_svc.retain_resources_on_retirement? ? false : true
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 @@ -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 is false" do
let(:remove_resources) { 'no_with_playbook' }
let(:can_children_be_retired?) { true }
it_behaves_like "#retain_resources_on_retirement"
end

context "no_without_playbook is false" do
let(:remove_resources) { 'no_without_playbook' }
let(:can_children_be_retired?) { true }
it_behaves_like "#retain_resources_on_retirement"
end

context "yes_with_playbook is true" do
let(:remove_resources) { 'yes_with_playbook' }
let(:can_children_be_retired?) { false }
it_behaves_like "#retain_resources_on_retirement"
end

context "yes_without_playbook is true" do
let(:remove_resources) { 'yes_without_playbook' }
let(:can_children_be_retired?) { false }
it_behaves_like "#retain_resources_on_retirement"
end
end

describe '#preprocess' do
context 'basic service' do
it 'prepares job options from service template' do
Expand Down
58 changes: 58 additions & 0 deletions spec/models/service_retire_task_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,64 @@
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 "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

context "no_without_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 "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

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 "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

context "yes_without_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 "AP service 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
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 b5599fa

Please sign in to comment.