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

Parse parent for most collections #251

Merged
merged 5 commits into from
May 9, 2018
Merged
Show file tree
Hide file tree
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
@@ -1,5 +1,9 @@
module ManageIQ::Providers::Vmware::InfraManager::Inventory::Collector::PropertyCollector
EmsRefreshPropMap = {
:ManagedEntity => [
"name",
"parent",
],
:VirtualMachine => [
"availableField",
"config.cpuAffinity.affinitySet",
Expand All @@ -24,6 +28,7 @@ module ManageIQ::Providers::Vmware::InfraManager::Inventory::Collector::Property
"resourceConfig.memoryAllocation.reservation",
"resourceConfig.memoryAllocation.shares.level",
"resourceConfig.memoryAllocation.shares.shares",
"resourcePool",
"snapshot",
"summary.vm",
"summary.config.annotation",
Expand All @@ -48,9 +53,7 @@ module ManageIQ::Providers::Vmware::InfraManager::Inventory::Collector::Property
"summary.storage.committed",
],
:ComputeResource => [
"name",
"host",
"parent",
"resourcePool",
],
:ClusterComputeResource => [
Expand All @@ -64,13 +67,9 @@ module ManageIQ::Providers::Vmware::InfraManager::Inventory::Collector::Property
"summary.effectiveCpu",
"summary.effectiveMemory",
"host",
"name",
"parent",
"resourcePool",
],
:ResourcePool => [
"name",
"parent",
"resourcePool",
"summary.config.cpuAllocation.expandableReservation",
"summary.config.cpuAllocation.limit",
Expand All @@ -85,16 +84,11 @@ module ManageIQ::Providers::Vmware::InfraManager::Inventory::Collector::Property
"vm",
],
:Folder => [
"childEntity",
"name",
"parent",
],
:Datacenter => [
"datastoreFolder",
"hostFolder",
"name",
"networkFolder",
"parent",
"vmFolder",
],
:HostSystem => [
Expand All @@ -115,7 +109,6 @@ module ManageIQ::Providers::Vmware::InfraManager::Inventory::Collector::Property
"config.storageDevice.scsiTopology.adapter",
"datastore",
"hardware.systemInfo.otherIdentifyingInfo",
"name",
"summary.host",
"summary.config.name",
"summary.config.product.build",
Expand Down Expand Up @@ -153,22 +146,18 @@ module ManageIQ::Providers::Vmware::InfraManager::Inventory::Collector::Property
"summary.type",
"summary.uncommitted",
"summary.url",
"parent",
],
:StoragePod => [
"summary.capacity",
"summary.freeSpace",
"summary.name",
"childEntity",
"parent",
],
:DistributedVirtualPortgroup => [
"summary.name",
"config.key",
"config.defaultPortConfig",
"config.distributedVirtualSwitch",
"config.name",
"parent",
"host",
"tag",
],
Expand All @@ -180,7 +169,6 @@ module ManageIQ::Providers::Vmware::InfraManager::Inventory::Collector::Property
"summary.uuid",
"summary.host",
"summary.hostMember",
"parent",
]
}.freeze

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ def customization_specs(extra_attributes = {})

def vms_and_templates(extra_attributes = {})
attributes = {
:model_class => ::VmOrTemplate,
:association => :vms_and_templates,
:builder_params => {
:model_class => ::VmOrTemplate,
:association => :vms_and_templates,
:attributes_blacklist => %i(parent resource_pool),
:builder_params => {
:ems_id => ->(persister) { persister.manager.id },
},
}
Expand All @@ -37,8 +38,36 @@ def storage_profiles(extra_attributes = {})
attributes.merge!(extra_attributes)
end

def ems_folders(extra_attributes = {})
attributes = {:attributes_blacklist => %i(parent)}
super(attributes.merge(extra_attributes))
end

def resource_pools(extra_attributes = {})
attributes = {:attributes_blacklist => %i(parent)}
super(attributes.merge(extra_attributes))
end

def ems_clusters(extra_attributes = {})
attributes = {:attributes_blacklist => %i(parent)}
super(attributes.merge(extra_attributes))
end

def storages(extra_attributes = {})
attributes = {:attributes_blacklist => %i(parent)}
super(attributes.merge(extra_attributes))
end

def hosts(extra_attributes = {})
attributes = {:model_class => ManageIQ::Providers::Vmware::InfraManager::HostEsx}
attributes = {
:attributes_blacklist => %i(parent),
:model_class => ManageIQ::Providers::Vmware::InfraManager::HostEsx,
}
super(attributes.merge(extra_attributes))
end

def switchs(extra_attributes = {})
attributes = {:attributes_blacklist => %i(parent)}
super(attributes.merge(extra_attributes))
end

Expand All @@ -61,5 +90,78 @@ def guest_devices(extra_attributes = {})
attributes = {:parent_inventory_collections => [:vms_and_templates]}
super(attributes.merge(extra_attributes))
end

def parent_blue_folders(extra_attributes = {})
relationships(:parent, :ems_metadata, nil, :parent_blue_folders, extra_attributes)
end

def vm_parent_blue_folders(extra_attributes = {})
relationships(:parent, :ems_metadata, "EmsFolder", :vm_parent_blue_folders, extra_attributes)
end

def vm_resource_pools(extra_attributes = {})
relationships(:resource_pool, :ems_metadata, "ResourcePool", :vm_resource_pools, extra_attributes)
end

def relationships(relationship_key, relationship_type, parent_type, association, extra_attributes = {})
relationship_save_block = lambda do |_ems, inventory_collection|
parents = Hash.new { |h, k| h[k] = {} }
children = Hash.new { |h, k| h[k] = {} }

inventory_collection.dependency_attributes.each_value do |dependency_collections|
next if dependency_collections.blank?

dependency_collections.each do |collection|
next if collection.blank?

collection.data.each do |obj|
parent = obj.data[relationship_key].try(&:load)
next if parent.nil?

parent_klass = parent.inventory_collection.model_class

# Save the model_class and id of the parent for each child
children[collection.model_class][obj.id] = [parent_klass, parent.id]

# This will be populated later when looking up all the parent ids
parents[parent_klass][parent.id] = nil
end
end
end

ActiveRecord::Base.transaction do
# Lookup all of the parent records
parents.each do |model_class, ids|
model_class.find(ids.keys).each { |record| ids[record.id] = record }
end

# Loop through all children and assign parents
children.each do |model_class, ids|
child_records = model_class.find(ids.keys).index_by(&:id)

ids.each do |id, parent_info|
child = child_records[id]

parent_klass, parent_id = parent_info
parent = parents[parent_klass][parent_id]

child.with_relationship_type(relationship_type) do
prev_parent = child.parent(:of_type => parent_type)
unless prev_parent == parent
prev_parent&.remove_child(child)
parent.add_child(child)
end
end
end
end
end
end

attributes = {
:association => association,
:custom_save_block => relationship_save_block,
}
attributes.merge!(extra_attributes)
end
end
end
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
class ManageIQ::Providers::Vmware::InfraManager::Inventory::Parser
include_concern :ComputeResource
include_concern :Datacenter
include_concern :Datastore
include_concern :DistributedVirtualSwitch
include_concern :Folder
include_concern :HostSystem
include_concern :ResourcePool
include_concern :VirtualMachine
Expand Down Expand Up @@ -32,12 +30,12 @@ def parse_compute_resource(object, props)
:ems_ref => object._ref,
:uid_ems => object._ref,
:name => CGI.unescape(props[:name]),
:parent => lazy_find_managed_object(props[:parent]),
}

parse_compute_resource_summary(cluster_hash, props)
parse_compute_resource_das_config(cluster_hash, props)
parse_compute_resource_drs_config(cluster_hash, props)
parse_compute_resource_children(cluster_hash, props)

persister.ems_clusters.build(cluster_hash)
end
Expand All @@ -48,15 +46,13 @@ def parse_datacenter(object, props)
return if props.nil?

