Skip to content

Commit

Permalink
initial support for migration plan and request
Browse files Browse the repository at this point in the history
  • Loading branch information
bzwei committed Feb 6, 2018
1 parent 03f19ed commit 34cb4fd
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 2 deletions.
33 changes: 33 additions & 0 deletions app/models/migration_plan_provision_request.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class MigrationPlanProvisionRequest < ServiceTemplateProvisionRequest
TASK_DESCRIPTION = 'VM Migrations'.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/migration_plan_provision_task.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class MigrationPlanProvisionTask < ServiceTemplateProvisionTask
def self.base_model
MigrationPlanProvisionTask
end

def after_request_task_create
update_attributes(:description => "Migrating VM #{source.name}")
end

def resource_action
miq_request.source.resource_actions.detect { |ra| ra.action == 'Provision' }
end

def migration_destination(source_obj)
miq_request.infra_mapping.find_by(:source => source_obj).destination
end

def update_migration_progress(progress)
options[:progress] = (options[:progress]||{}).merge(progress)
save
end
end

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")
}
},
:MigrationPlan => {
:MigrationPlanProvisionRequest => {
:migration_plan => N_("Migration Plan")
}
}
}.freeze

Expand Down
13 changes: 13 additions & 0 deletions app/models/service_template_migration_plan.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class ServiceTemplateMigrationPlan < 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
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

0 comments on commit 34cb4fd

Please sign in to comment.