From e77a19d13a30710f702f7d63f26c857b82f17f8b Mon Sep 17 00:00:00 2001 From: Alexander Zagaynov Date: Thu, 1 Feb 2018 20:10:48 +0100 Subject: [PATCH] move custom reconnect block to the base class and use it for both Vm and MiqTemplate --- .../inventory_collection_default.rb | 31 +++++++++++++++++-- .../infra_manager.rb | 31 ------------------- 2 files changed, 29 insertions(+), 33 deletions(-) diff --git a/app/models/manager_refresh/inventory_collection_default.rb b/app/models/manager_refresh/inventory_collection_default.rb index 6222151c6c9a..4f3105913390 100644 --- a/app/models/manager_refresh/inventory_collection_default.rb +++ b/app/models/manager_refresh/inventory_collection_default.rb @@ -1,4 +1,29 @@ class ManagerRefresh::InventoryCollectionDefault + VM_OR_TEMPLATE_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.freeze + class << self def vms(extra_attributes = {}) attributes = { @@ -42,7 +67,8 @@ def vms(extra_attributes = {}) :ems_id => ->(persister) { persister.manager.id }, :name => "unknown", :location => "unknown", - } + }, + :custom_reconnect_block => VM_OR_TEMPLATE_RECONNECT_BLOCK, } attributes.merge!(extra_attributes) @@ -82,7 +108,8 @@ def miq_templates(extra_attributes = {}) :name => "unknown", :location => "unknown", :template => true - } + }, + :custom_reconnect_block => VM_OR_TEMPLATE_RECONNECT_BLOCK, } attributes.merge!(extra_attributes) 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 b1f4d92a26b0..c8d69a991927 100644 --- a/app/models/manager_refresh/inventory_collection_default/infra_manager.rb +++ b/app/models/manager_refresh/inventory_collection_default/infra_manager.rb @@ -310,37 +310,6 @@ def hosts(extra_attributes = {}) attributes.merge!(extra_attributes) end - def vms(extra_attributes = {}) - attributes = { - :custom_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 - } - - super(attributes.merge!(extra_attributes)) - end - def host_storages(extra_attributes = {}) attributes = { :model_class => ::HostStorage,