-
Notifications
You must be signed in to change notification settings - Fork 898
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14173 from jameswnl/api-create-project
configuration_script_source aka project to create through Tower API
- Loading branch information
Showing
4 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
2
...models/manageiq/providers/ansible_tower/automation_manager/configuration_script_source.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
43 changes: 43 additions & 0 deletions
43
...manageiq/providers/ansible_tower/shared/automation_manager/configuration_script_source.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
2 changes: 2 additions & 0 deletions
2
...els/manageiq/providers/embedded_ansible/automation_manager/configuration_script_source.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
68 changes: 68 additions & 0 deletions
68
...s/manageiq/providers/ansible_tower/automation_manager/configuration_script_source_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |