diff --git a/app/models/manageiq/providers/redhat/inventory/persister.rb b/app/models/manageiq/providers/redhat/inventory/persister.rb index 64c469794..0265ff31b 100644 --- a/app/models/manageiq/providers/redhat/inventory/persister.rb +++ b/app/models/manageiq/providers/redhat/inventory/persister.rb @@ -10,13 +10,23 @@ 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 shared_options + {:strategy => strategy} + end + + def manager_refs + references(@collection_group) end end diff --git a/app/models/manageiq/providers/redhat/inventory/persister/infra_manager.rb b/app/models/manageiq/providers/redhat/inventory/persister/infra_manager.rb index 01951239d..5d5762332 100644 --- a/app/models/manageiq/providers/redhat/inventory/persister/infra_manager.rb +++ b/app/models/manageiq/providers/redhat/inventory/persister/infra_manager.rb @@ -1,65 +1,11 @@ class ManageIQ::Providers::Redhat::Inventory::Persister::InfraManager < ManageIQ::Providers::Redhat::Inventory::Persister - def initialize_inventory_collections - add_inventory_collections( - infra, - %i(ems_clusters hosts resource_pools vms miq_templates - storages vm_and_template_ems_custom_fields disks guest_devices hardwares - host_hardwares host_networks host_operating_systems host_storages - host_switches lans networks operating_systems snapshots switches) - ) - - add_inventory_collection( - infra.datacenters( - :arel => manager.ems_folders.where(:type => 'Datacenter'), - :strategy => :local_db_find_missing_references - ) - ) - - add_inventory_collection( - infra.ems_folder_children( - :dependency_attributes => { - :clusters => [collections[:ems_clusters]], - :datacenters => [collections[:datacenters]], - :vms => [collections[:vms]], - :templates => [collections[:miq_templates]] - } - ) - ) + include ManageIQ::Providers::Redhat::Inventory::Persister::Shared::InfraCollections - add_inventory_collection( - infra.ems_clusters_children( - :dependency_attributes => { - :vms => [collections[:vms]], - :clusters => [collections[:ems_clusters]] - } - ) - ) - - add_inventory_collection( - infra.snapshot_parent( - :dependency_attributes => { - :snapshots => [collections[:snapshots]] - } - ) - ) + def initialize_inventory_collections + initialize_infra_inventory_collections + end - add_inventory_collection( - infra.vm_folders( - :arel => manager.ems_folders.where(:name => 'vm'), - :strategy => :local_db_find_missing_references - ) - ) - add_inventory_collection( - infra.host_folders( - :arel => manager.ems_folders.where(:name => 'host'), - :strategy => :local_db_find_missing_references - ) - ) - add_inventory_collection( - infra.root_folders( - :arel => manager.ems_folders.where(:uid_ems => 'root_dc'), - :strategy => :local_db_find_missing_references - ) - ) + def strategy + @collection_group == :datacenters ? :local_db_find_missing_references : nil end end diff --git a/app/models/manageiq/providers/redhat/inventory/persister/network_manager.rb b/app/models/manageiq/providers/redhat/inventory/persister/network_manager.rb index 8c7219dce..8d123fc54 100644 --- a/app/models/manageiq/providers/redhat/inventory/persister/network_manager.rb +++ b/app/models/manageiq/providers/redhat/inventory/persister/network_manager.rb @@ -1,14 +1,9 @@ +# !! Inherited from OpenStack class ManageIQ::Providers::Redhat::Inventory::Persister::NetworkManager < ManageIQ::Providers::Openstack::Inventory::Persister::NetworkManager - def network - ManageIQ::Providers::Redhat::InventoryCollectionDefault::NetworkManager - end + include ManageIQ::Providers::Redhat::Inventory::Persister::Shared::NetworkCollections def initialize_inventory_collections super - add_inventory_collections( - network, - %i(cloud_tenants), - :builder_params => {:ext_management_system => manager.parent_manager} - ) + add_cloud_tenants end end diff --git a/app/models/manageiq/providers/redhat/inventory/persister/shared/infra_collections.rb b/app/models/manageiq/providers/redhat/inventory/persister/shared/infra_collections.rb new file mode 100644 index 000000000..80c312aac --- /dev/null +++ b/app/models/manageiq/providers/redhat/inventory/persister/shared/infra_collections.rb @@ -0,0 +1,97 @@ +module ManageIQ::Providers::Redhat::Inventory::Persister::Shared::InfraCollections + extend ActiveSupport::Concern + + include ::ManageIQ::Providers::Redhat::Inventory::Persister::Shared::InfraGroup::ClusterCollections + include ::ManageIQ::Providers::Redhat::Inventory::Persister::Shared::InfraGroup::VmsCollections + include ::ManageIQ::Providers::Redhat::Inventory::Persister::Shared::InfraGroup::HostsCollections + include ::ManageIQ::Providers::Redhat::Inventory::Persister::Shared::InfraGroup::DatacentersCollections + include ::ManageIQ::Providers::Redhat::Inventory::Persister::Shared::InfraGroup::StoragedomainsCollections + include ::ManageIQ::Providers::Redhat::Inventory::Persister::Shared::InfraGroup::NetworksCollections + include ::ManageIQ::Providers::Redhat::Inventory::Persister::Shared::InfraGroup::VmsDependencyCollections + + def infra + ::ManagerRefresh::InventoryCollection::Builder::InfraManager + end + + def initialize_infra_inventory_collections + @collection_group = nil + + 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 + add_vm_folders + add_host_folders + add_root_folders + 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 diff --git a/app/models/manageiq/providers/redhat/inventory/persister/shared/infra_group/cluster_collections.rb b/app/models/manageiq/providers/redhat/inventory/persister/shared/infra_group/cluster_collections.rb new file mode 100644 index 000000000..03bee251e --- /dev/null +++ b/app/models/manageiq/providers/redhat/inventory/persister/shared/infra_group/cluster_collections.rb @@ -0,0 +1,19 @@ +module ManageIQ::Providers::Redhat::Inventory::Persister::Shared::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 diff --git a/app/models/manageiq/providers/redhat/inventory/persister/shared/infra_group/datacenters_collections.rb b/app/models/manageiq/providers/redhat/inventory/persister/shared/infra_group/datacenters_collections.rb new file mode 100644 index 000000000..92649884d --- /dev/null +++ b/app/models/manageiq/providers/redhat/inventory/persister/shared/infra_group/datacenters_collections.rb @@ -0,0 +1,69 @@ +module ManageIQ::Providers::Redhat::Inventory::Persister::Shared::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 + + # group :datacenters + def add_vm_folders + add_collection(infra, :vm_folders, {}, {:auto_inventory_attributes => false}) do |builder| + builder.ems_folders + + builder.add_properties(:model_class => ::EmsFolder) + + arel = if targeted? + manager.ems_folders.where(:uid_ems => manager_refs.collect { |ref| "#{URI(ref).path.split('/').last}_vm" }) if manager_refs.present? + else + manager.ems_folders.where(:name => 'vm') + end + + builder.add_properties(:arel => arel) unless arel.nil? + + builder.add_inventory_attributes(%i(name type uid_ems hidden)) + end + end + + # group :datacenters + def add_host_folders + add_collection(infra, :host_folders, {}, {:auto_inventory_attributes => false}) do |builder| + builder.ems_folders + + builder.add_properties(:model_class => ::EmsFolder) + + arel = if targeted? + manager.ems_folders.where(:uid_ems => manager_refs.collect { |ref| "#{URI(ref).path.split('/').last}_host" }) if manager_refs.present? + else + manager.ems_folders.where(:name => 'host') + end + + builder.add_properties(:arel => arel) unless arel.nil? + + builder.add_inventory_attributes(%i(name type uid_ems hidden)) + end + end + + # group :datacenters + def add_root_folders + add_collection(infra, :root_folders, {}, {:auto_inventory_attributes => false}) do |builder| + builder.ems_folders + + builder.add_properties(:model_class => ::EmsFolder) + + arel = manager.ems_folders.where(:uid_ems => 'root_dc') if !targeted? || manager_refs.present? + + builder.add_properties(:arel => arel) unless arel.nil? + + builder.add_inventory_attributes(%i(name type uid_ems hidden)) + end + end +end diff --git a/app/models/manageiq/providers/redhat/inventory/persister/shared/infra_group/hosts_collections.rb b/app/models/manageiq/providers/redhat/inventory/persister/shared/infra_group/hosts_collections.rb new file mode 100644 index 000000000..ef36d9330 --- /dev/null +++ b/app/models/manageiq/providers/redhat/inventory/persister/shared/infra_group/hosts_collections.rb @@ -0,0 +1,94 @@ +module ManageIQ::Providers::Redhat::Inventory::Persister::Shared::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 diff --git a/app/models/manageiq/providers/redhat/inventory/persister/shared/infra_group/networks_collections.rb b/app/models/manageiq/providers/redhat/inventory/persister/shared/infra_group/networks_collections.rb new file mode 100644 index 000000000..fac255ab3 --- /dev/null +++ b/app/models/manageiq/providers/redhat/inventory/persister/shared/infra_group/networks_collections.rb @@ -0,0 +1,14 @@ +module ManageIQ::Providers::Redhat::Inventory::Persister::Shared::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 diff --git a/app/models/manageiq/providers/redhat/inventory/persister/shared/infra_group/storagedomains_collections.rb b/app/models/manageiq/providers/redhat/inventory/persister/shared/infra_group/storagedomains_collections.rb new file mode 100644 index 000000000..4e8d8a763 --- /dev/null +++ b/app/models/manageiq/providers/redhat/inventory/persister/shared/infra_group/storagedomains_collections.rb @@ -0,0 +1,14 @@ +module ManageIQ::Providers::Redhat::Inventory::Persister::Shared::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 diff --git a/app/models/manageiq/providers/redhat/inventory/persister/shared/infra_group/vms_collections.rb b/app/models/manageiq/providers/redhat/inventory/persister/shared/infra_group/vms_collections.rb new file mode 100644 index 000000000..5b7d6bd95 --- /dev/null +++ b/app/models/manageiq/providers/redhat/inventory/persister/shared/infra_group/vms_collections.rb @@ -0,0 +1,94 @@ +module ManageIQ::Providers::Redhat::Inventory::Persister::Shared::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 diff --git a/app/models/manageiq/providers/redhat/inventory/persister/shared/infra_group/vms_dependency_collections.rb b/app/models/manageiq/providers/redhat/inventory/persister/shared/infra_group/vms_dependency_collections.rb new file mode 100644 index 000000000..037b675c9 --- /dev/null +++ b/app/models/manageiq/providers/redhat/inventory/persister/shared/infra_group/vms_dependency_collections.rb @@ -0,0 +1,118 @@ +module ManageIQ::Providers::Redhat::Inventory::Persister::Shared::InfraGroup::VmsDependencyCollections + extend ActiveSupport::Concern + + # group :vms_dependency + def add_ems_folder_children + add_collection(infra, :ems_folder_children, {}, + { + :without_model_class => true, + :auto_inventory_attributes => false + }) do |builder| + + builder.add_properties(:custom_save_block => ems_folder_children_custom_save_block) + + if !targeted? || references(:vms).present? # correct condition? + builder.add_dependency_attributes( + :clusters => [collections[:ems_clusters]], + :datacenters => [collections[:datacenters]], + :vms => [collections[:vms]], + :templates => [collections[:miq_templates]] + ) + end + end + end + + # group :vms_dependency + def add_ems_cluster_children + add_collection(infra, :ems_cluster_children, {}, + { + :without_model_class => true, + :auto_inventory_attributes => false + }) do |builder| + + builder.add_properties(:custom_save_block => ems_cluster_children_save_block) + + if !targeted? || references(:vms).present? # correct condition? + builder.add_dependency_attributes( + :vms => [collections[:vms]], + :clusters => [collections[:ems_clusters]] + ) + end + end + end + + # group :vms_dependency + def add_snapshot_parent + add_collection(infra, :snapshot_parent, {}, + { + :auto_inventory_attributes => false, + :without_model_class => true + }) do |builder| + + if !targeted? || references(:vms).present? # correct condition? + builder.add_dependency_attributes(:snapshots => [collections[:snapshots]]) + end + end + end + + # --- + + # Custom save block for ems_folder_children IC + def ems_folder_children_custom_save_block + lambda do |ems, inventory_collection| + cluster_collection = inventory_collection.dependency_attributes[:clusters].try(:first) + vm_collection = inventory_collection.dependency_attributes[:vms].try(:first) + template_collection = inventory_collection.dependency_attributes[:templates].try(:first) + datacenter_collection = inventory_collection.dependency_attributes[:datacenters].try(:first) + + vms_and_templates = vm_collection.data + template_collection.data + indexed_vms_and_templates = vms_and_templates.each_with_object({}) { |vm, obj| (obj[vm.ems_cluster.ems_ref] ||= []) << vm } + + datacenter_collection.data.each do |dc| + uid = dc.uid_ems + + clusters = cluster_collection.data.select { |cluster| cluster.datacenter_id == uid } + cluster_refs = clusters.map(&:ems_ref) + + vms = cluster_refs.map { |x| indexed_vms_and_templates[x] }.flatten.compact + + ActiveRecord::Base.transaction do + host_folder = EmsFolder.find_by(:uid_ems => "#{uid}_host") + cs = EmsCluster.find(clusters.map(&:id)) + host_folder.with_relationship_type("ems_metadata") { host_folder.add_child(cs) } + + vm_folder = EmsFolder.find_by(:uid_ems => "#{uid}_vm") + vs = VmOrTemplate.find(vms.map(&:id)) + vm_folder.with_relationship_type("ems_metadata") { vm_folder.add_child(vs) } + + datacenter = EmsFolder.find(dc.id) + datacenter.with_relationship_type("ems_metadata") { datacenter.add_child(host_folder) } + datacenter.with_relationship_type("ems_metadata") { datacenter.add_child(vm_folder) } + root_dc = EmsFolder.find_by(:uid_ems => 'root_dc', :ems_id => ems.id) + root_dc.with_relationship_type("ems_metadata") { root_dc.add_child(datacenter) } + ems.with_relationship_type("ems_metadata") { ems.add_child(root_dc) } + end + end + end + end + + # Custom save block for ems_cluster_children IC + def ems_cluster_children_save_block + lambda do |_ems, inventory_collection| + cluster_collection = inventory_collection.dependency_attributes[:clusters].try(:first) + vm_collection = inventory_collection.dependency_attributes[:vms].try(:first) + + cluster_collection.each do |cluster| + vms = vm_collection.data.select { |vm| cluster.ems_ref == vm.ems_cluster.ems_ref } + + ActiveRecord::Base.transaction do + vs = VmOrTemplate.find(vms.map(&:id)) + rp = ResourcePool.find_by(:uid_ems => "#{cluster.uid_ems}_respool") + rp.with_relationship_type("ems_metadata") { rp.add_child(vs) } + c = EmsCluster.find(cluster.id) + c.with_relationship_type("ems_metadata") { c.add_child(rp) } + end + end + end + end +end diff --git a/app/models/manageiq/providers/redhat/inventory/persister/shared/network_collections.rb b/app/models/manageiq/providers/redhat/inventory/persister/shared/network_collections.rb new file mode 100644 index 000000000..2c50874e5 --- /dev/null +++ b/app/models/manageiq/providers/redhat/inventory/persister/shared/network_collections.rb @@ -0,0 +1,20 @@ +module ManageIQ::Providers::Redhat::Inventory::Persister::Shared::NetworkCollections + extend ActiveSupport::Concern + + def network + ::ManagerRefresh::InventoryCollection::Builder::NetworkManager + end + + # ------ IC provider specific definitions ------------------------- + + def add_cloud_tenants + add_collection(network, :cloud_tenants) do |builder| + builder.add_properties( + :model_class => ManageIQ::Providers::Openstack::CloudManager::CloudTenant + ) + builder.add_builder_params( + :ext_management_system => manager.parent_manager + ) + end + end +end diff --git a/app/models/manageiq/providers/redhat/inventory/persister/target_collection.rb b/app/models/manageiq/providers/redhat/inventory/persister/target_collection.rb index 4312124ee..c3f3b7ae8 100644 --- a/app/models/manageiq/providers/redhat/inventory/persister/target_collection.rb +++ b/app/models/manageiq/providers/redhat/inventory/persister/target_collection.rb @@ -1,243 +1,34 @@ class ManageIQ::Providers::Redhat::Inventory::Persister::TargetCollection < ManageIQ::Providers::Redhat::Inventory::Persister - def initialize_inventory_collections - add_targeted_inventory_collections - add_remaining_inventory_collections([infra], :strategy => :local_db_find_references) - add_vms_dependency_collections(references(:vms)) - end - - def references(collection) - target.manager_refs_by_association.try(:[], collection).try(:[], :ems_ref).try(:to_a) || [] - end - - def infra - ManageIQ::Providers::Redhat::InventoryCollectionDefault::InfraManager - end - - def add_vms_dependency_collections(manager_refs) - return if manager_refs.blank? - - add_inventory_collection( - infra.ems_folder_children( - :dependency_attributes => { - :clusters => [collections[:ems_clusters]], - :datacenters => [collections[:datacenters]], - :vms => [collections[:vms]], - :templates => [collections[:miq_templates]] - } - ) - ) - add_inventory_collection( - infra.ems_clusters_children( - :dependency_attributes => { - :vms => [collections[:vms]], - :clusters => [collections[:ems_clusters]] - } - ) - ) - add_inventory_collection( - infra.snapshot_parent( - :dependency_attributes => { - :snapshots => [collections[:snapshots]] - } - ) - ) - end - - def add_targeted_inventory_collections - add_vms_inventory_collections(references(:vms)) - add_clusters_inventory_collections(references(:ems_clusters)) - add_datacenters_inventory_collections(references(:datacenters)) - add_storages_inventory_collcetions(references(:storagedomains)) - add_networks_inventory_collcetions(references(:networks)) - add_hosts_inventory_collections(references(:hosts)) - end + include ManageIQ::Providers::Redhat::Inventory::Persister::Shared::InfraCollections - def add_vms_inventory_collections(manager_refs) - return if manager_refs.blank? - - add_inventory_collection( - infra.vms( - :arel => manager.vms.where(:ems_ref => manager_refs), - :strategy => :local_db_find_missing_references - ) - ) - add_inventory_collection( - infra.miq_templates( - :arel => manager.miq_templates.where(:ems_ref => manager_refs), - :strategy => :local_db_find_missing_references - ) - ) - add_inventory_collection( - infra.disks( - :arel => manager.disks.joins(:hardware => :vm_or_template).where( - :hardware => {'vms' => {:ems_ref => manager_refs}} - ), - :strategy => :local_db_find_missing_references - ) - ) - add_inventory_collection( - infra.networks( - :arel => manager.networks.joins(:hardware => :vm_or_template).where( - :hardware => {'vms' => {:ems_ref => manager_refs}} - ), - :strategy => :local_db_find_missing_references - ) - ) - add_inventory_collection( - infra.hardwares( - :arel => manager.hardwares.joins(:vm_or_template).where( - 'vms' => {:ems_ref => manager_refs} - ), - :strategy => :local_db_find_missing_references - ) - ) - add_inventory_collection( - infra.guest_devices( - :arel => GuestDevice.joins(:hardware => :vm_or_template).where( - :hardware => {'vms' => {:ems_ref => manager_refs}} - ), - :strategy => :local_db_find_missing_references - ) - ) - add_inventory_collection( - infra.snapshots( - :arel => Snapshot.joins(:vm_or_template).where( - 'vms' => {:ems_ref => manager_refs} - ), - :strategy => :local_db_find_missing_references - ) - ) - add_inventory_collection( - infra.operating_systems( - :arel => OperatingSystem.joins(:vm_or_template).where( - 'vms' => {:ems_ref => manager_refs} - ), - :strategy => :local_db_find_missing_references - ) - ) - add_inventory_collection( - infra.vm_and_template_ems_custom_fields( - :arel => CustomAttribute.joins("INNER JOIN vms ON vms.id = custom_attributes.resource_id") - .where(:vms => { :ems_ref => manager_refs }), - :strategy => :local_db_find_missing_references - ) - ) - end - - def add_clusters_inventory_collections(manager_refs) - return if manager_refs.blank? - - add_inventory_collection( - infra.ems_clusters( - :arel => manager.ems_clusters.where(:ems_ref => manager_refs), - :strategy => :local_db_find_missing_references - ) - ) - add_inventory_collection( - infra.resource_pools( - :arel => manager.resource_pools.where(:uid_ems => manager_refs.collect { |ref| "#{URI(ref).path.split('/').last}_respool" }), - :strategy => :local_db_find_missing_references - ) - ) - end - - def add_datacenters_inventory_collections(manager_refs) - return if manager_refs.blank? - - add_inventory_collection( - infra.datacenters( - :arel => manager.ems_folders.where(:ems_ref => manager_refs, :type => 'Datacenter'), - :strategy => :local_db_find_missing_references - ) - ) - add_inventory_collection( - infra.vm_folders( - :arel => manager.ems_folders.where(:uid_ems => manager_refs.collect { |ref| "#{URI(ref).path.split('/').last}_vm" }), - :strategy => :local_db_find_missing_references - ) - ) - add_inventory_collection( - infra.host_folders( - :arel => manager.ems_folders.where(:uid_ems => manager_refs.collect { |ref| "#{URI(ref).path.split('/').last}_host" }), - :strategy => :local_db_find_missing_references - ) - ) - add_inventory_collection( - infra.root_folders( - :arel => manager.ems_folders.where(:uid_ems => 'root_dc'), - :strategy => :local_db_find_missing_references - ) - ) - end + def initialize_inventory_collections + initialize_infra_inventory_collections - def add_storages_inventory_collcetions(manager_refs) - return if manager_refs.blank? + @collection_group = nil + %i(custom_attributes + ems_folders).each do |name| - add_inventory_collection( - infra.storages( - :arel => Storage.where(:ems_ref => manager_refs), - :strategy => :local_db_find_missing_references - ) - ) + add_collection(infra, name) + end end - def add_networks_inventory_collcetions(manager_refs) - return if manager_refs.blank? - - add_inventory_collection( - infra.switches( - :arel => Switch.where(:uid_ems => manager_refs), - :strategy => :local_db_find_missing_references - ) - ) + # not added to IC properties + # IC definitions not written like other providers (used arel property instead) + def targeted? + true end - def add_hosts_inventory_collections(manager_refs) - return if manager_refs.blank? + def strategy + ems_ref = if @collection_group == :vms_dependency + references(:vms) + else + references(@collection_group) + end - add_inventory_collection( - infra.hosts( - :arel => manager.hosts.where(:ems_ref => manager_refs), - :strategy => :local_db_find_missing_references - ) - ) - add_inventory_collection( - infra.host_hardwares( - :arel => manager.host_hardwares.joins(:host).where( - 'hosts' => {:ems_ref => manager_refs} - ), - :strategy => :local_db_find_missing_references - ) - ) - add_inventory_collection( - infra.host_operating_systems( - :arel => OperatingSystem.joins(:host).where( - 'hosts' => {:ems_ref => manager_refs} - ), - :strategy => :local_db_find_missing_references - ) - ) - add_inventory_collection( - infra.host_storages( - :arel => manager.host_storages.where(:ems_ref => manager_refs), - :strategy => :local_db_find_missing_references - ) - ) - add_inventory_collection( - infra.host_switches( - :arel => HostSwitch.joins(:host).where( - 'hosts' => {:ems_ref => manager_refs} - ), - :strategy => :local_db_find_missing_references - ) - ) - add_inventory_collection( - infra.host_networks( - :arel => manager.networks.joins(:hardware => :host).where( - :hardware => {'hosts' => {:ems_ref => manager_refs}} - ), - :strategy => :local_db_find_missing_references - ) - ) + if ems_ref.blank? + :local_db_find_references + else + :local_db_find_missing_references unless @collection_group == :vms_dependency + end end end diff --git a/app/models/manageiq/providers/redhat/inventory_collection_default/infra_manager.rb b/app/models/manageiq/providers/redhat/inventory_collection_default/infra_manager.rb deleted file mode 100644 index 44f3dd416..000000000 --- a/app/models/manageiq/providers/redhat/inventory_collection_default/infra_manager.rb +++ /dev/null @@ -1,222 +0,0 @@ -class ManageIQ::Providers::Redhat::InventoryCollectionDefault::InfraManager < ManagerRefresh::InventoryCollectionDefault::InfraManager - class << self - def vms(extra_attributes = {}) - attributes = { - :model_class => ::ManageIQ::Providers::Redhat::InfraManager::Vm, - } - - super(attributes.merge!(extra_attributes)) - end - - def miq_templates(extra_attributes = {}) - attributes = { - :model_class => ::ManageIQ::Providers::Redhat::InfraManager::Template, - } - - super(attributes.merge!(extra_attributes)) - end - - def vm_folders(extra_attributes = {}) - attributes = { - :model_class => ::EmsFolder, - :inventory_object_attributes => [ - :name, - :type, - :uid_ems, - :hidden - ], - :association => :vm_folders, - :manager_ref => [:uid_ems], - :attributes_blacklist => [:ems_children], - :builder_params => { - :ems_id => ->(persister) { persister.manager.id }, - }, - } - - attributes.merge!(extra_attributes) - end - - def host_folders(extra_attributes = {}) - attributes = { - :model_class => ::EmsFolder, - :inventory_object_attributes => [ - :name, - :type, - :uid_ems, - :hidden - ], - :association => :host_folders, - :manager_ref => [:uid_ems], - :attributes_blacklist => [:ems_children], - :builder_params => { - :ems_id => ->(persister) { persister.manager.id }, - }, - } - - attributes.merge!(extra_attributes) - end - - def hosts(extra_attributes = {}) - attributes = { - :model_class => ::Host, - :association => :hosts, - :manager_ref => [:uid_ems], - :inventory_object_attributes => %i( - type - ems_ref - ems_ref_obj - name - hostname - ipaddress - uid_ems - vmm_vendor - vmm_product - vmm_version - vmm_buildnumber - connection_state - power_state - ems_cluster - ipmi_address - maintenance - ), - :builder_params => { - :ems_id => ->(persister) { persister.manager.id }, - }, - :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 - } - - attributes.merge!(extra_attributes) - end - - def root_folders(extra_attributes = {}) - attributes = { - :model_class => ::EmsFolder, - :inventory_object_attributes => [ - :name, - :type, - :uid_ems, - :hidden - ], - :association => :root_folders, - :manager_ref => [:uid_ems], - :attributes_blacklist => [:ems_children], - :builder_params => { - :ems_id => ->(persister) { persister.manager.id }, - }, - } - - attributes.merge!(extra_attributes) - end - - def vm_and_template_ems_custom_fields(extra_attributes = {}) - attributes = { - :model_class => ::CustomAttribute, - :manager_ref => [:name], - :association => :vm_and_template_ems_custom_fields, - :inventory_object_attributes => %i( - section - name - value - source - resource - ), - } - - attributes.merge!(extra_attributes) - end - - def ems_folder_children(extra_attributes = {}) - folder_children_save_block = lambda do |ems, inventory_collection| - cluster_collection = inventory_collection.dependency_attributes[:clusters].try(:first) - vm_collection = inventory_collection.dependency_attributes[:vms].try(:first) - template_collection = inventory_collection.dependency_attributes[:templates].try(:first) - datacenter_collection = inventory_collection.dependency_attributes[:datacenters].try(:first) - - vms_and_templates = vm_collection.data + template_collection.data - indexed_vms_and_templates = vms_and_templates.each_with_object({}) { |vm, obj| (obj[vm.ems_cluster.ems_ref] ||= []) << vm } - - datacenter_collection.data.each do |dc| - uid = dc.uid_ems - - clusters = cluster_collection.data.select { |cluster| cluster.datacenter_id == uid } - cluster_refs = clusters.map(&:ems_ref) - - vms = cluster_refs.map { |x| indexed_vms_and_templates[x] }.flatten.compact - - ActiveRecord::Base.transaction do - host_folder = EmsFolder.find_by(:uid_ems => "#{uid}_host") - cs = EmsCluster.find(clusters.map(&:id)) - host_folder.with_relationship_type("ems_metadata") { host_folder.add_child cs } - - vm_folder = EmsFolder.find_by(:uid_ems => "#{uid}_vm") - vs = VmOrTemplate.find(vms.map(&:id)) - vm_folder.with_relationship_type("ems_metadata") { vm_folder.add_child vs } - - datacenter = EmsFolder.find(dc.id) - datacenter.with_relationship_type("ems_metadata") { datacenter.add_child host_folder } - datacenter.with_relationship_type("ems_metadata") { datacenter.add_child vm_folder } - root_dc = EmsFolder.find_by(:uid_ems => 'root_dc', :ems_id => ems.id) - root_dc.with_relationship_type("ems_metadata") { root_dc.add_child datacenter } - ems.with_relationship_type("ems_metadata") { ems.add_child root_dc } - end - end - end - - attributes = { - :association => :ems_folder_children, - :custom_save_block => folder_children_save_block, - } - - attributes.merge!(extra_attributes) - end - - def ems_clusters_children(extra_attributes = {}) - ems_cluster_children_save_block = lambda do |_ems, inventory_collection| - cluster_collection = inventory_collection.dependency_attributes[:clusters].try(:first) - vm_collection = inventory_collection.dependency_attributes[:vms].try(:first) - - cluster_collection.each do |cluster| - vms = vm_collection.data.select { |vm| cluster.ems_ref == vm.ems_cluster.ems_ref } - - ActiveRecord::Base.transaction do - vs = VmOrTemplate.find(vms.map(&:id)) - rp = ResourcePool.find_by(:uid_ems => "#{cluster.uid_ems}_respool") - rp.with_relationship_type("ems_metadata") { rp.add_child vs } - c = EmsCluster.find(cluster.id) - c.with_relationship_type("ems_metadata") { c.add_child rp } - end - end - end - - attributes = { - :association => :ems_cluster_children, - :custom_save_block => ems_cluster_children_save_block, - } - - attributes.merge!(extra_attributes) - end - end -end diff --git a/app/models/manageiq/providers/redhat/inventory_collection_default/network_manager.rb b/app/models/manageiq/providers/redhat/inventory_collection_default/network_manager.rb deleted file mode 100644 index 5b5edc81f..000000000 --- a/app/models/manageiq/providers/redhat/inventory_collection_default/network_manager.rb +++ /dev/null @@ -1,19 +0,0 @@ -class ManageIQ::Providers::Redhat::InventoryCollectionDefault::NetworkManager < ManageIQ::Providers::Openstack::InventoryCollectionDefault::NetworkManager - class << self - def cloud_tenants(extra_attributes = {}) - attributes = { - :model_class => ManageIQ::Providers::Openstack::CloudManager::CloudTenant, - :association => :cloud_tenants, - :inventory_object_attributes => [ - :type, - :name, - :description, - :enabled, - :parent, - :ems_ref - ] - } - attributes.merge!(extra_attributes) - end - end -end