Skip to content

Commit

Permalink
Return vm records with the same ems_ref first
Browse files Browse the repository at this point in the history
If there is a vm record with the same ems_ref then update that even if
it has a different uid_ems.

https://bugzilla.redhat.com/show_bug.cgi?id=1695008
  • Loading branch information
agrare committed Apr 4, 2019
1 parent 629474c commit ae7a083
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 66 deletions.
6 changes: 4 additions & 2 deletions app/models/ems_refresh/save_inventory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ def save_vms_inventory(ems, hashes, target = nil, disconnect = true)
]
remove_keys = child_keys + extra_infra_keys + extra_cloud_keys

vms_by_ems_ref = ems.vms_and_templates.index_by(&:ems_ref)

# Query for all of the Vms once across all EMSes, to handle any moving VMs
vms_uids = hashes.collect { |h| h[:uid_ems] }.compact
vms = VmOrTemplate.where(:uid_ems => vms_uids).to_a
Expand Down Expand Up @@ -89,7 +91,7 @@ def save_vms_inventory(ems, hashes, target = nil, disconnect = true)

# Find the Vm in the database with the current uid_ems. In the event
# of duplicates, try to determine which one is correct.
found = vms_by_uid_ems[h[:uid_ems]] || []
found = Array(vms_by_ems_ref[h[:ems_ref]] || vms_by_uid_ems[h[:uid_ems]])

if found.length > 1 || (found.length == 1 && found.first.ems_id)
found_dups = found
Expand All @@ -113,7 +115,7 @@ def save_vms_inventory(ems, hashes, target = nil, disconnect = true)
# build a type-specific vm or template
found = ems.vms_and_templates.klass.new(h)
else
vms_by_uid_ems[h[:uid_ems]].delete(found)
vms_by_uid_ems[h[:uid_ems]]&.delete(found)
h.delete(:type)

_log.info("#{log_header} Updating #{type} [#{found.name}] id: [#{found.id}] location: [#{found.location}] storage id: [#{found.storage_id}] uid_ems: [#{found.uid_ems}] ems_ref: [#{h[:ems_ref]}]")
Expand Down
88 changes: 24 additions & 64 deletions spec/models/ems_refresh/save_inventory_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,24 +128,14 @@
EmsRefresh.save_vms_inventory(@ems, data)

vms = Vm.all
expect(vms.length).to eq(3)

disconnected, connected = vms.partition { |v| v.ems_id.nil? }
expect(disconnected.length).to eq(1)
expect(connected.length).to eq(2)

d = disconnected.first
c1, c2 = connected.sort_by(&:id)

expect(d.id).to eq(@vm2.id)
expect(d.uid_ems).to eq(@vm2.uid_ems)
expect(vms.length).to eq(2)
v1, v2 = vms.sort_by(&:id)

expect(c1.id).to eq(@vm1.id)
expect(c1.uid_ems).to eq(@vm1.uid_ems)
expect(v1.id).to eq(@vm1.id)
expect(v1.uid_ems).to eq(@vm1.uid_ems)

expect(c2.id).not_to eq(@vm1.id)
expect(c2.id).not_to eq(@vm2.id)
expect(c2.uid_ems).to eq(@vm1.uid_ems)
expect(v2.id).to eq(@vm2.id)
expect(v2.uid_ems).to eq(@vm1.uid_ems)
end
end

Expand All @@ -161,24 +151,14 @@
EmsRefresh.save_vms_inventory(@ems, data)

vms = Vm.all
expect(vms.length).to eq(3)

disconnected, connected = vms.partition { |v| v.ems_id.nil? }
expect(disconnected.length).to eq(1)
expect(connected.length).to eq(2)

d = disconnected.first
c1, c2 = connected.sort_by(&:id)

expect(d.id).to eq(@vm2.id)
expect(d.uid_ems).to eq(@vm2.uid_ems)
expect(vms.length).to eq(2)
v1, v2 = vms.sort_by(&:id)

expect(c1.id).to eq(@vm1.id)
expect(c1.uid_ems).to eq(@vm1.uid_ems)
expect(v1.id).to eq(@vm1.id)
expect(v1.uid_ems).to eq(@vm1.uid_ems)

expect(c2.id).not_to eq(@vm1.id)
expect(c2.id).not_to eq(@vm2.id)
expect(c2.uid_ems).not_to eq(@vm1.uid_ems)
expect(v2.id).to eq(@vm2.id)
expect(v2.uid_ems).not_to eq(@vm1.uid_ems)
end

it "should handle dups in the raw data" do
Expand Down Expand Up @@ -209,24 +189,14 @@
EmsRefresh.save_vms_inventory(@ems, data)

vms = Vm.all
expect(vms.length).to eq(3)

disconnected, connected = vms.partition { |v| v.ems_id.nil? }
expect(disconnected.length).to eq(1)
expect(connected.length).to eq(2)

d = disconnected.first
c1, c2 = connected.sort_by(&:id)

expect(d.id).to eq(@vm2.id)
expect(d.uid_ems).to eq(@vm2.uid_ems)
expect(vms.length).to eq(2)
v1, v2 = vms.sort_by(&:id)

expect(c1.id).to eq(@vm1.id)
expect(c1.uid_ems).to eq(@vm1.uid_ems)
expect(v1.id).to eq(@vm1.id)
expect(v1.uid_ems).to eq(@vm1.uid_ems)

expect(c2.id).not_to eq(@vm1.id)
expect(c2.id).not_to eq(@vm2.id)
expect(c2.uid_ems).not_to eq(@vm1.uid_ems)
expect(v2.id).to eq(@vm2.id)
expect(v2.uid_ems).not_to eq(@vm2.uid_ems)
end

it "should handle dups in the raw data" do
Expand Down Expand Up @@ -329,24 +299,14 @@
EmsRefresh.save_vms_inventory(@ems, data)

vms = Vm.all
expect(vms.length).to eq(3)

disconnected, connected = vms.partition { |v| v.ems_id.nil? }
expect(disconnected.length).to eq(1)
expect(connected.length).to eq(2)

d = disconnected.first
c1, c2 = connected.sort_by(&:id)

expect(d.id).to eq(@vm2.id)
expect(d.uid_ems).to eq(@vm2.uid_ems)
expect(vms.length).to eq(2)
v1, v2 = vms.sort_by(&:id)

expect(c1.id).to eq(@vm1.id)
expect(c1.uid_ems).to eq(@vm1.uid_ems)
expect(v1.id).to eq(@vm1.id)
expect(v1.uid_ems).to eq(@vm1.uid_ems)

expect(c2.id).not_to eq(@vm1.id)
expect(c2.id).not_to eq(@vm2.id)
expect(c2.uid_ems).to eq(@vm1.uid_ems)
expect(v2.id).to eq(@vm2.id)
expect(v2.uid_ems).not_to eq(@vm2.uid_ems)
end
end
end
Expand Down

0 comments on commit ae7a083

Please sign in to comment.