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 840ecb1d437..d8b3c1df8fa 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 @@ -15,22 +15,15 @@ def provider_collection(manager) connection.api.projects end end - end - - def provider_object(connection = nil) - (connection || connection_source.connect).api.projects.find(manager_ref) - end - REFRESH_ON_TOWER_SLEEP = 1.second - def refresh_in_provider - with_provider_object do |project| + def refresh_in_provider(project, id = nil) return unless project.can_update? project_update = project.update # this is really just a quick hack. We should do this properly once # https://github.com/ManageIQ/manageiq/pull/14405 is merged - log_header = "updating project #{project.id} (#{self.class.name} #{id})" + log_header = "updating project #{project.id} (#{name} #{id})" _log.info "#{log_header}..." Timeout.timeout(5.minutes) do loop do @@ -45,4 +38,15 @@ def refresh_in_provider _log.info "#{log_header}...Complete" end end + + def provider_object(connection = nil) + (connection || connection_source.connect).api.projects.find(manager_ref) + end + + REFRESH_ON_TOWER_SLEEP = 1.second + def refresh_in_provider + with_provider_object do |project| + self.class.refresh_in_provider(project, id) + end + end 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 d814aba17e3..ed2874ae6d7 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 @@ -6,7 +6,9 @@ def create_in_provider(manager_id, params) params = provider_params(params) if respond_to?(:provider_params) manager = ExtManagementSystem.find(manager_id) tower_object = provider_collection(manager).create!(params) - + if respond_to?(:refresh_in_provider) + refresh_in_provider(tower_object) + end refresh(manager) find_by!(:manager_id => manager.id, :manager_ref => tower_object.id) rescue AnsibleTowerClient::ClientError, ActiveRecord::RecordNotFound => error @@ -34,10 +36,9 @@ def notify(op, manager_id, params, success) ) end - def refresh(manager) - # Get the record in our database - # TODO: This needs to be targeted refresh so it doesn't take too long - task_ids = EmsRefresh.queue_refresh_task(manager) + def refresh(target) + # Get the record into our database + task_ids = EmsRefresh.queue_refresh_task(target) task_ids.each { |tid| MiqTask.wait_for_taskid(tid) } end @@ -66,7 +67,7 @@ def update_in_provider(params) with_provider_object do |provider_object| provider_object.update_attributes!(params) end - self.class.send('refresh', manager) + self.class.send('refresh', self) reload rescue AnsibleTowerClient::ClientError => error raise 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 394a288db42..fff878810e8 100644 --- a/spec/support/ansible_shared/automation_manager/configuration_script_source.rb +++ b/spec/support/ansible_shared/automation_manager/configuration_script_source.rb @@ -10,6 +10,7 @@ context "create through API" do let(:projects) { double("AnsibleTowerClient::Collection", :create! => project) } let(:project) { AnsibleTowerClient::Project.new(nil, project_json) } + let(:css) { store_new_project(project, manager) } let(:project_json) do params.merge( @@ -41,7 +42,8 @@ it ".create_in_provider to succeed and send notification" do expect(AnsibleTowerClient::Connection).to receive(:new).and_return(atc) store_new_project(project, manager) - expect(EmsRefresh).to receive(:queue_refresh_task).and_return([finished_task]) + expect(described_class).to receive(:refresh_in_provider).and_return(nil) + expect(EmsRefresh).to receive(:queue_refresh_task).with(manager).and_return([finished_task]) expect(ExtManagementSystem).to receive(:find).with(manager.id).and_return(manager) expect(projects).to receive(:create!).with(params) expect(Notification).to receive(:create).with(expected_notify) @@ -50,6 +52,7 @@ it ".create_in_provider to fail(not found during refresh) and send notification" do expect(AnsibleTowerClient::Connection).to receive(:new).and_return(atc) + expect(described_class).to receive(:refresh_in_provider).and_return(nil) expect(EmsRefresh).to receive(:queue_refresh_task).and_return([finished_task]) expect(ExtManagementSystem).to receive(:find).with(manager.id).and_return(manager) expected_notify[:type] = :tower_op_failure @@ -61,7 +64,8 @@ params[:authentication_id] = credential.id expect(AnsibleTowerClient::Connection).to receive(:new).and_return(atc) store_new_project(project, manager) - expect(EmsRefresh).to receive(:queue_refresh_task).and_return([finished_task]) + expect(described_class).to receive(:refresh_in_provider).and_return(nil) + expect(EmsRefresh).to receive(:queue_refresh_task).with(manager).and_return([finished_task]) expect(ExtManagementSystem).to receive(:find).with(manager.id).and_return(manager) expected_params = params.clone.merge(:credential => '1') expected_params.delete(:authentication_id) @@ -111,7 +115,7 @@ def store_new_project(project, manager) it "#delete_in_provider to succeed and send notification" do expect(AnsibleTowerClient::Connection).to receive(:new).and_return(atc) - expect(EmsRefresh).to receive(:queue_refresh_task).and_return([finished_task]) + expect(EmsRefresh).to receive(:queue_refresh_task).with(manager).and_return([finished_task]) expect(Notification).to receive(:create).with(expected_notify) project.delete_in_provider end @@ -156,7 +160,7 @@ def store_new_project(project, manager) it "#update_in_provider to succeed and send notification" do expect(AnsibleTowerClient::Connection).to receive(:new).and_return(atc) - expect(EmsRefresh).to receive(:queue_refresh_task).and_return([finished_task]) + expect(EmsRefresh).to receive(:queue_refresh_task).with(project).and_return([finished_task]) expect(Notification).to receive(:create).with(expected_notify) expect(project.update_in_provider({})).to be_a(described_class) end diff --git a/spec/support/ansible_shared/automation_manager/credential.rb b/spec/support/ansible_shared/automation_manager/credential.rb index 0fa0d3cacb9..3b20b88b3ad 100644 --- a/spec/support/ansible_shared/automation_manager/credential.rb +++ b/spec/support/ansible_shared/automation_manager/credential.rb @@ -54,7 +54,7 @@ expected_params[:organization] = 1 if described_class.name.include?("::EmbeddedAnsible::") expect(AnsibleTowerClient::Connection).to receive(:new).and_return(atc) store_new_credential(credential, manager) - expect(EmsRefresh).to receive(:queue_refresh_task).and_return([finished_task]) + expect(EmsRefresh).to receive(:queue_refresh_task).with(manager).and_return([finished_task]) expect(ExtManagementSystem).to receive(:find).with(manager.id).and_return(manager) expect(credentials).to receive(:create!).with(expected_params) expect(Notification).to receive(:create).with(expected_notify)