Skip to content

Commit

Permalink
Support for v2v pre/post Ansible playbook service.
Browse files Browse the repository at this point in the history
  • Loading branch information
lfu committed Jul 3, 2018
1 parent 178f498 commit 11a51c0
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 12 deletions.
30 changes: 23 additions & 7 deletions app/models/service_template_transformation_plan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ def self.default_reconfiguration_entry_point
# :description
# :config_info
# :transformation_mapping_id
# :vm_ids
# :pre_service_id
# :post_service_id
# :actions => [
# {:vm_id => 1, :pre_service => true, :post_service => false},
# {:vm_id => 2, :pre_service => true, :post_service => true},
# ]
#
def self.create_catalog_item(options, _auth_user = nil)
enhanced_config_info = validate_config_info(options)
Expand All @@ -51,7 +56,7 @@ def self.create_catalog_item(options, _auth_user = nil)
transaction do
create_from_options(options.merge(default_options)).tap do |service_template|
service_template.add_resource(enhanced_config_info[:transformation_mapping])
enhanced_config_info[:vms].each { |vm| service_template.add_resource(vm, :status => ServiceResource::STATUS_QUEUED) }
enhanced_config_info[:vms].each { |vm_hash| service_template.add_resource(vm_hash[:vm], :status => ServiceResource::STATUS_QUEUED, :options => vm_hash[:options]) }
service_template.create_resource_actions(enhanced_config_info)
end
end
Expand All @@ -73,11 +78,22 @@ def self.validate_config_info(options)

raise _('Must provide an existing transformation mapping') if mapping.blank?

vms = if config_info[:vm_ids]
VmOrTemplate.find(config_info[:vm_ids])
else
config_info[:vms]
end
pre_service_id = config_info[:pre_service].try(:id) || config_info[:pre_service_id]
post_service_id = config_info[:post_service].try(:id) || config_info[:post_service_id]

vms = []
if config_info[:actions]
vm_objects = VmOrTemplate.where(:id => config_info[:actions].collect { |vm_hash| vm_hash[:vm_id] }.compact).group_by(&:id)
config_info[:actions].each do |vm_hash|
vm_obj = vm_objects[vm_hash[:vm_id]].try(:first) || vm_hash[:vm]
next if vm_obj.nil?

vm_options = {}
vm_options[:pre_ansible_playbook_service_template_id] = pre_service_id if vm_hash[:pre_service]
vm_options[:post_ansible_playbook_service_template_id] = post_service_id if vm_hash[:post_service]
vms << {:vm => vm_obj, :options => vm_options}
end
end

raise _('Must select a list of valid vms') if vms.blank?

Expand Down
8 changes: 8 additions & 0 deletions app/models/service_template_transformation_plan_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ def transformation_destination(source_obj)
miq_request.transformation_mapping.destination(source_obj)
end

def pre_ansible_playbook_service_template
ServiceTemplate.find_by(:id => vm_resource.options["pre_ansible_playbook_service_template_id"])
end

def post_ansible_playbook_service_template
ServiceTemplate.find_by(:id => vm_resource.options["post_ansible_playbook_service_template_id"])
end

def update_transformation_progress(progress)
options[:progress] = (options[:progress] || {}).merge(progress)
save
Expand Down
14 changes: 12 additions & 2 deletions spec/models/service_template_transformation_plan_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
end

let(:transformation_mapping) { FactoryGirl.create(:transformation_mapping) }
let(:apst) { FactoryGirl.create(:service_template_ansible_playbook) }
let(:vm1) { FactoryGirl.create(:vm_or_template) }
let(:vm2) { FactoryGirl.create(:vm_or_template) }

