From 58916b2ca8a0d95a8a4babeaa8f55a17ec2f2b45 Mon Sep 17 00:00:00 2001 From: Adam Grare Date: Mon, 8 Jan 2018 08:31:18 -0500 Subject: [PATCH] Merge pull request #16750 from pkliczewski/reconnect Reconnect host on provider add (cherry picked from commit 1caf7137d6472eb496e2d251aca9ecc2b49b8342) Fixes https://bugzilla.redhat.com/show_bug.cgi?id=1532332 --- .../infra_manager.rb | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/app/models/manager_refresh/inventory_collection_default/infra_manager.rb b/app/models/manager_refresh/inventory_collection_default/infra_manager.rb index 26486edf47c..2b7bb0a8ac9 100644 --- a/app/models/manager_refresh/inventory_collection_default/infra_manager.rb +++ b/app/models/manager_refresh/inventory_collection_default/infra_manager.rb @@ -140,11 +140,47 @@ def hosts(extra_attributes = {}) :builder_params => { :ems_id => ->(persister) { persister.manager.id }, }, + :custom_reconnect_block => reconnect_block } attributes.merge!(extra_attributes) end + def vms(extra_attributes = {}) + attributes = { + :custom_reconnect_block => reconnect_block + } + + super(attributes.merge!(extra_attributes)) + end + + def reconnect_block + lambda do |inventory_collection, inventory_objects_index, attributes_index| + relation = inventory_collection.model_class.where(:ems_id => nil) + + return if relation.count <= 0 + + inventory_objects_index.each_slice(100) do |batch| + relation.where(inventory_collection.manager_ref.first => batch.map(&:first)).each do |record| + index = inventory_collection.object_index_with_keys(inventory_collection.manager_ref_to_cols, record) + + # We need to delete the record from the inventory_objects_index and attributes_index, otherwise it + # would be sent for create. + inventory_object = inventory_objects_index.delete(index) + hash = attributes_index.delete(index) + + record.assign_attributes(hash.except(:id, :type)) + if !inventory_collection.check_changed? || record.changed? + record.save! + inventory_collection.store_updated_records(record) + end + + inventory_object.id = record.id + end + end + end + end + def host_storages(extra_attributes = {}) attributes = { :model_class => ::HostStorage,