Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

configuration_script_source aka project to create through Tower API #14173

Merged
merged 2 commits into from
Mar 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
class ManageIQ::Providers::AnsibleTower::AutomationManager::ConfigurationScriptSource <
ManageIQ::Providers::ExternalAutomationManager::ConfigurationScriptSource

include ManageIQ::Providers::AnsibleTower::Shared::AutomationManager::ConfigurationScriptSource
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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
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

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
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
class ManageIQ::Providers::EmbeddedAnsible::AutomationManager::ConfigurationScriptSource <
ManageIQ::Providers::EmbeddedAutomationManager::ConfigurationScriptSource

include ManageIQ::Providers::AnsibleTower::Shared::AutomationManager::ConfigurationScriptSource
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
require 'ansible_tower_client'

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) }
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)
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)
end

it "not found during refresh" do
expect(AnsibleTowerClient::Connection).to receive(:new).and_return(atc)
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)
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