Skip to content

Commit

Permalink
Merge pull request #14755 from mansam/miq_template_vm_genealogy_link
Browse files Browse the repository at this point in the history
Link MiqTemplates to their parent VM when one is present
  • Loading branch information
agrare committed Apr 17, 2017
2 parents ca5b77c + 7b8c06c commit 681583e
Showing 1 changed file with 25 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ def vms(extra_attributes = {})

def miq_templates(extra_attributes = {})
attributes = {
:model_class => ::ManageIQ::Providers::CloudManager::Template,
:association => :miq_templates,
:delete_method => :disconnect_inv,
:model_class => ::ManageIQ::Providers::CloudManager::Template,
:association => :miq_templates,
:delete_method => :disconnect_inv,
:attributes_blacklist => [:genealogy_parent]
}

attributes.merge!(extra_attributes)
Expand Down Expand Up @@ -205,16 +206,33 @@ def vm_and_miq_template_ancestry(extra_attributes = {})
obj[x.id] = genealogy_parent_id if genealogy_parent_id
end

miq_templates = miq_templates_inventory_collection.model_class
.select([:id])
.where(:id => vms_genealogy_parents.values).find_each.index_by(&:id)
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
end

# associate parent templates to child instances
parent_miq_templates = miq_templates_inventory_collection.model_class
.select([:id])
.where(:id => vms_genealogy_parents.values).find_each.index_by(&:id)
vms_inventory_collection.model_class
.select([:id])
.where(:id => vms_genealogy_parents.keys).find_each do |vm|
parent = miq_templates[vms_genealogy_parents[vm.id]]
parent = parent_miq_templates[vms_genealogy_parents[vm.id]]
vm.with_relationship_type('genealogy') { vm.parent = parent }
end
# associate parent instances to child templates
parent_vms = vms_inventory_collection.model_class
.select([:id])
.where(:id => miq_template_genealogy_parents.values).find_each.index_by(&:id)
miq_templates_inventory_collection.model_class
.select([:id])
.where(:id => miq_template_genealogy_parents.keys).find_each do |miq_template|
parent = parent_vms[miq_template_genealogy_parents[miq_template.id]]
miq_template.with_relationship_type('genealogy') { miq_template.parent = parent }
end
end

attributes = {
Expand Down

0 comments on commit 681583e

Please sign in to comment.