Skip to content

Commit

Permalink
Save host for a Vm after migration
Browse files Browse the repository at this point in the history
Fix saving the host of a Vm after it was migrated when doing
targeted refresh of the Vm.

Before if the hosts inventory was not fetched sperately by the
refresh, the host of a VM was not updated properly even though
its uid was returned as part of the target Vm inventory.

Now we take the information from the Vm inventory and use it
to update the Vms host properly.

https://bugzilla.redhat.com/show_bug.cgi?id=1368925
  • Loading branch information
Boris Odnopozov committed Jan 17, 2017
1 parent e12b361 commit e346b46
Show file tree
Hide file tree
Showing 4 changed files with 2,950 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ def self.ems_inv_to_hashes(inv)
result[:storages], uids[:storages] = storage_inv_to_hashes(inv[:storage])
result[:clusters], uids[:clusters], result[:resource_pools] = cluster_inv_to_hashes(inv[:cluster])
result[:hosts], uids[:hosts], uids[:lans], uids[:switches], uids[:guest_devices], uids[:scsi_luns] = host_inv_to_hashes(inv[:host], inv, uids[:clusters], uids[:storages])
result[:vms], uids[:vms] = vm_inv_to_hashes(inv[:vm] + inv[:template], inv[:storage], uids[:storages], uids[:clusters], uids[:hosts], uids[:lans])
vms_inv = inv[:vm] + inv[:template]
result[:vms], uids[:vms], added_hosts_from_vms =
vm_inv_to_hashes(vms_inv, inv[:storage], uids[:storages], uids[:clusters], uids[:hosts], uids[:lans])
result[:folders] = datacenter_inv_to_hashes(inv[:datacenter], uids[:clusters], uids[:vms], uids[:storages], uids[:hosts])

# Link up the root folder
Expand All @@ -18,6 +20,11 @@ def self.ems_inv_to_hashes(inv)
# Clean up the temporary cluster-datacenter references
result[:clusters].each { |c| c.delete(:datacenter_id) }

added_hosts_from_vms.each do |h|
result[:hosts] << h
uids[:hosts][h[:uid_ems]] = h
end

result
end

