Skip to content

Commit

Permalink
Merge pull request #252 from slemrmartin/inventory-collection-builder
Browse files Browse the repository at this point in the history
Persister: InventoryCollection building through add_collection()
  • Loading branch information
agrare authored Jun 14, 2018
2 parents 78dc600 + 2763e2b commit 5000fa7
Show file tree
Hide file tree
Showing 15 changed files with 533 additions and 453 deletions.
22 changes: 20 additions & 2 deletions app/models/manageiq/providers/redhat/inventory/persister.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,31 @@ def initialize(manager, target, collector)
@collector = collector

@collections = {}
@collection_group = nil

initialize_inventory_collections
end

protected

def infra
ManageIQ::Providers::Redhat::InventoryCollectionDefault::InfraManager
# should be overriden by subclasses
def strategy
nil
end

def parent
manager.presence
end

# Shared properties for InventoryCollections
def shared_options
{
:parent => parent,
:strategy => strategy
}
end

def manager_refs
references(@collection_group)
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
module ManageIQ::Providers::Redhat::Inventory::Persister::Definitions::InfraCollections
extend ActiveSupport::Concern

include ::ManageIQ::Providers::Redhat::Inventory::Persister::Definitions::InfraGroup::ClusterCollections
include ::ManageIQ::Providers::Redhat::Inventory::Persister::Definitions::InfraGroup::VmsCollections
include ::ManageIQ::Providers::Redhat::Inventory::Persister::Definitions::InfraGroup::HostsCollections
include ::ManageIQ::Providers::Redhat::Inventory::Persister::Definitions::InfraGroup::DatacentersCollections
include ::ManageIQ::Providers::Redhat::Inventory::Persister::Definitions::InfraGroup::StoragedomainsCollections
include ::ManageIQ::Providers::Redhat::Inventory::Persister::Definitions::InfraGroup::NetworksCollections
include ::ManageIQ::Providers::Redhat::Inventory::Persister::Definitions::InfraGroup::VmsDependencyCollections

def initialize_infra_inventory_collections
@collection_group = nil

add_collection(infra, :ems_folders)

add_clusters_group
add_vms_group
add_hosts_group
add_datacenters_group
add_storagedomains_group
add_networks_group
add_vms_dependency_collections_group
add_other_collections
end

# --- IC groups definitions ---

def add_clusters_group
@collection_group = :ems_clusters

add_ems_clusters
add_resource_pools
end

def add_vms_group
@collection_group = :vms

add_vms
add_miq_templates
add_disks
add_networks
add_hardwares
add_guest_devices
add_snapshots
add_operating_systems
add_vm_and_template_ems_custom_fields
end

def add_vms_dependency_collections_group
@collection_group = :vms_dependency

add_ems_folder_children
add_ems_cluster_children
add_snapshot_parent
end

def add_datacenters_group
@collection_group = :datacenters

add_datacenters
end

def add_hosts_group
@collection_group = :hosts

add_hosts
add_host_hardwares
add_host_networks
add_host_operating_systems
add_host_storages
add_host_switches
end

def add_storagedomains_group
@collection_group = :storagedomains

add_storages
end

def add_networks_group
@collection_group = :networks

add_switches
end

def add_other_collections
@collection_group = nil

add_collection(infra, :lans)
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module ManageIQ::Providers::Redhat::Inventory::Persister::Definitions::InfraGroup::ClusterCollections
extend ActiveSupport::Concern

# group :ems_clusters
def add_ems_clusters
add_collection(infra, :ems_clusters) do |builder|
if targeted?
builder.add_properties(:arel => manager.ems_clusters.where(:ems_ref => manager_refs))
end
end
end

# group :ems_clusters
def add_resource_pools
add_collection(infra, :resource_pools) do |builder|
builder.add_properties(:arel => manager.resource_pools.where(:uid_ems => manager_refs.collect { |ref| "#{URI(ref).path.split('/').last}_respool" })) if targeted?
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module ManageIQ::Providers::Redhat::Inventory::Persister::Definitions::InfraGroup::DatacentersCollections
extend ActiveSupport::Concern

# group :datacenters
def add_datacenters
add_collection(infra, :datacenters) do |builder|
arel = if targeted?
manager.ems_folders.where(:type => 'Datacenter').where(:ems_ref => manager_refs) if manager_refs.present?
else
manager.ems_folders.where(:type => 'Datacenter')
end

builder.add_properties(:arel => arel) unless arel.nil?
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
module ManageIQ::Providers::Redhat::Inventory::Persister::Definitions::InfraGroup::HostsCollections
extend ActiveSupport::Concern

# group :hosts
def add_hosts
add_collection(infra, :hosts) do |builder|
builder.add_properties(
:manager_ref => %i(uid_ems),
:custom_reconnect_block => hosts_custom_reconnect_block
)

if targeted?
builder.add_properties(:arel => manager.hosts.where(:ems_ref => manager_refs)) if manager_refs.present?
end

builder.add_builder_params(:ems_id => ->(persister) { persister.manager.id })
end
end

# group :hosts
def add_host_hardwares
add_collection(infra, :host_hardwares) do |builder|
if targeted? && manager_refs.present?
builder.add_properties(:arel => manager.host_hardwares.joins(:host).where('hosts' => {:ems_ref => manager_refs}))
end
end
end

# group :hosts
def add_host_networks
add_collection(infra, :host_networks) do |builder|
if targeted? && manager_refs.present?
builder.add_properties(:arel => manager.networks.joins(:hardware => :host).where(:hardware => {'hosts' => {:ems_ref => manager_refs}}))
end
end
end

