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 Jun 22, 2018
1 parent adb2759 commit 49486b1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
26 changes: 20 additions & 6 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
# :tasks => [
# {: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,12 +56,22 @@ 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| service_template.add_vm_resource(vm) }
service_template.create_resource_actions(enhanced_config_info)
end
end
end

def add_vm_resource(vm)
config_info = options[:config_info]
vm_options = config_info[:tasks].detect { |v| v[:vm_id] == vm.id }

resource_options = {}
resource_options[:pre_ansible_playbook_service_template_id] = config_info[:pre_service_id] if vm_options[:pre_service]
resource_options[:post_ansible_playbook_service_template_id] = config_info[:post_service_id] if vm_options[:post_service]
add_resource(vm, :status => ServiceResource::STATUS_QUEUED, :options => resource_options)
end

private

def enforce_single_service_parent(_resource)
Expand All @@ -73,10 +88,9 @@ 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]
vms = if config_info[:tasks]
vm_ids = config_info[:tasks].collect { |vm| vm[:vm_id] }
VmOrTemplate.find(vm_ids)
end

raise _('Must select a list of valid vms') if vms.blank?
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,
:tasks => [
{: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(:tasks)

expect do
described_class.create_catalog_item(catalog_item_options)
Expand Down

0 comments on commit 49486b1

Please sign in to comment.