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

Create catalog item after job templates are created #13893

Merged
merged 1 commit into from
Feb 15, 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
29 changes: 18 additions & 11 deletions app/models/service_template_ansible_playbook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,39 @@ def job_template(action)
# options
# :name
# :description
# :service_template_catalog
# :service_template_catalog_id
# :config_info
# :provision
# :service_dialog_id (or)
# :new_dialog_name
# :variables
# :hosts
# :credentials
# :credential_id
# :network_credential_id
# :cloud_credential_id
# :playbook_id
# :retirement (same as provision)
# :reconfigure (same as provision)
#
def self.create_catalog_item(options, _auth_user)
def self.create_catalog_item(options, auth_user)
options = options.merge(:service_type => 'atomic', :prov_type => 'generic_ansible_playbook')
service_name = options[:name]
description = options[:description]
config_info = validate_config_info(options)
config_info = validate_config_info(options[:config_info])

enhanced_config = config_info.deep_merge(create_job_templates(service_name, description, config_info, auth_user))

transaction do
create(options.except(:config_info)).tap do |service_template|
create_from_options(options).tap do |service_template|
[:provision, :retirement, :reconfigure].each do |action|
prepare_job_template_and_dialog(action, service_name, description, options) if config_info.key?(action)
dialog_name = config_info.fetch_path(action, :new_dialog_name)
next unless dialog_name

job_template = enhanced_config.fetch_path(action, :configuration_template)
enhanced_config[action][:dialog] =
Dialog::AnsiblePlaybookServiceDialog.create_dialog(dialog_name, job_template)
end
service_template.create_resource_actions(config_info)
service_template.create_resource_actions(enhanced_config)
end
end
end
Expand Down Expand Up @@ -97,10 +106,8 @@ def self.build_parameter_list(name, description, info)
end
private_class_method :build_parameter_list

def self.validate_config_info(options)
info = options[:config_info]

info[:provision][:fqname] ||= default_provisioning_entry_point if info.key?(:provision)
def self.validate_config_info(info)
info[:provision][:fqname] ||= default_provisioning_entry_point('atomic') if info.key?(:provision)
info[:retirement][:fqname] ||= default_retirement_entry_point if info.key?(:retirement)
info[:reconfigure][:fqname] ||= default_reconfiguration_entry_point if info.key?(:reconfigure)

