-
Notifications
You must be signed in to change notification settings - Fork 897
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 #16960 from bzwei/v2v_service_template
Transformation Plan, Request, and Task for V2V
- Loading branch information
Showing
14 changed files
with
337 additions
and
20 deletions.
There are no files selected for viewing
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
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
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
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
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,90 @@ | ||
class ServiceTemplateTransformationPlan < ServiceTemplate | ||
def request_class | ||
ServiceTemplateTransformationPlanRequest | ||
end | ||
|
||
def request_type | ||
"transformation_plan" | ||
end | ||
|
||
def transformation_mapping | ||
service_resources.find_by(:resource_type => 'TransformationMapping').resource | ||
end | ||
|
||
def vm_requests | ||
service_resources.where(:resource_type => 'VmOrTemplate') | ||
end | ||
|
||
def validate_order | ||
true | ||
end | ||
|
||
def self.default_provisioning_entry_point(_service_type) | ||
'/Transformation/StateMachines/VMTransformation/Transformation' | ||
end | ||
|
||
def self.default_retirement_entry_point | ||
nil | ||
end | ||
|
||
def self.default_reconfiguration_entry_point | ||
nil | ||
end | ||
|
||
# create ServiceTemplate and supporting ServiceResources and ResourceActions | ||
# options | ||
# :name | ||
# :description | ||
# :config_info | ||
# :transformation_mapping_id | ||
# :vm_ids | ||
# | ||
def self.create_catalog_item(options, _auth_user = nil) | ||
enhanced_config_info = validate_config_info(options) | ||
default_options = { | ||
:display => false, | ||
:service_type => 'atomic', | ||
:prov_type => 'transformation_plan' | ||
} | ||
|
||
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 => 'Queued') } | ||
service_template.create_resource_actions(enhanced_config_info) | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def enforce_single_service_parent(_resource) | ||
end | ||
|
||
def self.validate_config_info(options) | ||
config_info = options[:config_info] | ||
|
||
mapping = if config_info[:transformation_mapping_id] | ||
TransformationMapping.find(config_info[:transformation_mapping_id]) | ||
else | ||
config_info[:transformation_mapping] | ||
end | ||
|
||
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 | ||
|
||
raise _('Must select a list of valid vms') if vms.blank? | ||
|
||
{ | ||
:transformation_mapping => mapping, | ||
:vms => vms, | ||
:provision => config_info[:provision] || {} | ||
} | ||
end | ||
private_class_method :validate_config_info | ||
end |
26 changes: 26 additions & 0 deletions
26
app/models/service_template_transformation_plan_request.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,26 @@ | ||
class ServiceTemplateTransformationPlanRequest < ServiceTemplateProvisionRequest | ||
TASK_DESCRIPTION = 'VM Transformations'.freeze | ||
|
||
delegate :transformation_mapping, :vm_requests, :to => :source | ||
|
||
def requested_task_idx | ||
vm_requests.where(:status => 'Approved') | ||
end | ||
|
||
def customize_request_task_attributes(req_task_attrs, vm_request) | ||
req_task_attrs[:source] = vm_request.resource | ||
end | ||
|
||
def source_vms | ||
vm_requests.where(:status => %w(Queued Failed)).pluck(:resource_id) | ||
end | ||
|
||
def validate_vm(_vm_id) | ||
# TODO: enhance the logic to determine whether this VM can be included in this request | ||
true | ||
end | ||
|
||
def approve_vm(vm_id) | ||
vm_requests.find_by(:resource_id => vm_id).update_attributes!(:status => 'Approved') | ||
end | ||
end |
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,22 @@ | ||
class ServiceTemplateTransformationPlanTask < ServiceTemplateProvisionTask | ||
def self.base_model | ||
ServiceTemplateTransformationPlanTask | ||
end | ||
|
||
def after_request_task_create | ||
update_attributes(:description => "Transforming VM #{source.name}") | ||
end | ||
|
||
def resource_action | ||
miq_request.source.resource_actions.detect { |ra| ra.action == 'Provision' } | ||
end | ||
|
||
def transformation_destination(source_obj) | ||
miq_request.transformation_mapping.destination(source_obj) | ||
end | ||
|
||
def update_transformation_progress(progress) | ||
options[:progress] = (options[:progress] || {}).merge(progress) | ||
save | ||
end | ||
end |
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
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
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
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
41 changes: 41 additions & 0 deletions
41
spec/models/service_template_transformation_plan_request_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,41 @@ | ||
describe ServiceTemplateTransformationPlanRequest do | ||
let(:vms) { Array.new(3) { FactoryGirl.create(:vm_or_template) } } | ||
let(:vm_requests) do | ||
%w(Queued Failed Approved).zip(vms).collect do |status, vm| | ||
ServiceResource.new(:resource => vm, :status => status) | ||
end | ||
end | ||
let(:plan) { FactoryGirl.create(:service_template_transformation_plan, :service_resources => vm_requests) } | ||
let(:request) { FactoryGirl.create(:service_template_transformation_plan_request, :source => plan) } | ||
|
||
describe '#requested_task_idx' do | ||
it 'selects approved vm requests' do | ||
expect(request.requested_task_idx.first).to have_attributes(:resource => vms[2], :status => 'Approved') | ||
end | ||
end | ||
|
||
describe 'customize_request_task_attributes' do | ||
it 'sets the source option to be the vm in the vm_request' do | ||
req_task_attrs = {} | ||
request.customize_request_task_attributes(req_task_attrs, vm_requests[0]) | ||
expect(req_task_attrs[:source]).to eq(vms[0]) | ||
end | ||
end | ||
|
||
describe '#source_vms' do | ||
it 'selects queued and failed vm in the request' do | ||
expect(request.source_vms).to match_array([vms[0].id, vms[1].id]) | ||
end | ||
end | ||
|
||
describe '#validate_vm' do | ||
it { expect(request.validate_vm(vms[0].id)).to be_truthy } | ||
end | ||
|
||
describe '#approve_vm' do | ||
it 'turns the status to Approved' do | ||
request.approve_vm(vms[0].id) | ||
expect(ServiceResource.find_by(:resource => vms[0]).status).to eq('Approved') | ||
end | ||
end | ||
end |
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,64 @@ | ||
describe ServiceTemplateTransformationPlan do | ||
subject { FactoryGirl.create(:service_template_transformation_plan) } | ||
|
||
describe '#request_class' do | ||
it { expect(subject.request_class).to eq(ServiceTemplateTransformationPlanRequest) } | ||
end | ||
|
||
describe '#request_type' do | ||
it { expect(subject.request_type).to eq("transformation_plan") } | ||
end | ||
|
||
describe '#validate_order' do | ||
it 'always allows a plan to be ordered' do | ||
expect(subject.validate_order).to be_truthy | ||
end | ||
end | ||
|
||
let(:transformation_mapping) { FactoryGirl.create(:transformation_mapping) } | ||
let(:vm1) { FactoryGirl.create(:vm_or_template) } | ||
let(:vm2) { FactoryGirl.create(:vm_or_template) } | ||
|
||
let(:catalog_item_options) do | ||
{ | ||
:name => 'Transformation Plan', | ||
:description => 'a description', | ||
:config_info => { | ||
:transformation_mapping_id => transformation_mapping.id, | ||
:vm_ids => [vm1.id, vm2.id], | ||
} | ||
} | ||
end | ||
|
||
describe '.create_catalog_item' do | ||
it 'creates and returns a transformation plan' do | ||
service_template = described_class.create_catalog_item(catalog_item_options) | ||
|
||
expect(service_template.name).to eq('Transformation Plan') | ||
expect(service_template.transformation_mapping).to eq(transformation_mapping) | ||
expect(service_template.vm_requests.collect(&:resource)).to match_array([vm1, vm2]) | ||
expect(service_template.vm_requests.collect(&:status)).to eq(%w(Queued Queued)) | ||
expect(service_template.config_info).to eq(catalog_item_options[:config_info]) | ||
expect(service_template.resource_actions.first).to have_attributes( | ||
:action => 'Provision', | ||
:fqname => described_class.default_provisioning_entry_point(nil) | ||
) | ||
end | ||
|
||
it 'requires a transformation mapping' do | ||
catalog_item_options[:config_info].delete(:transformation_mapping_id) | ||
|
||
expect do | ||
described_class.create_catalog_item(catalog_item_options) | ||
end.to raise_error(StandardError, 'Must provide an existing transformation mapping') | ||
end | ||
|
||
it 'requires selected vms' do | ||
catalog_item_options[:config_info].delete(:vm_ids) | ||
|
||
expect do | ||
described_class.create_catalog_item(catalog_item_options) | ||
end.to raise_error(StandardError, 'Must select a list of valid vms') | ||
end | ||
end | ||
end |
Oops, something went wrong.