Skip to content

Commit

Permalink
Merge pull request ManageIQ#15777 from Ladas/add_custom_reconnect_log…
Browse files Browse the repository at this point in the history
…ic_also_to_the_batch_saver

Add custom reconnect logic also to the batch saver
  • Loading branch information
agrare authored Aug 11, 2017
2 parents 8f00582 + 907ea79 commit 56b42bb
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ def save!(association)
collect_pg_types!(all_attribute_keys)
update_or_destroy_records!(batch_iterator(association), inventory_objects_index, attributes_index, all_attribute_keys)

unless inventory_collection.custom_reconnect_block.nil?
inventory_collection.custom_reconnect_block.call(inventory_collection, inventory_objects_index, attributes_index)
end

all_attribute_keys << :type if inventory_collection.supports_sti?
# Records that were not found in the DB but sent for saving, we will be creating these in the DB.
if inventory_collection.create_allowed?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -699,60 +699,63 @@
end
end

context 'testing reconnect logic' do
it 'reconnects existing VM' do
# Fill DB with test Vms
@vm1 = FactoryGirl.create(:vm_cloud, vm_data(1).merge(:ext_management_system => nil))
@vm2 = FactoryGirl.create(:vm_cloud, vm_data(2).merge(:ext_management_system => @ems))

vms_custom_reconnect_block = lambda do |inventory_collection, inventory_objects_index, attributes_index|
inventory_objects_index.each_slice(1000) do |batch|
Vm.where(:ems_ref => batch.map(&:second).map(&:manager_uuid)).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)
[:default, :batch].each do |saver_strategy|
context "testing reconnect logic with saver_strategy: :#{saver_strategy}" do
it 'reconnects existing VM' do
# Fill DB with test Vms
@vm1 = FactoryGirl.create(:vm_cloud, vm_data(1).merge(:ext_management_system => nil))
@vm2 = FactoryGirl.create(:vm_cloud, vm_data(2).merge(:ext_management_system => @ems))

vms_custom_reconnect_block = lambda do |inventory_collection, inventory_objects_index, attributes_index|
inventory_objects_index.each_slice(1000) do |batch|
Vm.where(:ems_ref => batch.map(&:second).map(&:manager_uuid)).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

inventory_object.id = record.id
end
end
end

@data = {}
@data[:vms] = ::ManagerRefresh::InventoryCollection.new(
:model_class => ManageIQ::Providers::CloudManager::Vm,
:parent => @ems,
:association => :vms,
:custom_reconnect_block => vms_custom_reconnect_block,
)
@data = {}
@data[:vms] = ::ManagerRefresh::InventoryCollection.new(
:model_class => ManageIQ::Providers::CloudManager::Vm,
:parent => @ems,
:association => :vms,
:saver_strategy => saver_strategy,
:custom_reconnect_block => vms_custom_reconnect_block,
)

# Fill the InventoryCollections with data, that have a modified name
add_data_to_inventory_collection(@data[:vms],
vm_data(1).merge(:name => "vm_changed_name_1"),
vm_data(2).merge(:name => "vm_changed_name_2"))
# Fill the InventoryCollections with data, that have a modified name
add_data_to_inventory_collection(@data[:vms],
vm_data(1).merge(:name => "vm_changed_name_1"),
vm_data(2).merge(:name => "vm_changed_name_2"))

# Invoke the InventoryCollections saving
ManagerRefresh::SaveInventory.save_inventory(@ems, @data.values)
# Invoke the InventoryCollections saving
ManagerRefresh::SaveInventory.save_inventory(@ems, @data.values)

# Check the InventoryCollection result matches what was created/deleted/updated
expect(@data[:vms].created_records).to match_array([])
expect(@data[:vms].updated_records).to match_array([{:id => @vm1.id}, {:id => @vm2.id}])
expect(@data[:vms].deleted_records).to match_array([])
# Check the InventoryCollection result matches what was created/deleted/updated
expect(@data[:vms].created_records).to match_array([])
expect(@data[:vms].updated_records).to match_array([{:id => @vm1.id}, {:id => @vm2.id}])
expect(@data[:vms].deleted_records).to match_array([])

# Assert that saved data have the updated values, checking id to make sure the original records are updated
assert_all_records_match_hashes(
[Vm.all, @ems.vms],
{:id => @vm1.id, :ems_ref => "vm_ems_ref_1", :name => "vm_changed_name_1", :location => "vm_location_1"},
{:id => @vm2.id, :ems_ref => "vm_ems_ref_2", :name => "vm_changed_name_2", :location => "vm_location_2"}
)
# Assert that saved data have the updated values, checking id to make sure the original records are updated
assert_all_records_match_hashes(
[Vm.all, @ems.vms],
{:id => @vm1.id, :ems_ref => "vm_ems_ref_1", :name => "vm_changed_name_1", :location => "vm_location_1"},
{:id => @vm2.id, :ems_ref => "vm_ems_ref_2", :name => "vm_changed_name_2", :location => "vm_location_2"}
)
end
end
end
end
Expand Down

0 comments on commit 56b42bb

Please sign in to comment.