# group :hosts
def add_host_operating_systems
add_collection(infra, :host_operating_systems) do |builder|
if targeted? && manager_refs.present?
builder.add_properties(:arel => ::OperatingSystem.joins(:host).where('hosts' => {:ems_ref => manager_refs}))
end
end
end

# group :hosts
def add_host_storages
add_collection(infra, :host_storages) do |builder|
if targeted? && manager_refs.present?
builder.add_properties(:arel => manager.host_storages.where(:ems_ref => manager_refs))
end
end
end

# group :hosts
def add_host_switches
add_collection(infra, :host_switches) do |builder|
if targeted? && manager_refs.present?
builder.add_properties(:arel => HostSwitch.joins(:host).where('hosts' => {:ems_ref => manager_refs}))
end
end
end

# ---

# Custom reconnect block for Hosts IC
def hosts_custom_reconnect_block
lambda do |inventory_collection, inventory_objects_index, attributes_index|
relation = inventory_collection.model_class.where(:ems_id => nil)

return if relation.count <= 0

inventory_objects_index.each_slice(100) do |batch|
relation.where(inventory_collection.manager_ref.first => batch.map(&:first)).each do |record|
index = inventory_collection.object_index_with_keys(inventory_collection.manager_ref_to_cols, record)

# We need to delete the record from the inventory_objects_index and attributes_index, otherwise it
# would be sent for create.
inventory_object = inventory_objects_index.delete(index)
hash = attributes_index.delete(index)

record.assign_attributes(hash.except(:id, :type))
if !inventory_collection.check_changed? || record.changed?
record.save!
inventory_collection.store_updated_records(record)
end

inventory_object.id = record.id
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module ManageIQ::Providers::Redhat::Inventory::Persister::Definitions::InfraGroup::NetworksCollections
extend ActiveSupport::Concern

# group :networks
def add_switches
add_collection(infra, :switches) do |builder|
if targeted?
arel = ::Switch.where(:uid_ems => manager_refs) if manager_refs.present?

builder.add_properties(:arel => arel) unless arel.nil?
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module ManageIQ::Providers::Redhat::Inventory::Persister::Definitions::InfraGroup::StoragedomainsCollections
extend ActiveSupport::Concern

# group :storagedomains
def add_storages
add_collection(infra, :storages) do |builder|
if targeted?
arel = ::Storage.where(:ems_ref => manager_refs) if manager_refs.present?

builder.add_properties(:arel => arel) unless arel.nil?
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
module ManageIQ::Providers::Redhat::Inventory::Persister::Definitions::InfraGroup::VmsCollections
extend ActiveSupport::Concern

# group :vms
def add_vms
add_collection(infra, :vms) do |builder|
if targeted?
builder.add_properties(:arel => manager.vms.where(:ems_ref => manager_refs))
# bug?
builder.add_properties(:strategy => nil) if manager_refs.blank?
end
end
end

# group :vms
def add_miq_templates
add_collection(infra, :miq_templates) do |builder|
builder.add_properties(:model_class => ::ManageIQ::Providers::Redhat::InfraManager::Template)

if targeted?
builder.add_properties(:arel => manager.miq_templates.where(:ems_ref => manager_refs))
# bug?
builder.add_properties(:strategy => nil) if manager_refs.blank?
end
end
end

# group :vms
def add_disks
add_collection(infra, :disks) do |builder|
if targeted?
builder.add_properties(:arel => manager.disks.joins(:hardware => :vm_or_template).where(:hardware => {'vms' => {:ems_ref => manager_refs}}))
# bug?
builder.add_properties(:strategy => nil) if manager_refs.blank?
end
end
end

# group :vms
def add_networks
add_collection(infra, :networks) do |builder|
builder.add_properties(:arel => manager.networks.joins(:hardware => :vm_or_template).where(:hardware => {'vms' => {:ems_ref => manager_refs}})) if targeted?
end
end

# group :vms
def add_hardwares
add_collection(infra, :hardwares) do |builder|
if targeted?
builder.add_properties(:arel => manager.hardwares.joins(:vm_or_template).where('vms' => {:ems_ref => manager_refs}))
# bug?
builder.add_properties(:strategy => nil) if manager_refs.blank?
end
end
end

# group :vms
def add_guest_devices
add_collection(infra, :guest_devices) do |builder|
builder.add_properties(:arel => GuestDevice.joins(:hardware => :vm_or_template).where(:hardware => {'vms' => {:ems_ref => manager_refs}})) if targeted?
end
end

# group :vms
def add_snapshots
add_collection(infra, :snapshots) do |builder|
builder.add_properties(:arel => Snapshot.joins(:vm_or_template).where('vms' => {:ems_ref => manager_refs})) if targeted?
end
end

# group :vms
def add_operating_systems
add_collection(infra, :operating_systems) do |builder|
if targeted?
builder.add_properties(:arel => OperatingSystem.joins(:vm_or_template).where('vms' => {:ems_ref => manager_refs}))
# bug?
builder.add_properties(:strategy => nil) if manager_refs.blank?
end
end
end

# group :vms
def add_vm_and_template_ems_custom_fields
add_collection(infra, :vm_and_template_ems_custom_fields, {}, {:auto_inventory_attributes => false}) do |builder|
builder.add_properties(
:model_class => ::CustomAttribute,
:manager_ref => %i(name)
)
builder.add_properties(:arel => CustomAttribute.joins("INNER JOIN vms ON vms.id = custom_attributes.resource_id").where(:vms => { :ems_ref => manager_refs })) if targeted?

builder.add_inventory_attributes(%i(section name value source resource))
end
end
end
Loading

0 comments on commit 5000fa7

Please sign in to comment.