From 6d35f13657e82711046ff902c9ce6cef51e6d76d Mon Sep 17 00:00:00 2001 From: James Wong Date: Mon, 8 May 2017 19:11:57 -0400 Subject: [PATCH] Allow Tower Project to reset credential https://bugzilla.redhat.com/show_bug.cgi?id=1448483 --- .../configuration_script_source.rb | 6 +++-- .../shared/automation_manager/tower_api.rb | 1 + .../configuration_script_source.rb | 25 ++++++++++++++++--- 3 files changed, 27 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 0f91575b41f..c5168e31e7d 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/app/models/manageiq/providers/ansible_tower/shared/automation_manager/tower_api.rb b/app/models/manageiq/providers/ansible_tower/shared/automation_manager/tower_api.rb index 33c2dbf7771..4bae82ec76b 100644 --- a/app/models/manageiq/providers/ansible_tower/shared/automation_manager/tower_api.rb +++ b/app/models/manageiq/providers/ansible_tower/shared/automation_manager/tower_api.rb @@ -24,6 +24,7 @@ def create_in_provider_queue(manager_id, params) end private + def notify(op, manager_id, params, success) params = hide_secrets(params) if respond_to?(:hide_secrets) _log.info "#{name} in_provider #{op} with parameters: #{params} #{success ? 'succeeded' : 'failed'}" 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})")