Skip to content

Commit

Permalink
Add a filter to the changes
Browse files Browse the repository at this point in the history
  • Loading branch information
agrare committed Dec 20, 2022
1 parent e026a93 commit 0804312
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/inventory_refresh/inventory_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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<Symbol>] all columns that are part of the best fit unique index
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
91 changes: 91 additions & 0 deletions spec/save_inventory/single_inventory_collection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
[
Expand Down

0 comments on commit 0804312

Please sign in to comment.