Skip to content

Commit

Permalink
Merge pull request ManageIQ#13822 from jntullo/enhancement/read_catal…
Browse files Browse the repository at this point in the history
…og_item

Save and Return configuration information of a ServiceTemplate
  • Loading branch information
gmcculloug authored Feb 9, 2017
2 parents 956679a + 9221c11 commit 9b76d72
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 3 deletions.
35 changes: 34 additions & 1 deletion app/models/service_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ class ServiceTemplate < ApplicationRecord

virtual_has_one :custom_actions, :class_name => "Hash"
virtual_has_one :custom_action_buttons, :class_name => "Array"
virtual_has_one :config_info, :class_name => "Hash"

def self.create_catalog_item(options, auth_user)
transaction do
create(options.except(:config_info)).tap do |service_template|
create_from_options(options).tap do |service_template|
config_info = options[:config_info].except(:provision, :retirement, :reconfigure)

workflow_class = MiqProvisionWorkflow.class_for_source(config_info[:src_vm_id])
Expand Down Expand Up @@ -139,6 +140,10 @@ def request_type
"clone_to_service"
end

def config_info
options[:config_info] || construct_config_info
end

def create_service(service_task, parent_svc = nil)
nh = attributes.dup
nh['options'][:dialog] = service_task.options[:dialog]
Expand Down Expand Up @@ -356,4 +361,32 @@ def create_resource_actions(ae_endpoints)
end
save!
end

def self.create_from_options(options)
create(options.except(:config_info).merge(:options => { :config_info => options[:config_info] }))
end
private_class_method :create_from_options

private

def construct_config_info
config_info = {}
if service_resources.where(:resource_type => 'MiqRequest').exists?
config_info.merge!(service_resources.find_by(:resource_type => 'MiqRequest').resource.options.compact)
end

config_info.merge!(resource_actions_info)
end

def resource_actions_info
config_info = {}
resource_actions.each do |resource_action|
resource_options = resource_action.slice(:dialog_id,
:configuration_template_type,
:configuration_template_id).compact
resource_options[:fqname] = resource_action.fqname
config_info.merge!(resource_action.action.downcase.to_sym => resource_options.symbolize_keys)
end
config_info
end
end
10 changes: 9 additions & 1 deletion app/models/service_template_ansible_tower.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class ServiceTemplateAnsibleTower < ServiceTemplate

def self.create_catalog_item(options, _auth_user = nil)
transaction do
create(options.except(:config_info)) do |service_template|
create_from_options(options).tap do |service_template|
config_info = validate_config_info(options)

service_template.job_template = if config_info[:configuration_script_id]
Expand Down Expand Up @@ -52,4 +52,12 @@ def self.validate_config_info(options)
config_info
end
private_class_method :validate_config_info

private

def construct_config_info
config_info = {}
config_info[:configuration_script_id] = job_template.id if job_template
config_info.merge!(resource_actions_info)
end
end
13 changes: 12 additions & 1 deletion app/models/service_template_orchestration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ServiceTemplateOrchestration < ServiceTemplate

def self.create_catalog_item(options, _auth_user = nil)
transaction do
create(options.except(:config_info)).tap do |service_template|
create_from_options(options).tap do |service_template|
config_info = validate_config_info(options)

service_template.orchestration_template = if config_info[:template_id]
Expand Down Expand Up @@ -54,4 +54,15 @@ def self.validate_config_info(options)
config_info
end
private_class_method :validate_config_info

private

def construct_config_info
config_info = {}

config_info[:template_id] = orchestration_template.id if orchestration_template
config_info[:manager_id] = orchestration_manager.id if orchestration_manager

config_info.merge!(resource_actions_info)
end
end
18 changes: 18 additions & 0 deletions spec/models/service_template_ansible_tower_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
expect(service_template.dialogs.first).to eq(service_dialog)
expect(service_template.resource_actions.pluck(:action)).to include('Provision', 'Retirement')
expect(service_template.job_template).to eq(configuration_script)
expect(service_template.config_info).to eq(catalog_item_options[:config_info])
end

