From 05287639d6ff4a4419cbe5504de62ffc2091065f Mon Sep 17 00:00:00 2001 From: Adam Grare Date: Fri, 10 Jan 2025 10:53:55 -0500 Subject: [PATCH] Prevent a vm or template being its own parent --- .../persister/builder/cloud_manager.rb | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/app/models/manageiq/providers/inventory/persister/builder/cloud_manager.rb b/app/models/manageiq/providers/inventory/persister/builder/cloud_manager.rb index 3e20deef03f..a9cbe644599 100644 --- a/app/models/manageiq/providers/inventory/persister/builder/cloud_manager.rb +++ b/app/models/manageiq/providers/inventory/persister/builder/cloud_manager.rb @@ -165,17 +165,21 @@ def vm_and_miq_template_ancestry_save_block # Fetch IDs of all vms and genealogy_parents, only if genealogy_parent is present vms_genealogy_parents = vms_inventory_collection.data.each_with_object({}) do |x, obj| - unless x.data[:genealogy_parent].nil? - genealogy_parent_id = x.data[:genealogy_parent].load.try(:id) - obj[x.id] = genealogy_parent_id if genealogy_parent_id - end + next if x.data[:genealogy_parent].nil? + + genealogy_parent_id = x.data[:genealogy_parent].load.try(:id) + next if genealogy_parent_id.nil? || genealogy_parent_id == x.id + + obj[x.id] = genealogy_parent_id end miq_template_genealogy_parents = miq_templates_inventory_collection.data.each_with_object({}) do |x, obj| - unless x.data[:genealogy_parent].nil? - genealogy_parent_id = x.data[:genealogy_parent].load.try(:id) - obj[x.id] = genealogy_parent_id if genealogy_parent_id - end + next if x.data[:genealogy_parent].nil? + + genealogy_parent_id = x.data[:genealogy_parent].load.try(:id) + next if genealogy_parent_id.nil? || genealogy_parent_id == x.id + + obj[x.id] = genealogy_parent_id end ActiveRecord::Base.transaction do