diff --git a/app/models/service_template_ansible_playbook.rb b/app/models/service_template_ansible_playbook.rb index 76e7a16d2dc..1900c534cee 100644 --- a/app/models/service_template_ansible_playbook.rb +++ b/app/models/service_template_ansible_playbook.rb @@ -1,4 +1,6 @@ class ServiceTemplateAnsiblePlaybook < ServiceTemplateGeneric + before_destroy :check_retirement_potential + RETIREMENT_ENTRY_POINTS = { 'yes_without_playbook' => '/Service/Generic/StateMachines/GenericLifecycle/Retire_Basic_Resource', 'no_without_playbook' => '/Service/Generic/StateMachines/GenericLifecycle/Retire_Basic_Resource_None', @@ -163,4 +165,24 @@ def destroy end super end + + # ServiceTemplate includes a retirement resource action + # with a defined job template: + # + # 1. A resource_action that includes a configuration_template_id. + # 2. At least one service instance where :retired is set to false. + # + def retirement_potential? + retirement_jt_exists = resource_actions.where(:action => 'Retirement').where.not(:configuration_template_id => nil).present? + retirement_jt_exists && services.where(:retired => false).exists? + end + + private + + def check_retirement_potential + return true unless retirement_potential? + error_text = 'Destroy aborted. Active Services require retirement resources associated with this instance.' + errors[:base] << error_text + throw :abort + end end diff --git a/spec/models/service_template_ansible_playbook_spec.rb b/spec/models/service_template_ansible_playbook_spec.rb index 2bd6202b59f..e5fac7db0f4 100644 --- a/spec/models/service_template_ansible_playbook_spec.rb +++ b/spec/models/service_template_ansible_playbook_spec.rb @@ -238,6 +238,8 @@ end describe '#destroy' do + let(:service) { FactoryGirl.create(:service_ansible_tower) } + it 'destroys a job template if there is an associated configuration_template' do service_template = prebuild_service_template(:job_template => false) adjust_resource_actions(service_template, job_template.id) @@ -256,8 +258,16 @@ service_template.destroy end - def adjust_resource_actions(service_template, item) - service_template.resource_actions.first.tap do |resource_action| + it '#retirement_potential?' do + service.update_attributes(:retired => false) + service_template = prebuild_service_template(:job_template => false) + adjust_resource_actions(service_template, job_template.id, :last) + service_template.services << service + expect(service_template.retirement_potential?).to be_truthy + end + + def adjust_resource_actions(service_template, item, list_name = :first) + service_template.resource_actions.send(list_name).tap do |resource_action| resource_action.configuration_template_id = item end.save end