From 08043127257d43ce98a8a479feb8908913883397 Mon Sep 17 00:00:00 2001 From: Adam Grare Date: Tue, 20 Dec 2022 11:00:32 -0500 Subject: [PATCH] Add a filter to the changes --- lib/inventory_refresh/inventory_collection.rb | 2 +- .../helpers/initialize_helper.rb | 17 +++- .../single_inventory_collection_spec.rb | 91 +++++++++++++++++++ 3 files changed, 106 insertions(+), 4 deletions(-) diff --git a/lib/inventory_refresh/inventory_collection.rb b/lib/inventory_refresh/inventory_collection.rb index 2444a709..83615a0f 100644 --- a/lib/inventory_refresh/inventory_collection.rb +++ b/lib/inventory_refresh/inventory_collection.rb @@ -217,7 +217,7 @@ def store_deleted_records(records) def store_record_changes(records) records = [records] unless records.respond_to?(:map) - @record_changes.merge!(records.to_h { |r| [record_identity(r)[:id], r.changes] }) + @record_changes.merge!(records.to_h { |r| [record_identity(r)[:id], r.changes.slice(*track_record_changes)] }) end # @return [Array] all columns that are part of the best fit unique index diff --git a/lib/inventory_refresh/inventory_collection/helpers/initialize_helper.rb b/lib/inventory_refresh/inventory_collection/helpers/initialize_helper.rb index 49885943..b28fa707 100644 --- a/lib/inventory_refresh/inventory_collection/helpers/initialize_helper.rb +++ b/lib/inventory_refresh/inventory_collection/helpers/initialize_helper.rb @@ -177,8 +177,9 @@ def init_ic_relations(dependency_attributes, parent_inventory_collections = nil) # for the batch saver strategy. # @param targeted [Boolean] True if the collection is targeted, in that case it will be leveraging :manager_uuids # :parent_inventory_collections and :targeted_arel to save a subgraph of a data. - # @param track_record_changes [Boolean] By default false. If true changes to the InventoryObject will be stored for use - # by other inventory collections + # @param track_record_changes [Boolean/Array] By default false. If false no changes to the InventoryObject will be stored, + # otherwise changes to properties enumerated in the array will be stored for use by other inventory collections + # Example: :track_record_changes => [:name, :raw_power_state] def init_flags(complete, create_only, check_changed, update_only, use_ar_object, targeted, assert_graph_integrity, track_record_changes) @@ -190,7 +191,7 @@ def init_flags(complete, create_only, check_changed, @use_ar_object = use_ar_object || false @targeted = !!targeted @assert_graph_integrity = assert_graph_integrity.nil? ? true : assert_graph_integrity - @track_record_changes = track_record_changes.nil? ? false : track_record_changes.map(&:to_s) + @track_record_changes = process_track_record_changes(track_record_changes) end # @param attributes_blacklist [Array] Attributes we do not want to include into saving. We cannot blacklist an @@ -454,6 +455,16 @@ def process_retention_strategy(retention_strategy) ":destroy and :archive" end end + + def process_track_record_changes(track_record_changes) + return false unless track_record_changes + + if !track_record_changes.kind_of?(Array) + raise "Invalid value for track_record_changes: #{track_record_changes}, allowed values are false or an Array" + end + + track_record_changes.map(&:to_s) + end end end end diff --git a/spec/save_inventory/single_inventory_collection_spec.rb b/spec/save_inventory/single_inventory_collection_spec.rb index b518f46b..5853fe95 100644 --- a/spec/save_inventory/single_inventory_collection_spec.rb +++ b/spec/save_inventory/single_inventory_collection_spec.rb @@ -258,6 +258,97 @@ end end + describe "#track_record_changes" do + context 'with VM InventoryCollection track_record_changes not used' do + let :changed_data do + [ + vm_data(1).merge(:name => "vm_changed_name_1", + :location => "vm_changed_location_1", + :uid_ems => "uid_ems_changed_1", + :raw_power_state => "raw_power_state_changed_1"), + vm_data(2).merge(:name => "vm_changed_name_2", + :location => "vm_changed_location_2", + :uid_ems => "uid_ems_changed_2", + :raw_power_state => "raw_power_state_changed_2"), + vm_data(3).merge(:name => "vm_changed_name_3", + :location => "vm_changed_location_3", + :uid_ems => "uid_ems_changed_3", + :raw_power_state => "raw_power_state_changed_3") + ] + end + + it "doesn't track record changes" do + @persister.add_collection(:vms) do |builder| + builder.add_properties( + :model_class => ManageIQ::Providers::CloudManager::Vm, + :track_record_changes => false + ) + end + + changed_data.each { |vm_data| @persister.vms.build(vm_data) } + + InventoryRefresh::SaveInventory.save_inventory(@ems, @persister.inventory_collections) + + expect(@persister.vms.record_changes).to be_blank + end + end + + context 'with VM InventoryCollection track_record_changes used' do + let :changed_data do + [ + vm_data(1).merge(:name => "vm_changed_name_1", + :location => "vm_changed_location_1", + :uid_ems => "uid_ems_changed_1", + :raw_power_state => "raw_power_state_changed_1"), + vm_data(2).merge(:name => "vm_changed_name_2", + :location => "vm_changed_location_2", + :uid_ems => "uid_ems_changed_2", + :raw_power_state => "raw_power_state_changed_2"), + vm_data(3).merge(:name => "vm_changed_name_3", + :location => "vm_changed_location_3", + :uid_ems => "uid_ems_changed_3", + :raw_power_state => "raw_power_state_changed_3") + ] + end + + it 'records changes to the records' do + @persister.add_collection(:vms) do |builder| + builder.add_properties( + :model_class => ManageIQ::Providers::CloudManager::Vm, + :track_record_changes => %i[location raw_power_state] + ) + end + + changed_data.each { |vm_data| @persister.vms.build(vm_data) } + + InventoryRefresh::SaveInventory.save_inventory(@ems, @persister.inventory_collections) + + vm_1_changes = @persister.vms.record_changes[@vm1.id] + expect(vm_1_changes).to include( + "location" => ["vm_location_1", "vm_changed_location_1"], + "raw_power_state" => ["unknown", "raw_power_state_changed_1"] + ) + end + + it 'ignores changes not in the filter' do + @persister.add_collection(:vms) do |builder| + builder.add_properties( + :model_class => ManageIQ::Providers::CloudManager::Vm, + :track_record_changes => %i[location raw_power_state] + ) + end + + changed_data.each { |vm_data| @persister.vms.build(vm_data) } + + InventoryRefresh::SaveInventory.save_inventory(@ems, @persister.inventory_collections) + + vm_1_changes = @persister.vms.record_changes[@vm1.id] + + expect(vm_1_changes.keys).not_to include("uid_ems", "name") + end + end + end + context 'with VM InventoryCollection blacklist or whitelist used' do let :changed_data do [