Skip to content

Commit

Permalink
initial support for transformation plan and request
Browse files Browse the repository at this point in the history
  • Loading branch information
bzwei committed Feb 7, 2018
1 parent 4f06547 commit 90f64b9
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 2 deletions.
5 changes: 5 additions & 0 deletions app/models/miq_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ class MiqRequest < ApplicationRecord
:AutomationRequest => {
:automation => N_("Automation")
}
},
:Transformation => {
:ServiceTemplateTransformationPlanRequest => {
:transformation_plan => N_("Transformation Plan")
}
}
}.freeze

Expand Down
8 changes: 6 additions & 2 deletions app/models/service_template_provision_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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
Expand All @@ -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])
Expand Down
13 changes: 13 additions & 0 deletions app/models/service_template_transformation_plan.rb
Original file line number Diff line number Diff line change
@@ -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
33 changes: 33 additions & 0 deletions app/models/service_template_transformation_plan_request.rb
Original file line number Diff line number Diff line change
@@ -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
23 changes: 23 additions & 0 deletions app/models/service_template_transformation_plan_task.rb
Original file line number Diff line number Diff line change
@@ -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

4 changes: 4 additions & 0 deletions spec/factories/miq_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions spec/factories/miq_request_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions spec/factories/service_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 90f64b9

Please sign in to comment.