dc_hash = {
:ems_ref => object._ref,
:uid_ems => object._ref,
:type => "Datacenter",
:name => CGI.unescape(props[:name]),
:ems_children => {},
:ems_ref => object._ref,
:uid_ems => object._ref,
:type => "Datacenter",
:name => CGI.unescape(props[:name]),
:parent => lazy_find_managed_object(props[:parent]),
}

parse_datacenter_children(dc_hash, props)

persister.ems_folders.build(dc_hash)
end

Expand All @@ -66,6 +62,7 @@ def parse_datastore(object, props)

storage_hash = {
:ems_ref => object._ref,
:parent => lazy_find_managed_object(props[:parent]),
}

parse_datastore_summary(storage_hash, props)
Expand Down Expand Up @@ -102,24 +99,25 @@ def parse_folder(object, props)
return if props.nil?

folder_hash = {
:ems_ref => object._ref,
:uid_ems => object._ref,
:type => "EmsFolder",
:name => CGI.unescape(props[:name]),
:ems_children => {},
:ems_ref => object._ref,
:uid_ems => object._ref,
:type => "EmsFolder",
:name => CGI.unescape(props[:name]),
:parent => lazy_find_managed_object(props[:parent]),
}

parse_folder_children(folder_hash, props)

persister.ems_folders.build(folder_hash)
end

def parse_host_system(object, props)
persister.hosts.manager_uuids << object._ref
return if props.nil?

cluster = lazy_find_managed_object(props[:parent])
host_hash = {
:ems_ref => object._ref,
:ems_cluster => cluster,
:ems_ref => object._ref,
:parent => cluster,
}

parse_host_system_config(host_hash, props)
Expand Down Expand Up @@ -186,11 +184,11 @@ def parse_resource_pool(object, props)
:uid_ems => object._ref,
:name => CGI.unescape(props[:name]),
:vapp => object.kind_of?(RbVmomi::VIM::VirtualApp),
:parent => lazy_find_managed_object(props[:parent]),
}

parse_resource_pool_memory_allocation(rp_hash, props)
parse_resource_pool_cpu_allocation(rp_hash, props)
parse_resource_pool_children(rp_hash, props)

persister.resource_pools.build(rp_hash)
end
Expand All @@ -207,6 +205,7 @@ def parse_storage_pod(object, props)
:uid_ems => object._ref,
:type => "StorageCluster",
:name => CGI.unescape(name),
:parent => lazy_find_managed_object(props[:parent]),
}

persister.ems_folders.build(pod_hash)
Expand All @@ -217,8 +216,10 @@ def parse_virtual_machine(object, props)
return if props.nil?

vm_hash = {
:ems_ref => object._ref,
:vendor => "vmware",
:ems_ref => object._ref,
:vendor => "vmware",
:parent => lazy_find_managed_object(props[:parent]),
:resource_pool => lazy_find_managed_object(props[:resourcePool]),
}

parse_virtual_machine_config(vm_hash, props)
Expand All @@ -232,4 +233,11 @@ def parse_virtual_machine(object, props)
parse_virtual_machine_custom_attributes(vm, props)
parse_virtual_machine_snapshots(vm, props)
end

def lazy_find_managed_object(managed_object)
return if managed_object.nil?

parent_collection = persister.vim_class_to_collection(managed_object)
parent_collection.lazy_find(managed_object._ref)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,5 @@ def parse_compute_resource_drs_config(cluster_hash, props)
cluster_hash[:drs_automation_level] = drs_config[:defaultVmBehavior]
cluster_hash[:drs_migration_threshold] = drs_config[:vmotionRate]
end

def parse_compute_resource_children(cluster_hash, props)
cluster_hash[:ems_children] = {:rp => []}
rp = props[:resourcePool]
unless rp.nil?
cluster_hash[:ems_children][:rp] << persister.resource_pools.lazy_find(rp._ref)
end
end
end
end

This file was deleted.

Loading