diff --git a/app/models/manageiq/providers/ansible_tower/shared/automation_manager/configuration_script.rb b/app/models/manageiq/providers/ansible_tower/shared/automation_manager/configuration_script.rb index 2491c248a8b..ef6e62e235b 100644 --- a/app/models/manageiq/providers/ansible_tower/shared/automation_manager/configuration_script.rb +++ b/app/models/manageiq/providers/ansible_tower/shared/automation_manager/configuration_script.rb @@ -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) @@ -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 = { diff --git a/app/models/service_template_ansible_playbook.rb b/app/models/service_template_ansible_playbook.rb index 921eb5d079c..48fa131ccd5 100644 --- a/app/models/service_template_ansible_playbook.rb +++ b/app/models/service_template_ansible_playbook.rb @@ -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 diff --git a/spec/models/service_template_ansible_playbook_spec.rb b/spec/models/service_template_ansible_playbook_spec.rb index 8bfff709fb1..eb82627a458 100644 --- a/spec/models/service_template_ansible_playbook_spec.rb +++ b/spec/models/service_template_ansible_playbook_spec.rb @@ -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 @@ -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