Skip to content

Commit

Permalink
Support moving a VM to another folder during VM Migrate.
Browse files Browse the repository at this point in the history
  • Loading branch information
lfu committed Jun 8, 2018
1 parent edd6a39 commit e9f0e98
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 12 deletions.
17 changes: 12 additions & 5 deletions app/models/vm_migrate_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ def self.get_description(req_obj)
new_settings << "Host: #{host_name}" unless host_name.blank?
respool_name = req_obj.get_option_last(:placement_rp_name)
new_settings << "Resource Pool: #{respool_name}" unless respool_name.blank?
folder_name = req_obj.get_option_last(:placement_folder_name)
new_settings << "Folder: #{folder_name}" if folder_name.present?
storage = req_obj.get_option_last(:placement_ds_name)
new_settings << "Storage: #{storage}" unless storage.blank?
"#{request_class::TASK_DESCRIPTION} for: #{name} - #{new_settings.join(", ")}"
Expand All @@ -44,6 +46,9 @@ def do_request
respool_id = get_option(:placement_rp_name)
respool = ResourcePool.find_by(:id => respool_id)

folder_id = get_option(:placement_folder_name)
folder = EmsFolder.find_by(:id => folder_id)

datastore_id = get_option(:placement_ds_name)
datastore = Storage.find_by(:id => datastore_id)

Expand All @@ -57,7 +62,7 @@ def do_request
:relocate
elsif respool && host.nil?
:relocate
else
elsif host
:migrate
end

Expand All @@ -66,18 +71,20 @@ def do_request
begin
if vc_method == :migrate
vm.migrate(host, respool)
else
elsif vc_method == :relocate
vm.relocate(host, respool, datastore, nil, disk_transform)
end

folder.try(:move_into_folder, vm) if folder.present?
rescue => err
update_and_notify_parent(:state => 'finished', :status => 'error', :message => "Failed. Reason[#{err.message}]")
update_and_notify_parent(:state => 'finished', :status => 'Error', :message => "Failed. Reason[#{err.message}]")
return
end

if AUTOMATE_DRIVES
update_and_notify_parent(:state => 'migrated', :message => "Finished #{request_class::TASK_DESCRIPTION}")
update_and_notify_parent(:state => 'migrated', :message => "Finished #{request_class::TASK_DESCRIPTION}", :status => 'Ok')
else
update_and_notify_parent(:state => 'finished', :message => "#{request_class::TASK_DESCRIPTION} complete")
update_and_notify_parent(:state => 'finished', :message => "#{request_class::TASK_DESCRIPTION} complete", :status => 'Ok')
end
end
end
8 changes: 8 additions & 0 deletions product/dialogs/miq_dialogs/vm_migrate_dialogs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,14 @@
:required: false
:display: :edit
:data_type: :integer
:placement_folder_name:
:values_from:
:method: :allowed_folders
:auto_select_single: false
:description: Name
:required: false
:display: :edit
:data_type: :integer
:display: :show
:schedule:
:description: Schedule
Expand Down
10 changes: 5 additions & 5 deletions spec/factories/ems_folder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,27 @@
# VMware specific folders
#

factory :vmware_folder, :parent => :ems_folder do
factory :vmware_folder, :parent => :ems_folder, :class => "ManageIQ::Providers::Vmware::InfraManager::EmsFolder" do
sequence(:ems_ref) { |n| "group-d#{n}" }
sequence(:ems_ref_obj) { |n| VimString.new("group-d#{n}", "Folder", "ManagedObjectReference") }
end

factory :vmware_folder_vm, :parent => :ems_folder do
factory :vmware_folder_vm, :parent => :ems_folder, :class => "ManageIQ::Providers::Vmware::InfraManager::EmsFolder" do
sequence(:ems_ref) { |n| "group-v#{n}" }
sequence(:ems_ref_obj) { |n| VimString.new("group-v#{n}", "Folder", "ManagedObjectReference") }
end

factory :vmware_folder_host, :parent => :ems_folder do
factory :vmware_folder_host, :parent => :ems_folder, :class => "ManageIQ::Providers::Vmware::InfraManager::EmsFolder" do
sequence(:ems_ref) { |n| "group-h#{n}" }
sequence(:ems_ref_obj) { |n| VimString.new("group-h#{n}", "Folder", "ManagedObjectReference") }
end

factory :vmware_folder_datastore, :parent => :ems_folder do
factory :vmware_folder_datastore, :parent => :ems_folder, :class => "ManageIQ::Providers::Vmware::InfraManager::EmsFolder" do
sequence(:ems_ref) { |n| "group-s#{n}" }
sequence(:ems_ref_obj) { |n| VimString.new("group-s#{n}", "Folder", "ManagedObjectReference") }
end

factory :vmware_folder_network, :parent => :ems_folder do
factory :vmware_folder_network, :parent => :ems_folder, :class => "ManageIQ::Providers::Vmware::InfraManager::EmsFolder" do
sequence(:ems_ref) { |n| "group-n#{n}" }
sequence(:ems_ref_obj) { |n| VimString.new("group-n#{n}", "Folder", "ManagedObjectReference") }
end
Expand Down
9 changes: 7 additions & 2 deletions spec/models/vm_migrate_task_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
describe VmMigrateTask do
describe '.do_request' do
let(:vm) { Vm.new }
before { subject.vm = vm }
before do
subject.vm = vm
host = FactoryGirl.create(:host, :name => "test")
options = {:placement_host_name => [host.id, host.name] }
subject.update_attributes(:options => options)
end

it 'migrates the vm and updates the status' do
expect(vm).to receive(:migrate)
Expand All @@ -11,7 +16,7 @@

it 'catches migrate error and update the status' do
expect(vm).to receive(:migrate).and_raise("Bad things happened")
expect(subject).to receive(:update_and_notify_parent).with(hash_including(:state => 'finished', :status => 'error', :message => 'Failed. Reason[Bad things happened]'))
expect(subject).to receive(:update_and_notify_parent).with(hash_including(:state => 'finished', :status => 'Error', :message => 'Failed. Reason[Bad things happened]'))
subject.do_request
end
end
Expand Down

0 comments on commit e9f0e98

Please sign in to comment.