Skip to content

Commit

Permalink
Merge pull request #14173 from jameswnl/api-create-project
Browse files Browse the repository at this point in the history
configuration_script_source aka project to create through Tower API
  • Loading branch information
blomquisg authored Mar 13, 2017
2 parents f2b1bb8 + 4249bc8 commit 3371b89
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 0 deletions.
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

0 comments on commit 3371b89

Please sign in to comment.