Expand Down
177 changes: 105 additions & 72 deletions spec/models/service_template_ansible_playbook_spec.rb
Original file line number Diff line number Diff line change
@@ -1,93 +1,105 @@
describe ServiceTemplateAnsiblePlaybook do
describe 'building_job_templates' do
let(:user) { FactoryGirl.create(:user_with_group) }
let(:job_template) do
FactoryGirl.create(:configuration_script,
:variables => catalog_item_options.fetch_path(:config_info, :provision, :extra_vars))
end
let(:auth_one) { FactoryGirl.create(:authentication, :manager_ref => 6) }
let(:auth_two) { FactoryGirl.create(:authentication, :manager_ref => 10) }
let(:user) { FactoryGirl.create(:user_with_group) }
let(:inventory_root_group) { FactoryGirl.create(:inventory_root_group) }
let(:ems) do
FactoryGirl.create(:automation_manager_ansible_tower, :inventory_root_groups => [inventory_root_group])
end
let(:config_script) { FactoryGirl.create(:configuration_script) }
let(:script_source) { FactoryGirl.create(:configuration_script_source, :manager => ems) }
let(:playbook) do
FactoryGirl.create(:configuration_script_payload,
:configuration_script_source => script_source,
:manager => ems,
:inventory_root_group => inventory_root_group,
:type => 'ManageIQ::Providers::AnsibleTower::AutomationManager::Playbook')
end
let(:service_template_catalog) { FactoryGirl.create(:service_template_catalog) }
let(:catalog_item_options) do
{
:name => 'test_ansible_catalog_item',
:description => 'test ansible',
:service_template_catalog_id => service_template_catalog.id,
:config_info => {
:provision => {
:new_dialog_name => 'test_dialog',
:hosts => 'many',
:credential_id => auth_one.id,
:network_credential_id => auth_two.id,
:playbook_id => playbook.id,
},
}
let(:user) { FactoryGirl.create(:user_with_group) }
let(:auth_one) { FactoryGirl.create(:authentication, :manager_ref => 6) }
let(:auth_two) { FactoryGirl.create(:authentication, :manager_ref => 10) }

let(:config_script) { FactoryGirl.create(:configuration_script) }
let(:script_source) { FactoryGirl.create(:configuration_script_source, :manager => ems) }

let(:inventory_root_group) { FactoryGirl.create(:inventory_root_group) }
let(:service_template_catalog) { FactoryGirl.create(:service_template_catalog) }
let(:ems) do
FactoryGirl.create(:automation_manager_ansible_tower, :inventory_root_groups => [inventory_root_group])
end

let(:playbook) do
FactoryGirl.create(:configuration_script_payload,
:configuration_script_source => script_source,
:manager => ems,
:inventory_root_group => inventory_root_group,
:type => 'ManageIQ::Providers::AnsibleTower::AutomationManager::Playbook')
end

let(:job_template) do
FactoryGirl.create(:configuration_script,
:variables => catalog_item_options.fetch_path(:config_info, :provision, :extra_vars))
end

let(:catalog_item_options) do
{
:name => 'test_ansible_catalog_item',
:description => 'test ansible',
:service_template_catalog_id => service_template_catalog.id,
:display => true,
:config_info => {
:provision => {
:new_dialog_name => 'test_dialog',
:hosts => 'many',
:credential_id => auth_one.id,
:network_credential_id => auth_two.id,
:playbook_id => playbook.id
},
}
end
}
end

let(:catalog_item_options_two) do
{
:name => 'playbook service',
:display => 'false',
:service_template_catalog_id => service_template_catalog.id,
:description => 'a description',
:config_info => {
:provision => {
:new_dialog_name => 'playbook dialog',
:playbook_id => playbook.id,
:extra_vars => {
'key1' => 'val1',
'key2' => 'val2'
}
},
:reconfigure => {
:new_dialog_name => 'playbook dialog reconfigure',
:playbook_id => 5,
},
:retirement => {
:new_dialog_name => 'playbook dialog retirement',
:playbook_id => 3,
},
}
let(:catalog_item_options_two) do
catalog_item_options.deep_merge(
:config_info => {
:provision => {
:extra_vars => {
'key1' => 'val1',
'key2' => 'val2'
}
},
:retirement => {
:credential_id => auth_one.id,
:playbook_id => 3,
},
}
end
)
end

describe 'building_job_templates' do
it '#create_job_templates' do
expect(described_class).to receive(:create_job_template).exactly(3).times.and_return(job_template)
options_hash = described_class.send(:create_job_templates, catalog_item_options_two[:name], catalog_item_options_two[:description], catalog_item_options_two[:config_info], 'system')
[:provision, :retirement, :reconfigure].each do |action|
expect(described_class).to receive(:create_job_template).exactly(2).times.and_return(job_template)
options_hash = described_class.send(:create_job_templates,
catalog_item_options_two[:name],
catalog_item_options_two[:description],
catalog_item_options_two[:config_info], 'system')
[:provision, :retirement].each do |action|
expect(options_hash[action.to_sym][:configuration_template]).to eq job_template
end
end

it '#create_job_template' do
expect(described_class).to receive(:build_parameter_list).and_return([ems, {}])
expect(ManageIQ::Providers::AnsibleTower::AutomationManager::ConfigurationScript).to receive(:create_in_provider_queue).once.with(ems.id, {}, 'system')
expect(MiqTask).to receive(:wait_for_taskid).with(any_args).once.and_return(instance_double('MiqTask', :task_results => {}, :status => 'Ok'))
expect(ManageIQ::Providers::AnsibleTower::AutomationManager::ConfigurationScript)
.to receive(:create_in_provider_queue).once.with(ems.id, {}, 'system')
expect(MiqTask).to receive(:wait_for_taskid).with(any_args).once.and_return(
instance_double('MiqTask', :task_results => {}, :status => 'Ok')
)

described_class.send(:create_job_template, catalog_item_options[:name], catalog_item_options[:description], catalog_item_options[:config_info], 'system')
described_class.send(:create_job_template,
catalog_item_options[:name],
catalog_item_options[:description],
catalog_item_options[:config_info],
'system')
end

it 'create_job_template exception' do
expect(described_class).to receive(:build_parameter_list).and_return([ems, {}])
expect(ManageIQ::Providers::AnsibleTower::AutomationManager::ConfigurationScript).to receive(:create_in_provider_queue).once.with(ems.id, {}, 'system')
expect(ManageIQ::Providers::AnsibleTower::AutomationManager::ConfigurationScript)
.to receive(:create_in_provider_queue).once.with(ems.id, {}, 'system')
expect(MiqTask).to receive(:wait_for_taskid).with(any_args).once.and_raise(Exception, 'bad job template')

expect { described_class.send(:create_job_template, catalog_item_options[:name], catalog_item_options[:description], catalog_item_options[:config_info], 'system') }.to raise_error(Exception)
expect do
described_class.send(:create_job_template,
catalog_item_options[:name],
catalog_item_options[:description],
catalog_item_options[:config_info],
'system')
end.to raise_error(Exception)
end

it '#build_parameter_list' do
Expand All @@ -96,7 +108,10 @@
description = catalog_item_options[:description]
info = catalog_item_options[:config_info][:provision]
_tower, params = described_class.send(:build_parameter_list, name, description, info)
_tower_two, params_two = described_class.send(:build_parameter_list, catalog_extra_vars[:name], catalog_extra_vars[:description], catalog_extra_vars[:config_info][:provision])
_tower_two, params_two = described_class.send(:build_parameter_list,
catalog_extra_vars[:name],
catalog_extra_vars[:description],
catalog_extra_vars[:config_info][:provision])

expect(params).to have_attributes(
:name => name,
Expand All @@ -113,4 +128,22 @@
)
end
end

describe '#create_catalog_item' do
it 'creates and returns a catalog item' do
expect(described_class)
.to receive(:create_job_templates).and_return(:provision => {:configuration_template => job_template})
service_template = described_class.create_catalog_item(catalog_item_options_two, user)

expect(service_template.name).to eq(catalog_item_options_two[:name])
expect(service_template.config_info[:provision]).to include(catalog_item_options[:config_info][:provision])
expect(service_template.dialogs.first.name)
.to eq(catalog_item_options.fetch_path(:config_info, :provision, :new_dialog_name))
expect(service_template.resource_actions.first).to have_attributes(
:action => 'Provision',
:fqname => described_class.default_provisioning_entry_point('atomic'),
:configuration_template => job_template
)
end
end
end