Expand Down Expand Up @@ -360,7 +367,8 @@ def self.vm_inv_to_hashes(inv, _storage_inv, storage_uids, cluster_uids, host_ui
result = []
result_uids = {}
guest_device_uids = {}
return result, result_uids if inv.nil?
added_hosts = []
return result, result_uids, added_hosts if inv.nil?

inv.each do |vm_inv|
mor = vm_inv[:id]
Expand Down Expand Up @@ -392,6 +400,12 @@ def self.vm_inv_to_hashes(inv, _storage_inv, storage_uids, cluster_uids, host_ui
host_id = vm_inv.attributes.fetch_path(:placement_policy, :host, :id) if host_id.blank?
host = host_uids.values.detect { |h| h[:uid_ems] == host_id } unless host_id.blank?

# If the vm has a host but the refresh does not include it in the "hosts" hash
if host.blank? && vm_inv[:host].present?
host = partial_host_hash(vm_inv[:host])
added_hosts << host if host
end

host_mor = host_id
hardware = vm_inv_to_hardware_hash(vm_inv)
hardware[:disks] = vm_inv_to_disk_hashes(vm_inv, storage_uids)
Expand Down Expand Up @@ -424,7 +438,12 @@ def self.vm_inv_to_hashes(inv, _storage_inv, storage_uids, cluster_uids, host_ui
result << new_result
result_uids[mor] = new_result
end
return result, result_uids
return result, result_uids, added_hosts
end

def self.partial_host_hash(partial_host_inv)
ems_ref = ManageIQ::Providers::Redhat::InfraManager.make_ems_ref(partial_host_inv[:href])
{ :ems_ref => ems_ref, :uid_ems => partial_host_inv[:id] }
end

def self.create_vm_hash(template, ems_ref, vm_id, name)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,69 +1,98 @@
describe ManageIQ::Providers::Redhat::InfraManager::Refresh::Refresher do
before(:each) do
_, _, zone = EvmSpecHelper.create_guid_miq_server_zone
@ems = FactoryGirl.create(:ems_redhat, :zone => zone, :hostname => "192.168.1.31", :ipaddress => "192.168.1.31", :port => 8443)
@ems.update_authentication(:default => {:userid => "admin@internal", :password => "engine"})
context 'targeted refresh of a Vm' do
before(:each) do
_, _, zone = EvmSpecHelper.create_guid_miq_server_zone
@ems = FactoryGirl.create(:ems_redhat, :zone => zone, :hostname => "192.168.1.31", :ipaddress => "192.168.1.31",
:port => 8443)
@ems.update_authentication(:default => {:userid => "admin@internal", :password => "engine"})

@cluster = FactoryGirl.create(:ems_cluster,
:ems_ref => "/api/clusters/00000002-0002-0002-0002-0000000001e9",
:uid_ems => "00000002-0002-0002-0002-0000000001e9",
:ems_id => @ems.id,
:name => "Default")
@cluster = FactoryGirl.create(:ems_cluster,
:ems_ref => "/api/clusters/00000002-0002-0002-0002-0000000001e9",
:uid_ems => "00000002-0002-0002-0002-0000000001e9",
:ems_id => @ems.id,
:name => "Default")

allow(@ems).to receive(:supported_api_versions).and_return([3, 4])
end
allow(@ems).to receive(:supported_api_versions).and_return([3, 4])
end

it "should refresh a vm" do
storage = FactoryGirl.create(:storage,
:ems_ref => "/api/storagedomains/ee745353-c069-4de8-8d76-ec2e155e2ca0",
:location => "192.168.1.106:/home/pkliczewski/export/hosted")

it "should refresh a vm" do
storage = FactoryGirl.create(:storage,
:ems_ref => "/api/storagedomains/ee745353-c069-4de8-8d76-ec2e155e2ca0",
:location => "192.168.1.106:/home/pkliczewski/export/hosted")
disk = FactoryGirl.create(:disk,
:storage => storage,
:filename => "da123bb9-095a-4933-95f2-8032dfa332e1")
hardware = FactoryGirl.create(:hardware,
:disks => [disk])

disk = FactoryGirl.create(:disk,
:storage => storage,
:filename => "da123bb9-095a-4933-95f2-8032dfa332e1")
hardware = FactoryGirl.create(:hardware,
:disks => [disk])
vm = FactoryGirl.create(:vm_redhat,
:ext_management_system => @ems,
:uid_ems => "4f6dd4c3-5241-494f-8afc-f1c67254bf77",
:ems_cluster => @cluster,
:ems_ref => "/api/vms/4f6dd4c3-5241-494f-8afc-f1c67254bf77",
:storage => storage,
:storages => [storage],
:hardware => hardware)

vm = FactoryGirl.create(:vm_redhat,
:ext_management_system => @ems,
:uid_ems => "4f6dd4c3-5241-494f-8afc-f1c67254bf77",
:ems_cluster => @cluster,
:ems_ref => "/api/vms/4f6dd4c3-5241-494f-8afc-f1c67254bf77",
:storage => storage,
:storages => [storage],
:hardware => hardware)
VCR.use_cassette("#{described_class.name.underscore}_target_vm") do
EmsRefresh.refresh(vm)
end

VCR.use_cassette("#{described_class.name.underscore}_target_vm") do
EmsRefresh.refresh(vm)
assert_table_counts
assert_vm(vm, storage)
assert_vm_rels(vm, hardware, storage)
assert_cluster(vm)
assert_storage(storage, vm)
end

assert_table_counts
assert_vm(vm, storage)
assert_vm_rels(vm, hardware, storage)
assert_cluster(vm)
assert_storage(storage, vm)
end
it "should refresh new vm" do
vm = FactoryGirl.create(:vm_redhat,
:ext_management_system => @ems,
:uid_ems => "4f6dd4c3-5241-494f-8afc-f1c67254bf77",
:ems_cluster => @cluster,
:ems_ref => "/api/vms/4f6dd4c3-5241-494f-8afc-f1c67254bf77")

VCR.use_cassette("#{described_class.name.underscore}_target_new_vm") do
EmsRefresh.refresh(vm)
end

it "should refresh new vm" do
vm = FactoryGirl.create(:vm_redhat,
:ext_management_system => @ems,
:uid_ems => "4f6dd4c3-5241-494f-8afc-f1c67254bf77",
:ems_cluster => @cluster,
:ems_ref => "/api/vms/4f6dd4c3-5241-494f-8afc-f1c67254bf77")
assert_table_counts

VCR.use_cassette("#{described_class.name.underscore}_target_new_vm") do
EmsRefresh.refresh(vm)
storage = Storage.find_by(:ems_ref => "/api/storagedomains/ee745353-c069-4de8-8d76-ec2e155e2ca0")
assert_vm(vm, storage)

hardware = Hardware.find_by(:vm_or_template_id => vm.id)
assert_vm_rels(vm, hardware, storage)
assert_cluster(vm)
assert_storage(storage, vm)
end
end

assert_table_counts
context 'targeted refresh after vm migration' do
before(:each) do
_guid, _server, zone = EvmSpecHelper.create_guid_miq_server_zone
@ems = FactoryGirl.create(:ems_redhat, :zone => zone,
:hostname => "10.35.161.51", :ipaddress => "10.35.161.51", :port => 443)
@ems.update_authentication(:default => {:userid => "admin@internal", :password => "password"})
allow(@ems).to receive(:supported_api_versions).and_return([3])
end

storage = Storage.find_by(:ems_ref => "/api/storagedomains/ee745353-c069-4de8-8d76-ec2e155e2ca0")
assert_vm(vm, storage)
it 'should save the vms new host' do
VCR.use_cassette("#{described_class.name.underscore}_before_migration") do
EmsRefresh.refresh(@ems)
end
@ems.reload
vm = @ems.vms.where(:name => "migrate_test_1").first
expect(vm.host.name).to eq("pluto-vdsg.eng.lab.tlv.redhat.com")

hardware = Hardware.find_by(:vm_or_template_id => vm.id)
assert_vm_rels(vm, hardware, storage)
assert_cluster(vm)
assert_storage(storage, vm)
VCR.use_cassette("#{described_class.name.underscore}_after_migration", :allow_unused_http_interactions => true) do
EmsRefresh.refresh(vm)
end

vm.reload
expect(vm.host.name).to eq("lilach-vdsa.tlv.redhat.com")
end
end

def assert_table_counts
Expand Down
Loading

0 comments on commit e346b46

Please sign in to comment.