Skip to content

Commit

Permalink
Destroy Ansible Playbook job templates
Browse files Browse the repository at this point in the history
  • Loading branch information
syncrou committed Mar 24, 2017
1 parent 02c1f12 commit 731e8dd
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ module ManageIQ::Providers::AnsibleTower::Shared::AutomationManager::Configurati
extend ActiveSupport::Concern

module ClassMethods
NAME_MAP = {
'create' => 'Creating',
'update' => 'Updating',
'delete' => 'Deleting'
}.freeze
private_constant :NAME_MAP

def create_in_provider(manager_id, params)
manager = ExtManagementSystem.find(manager_id)
Expand All @@ -25,24 +31,39 @@ def update_in_provider(manager_id, params)

# Get the record in our database
# TODO: This needs to be targeted refresh so it doesn't take too long
EmsRefresh.queue_refresh(manager, nil, true)
EmsRefresh.queue_refresh(manager)
end

def delete_in_provider(manager_id, params)
manager = ExtManagementSystem.find(manager_id)
manager.with_provider_connection do |connection|
connection.api.job_templates.find(params[:manager_ref]).destroy!
end

# Get the record in our database
# TODO: This needs to be targeted refresh so it doesn't take too long
EmsRefresh.queue_refresh(manager)
end

def delete_in_provider_queue(manager_id, params, auth_user = nil)
operate_in_provider_queue('delete', manager_id, params, auth_user)
end

def update_in_provider_queue(manager_id, params, auth_user = nil)
create_or_update_in_provider_queue('update', manager_id, params, auth_user)
operate_in_provider_queue('update', manager_id, params, auth_user)
end

def create_in_provider_queue(manager_id, params, auth_user = nil)
create_or_update_in_provider_queue('create', manager_id, params, auth_user)
operate_in_provider_queue('create', manager_id, params, auth_user)
end

private

def create_or_update_in_provider_queue(action, manager_id, params, auth_user)
def operate_in_provider_queue(action, manager_id, params, auth_user)
manager = ExtManagementSystem.find(manager_id)

task_opts = {
:action => "#{(action == "create" ? "Creating" : "Updating")} Ansible Tower Job Template",
:action => "#{NAME_MAP[action]} Ansible Tower Job Template",
:userid => auth_user || "system"
}
queue_opts = {
Expand Down
10 changes: 10 additions & 0 deletions app/models/service_template_ansible_playbook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,14 @@ def update_catalog_item(options, auth_user = nil)
end
super
end

def destroy
auth_user = User.current_userid || 'system'
resource_actions.where.not(:configuration_template_id => nil).each do |resource_action|
job_template = resource_action.configuration_template
ManageIQ::Providers::EmbeddedAnsible::AutomationManager::ConfigurationScript
.delete_in_provider_queue(job_template.manager.id, { :manager_ref => job_template.manager_ref }, auth_user)
end
super
end
end
44 changes: 36 additions & 8 deletions spec/models/service_template_ansible_playbook_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@

let(:job_template) do
FactoryGirl.create(:configuration_script,
:variables => catalog_item_options.fetch_path(:config_info, :provision, :extra_vars))
:variables => catalog_item_options.fetch_path(:config_info, :provision, :extra_vars),
:manager => ems)
end

let(:catalog_item_options) do
Expand Down Expand Up @@ -237,14 +238,41 @@

expect(service_template.dialogs.first.id).to eq info[:service_dialog_id]
end
end

def prebuild_service_template
expect(described_class)
.to receive(:create_job_templates).and_return(:provision => {:configuration_template => job_template})
service_template = described_class.create_catalog_item(catalog_item_options_two, user)
expect(service_template).to receive(:job_template)
.and_return(job_template).at_least(:once)
service_template
describe '#destroy' do
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)

expect(ManageIQ::Providers::EmbeddedAnsible::AutomationManager::ConfigurationScript)
.to receive(:delete_in_provider_queue)
service_template.destroy
end

it 'does not destroy a job template if there is no associated configuration_template' do
service_template = prebuild_service_template(:job_template => false)
adjust_resource_actions(service_template, nil)

expect(ManageIQ::Providers::EmbeddedAnsible::AutomationManager::ConfigurationScript)
.to receive(:delete_in_provider_queue).never
service_template.destroy
end

def adjust_resource_actions(service_template, item)
service_template.resource_actions.first.tap do |resource_action|
resource_action.configuration_template_id = item
end.save
end
end

def prebuild_service_template(options = { :job_template => true })
ret = {:provision => {:configuration_template => job_template}}
expect(described_class).to receive(:create_job_templates).and_return(ret).at_least(:once)
described_class.create_catalog_item(catalog_item_options_two, user).tap do |service_template|
if options[:job_template]
expect(service_template).to receive(:job_template).and_return(job_template).at_least(:once)
end
end
end
end

0 comments on commit 731e8dd

Please sign in to comment.