From c6e5168b0dd15bd0f4d86461a822ba8a14ebb1c0 Mon Sep 17 00:00:00 2001 From: Piotr Kliczewski Date: Fri, 5 Jan 2018 16:06:32 +0100 Subject: [PATCH] Reconnect host on provider add We need to make sure that when we remove a provider and add it again we will keep using the same host entities. Without this change add and remove provider cause to duplicate hosts. Fixes https://bugzilla.redhat.com/1528859 --- .../inventory_collection_default.rb | 30 ++++++++++++++++++- .../infra_manager.rb | 1 + 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/app/models/manager_refresh/inventory_collection_default.rb b/app/models/manager_refresh/inventory_collection_default.rb index 6222151c6c9a..2391d6c099b8 100644 --- a/app/models/manager_refresh/inventory_collection_default.rb +++ b/app/models/manager_refresh/inventory_collection_default.rb @@ -42,7 +42,8 @@ def vms(extra_attributes = {}) :ems_id => ->(persister) { persister.manager.id }, :name => "unknown", :location => "unknown", - } + }, + :custom_reconnect_block => reconnect_block } attributes.merge!(extra_attributes) @@ -199,4 +200,31 @@ def disks(extra_attributes = {}) attributes.merge!(extra_attributes) end 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 end 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 201e2a4bc9bf..dc863935d770 100644 --- a/app/models/manager_refresh/inventory_collection_default/infra_manager.rb +++ b/app/models/manager_refresh/inventory_collection_default/infra_manager.rb @@ -281,6 +281,7 @@ def hosts(extra_attributes = {}) :builder_params => { :ems_id => ->(persister) { persister.manager.id }, }, + :custom_reconnect_block => reconnect_block } attributes.merge!(extra_attributes)