From c15b9c8b994f5593719effeaad2b1c52225ab5b0 Mon Sep 17 00:00:00 2001 From: James Wong Date: Fri, 3 Mar 2017 14:01:07 -0500 Subject: [PATCH 1/2] configuration_script_source api_create https://www.pivotaltracker.com/story/show/140789269 --- .../configuration_script_source.rb | 2 + .../configuration_script_source.rb | 42 ++++++++++++ .../configuration_script_source.rb | 2 + .../configuration_script_source_spec.rb | 66 +++++++++++++++++++ 4 files changed, 112 insertions(+) create mode 100644 app/models/manageiq/providers/ansible_tower/shared/automation_manager/configuration_script_source.rb create mode 100644 spec/models/manageiq/providers/ansible_tower/automation_manager/configuration_script_source_spec.rb diff --git a/app/models/manageiq/providers/ansible_tower/automation_manager/configuration_script_source.rb b/app/models/manageiq/providers/ansible_tower/automation_manager/configuration_script_source.rb index f6d5157590c..bee45f66d72 100644 --- a/app/models/manageiq/providers/ansible_tower/automation_manager/configuration_script_source.rb +++ b/app/models/manageiq/providers/ansible_tower/automation_manager/configuration_script_source.rb @@ -1,3 +1,5 @@ class ManageIQ::Providers::AnsibleTower::AutomationManager::ConfigurationScriptSource < ManageIQ::Providers::ExternalAutomationManager::ConfigurationScriptSource + + include ManageIQ::Providers::AnsibleTower::Shared::AutomationManager::ConfigurationScriptSource end 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 new file mode 100644 index 00000000000..f41d492fe7f --- /dev/null +++ b/app/models/manageiq/providers/ansible_tower/shared/automation_manager/configuration_script_source.rb @@ -0,0 +1,42 @@ +module ManageIQ::Providers::AnsibleTower::Shared::AutomationManager::ConfigurationScriptSource + extend ActiveSupport::Concern + + module ClassMethods + def create_in_provider(manager_id, params) + manager = ExtManagementSystem.find(manager_id) + project = manager.with_provider_connection do |connection| + connection.api.projects.create!(params) + 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, nil, true) + + find_by!(:manager_id => manager.id, :manager_ref => project.id) + end + + def create_in_provider_queue(manager_id, params) + task_opts = { + :action => "Creating Ansible Tower Project", + :userid => "system" + } + + manager = ExtManagementSystem.find(manager_id) + + queue_opts = { + :args => [manager_id, params], + :class_name => "ManageIQ::Providers::AnsibleTower::AutomationManager::ConfigurationScriptSource", + :method_name => "create_in_provider", + :priority => MiqQueue::HIGH_PRIORITY, + :role => "ems_operations", + :zone => manager.my_zone + } + + MiqTask.generic_action_with_callback(task_opts, queue_opts) + end + + def provider_object(connection = nil) + (connection || connection_source.connect).api.projects.find(manager_ref) + end + end +end diff --git a/app/models/manageiq/providers/embedded_ansible/automation_manager/configuration_script_source.rb b/app/models/manageiq/providers/embedded_ansible/automation_manager/configuration_script_source.rb index fbe0eda77ff..59e99ceddf3 100644 --- a/app/models/manageiq/providers/embedded_ansible/automation_manager/configuration_script_source.rb +++ b/app/models/manageiq/providers/embedded_ansible/automation_manager/configuration_script_source.rb @@ -1,3 +1,5 @@ class ManageIQ::Providers::EmbeddedAnsible::AutomationManager::ConfigurationScriptSource < ManageIQ::Providers::EmbeddedAutomationManager::ConfigurationScriptSource + + include ManageIQ::Providers::AnsibleTower::Shared::AutomationManager::ConfigurationScriptSource end diff --git a/spec/models/manageiq/providers/ansible_tower/automation_manager/configuration_script_source_spec.rb b/spec/models/manageiq/providers/ansible_tower/automation_manager/configuration_script_source_spec.rb new file mode 100644 index 00000000000..eb4184ba0bb --- /dev/null +++ b/spec/models/manageiq/providers/ansible_tower/automation_manager/configuration_script_source_spec.rb @@ -0,0 +1,66 @@ +require 'ansible_tower_client' + +describe ManageIQ::Providers::AnsibleTower::AutomationManager::ConfigurationScriptSource do + context "create through API" do + let(:provider) { FactoryGirl.create(:provider_ansible_tower, :with_authentication) } + let(:manager) { provider.managers.first } + let(:atc) { double("AnsibleTowerClient::Connection", :api => api) } + let(:api) { double("AnsibleTowerClient::Api", :projects => projects) } + let(:projects) { double("AnsibleTowerClient::Collection", :create! => project) } + let(:project) { AnsibleTowerClient::Project.new(nil, project_json) } + + let(:project_json) do + params.merge( + :id => 10, + "scm_type" => "git", + "scm_url" => "https://github.com/ansible/ansible-tower-samples" + ).stringify_keys.to_json + end + + let(:params) do + { + :description => "Description", + :name => "My Project", + :related => {} + } + end + + it ".create_in_provider" do + expect(AnsibleTowerClient::Connection).to receive(:new).and_return(atc) + expect(EmsRefresh).to receive(:queue_refresh).and_return(store_new_project(project, manager)) + expect(ExtManagementSystem).to receive(:find).with(manager.id).and_return(manager) + + expect(described_class.create_in_provider(manager.id, params)).to be_a(described_class) + end + + it "not found during refresh" do + expect(AnsibleTowerClient::Connection).to receive(:new).and_return(atc) + expect(EmsRefresh).to receive(:queue_refresh) + expect(ExtManagementSystem).to receive(:find).with(manager.id).and_return(manager) + + expect { described_class.create_in_provider(manager.id, params) }.to raise_error(ActiveRecord::RecordNotFound) + end + + it ".create_in_provider_queue" do + EvmSpecHelper.local_miq_server + task_id = described_class.create_in_provider_queue(manager.id, params) + expect(MiqTask.find(task_id)).to have_attributes(:name => "Creating Ansible Tower Project") + expect(MiqQueue.first).to have_attributes( + :args => [manager.id, params], + :class_name => "ManageIQ::Providers::AnsibleTower::AutomationManager::ConfigurationScriptSource", + :method_name => "create_in_provider", + :priority => MiqQueue::HIGH_PRIORITY, + :role => "ems_operations", + :zone => manager.my_zone + ) + end + + def store_new_project(project, manager) + described_class.create!( + :manager => manager, + :manager_ref => project.id.to_s, + :name => project.name, + ) + end + end +end From 4249bc858cc02907a737874c5363f26b6e284749 Mon Sep 17 00:00:00 2001 From: James Wong Date: Thu, 9 Mar 2017 13:23:35 -0500 Subject: [PATCH 2/2] use queue_refresh_task --- .../automation_manager/configuration_script_source.rb | 3 ++- .../automation_manager/configuration_script_source_spec.rb | 6 ++++-- 2 files changed, 6 insertions(+), 3 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 f41d492fe7f..394c891cfc5 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 @@ -10,7 +10,8 @@ def create_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) + task_ids = EmsRefresh.queue_refresh_task(manager) + task_ids.each { |tid| MiqTask.wait_for_taskid(tid) } find_by!(:manager_id => manager.id, :manager_ref => project.id) end diff --git a/spec/models/manageiq/providers/ansible_tower/automation_manager/configuration_script_source_spec.rb b/spec/models/manageiq/providers/ansible_tower/automation_manager/configuration_script_source_spec.rb index eb4184ba0bb..dd0a0bad044 100644 --- a/spec/models/manageiq/providers/ansible_tower/automation_manager/configuration_script_source_spec.rb +++ b/spec/models/manageiq/providers/ansible_tower/automation_manager/configuration_script_source_spec.rb @@ -2,6 +2,7 @@ describe ManageIQ::Providers::AnsibleTower::AutomationManager::ConfigurationScriptSource do context "create through API" do + let(:finished_task) { FactoryGirl.create(:miq_task, :state => "Finished") } let(:provider) { FactoryGirl.create(:provider_ansible_tower, :with_authentication) } let(:manager) { provider.managers.first } let(:atc) { double("AnsibleTowerClient::Connection", :api => api) } @@ -27,7 +28,8 @@ it ".create_in_provider" do expect(AnsibleTowerClient::Connection).to receive(:new).and_return(atc) - expect(EmsRefresh).to receive(:queue_refresh).and_return(store_new_project(project, manager)) + store_new_project(project, manager) + expect(EmsRefresh).to receive(:queue_refresh_task).and_return([finished_task]) expect(ExtManagementSystem).to receive(:find).with(manager.id).and_return(manager) expect(described_class.create_in_provider(manager.id, params)).to be_a(described_class) @@ -35,7 +37,7 @@ it "not found during refresh" do expect(AnsibleTowerClient::Connection).to receive(:new).and_return(atc) - expect(EmsRefresh).to receive(:queue_refresh) + expect(EmsRefresh).to receive(:queue_refresh_task).and_return([finished_task]) expect(ExtManagementSystem).to receive(:find).with(manager.id).and_return(manager) expect { described_class.create_in_provider(manager.id, params) }.to raise_error(ActiveRecord::RecordNotFound)