Skip to content

Commit

Permalink
Merge pull request #14586 from syncrou/raise_error_on_delete
Browse files Browse the repository at this point in the history
Modified destroying an Ansible Service Template
  • Loading branch information
gmcculloug authored Mar 31, 2017
2 parents 2846cb3 + 492c00b commit 4ea0b83
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
22 changes: 22 additions & 0 deletions app/models/service_template_ansible_playbook.rb
Original file line number Diff line number Diff line change
@@ -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',
Expand Down Expand Up @@ -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
14 changes: 12 additions & 2 deletions spec/models/service_template_ansible_playbook_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 4ea0b83

Please sign in to comment.