diff --git a/app/models/miq_request.rb b/app/models/miq_request.rb index 30e24b099283..e5a8c5fa4e67 100644 --- a/app/models/miq_request.rb +++ b/app/models/miq_request.rb @@ -96,6 +96,11 @@ class MiqRequest < ApplicationRecord :AutomationRequest => { :automation => N_("Automation") } + }, + :Transformation => { + :ServiceTemplateTransformationPlanRequest => { + :transformation_plan => N_("Transformation Plan") + } } }.freeze diff --git a/app/models/service_template_provision_task.rb b/app/models/service_template_provision_task.rb index c2961cfe38dd..14b172c9fa3d 100644 --- a/app/models/service_template_provision_task.rb +++ b/app/models/service_template_provision_task.rb @@ -128,7 +128,7 @@ def deliver_to_automate(req_type = request_type, _zone = nil) } # Automate entry point overrides from the resource_action - ra = source.resource_actions.detect { |ra| ra.action == 'Provision' } if source.respond_to?(:resource_actions) + ra = resource_action unless ra.nil? args[:namespace] = ra.ae_namespace unless ra.ae_namespace.blank? @@ -138,7 +138,7 @@ def deliver_to_automate(req_type = request_type, _zone = nil) args[:attrs].merge!(ra.ae_attributes) end - args[:attrs].merge!(MiqAeEngine.create_automation_attributes(destination.class.base_model.name => destination)) + args[:attrs].merge!(MiqAeEngine.create_automation_attributes(destination.class.base_model.name => destination)) unless destination.nil? args[:user_id] = get_user.id args[:miq_group_id] = get_user.current_group.id args[:tenant_id] = get_user.current_tenant.id @@ -158,6 +158,10 @@ def deliver_to_automate(req_type = request_type, _zone = nil) end end + def resource_action + source.resource_actions.detect { |ra| ra.action == 'Provision' } if source.respond_to?(:resource_actions) + end + def service_resource return nil if options[:service_resource_id].blank? ServiceResource.find_by(:id => options[:service_resource_id]) diff --git a/app/models/service_template_transformation_plan.rb b/app/models/service_template_transformation_plan.rb new file mode 100644 index 000000000000..9d7c529c1182 --- /dev/null +++ b/app/models/service_template_transformation_plan.rb @@ -0,0 +1,13 @@ +class ServiceTemplateTransformationPlan < ServiceTemplate + def add_vms(vm_ids) + vm_ids.each { |vm_id| add_vm(vm_id) } + save! + end + + def add_vm(vm_id) + sr = service_resources.detect { |r| r.resource_type == 'VmOrTemplate' && r.resource_id == vm_id } + return unless sr.nil? + + service_resources.new(:resource_type => 'VmOrTemplate', :resource_id => vm_id, :status => 'Queued') + end +end diff --git a/app/models/service_template_transformation_plan_request.rb b/app/models/service_template_transformation_plan_request.rb new file mode 100644 index 000000000000..9a5c00303c18 --- /dev/null +++ b/app/models/service_template_transformation_plan_request.rb @@ -0,0 +1,33 @@ +class ServiceTemplateTransformationPlanRequest < ServiceTemplateProvisionRequest + TASK_DESCRIPTION = 'VM Transformations'.freeze + + 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 infra_mapping + source.service_resources.find_by(:resource_type => 'TransformationMapping') + end + + def source_vms + vm_requests.where(:status => %w(Queued Failed)).pluck(:resource_id) + end + + def validate_vm(vm_id) + true + end + + def approve_vm(vm_id) + vm_requests.find_by(:resource_id => vm_id).update_attributes!(:status => 'Approved') + end + + private + + def vm_requests + source.service_resources.where(:resource_type => 'VmOrTemplate') + end +end diff --git a/app/models/service_template_transformation_plan_task.rb b/app/models/service_template_transformation_plan_task.rb new file mode 100644 index 000000000000..9547c0b3f658 --- /dev/null +++ b/app/models/service_template_transformation_plan_task.rb @@ -0,0 +1,23 @@ +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.infra_mapping.destination(source_obj) + end + + def update_transformation_progress(progress) + options[:progress] = (options[:progress]||{}).merge(progress) + save + end +end + diff --git a/spec/factories/miq_request.rb b/spec/factories/miq_request.rb index 5971cacf8d1f..97cf5d72dce7 100644 --- a/spec/factories/miq_request.rb +++ b/spec/factories/miq_request.rb @@ -12,5 +12,9 @@ factory :miq_provision_request, :class => "MiqProvisionRequest" do source { create(:miq_template) } end + + factory :service_template_transformation_plan_request, :class => "ServiceTemplateTransformationPlanRequest" do + source { create(:service_template_transformation_plan) } + end end end diff --git a/spec/factories/miq_request_task.rb b/spec/factories/miq_request_task.rb index 7601a549bd42..ff02e830d9ee 100644 --- a/spec/factories/miq_request_task.rb +++ b/spec/factories/miq_request_task.rb @@ -40,4 +40,6 @@ state 'pending' request_type 'clone_to_service' end + + factory :service_template_transformation_plan_task, :parent => :service_template_provision_task, :class => 'ServiceTemplateTransformationPlanTask' end diff --git a/spec/factories/service_template.rb b/spec/factories/service_template.rb index a9a4dea1f112..ec6f42302226 100644 --- a/spec/factories/service_template.rb +++ b/spec/factories/service_template.rb @@ -3,4 +3,5 @@ factory :service_template_orchestration, :class => 'ServiceTemplateOrchestration', :parent => :service_template factory :service_template_ansible_playbook, :class => 'ServiceTemplateAnsiblePlaybook', :parent => :service_template factory :service_template_container_template, :class => 'ServiceTemplateContainerTemplate', :parent => :service_template + factory :service_template_transformation_plan, :class => 'ServiceTemplateTransformationPlan', :parent => :service_template end