From 6427c2ac17828584a46c318a33aed444eeea79ff Mon Sep 17 00:00:00 2001 From: d-m-u Date: Thu, 1 Aug 2019 09:37:24 -0400 Subject: [PATCH] Allow folder to optionally be id for automate engine exposure --- .../vm_or_template/operations/relocation.rb | 19 ++++++++++++- spec/models/vm_migrate_task_spec.rb | 27 ++++++++++++++++++- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/app/models/vm_or_template/operations/relocation.rb b/app/models/vm_or_template/operations/relocation.rb index 0fd0247ab1ea..cabb86b20661 100644 --- a/app/models/vm_or_template/operations/relocation.rb +++ b/app/models/vm_or_template/operations/relocation.rb @@ -106,10 +106,27 @@ def raw_move_into_folder(folder) run_command_via_parent(:vm_move_into_folder, :folder => folder) end - def move_into_folder(folder) + def move_into_folder(folder_or_id) + folder = folder_or_id.kind_of?(Integer) ? EmsFolder.find(folder_or_id) : folder_or_id raw_move_into_folder(folder) end + def move_into_folder_queue(userid, folder) + task_opts = { + :action => "moving Vm to Folder #{folder.name} for user #{userid}", + :userid => userid + } + queue_opts = { + :class_name => self.class.name, + :method_name => 'move_into_folder', + :instance_id => id, + :role => 'ems_operations', + :zone => my_zone, + :args => [folder.id] + } + MiqTask.generic_action_with_callback(task_opts, queue_opts) + end + def migrate_via_ids(host_id, pool_id = nil, priority = "defaultPriority", state = nil) host = Host.find_by(:id => host_id) raise _("Host with ID=%{host_id} was not found") % {:host_id => host_id} if host.nil? diff --git a/spec/models/vm_migrate_task_spec.rb b/spec/models/vm_migrate_task_spec.rb index c86e4e139a88..bb69f6b82201 100644 --- a/spec/models/vm_migrate_task_spec.rb +++ b/spec/models/vm_migrate_task_spec.rb @@ -1,6 +1,7 @@ describe VmMigrateTask do describe '.do_request' do let(:vm) { Vm.new } + let(:folder) { FactoryBot.create(:ems_folder) } before do subject.vm = vm host = FactoryBot.create(:host, :name => "test") @@ -13,7 +14,31 @@ subject.do_request end - it 'catches migrate error and update the status' do + it "doesn't move vm without ems" do + subject.update_attributes(:options => {:placement_folder_name => folder.id}) + expect(subject).to receive(:update_and_notify_parent).with(hash_including(:state => "finished", :status => "Error", :message => "Failed. Reason[VM has no EMS, unable to move VM into a new folder]")) + subject.do_request + end + + it "doesn't move vm if already in folder" do + vm.update(:name => 'aaa', :vendor => 'vmware', :location => 'somewhere') + vm.ext_management_system = FactoryBot.create(:ext_management_system) + folder.set_child(vm) + subject.update_attributes(:options => {:placement_folder_name => folder.id}) + expect(subject).to receive(:update_and_notify_parent).with(hash_including(:state => "finished", :status => "Error", :message => "Failed. Reason[The VM '#{vm.name}' is already running on the same folder as the destination.]")) + subject.do_request + end + + it "does move vm if not already in folder" do + vm.update(:name => 'aaa', :vendor => 'vmware', :location => 'somewhere') + vm.ext_management_system = FactoryBot.create(:ext_management_system, :with_authentication) + subject.update_attributes(:options => {:placement_folder_name => folder.id}) + expect(vm).to receive(:run_command_via_parent).with(:vm_move_into_folder, :folder => folder) + expect(subject).to receive(:update_and_notify_parent).with(hash_including(:state => "migrated", :status => "Ok", :message => "Finished VM Migrate")) + subject.do_request + end + + 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]')) subject.do_request