Skip to content

Commit

Permalink
Extract find_vm method from save_vms_inventory
Browse files Browse the repository at this point in the history
  • Loading branch information
agrare committed Apr 3, 2019
1 parent 92e3d2b commit 228e8d4
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 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 @@ -87,23 +89,9 @@ def save_vms_inventory(ems, hashes, target = nil, disconnect = true)
begin
raise MiqException::MiqIncompleteData if h[:invalid]

# 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]] || []

if found.length > 1 || (found.length == 1 && found.first.ems_id)
found_dups = found
found = found_dups.select { |v| v.ems_id == h[:ems_id] && (v.ems_ref.nil? || v.ems_ref == h[:ems_ref]) }
if found.empty?
found_dups = found_dups.select { |v| v.ems_id.nil? }
found = found_dups.select { |v| v.ems_ref == h[:ems_ref] }
found = found_dups if found.empty?
end
end

type = h[:template] ? "Template" : "Vm"

found = found.first
found = find_vm(h, vms_by_ems_ref, vms_by_uid_ems)
if found.nil?
_log.info("#{log_header} Creating #{type} [#{h[:name]}] location: [#{h[:location]}] storage id: [#{h[:storage_id]}] uid_ems: [#{h[:uid_ems]}] ems_ref: [#{h[:ems_ref]}]")

Expand Down Expand Up @@ -389,4 +377,21 @@ def save_ems_storage_inventory(ems, hashes, target = nil)
save_ems_block_storage_inventory(ems, hashes, target) if ems.supports?(:block_storage)
save_ems_object_storage_inventory(ems, hashes, target) if ems.supports?(:object_storage)
end

def find_vm(h, vms_by_ems_ref, vms_by_uid_ems)
# 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]] || []
if found.length > 1 || (found.length == 1 && found.first.ems_id)
found_dups = found
found = found_dups.select { |v| v.ems_id == h[:ems_id] && (v.ems_ref.nil? || v.ems_ref == h[:ems_ref]) }
if found.empty?
found_dups = found_dups.select { |v| v.ems_id.nil? }
found = found_dups.select { |v| v.ems_ref == h[:ems_ref] }
found = found_dups if found.empty?
end
end

found.first
end
end

0 comments on commit 228e8d4

Please sign in to comment.