it 'validates the presence of a configuration_script_id or configuration' do
Expand All @@ -51,4 +52,21 @@
expect(service_template.job_template).to eq(configuration_script)
end
end

describe '#config_info' do
it 'returns the correct format' do
job_template = FactoryGirl.create(:configuration_script)
service_template = FactoryGirl.create(:service_template_ansible_tower, :job_template => job_template)
ra = FactoryGirl.create(:resource_action, :action => 'Provision', :fqname => '/a/b/c')
service_template.create_resource_actions(:provision => { :fqname => ra.fqname })

expected_config_info = {
:configuration_script_id => job_template.id,
:provision => {
:fqname => ra.fqname
}
}
expect(service_template.config_info).to eq(expected_config_info)
end
end
end
22 changes: 22 additions & 0 deletions spec/models/service_template_orchestration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@
expect(service_template.orchestration_template).to eq(template)
expect(service_template.orchestration_manager).to eq(manager)
expect(service_template.resource_actions.pluck(:action)).to include('Provision', 'Retirement')
expect(service_template.config_info).to eq(catalog_item_options[:config_info])
end

it 'requires both a template_id and manager_id' do
Expand All @@ -164,4 +165,25 @@
expect(service_template.orchestration_manager).to eq(manager)
end
end

describe '#config_info' do
it 'returns the correct format' do
template = FactoryGirl.create(:orchestration_template)
manager = FactoryGirl.create(:ext_management_system)
service_template = FactoryGirl.create(:service_template_orchestration,
:orchestration_template => template,
:orchestration_manager => manager)
ra = FactoryGirl.create(:resource_action, :action => 'Provision', :fqname => '/a/b/c')
service_template.create_resource_actions(:provision => { :fqname => ra.fqname })

expected_config_info = {
:template_id => template.id,
:manager_id => manager.id,
:provision => {
:fqname => ra.fqname
}
}
expect(service_template.config_info).to eq(expected_config_info)
end
end
end
45 changes: 45 additions & 0 deletions spec/models/service_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,50 @@
end
end

describe '#config_info' do
before do
@user = FactoryGirl.create(:user_with_group)
@ra = FactoryGirl.create(:resource_action, :action => 'Provision', :fqname => '/a/b/c')
end

it 'returns the config_info passed to #create_catalog_item' do
options = {
:name => 'foo',
:config_info => {
:provision => {
:fqname => @ra.fqname
},
:retirement => {
:fqname => @ra.fqname
}
}
}

template = ServiceTemplate.create_catalog_item(options, @user)
expect(template.config_info).to eq(options[:config_info])
end

it 'will build the config_info if not created through #create_catalog_item' do
dialog = FactoryGirl.create(:dialog)
template = FactoryGirl.create(:service_template)
request = FactoryGirl.create(:service_template_provision_request,
:requester => @user,
:options => {:foo => 'bar', :baz => nil })
template.create_resource_actions(:provision => { :fqname => @ra.fqname, :dialog_id => dialog.id })
add_and_save_service(template, request)
template.reload

expected_config_info = {
:foo => 'bar',
:provision => {
:fqname => '/a/b/c',
:dialog_id => dialog.id
}
}
expect(template.config_info).to eq(expected_config_info)
end
end

describe '#create_catalog_item' do
let(:user) { FactoryGirl.create(:user_with_group) }
let(:ra1) { FactoryGirl.create(:resource_action, :action => 'Provision') }
Expand Down Expand Up @@ -486,6 +530,7 @@
expect(service_template.resource_actions.pluck(:ae_attributes)).to include({:service_action=>"Provision"}, {:service_action=>"Retirement"})
expect(service_template.resource_actions.first.dialog).to eq(service_dialog)
expect(service_template.resource_actions.last.dialog).to eq(service_dialog)
expect(service_template.config_info).to eq(catalog_item_options[:config_info])
end
end
end
Expand Down

0 comments on commit 9b76d72

Please sign in to comment.