From 46c68927589fe98e01a7793ee2495c51aac2bb6a Mon Sep 17 00:00:00 2001 From: Greg Blomquist Date: Tue, 23 May 2017 09:43:24 -0400 Subject: [PATCH] Merge pull request #15029 from jameswnl/project-cred Allow Tower Project to reset credential (cherry picked from commit 4d1624833a316c9cf7c63b4ab1a17f9664679d96) https://bugzilla.redhat.com/show_bug.cgi?id=1460304 --- .../configuration_script_source.rb | 6 +++-- .../configuration_script_source.rb | 25 ++++++++++++++++--- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/app/models/manageiq/providers/ansible_tower/shared/automation_manager/configuration_script_source.rb b/app/models/manageiq/providers/ansible_tower/shared/automation_manager/configuration_script_source.rb index 2c5dd6bf3b6..8e44b1b3132 100644 --- a/app/models/manageiq/providers/ansible_tower/shared/automation_manager/configuration_script_source.rb +++ b/app/models/manageiq/providers/ansible_tower/shared/automation_manager/configuration_script_source.rb @@ -5,8 +5,10 @@ module ManageIQ::Providers::AnsibleTower::Shared::AutomationManager::Configurati module ClassMethods def provider_params(params) - authentication_id = params.delete(:authentication_id) - params[:credential] = Authentication.find(authentication_id).manager_ref if authentication_id + if params.keys.include?(:authentication_id) + authentication_id = params.delete(:authentication_id) + params[:credential] = authentication_id ? Authentication.find(authentication_id).manager_ref : nil + end params end diff --git a/spec/support/ansible_shared/automation_manager/configuration_script_source.rb b/spec/support/ansible_shared/automation_manager/configuration_script_source.rb index d29cf84e021..901f9101c9e 100644 --- a/spec/support/ansible_shared/automation_manager/configuration_script_source.rb +++ b/spec/support/ansible_shared/automation_manager/configuration_script_source.rb @@ -22,9 +22,9 @@ let(:params) do { - :description => "Description", - :name => "My Project", - :related => {} + :description => "Description", + :name => "My Project", + :related => {} } end @@ -146,6 +146,7 @@ def store_new_project(project, manager) let(:projects) { double("AnsibleTowerClient::Collection", :find => tower_project) } let(:tower_project) { double("AnsibleTowerClient::Project", :update_attributes! => {}, :id => 1) } let(:project) { described_class.create!(:manager => manager, :manager_ref => tower_project.id) } + let(:tower_cred) { FactoryGirl.create(:ansible_scm_credential, :manager_ref => '100') } let(:expected_notify) do { :type => :tower_op_success, @@ -173,6 +174,24 @@ def store_new_project(project, manager) expect { project.update_in_provider({}) }.to raise_error(AnsibleTowerClient::ClientError) end + it "#update_in_provider with credential" do + expect(AnsibleTowerClient::Connection).to receive(:new).and_return(atc) + expect(EmsRefresh).to receive(:queue_refresh_task).with(manager).and_return([finished_task]) + expect(project).to receive(:refresh_in_provider) + expect(tower_project).to receive(:update_attributes!).with(:credential => tower_cred.manager_ref) + expect(Notification).to receive(:create).with(expected_notify) + expect(project.update_in_provider(:authentication_id => tower_cred.id)).to be_a(described_class) + end + + it "#update_in_provider with nil credential" do + expect(AnsibleTowerClient::Connection).to receive(:new).and_return(atc) + expect(EmsRefresh).to receive(:queue_refresh_task).with(manager).and_return([finished_task]) + expect(project).to receive(:refresh_in_provider) + expect(tower_project).to receive(:update_attributes!).with(:credential => nil) + expect(Notification).to receive(:create).with(expected_notify) + expect(project.update_in_provider(:authentication_id => nil)).to be_a(described_class) + end + it "#update_in_provider_queue" do task_id = project.update_in_provider_queue({}) expect(MiqTask.find(task_id)).to have_attributes(:name => "Updating #{described_class::FRIENDLY_NAME} (Tower internal reference=#{project.manager_ref})")