Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Prevent a vm or template being its own parent #23302

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -165,17 +165,31 @@ 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
next if x.data[:genealogy_parent].nil?

genealogy_parent_id = x.data[:genealogy_parent].load.try(:id)
next if genealogy_parent_id.nil?

if genealogy_parent_id == x.id
_log.warn("Cannot assign genealogy_parent to same object, ID [#{x.id}]")
next
end

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
next if x.data[:genealogy_parent].nil?

genealogy_parent_id = x.data[:genealogy_parent].load.try(:id)
next if genealogy_parent_id.nil?

if genealogy_parent_id == x.id
_log.warn("Cannot assign genealogy_parent to same object, ID [#{x.id}]")
next
end

obj[x.id] = genealogy_parent_id
end

ActiveRecord::Base.transaction do
Expand All @@ -184,7 +198,13 @@ def vm_and_miq_template_ancestry_save_block
.where(:id => vms_genealogy_parents.values).find_each.index_by(&:id)
vms_inventory_collection.model_class
.where(:id => vms_genealogy_parents.keys).find_each do |vm|
vm.update!(:genealogy_parent => parent_miq_templates[vms_genealogy_parents[vm.id]])
genealogy_parent = parent_miq_templates[vms_genealogy_parents[vm.id]]

begin
vm.update!(:genealogy_parent => genealogy_parent)
rescue ActiveRecord::RecordInvalid => err
$log.error("#{err} VM id: [#{vm.id}] ems_ref: [#{vm.ems_ref}] genealogy_parent: id: [#{genealogy_parent.id}] ems_ref: [#{genealogy_parent.ems_ref}]")
end
end
end

Expand All @@ -194,7 +214,13 @@ def vm_and_miq_template_ancestry_save_block
.where(:id => miq_template_genealogy_parents.values).find_each.index_by(&:id)
miq_templates_inventory_collection.model_class
.where(:id => miq_template_genealogy_parents.keys).find_each do |miq_template|
miq_template.update!(:genealogy_parent => parent_vms[miq_template_genealogy_parents[miq_template.id]])
genealogy_parent = parent_vms[miq_template_genealogy_parents[miq_template.id]]

begin
miq_template.update!(:genealogy_parent => genealogy_parent)
rescue ActiveRecord::RecordInvalid => err
$log.error("#{err} MiqTemplate id: [#{miq_template.id}] ems_ref: [#{miq_template.ems_ref}] genealogy_parent: id: [#{genealogy_parent.id}] ems_ref: [#{genealogy_parent.ems_ref}]")
end
end
end
end
Expand Down
Loading