Expand All @@ -26,7 +27,12 @@
:description => 'a description',
:config_info => {
:transformation_mapping_id => transformation_mapping.id,
:vm_ids => [vm1.id, vm2.id],
:pre_service_id => apst.id,
:post_service_id => apst.id,
:actions => [
{:vm_id => vm1.id, :pre_service => true, :post_service => false},
{:vm_id => vm2.id, :pre_service => true, :post_service => true}
],
}
}
end
Expand All @@ -39,6 +45,10 @@
expect(service_template.transformation_mapping).to eq(transformation_mapping)
expect(service_template.vm_resources.collect(&:resource)).to match_array([vm1, vm2])
expect(service_template.vm_resources.collect(&:status)).to eq([ServiceResource::STATUS_QUEUED, ServiceResource::STATUS_QUEUED])
expect(service_template.vm_resources.find_by(:resource_id => vm1.id).options)
.to eq("pre_ansible_playbook_service_template_id" => apst.id)
expect(service_template.vm_resources.find_by(:resource_id => vm2.id).options)
.to eq("pre_ansible_playbook_service_template_id" => apst.id, "post_ansible_playbook_service_template_id" => apst.id)
expect(service_template.config_info).to eq(catalog_item_options[:config_info])
expect(service_template.resource_actions.first).to have_attributes(
:action => 'Provision',
Expand All @@ -55,7 +65,7 @@
end

it 'requires selected vms' do
catalog_item_options[:config_info].delete(:vm_ids)
catalog_item_options[:config_info].delete(:actions)

expect do
described_class.create_catalog_item(catalog_item_options)
Expand Down
24 changes: 21 additions & 3 deletions spec/models/service_template_transformation_plan_task_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
let(:src) { FactoryGirl.create(:ems_cluster) }
let(:dst) { FactoryGirl.create(:ems_cluster) }
let(:vm) { FactoryGirl.create(:vm_or_template) }
let(:vm2) { FactoryGirl.create(:vm_or_template) }
let(:apst) { FactoryGirl.create(:service_template_ansible_playbook) }
let(:mapping) do
FactoryGirl.create(
:transformation_mapping,
Expand All @@ -29,7 +31,12 @@
:description => 'a description',
:config_info => {
:transformation_mapping_id => mapping.id,
:vm_ids => [vm.id],
:pre_service_id => apst.id,
:post_service_id => apst.id,
:tasks => [
{:vm_id => vm.id, :pre_service => true, :post_service => true},
{:vm_id => vm2.id, :pre_service => false, :post_service => false},
],
}
}
end
Expand All @@ -38,6 +45,7 @@

let(:request) { FactoryGirl.create(:service_template_transformation_plan_request, :source => plan) }
let(:task) { FactoryGirl.create(:service_template_transformation_plan_task, :miq_request => request, :request_type => 'transformation_plan', :source => vm) }
let(:task2) { FactoryGirl.create(:service_template_transformation_plan_task, :miq_request => request, :request_type => 'transformation_plan', :source => vm2) }

describe '#resource_action' do
it 'has a resource action points to the entry point for transformation' do
Expand All @@ -52,6 +60,16 @@
it { expect(task.transformation_destination(src)).to eq(dst) }
end

describe '#pre_ansible_playbook_service_template' do
it { expect(task.pre_ansible_playbook_service_template).to eq(apst) }
it { expect(task2.pre_ansible_playbook_service_template).to be_nil }
end

describe '#post_ansible_playbook_service_template' do
it { expect(task.post_ansible_playbook_service_template).to eq(apst) }
it { expect(task2.post_ansible_playbook_service_template).to be_nil }
end

describe '#update_transformation_progress' do
it 'saves the progress in options' do
task.update_transformation_progress(:vm_percent => '80')
Expand All @@ -62,14 +80,14 @@
describe 'task_active' do
it 'sets vm_request status to Started' do
task.task_active
expect(plan.vm_resources.first.status).to eq(ServiceResource::STATUS_ACTIVE)
expect(plan.vm_resources.find_by(:resource => task.source).status).to eq(ServiceResource::STATUS_ACTIVE)
end
end

describe 'task_finished' do
it 'sets vm_request status to Completed' do
task.task_finished
expect(plan.vm_resources.first.status).to eq(ServiceResource::STATUS_COMPLETED)
expect(plan.vm_resources.find_by(:resource => task.source).status).to eq(ServiceResource::STATUS_COMPLETED)
end
end

Expand Down

0 comments on commit 11a51c0

Please sign in to comment.