diff --git a/app/models/manageiq/providers/redhat/infra_manager/refresh/refresher.rb b/app/models/manageiq/providers/redhat/infra_manager/refresh/refresher.rb index 31f4e6a4d..828795d00 100644 --- a/app/models/manageiq/providers/redhat/infra_manager/refresh/refresher.rb +++ b/app/models/manageiq/providers/redhat/infra_manager/refresh/refresher.rb @@ -37,7 +37,6 @@ def collect_inventory_for_targets(ems, targets) end def preprocess_targets - # See if any should be escalated to a full refresh and do not use full_refresh_threshold @targets_by_ems_id.each do |ems_id, targets| ems = @ems_by_ems_id[ems_id] ems_in_list = targets.any? { |t| t.kind_of?(ExtManagementSystem) } diff --git a/app/models/manageiq/providers/redhat/inventory/collector.rb b/app/models/manageiq/providers/redhat/inventory/collector.rb index 270b0c8f7..7e42bc046 100644 --- a/app/models/manageiq/providers/redhat/inventory/collector.rb +++ b/app/models/manageiq/providers/redhat/inventory/collector.rb @@ -1,9 +1,9 @@ class ManageIQ::Providers::Redhat::Inventory::Collector < ManagerRefresh::Inventory::Collector + # TODO: review the changes here and find common parts with ManageIQ::Providers::Redhat::InfraManager::Inventory::Strategies::V4 require_nested :InfraManager require_nested :TargetCollection - attr_reader :clusters - attr_reader :vmpools + attr_reader :ems_clusters attr_reader :networks attr_reader :storagedomains attr_reader :datacenters @@ -11,15 +11,16 @@ class ManageIQ::Providers::Redhat::Inventory::Collector < ManagerRefresh::Invent attr_reader :vms attr_reader :templates - def initialize(_manager, _target) + VERSION_HASH = {:version => 4}.freeze + + def initialize(manager, _target) super initialize_inventory_sources end def initialize_inventory_sources - @clusters = [] - @vmpools = [] + @ems_clusters = [] @networks = [] @storagedomains = [] @datacenters = [] @@ -28,7 +29,70 @@ def initialize_inventory_sources @templates = [] end - def hash_collection - ::ManageIQ::Providers::Redhat::Inventory::HashCollection + def collect_ems_clusters + manager.with_provider_connection(VERSION_HASH) do |connection| + connection.system_service.clusters_service.list + end + end + + def collect_networks + manager.with_provider_connection(VERSION_HASH) do |connection| + connection.system_service.networks_service.list + end + end + + def collect_storagedomains + manager.with_provider_connection(VERSION_HASH) do |connection| + connection.system_service.storage_domains_service.list + end + end + + def collect_datacenters + manager.with_provider_connection(VERSION_HASH) do |connection| + connection.system_service.data_centers_service.list + end + end + + def collect_attached_disks(disks_owner) + manager.with_provider_connection(VERSION_HASH) do |connection| + attachments = connection.follow_link(disks_owner.disk_attachments) + attachments.map do |attachment| + res = connection.follow_link(attachment.disk) + res.interface = attachment.interface + res.bootable = attachment.bootable + res.active = attachment.active + res + end + end + end + + def collect_nics(nic_owner) + manager.with_provider_connection(VERSION_HASH) do |connection| + connection.follow_link(nic_owner.nics) + end + end + + def collect_vm_devices(vm) + manager.with_provider_connection(VERSION_HASH) do |connection| + connection.follow_link(vm.reported_devices) + end + end + + def collect_snapshots(vm) + manager.with_provider_connection(VERSION_HASH) do |connection| + connection.follow_link(vm.snapshots) + end + end + + def collect_host_nics(host) + manager.with_provider_connection(VERSION_HASH) do |connection| + connection.follow_link(host.nics) + end + end + + def collect_dc_domains(dc) + manager.with_provider_connection(VERSION_HASH) do |connection| + connection.follow_link(dc.storage_domains) + end end end diff --git a/app/models/manageiq/providers/redhat/inventory/collector/infra_manager.rb b/app/models/manageiq/providers/redhat/inventory/collector/infra_manager.rb index b17b54893..9ff306965 100644 --- a/app/models/manageiq/providers/redhat/inventory/collector/infra_manager.rb +++ b/app/models/manageiq/providers/redhat/inventory/collector/infra_manager.rb @@ -1,33 +1,41 @@ class ManageIQ::Providers::Redhat::Inventory::Collector::InfraManager < ManageIQ::Providers::Redhat::Inventory::Collector - def clusters - # TODO - end - - def vmpools - # TODO + def ems_clusters + collect_ems_clusters end def networks - # TODO + collect_networks end def storagedomains - # TODO + collect_storagedomains end def datacenters - # TODO + collect_datacenters end def hosts - # TODO + manager.with_provider_connection(VERSION_HASH) do |connection| + connection.system_service.hosts_service.list + end + end + + def host_stats(host) + manager.with_provider_connection(VERSION_HASH) do |connection| + connection.link?(host.statistics) ? connection.follow_link(host.statistics) : host.statistics + end end def vms - # TODO + manager.with_provider_connection(VERSION_HASH) do |connection| + connection.system_service.vms_service.list + end end def templates - # TODO + manager.with_provider_connection(VERSION_HASH) do |connection| + connection.system_service.templates_service.list + end end end diff --git a/app/models/manageiq/providers/redhat/inventory/collector/target_collection.rb b/app/models/manageiq/providers/redhat/inventory/collector/target_collection.rb index 14ba5184b..5b753c2d2 100644 --- a/app/models/manageiq/providers/redhat/inventory/collector/target_collection.rb +++ b/app/models/manageiq/providers/redhat/inventory/collector/target_collection.rb @@ -1,4 +1,5 @@ class ManageIQ::Providers::Redhat::Inventory::Collector::TargetCollection < ManageIQ::Providers::Redhat::Inventory::Collector + # TODO: review the changes here and find common parts with ManageIQ::Providers::Redhat::InfraManager::Inventory::Strategies::V4 def initialize(_manager, _target) super parse_targets! @@ -8,42 +9,105 @@ def initialize(_manager, _target) target.manager_refs_by_association_reset end - def clusters - # TODO - end + def ems_clusters + clusters = [] + return clusters if references(:ems_clusters).blank? + + manager.with_provider_connection(VERSION_HASH) do |connection| + references(:ems_clusters).each do |id| + clusters += connection.system_service.clusters_service.cluster_service(id).get + end + end - def vmpools - # TODO + clusters end def networks - # TODO + nets = [] + return domains if references(:networks).blank? + + manager.with_provider_connection(VERSION_HASH) do |connection| + references(:networks).each do |id| + nets += connection.system_service.networks_service.network_service(id).get + end + end + + nets end def storagedomains - # TODO + domains = [] + return domains if references(:storagedomains).blank? + + manager.with_provider_connection(VERSION_HASH) do |connection| + references(:storagedomains).each do |id| + domains += connection.system_service.storage_domains_service.storage_domain_service(id).get + end + end + + domains end def datacenters - # TODO + dcs = [] + return dcs if references(:datacenters).blank? + + manager.with_provider_connection(VERSION_HASH) do |connection| + references(:datacenters).each do |id| + dcs += connection.system_service.data_centers_service.data_center_service(id).get + end + end + + dcs end def hosts - # TODO + h = [] + return h if references(:hosts).blank? + + manager.with_provider_connection(VERSION_HASH) do |connection| + references(:vms).each do |ems_ref| + h += connection.system_service.hosts_service.host_service(uuid_from_ems_ref(ems_ref)).get + end + end + + h end def vms - # TODO + v = [] + return v if references(:vms).blank? + + manager.with_provider_connection(VERSION_HASH) do |connection| + references(:vms).each do |ems_ref| + v += connection.system_service.vms_service.vm_service(uuid_from_ems_ref(ems_ref)).get + end + end + + v end def templates - # TODO + temp = [] + return temp if references(:templates).blank? + + manager.with_provider_connection(VERSION_HASH) do |connection| + references(:templates).each do |id| + temp += connection.system_service.templates_service.list(:search => "vm.id=#{id}") + end + end + + temp end def references(collection) target.manager_refs_by_association.try(:[], collection).try(:[], :ems_ref).try(:to_a) || [] end + def name_references(collection) + target.manager_refs_by_association.try(:[], collection).try(:[], :name).try(:to_a) || [] + end + def parse_targets! target.targets.each do |t| case t @@ -63,25 +127,78 @@ def parse_host_target!(t) add_simple_target!(:hosts, t.ems_ref) end - def add_simple_target!(association, ems_ref) - return if ems_ref.blank? - - target.add_target(:association => association, :manager_ref => {:ems_ref => ems_ref}) - end - def infer_related_ems_refs! - # TODO: check whether we can do it for either vms or hosts - unless references(:vms).blank? || references(:hosts).blank? + unless references(:vms).blank? infer_related_vm_ems_refs_db! infer_related_vm_ems_refs_api! end + + unless references(:hosts).blank? + infer_related_host_ems_refs_db! + infer_related_host_ems_refs_api! + end end def infer_related_vm_ems_refs_db! - # TODO + changed_vms = manager.vms.where(:ems_ref => references(:vms)) + + changed_vms.each do |vm| + add_simple_target!(:ems_clusters, vm.ems_cluster.ems_ref) + vm.storages.collect(&:ems_ref).compact.each { |ems_ref| add_simple_target!(:storagedomains, ems_ref) } + add_simple_target!(:datacenters, vm.parent_datacenter.ems_ref) + add_simple_target!(:templates, vm.ems_ref) + end end def infer_related_vm_ems_refs_api! - # TODO + vms.each do |vm| + add_simple_target!(:ems_clusters, ems_ref_from_sdk(vm.cluster)) + disks = collect_attached_disks(vm) + disks.each do |disk| + disk.storage_domains.to_miq_a.each do |sd| + add_simple_target!(:storagedomains, ems_ref_from_sdk(sd)) + end + end + add_simple_target!(:datacenters, ems_ref_from_sdk(vm.cluster.data_center)) + add_simple_target!(:templates, ems_ref_from_sdk(vm)) + end + end + + def infer_related_host_ems_refs_db! + changed_hosts = manager.hosts.where(:ems_ref => references(:hosts)) + + changed_hosts.each do |host| + add_simple_target!(:ems_clusters, uuid_from_target(host.ems_cluster)) + # TODO: host.hardware.networks do not have ems_ref nor ems_uid + end + end + + def infer_related_host_ems_refs_api! + hosts.each do |host| + add_simple_target!(:ems_clusters, host.cluster.id) + host.network_attachments.each do |attachement| + add_simple_target!(:networks, attachement.network.id) + end + end + end + + private + + def uuid_from_target(t) + uuid_from_ems_ref(t.ems_ref) + end + + def uuid_from_ems_ref(ems_ref) + URI(ems_ref).path.split('/').last + end + + def ems_ref_from_sdk(object) + ManageIQ::Providers::Redhat::InfraManager.make_ems_ref(object.href) + end + + def add_simple_target!(association, ems_ref) + return if ems_ref.blank? + + target.add_target(:association => association, :manager_ref => {:ems_ref => ems_ref}) end end diff --git a/app/models/manageiq/providers/redhat/inventory/parser/infra_manager.rb b/app/models/manageiq/providers/redhat/inventory/parser/infra_manager.rb index 38b7f6985..92200b3d8 100644 --- a/app/models/manageiq/providers/redhat/inventory/parser/infra_manager.rb +++ b/app/models/manageiq/providers/redhat/inventory/parser/infra_manager.rb @@ -1,83 +1,519 @@ class ManageIQ::Providers::Redhat::Inventory::Parser::InfraManager < ManageIQ::Providers::Redhat::Inventory::Parser + # TODO: review the changes here and find common parts with ManageIQ::Providers::Redhat::InfraManager::Refresh::Parse::* def parse log_header = "MIQ(#{self.class.name}.#{__method__}) Collecting data for EMS name: [#{collector.manager.name}] id: [#{collector.manager.id}]" $rhevm_log.info("#{log_header}...") - clusters - vmpools - networks - storagedomains + ems_clusters datacenters + storagedomains hosts vms - templates $rhevm_log.info("#{log_header}...Complete") end - private + def ems_clusters + collector.ems_clusters.each do |cluster| + r_id = "#{cluster.id}_respool" - def clusters - parse_clusters(collector.clusters) - end + persister.resource_pools.find_or_build(:ems_uid => r_id).assign_attributes( + :name => "Default for Cluster #{cluster.name}", + :uid_ems => r_id, + :is_default => true, + ) - def vmpools - parse_vmpools(collector.vmpools) - end + ems_ref = ManageIQ::Providers::Redhat::InfraManager.make_ems_ref(cluster.href) - def networks - parse_networks(collector.networks) + persister.ems_clusters.find_or_build(ems_ref).assign_attributes( + :ems_ref => ems_ref, + :ems_ref_obj => ems_ref, + :uid_ems => cluster.id, + :name => cluster.name, + :datacenter_id => cluster.data_center.id, + ) + end end def storagedomains - parse_storagedomains(collector.storagedomains) + collector.storagedomains.each do |storagedomain| + storage_type = storagedomain.dig(:storage, :type).upcase + location = if storage_type == 'NFS' || storage_type == 'GLUSTERFS' + "#{storagedomain.dig(:storage, :address)}:#{storagedomain.dig(:storage, :path)}" + else + logical_units = storagedomain.dig(:storage, :volume_group, :logical_units) + logical_unit = logical_units && logical_units.first + logical_unit && logical_unit.id + end + + free = storagedomain.try(:available).to_i + used = storagedomain.try(:used).to_i + total = free + used + committed = storagedomain.try(:committed).to_i + + ems_ref = ManageIQ::Providers::Redhat::InfraManager.make_ems_ref(storagedomain.try(:href)) + + persister.storages.find_or_build(ems_ref).assign_attributes( + :ems_ref => ems_ref, + :ems_ref_obj => ems_ref, + :name => storagedomain.try(:name), + :store_type => storage_type, + :storage_domain_type => storagedomain.dig(:type, :downcase), + :total_space => total, + :free_space => free, + :uncommitted => total - committed, + :multiplehostaccess => true, + :location => location, + :master => storagedomain.try(:master) + ) + end end def datacenters - parse_datacenters(collector.datacenters) + collector.datacenters.each do |datacenter| + ems_ref = ManageIQ::Providers::Redhat::InfraManager.make_ems_ref(datacenter.href) + + persister.datacenters.find_or_build(datacenter.id).assign_attributes( + :name => datacenter.name, + :type => 'Datacenter', + :ems_ref => ems_ref, + :ems_ref_obj => ems_ref, + :uid_ems => datacenter.id, + ) + end end def hosts - parse_hosts(collector.hosts) + dcs = collector.datacenters + + collector.hosts.each do |host| + host_id = host.id + + power_state = host_inv.status + power_state, connection_state = case power_state + when 'up' then %w(on connected) + when 'maintenance' then [power_state, 'connected'] + when 'down' then %w(off disconnected) + when 'non_responsive' then %w(unknown connected) + else [power_state, 'disconnected'] + end + + hostname = host.address + hostname = hostname.split(',').first + + nics = collector.collect_host_nics(host) + ipaddress = host_to_ip(nics, hostname) || host.address + + ipmi_address = nil + if host.dig(:power_management, :type).to_s.include?('ipmi') + ipmi_address = host.dig(:power_management, :address) + end + + host_os_version = host.dig(:os, :version) + ems_ref = ManageIQ::Providers::Redhat::InfraManager.make_ems_ref(host_inv.href) + + cluster = persister.ems_clusters.lazy_find(host.dig(:cluster, :id)) + dc_id = cluster[:datacenter_id] + storage_ids = [] + dcs.each do |dc| + if dc_id == dc.id + # TODO: check whether we could resolve this in a lazy way + storage_ids << collector.collect_dc_domains(dc).to_miq_a.collect { |s| s[:id] } + end + end + + persister_host = persister.hosts.find_or_build(host.id).assign_attributes( + :type => 'ManageIQ::Providers::Redhat::InfraManager::Host', + :ems_ref => ems_ref, + :ems_ref_obj => ems_ref, + :name => host.name || hostname, + :hostname => hostname, + :ipaddress => ipaddress, + :uid_ems => host_id, + :vmm_vendor => 'redhat', + :vmm_product => host.type, + :vmm_version => extract_host_version(host_os_version), + :vmm_buildnumber => (host_os_version.build if host_os_version), + :connection_state => connection_state, + :power_state => power_state, + :ems_cluster => cluster, + :ipmi_address => ipmi_address, + :storages => storage_ids + ) + + host_operating_systems(persister_host, host, hostname) + networks = collector.collect_networks + switches(persister_host, host, nics, networks) + host_hardware(persister_host, host, networks, nics) + end + end + + def host_hardware(persister_host, host, networks, nics) + hdw = host.cpu + + memory_total_attr = host.statistics.to_miq_a.detect { |stat| stat.name == 'memory.total' } + memory_total = memory_total_attr && memory_total_attr.dig(:values, :first, :datum) + hw_info = host.hardware_information + + cpu_cores = hdw.dig(:topology, :cores) || 1 + cpu_sockets = hdw.dig(:topology, :sockets) || 1 + + persister_hardware = persister.hosts.find_or_build(persister_host).assign_attributes( + :cpu_speed => hdw.speed, + :cpu_type => hdw.name, + :memory_mb => memory_total.nil? ? 0 : memory_total.to_i / 1.megabyte, + :cpu_cores_per_socket => cpu_cores, + :cpu_sockets => cpu_sockets, + :cpu_total_cores => cpu_sockets * cpu_cores, + :manufacturer => hw_info.manufacturer, + :model => hw_info.product_name + ) + + host_networks(persister_hardware, nics) + host_guest_devices(persister_hardware, nics, networks) + end + + def host_networks(persister_hardware, nics) + nics.to_miq_a.each do |vnic| + # Get the ip section + ip = vnic.ip.presence || {} + + persister.host_nics.find_or_build(persister_hardware).assign_attributes( + :description => vnic.name, + :ipaddress => ip.address, + :subnet_mask => ip.netmask, + ) + end + end + + def host_guest_devices(persister_hardware, nics, networks) + nics.to_miq_a.each do |nic| + network = network_from_nic(nic, host, networks) + + switch_uid = network.try(:id) || network_name + + location = nil + location = $1 if nic.name =~ /(\d+)$/ + + persister.guest_devices.find_or_build(persister_hardware).assign_attributes( + :uid_ems => nic.id, + :device_name => nic.name, + :device_type => 'ethernet', + :location => location, + :present => true, + :controller_type => 'ethernet', + :switch => persister.switches.lazy_find(switch_uid), + :network => persister.host_nics.lazy_find(network.id) + ) + end + end + + def switches(persister_host, host, nics, networks) + nics.to_miq_a.each do |nic| + network = network_from_nic(nic, host, networks) + + persister_switch = persister.switches.find_or_build(persister_host).assign_attributes( + :uid_ems => uid, + :name => name, + ) + + lans(persister_switch, network) + end + end + + def network_for_nic(nic, host, networks) + network_id = nic.dig(:network, :id) + if network_id + # TODO: check to indexed_networks = networks.index_by(:id) + network = networks.detect { |n| n.id == network_id } + else + network_name = nic.dig(:network, :name) + cluster_id = host.dig(:cluster, :id) + cluster = persister.clusters.lazy_find(cluster_id) + datacenter_id = cluster.dig(:data_center, :id) + network = networks.detect { |n| n.name == network_name && n.dig(:data_center, :id) == datacenter_id } + end + + network + end + + def lans(persister_switch, network) + tag_value = nil + if network + uid = network.id + name = network.name + tag_value = network.try(:vlan).try(:id) + else + uid = name = network_name unless network_name.nil? + end + + if uid.nil? + return + end + + persister.lans.find_or_build_by( + :switch => persister_switch, + :uid_ems => uid + ).assign_attributes( + :name => name, + :uid_ems => uid, + :tag => tag_value + ) + end + + def host_operating_systems(persister_host, host, hostname) + persister.operating_systems.find_or_build_by(persister_host).assign_attributes( + :name => hostname, + :product_type => 'linux', + :product_name => extract_host_os_name(host), + :version => extract_host_os_full_version(host.os) + ) end def vms - parse_vms(collector.vms) + vms = Array(collector.vms) + Array(collector.templates) + + vms.each do |vm| + # Skip the place holder template + next if vm.id == '00000000-0000-0000-0000-000000000000' + + template = vm.href.include?('/templates/') + + ems_ref = ManageIQ::Providers::Redhat::InfraManager.make_ems_ref(vm.href) + + host_id = vm.try(:host).try(:id) + host_id = vm.try(:placement_policy).try(:hosts).try(:id) if host_id.blank? + + storages = storages(vm) + + persister_vm = persister.vms.find_or_build(vm.id).assign_attributes( + :type => template ? "ManageIQ::Providers::Redhat::InfraManager::Template" : "ManageIQ::Providers::Redhat::InfraManager::Vm", + :ems_ref => ems_ref, + :ems_ref_obj => ems_ref, + :uid_ems => vm.id, + :connection_state => "connected", + :vendor => "redhat", + :name => URI.decode(vm.name), + :location => "#{vm.id}.ovf", + :template => template, + :memory_reserve => vm_memory_reserve(vm), + :raw_power_state => template ? "never" : vm.status, + :boot_time => vm.try(:start_time), + :host => persister.hosts.lazy_find(host_id), + :ems_cluster => persister.ems_clusters.lazy_find(ManageIQ::Providers::Redhat::InfraManager.make_ems_ref(vm.cluster.href)), + :storages => storages, + :storage => storages.first, + :snapshots => snapshots(vm) + ) + + vm_hardware(persister_vm, vm) + operating_systems(persister_vm, vm) + custom_attributes(persister_vm, vm) + end end - def templates - parse_templates(collector.templates) + def storages(vm) + storages = [] + collector.collect_attached_disks(vm).to_miq_a.each do |disk| + disk.storage_domains.to_miq_a.each do |sd| + storages << persister.storages.lazy_find(ManageIQ::Providers::Redhat::InfraManager.make_ems_ref(sd.href)) + end + end + storages.compact! + storages.uniq! + + storages end - def parse_clusters(cluster) - # TODO + def vm_hardware(presister_vm, vm) + topology = vm.cpu.topology + cpu_socks = topology.try(:sockets) || 1 + cpu_cores = topology.try(:cores) || 1 + + persister_hardware = persister.hardwares.find_or_build(presister_vm).assign_attributes( + :guest_os => vm.dig(:os, :type), + :annotation => vm.try(:description), + :cpu_cores_per_socket => cpu_socks, + :cpu_sockets => cpu_cores, + :cpu_total_cores => cpu_cores * cpu_socks, + :memory_mb => vm.memory / 1.megabyte + ) + + hardware_disks(persister_hardware, vm) + addresses = hardware_networks(persister_hardware, vm) + hardware_guest_devices(persister_hardware, vm, addresses) end - def parse_vmpools(pools) - # TODO + def hardware_networks(persister_hardware, vm) + devices = collector.collect_vm_devices(vm) + device = devices[0] + + nets = device.ips if device + if nets.nil? + return + end + + addresses = [] + + nets.to_miq_a.each do |net| + addresses << net.address + persister.nics.find_or_build_by( + :hardware => persister_hardware, + :ipaddress => net.address + ).assign_attributes( + :ipaddress => net.address, + :hostname => net.fqdn + ) + end + addresses end - def parse_networks(networks) - # TODO + def hardware_disks(persister_hardware, vm) + disks = collector.collect_attached_disks(vm) + + disks = disks.to_miq_a.sort_by do |disk| + match = disk.try(:name).match(/disk[^\d]*(?\d+)/i) + [disk.try(:bootable) ? 0 : 1, match ? match[:index].to_i : Float::INFINITY, disk.name] + end.group_by { |d| d.try(:interface) } + + disks.each do |interface, devices| + devices.each_with_index do |device, index| + storage_domain = device.storage_domains && device.storage_domains.first + storage_ref = storage_domain && storage_domain.href + + persister.disks.find_or_build_by( + :hardware => persister_hardware, + :device_name => device.name + ).assign_attributes( + :device_name => device.name, + :device_type => 'disk', + :controller_type => interface, + :present => true, + :filename => device.id, + :location => index.to_s, + :size => device.provisioned_size.to_i, + :size_on_disk => device.actual_size.to_i, + :disk_type => device.sparse == true ? 'thin' : 'thick', + :mode => 'persistent', + :bootable => device.try(:bootable), + :storage => persister.storages.lazy_find(ManageIQ::Providers::Redhat::InfraManager.make_ems_ref(storage_ref)) + ) + end + end + end + + def hardware_guest_devices(persister_hardware, vm, addresses) + network = addresses.to_a.empty? ? nil : persister.nics.lazy_find(addresses[0]) + + collector.collect_nics(vm).each do |nic| + persister.guest_devices.find_or_build_by( + :hardware => persister_hardware, + :uid_ems => nic.id + ).assign_attributes( + :uid_ems => nic.id, + :device_name => nic.name, + :device_type => 'ethernet', + :controller_type => 'ethernet', + :address => nic.dig(:mac, :address), + :lan => nic.dig(:network, :id), + :network => network + ) + end + end + + def snapshots(vm) + snapshots = collector.collect_snapshots(vm) + parent_id = nil + snaps = [] + snapshots.each_with_index do |snapshot, idx| + name = description = snapshot.description + name = "Active Image" if name[0, 13] == '_ActiveImage_' + + snaps << persister.snapshots.find_or_build(:uid => snapshot.id).assign_attributes( + :uid_ems => snapshot.id, + :uid => snapshot.id, + :parent_uid => parent_id, + :name => name, + :description => description, + :create_time => snapshot.date.getutc, + :current => idx == snapshots.length - 1, + ) + parent_id = snapshot.id + end + snaps end - def parse_storagedomains(domains) - # TODO + def operating_systems(persister_vm, vm) + guest_os = vm.dig(:os, :type) + + persister.operating_systems.find_or_build(persister_vm).assign_attributes( + :product_name => guest_os.blank? ? "Other" : guest_os, + :system_type => vm.type + ) end - def parse_datacenters(datacenters) - # TODO + def custom_attributes(persister_vm, vm) + custom_attrs = vm.try(:custom_properties) + + custom_attrs.to_a.each do |ca| + persister.custom_attributes.find_or_build( + :resource => persister_vm, + :name => ca.name, + ).assign_attributes( + :section => 'custom_field', + :name => ca.name, + :value => ca.value.try(:truncate, 255), + :source => "VC", + ) + end end - def parse_hosts(hosts) - # TODO + private + + require 'ostruct' + + def vm_memory_reserve(vm) + in_bytes = vm.dig(:memory_policy, :guaranteed) + in_bytes.nil? ? nil : in_bytes / Numeric::MEGABYTE + end + + def host_to_ip(nics, hostname = nil) + ipaddress = nil + nics.to_miq_a.each do |nic| + ip_data = nic.ip + if !ip_data.nil? && !ip_data.gateway.blank? && !ip_data.address.blank? + ipaddress = ip_data.address + break + end + end + + unless ipaddress.nil? + unless [nil, "localhost", "localhost.localdomain", "127.0.0.1"].include?(hostname) + begin + ipaddress = Socket.getaddrinfo(hostname, nil)[0][3] + rescue => err + $rhevm_log.warn "IP lookup by hostname [#{hostname}]...Failed with the following error: #{err}" + end + end + end + + ipaddress + end + + def extract_host_version(host_os_version) + return unless host_os_version && host_os_version.major + + version = host_os_version.major + version = "#{version}.#{host_os_version.minor}" if host_os_version.minor + version end - def parse_vms(vms) - # TODO + def extract_host_os_name(host) + host_os = host.os + host_os && host_os.type || host.type end - def parse_templates(templates) - # TODO + def extract_host_os_full_version(host_os) + host_os.dig(:version, :full_version) 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 53c88a8b9..c3da30ce7 100644 --- a/app/models/manageiq/providers/redhat/inventory/persister/infra_manager.rb +++ b/app/models/manageiq/providers/redhat/inventory/persister/infra_manager.rb @@ -2,12 +2,37 @@ class ManageIQ::Providers::Redhat::Inventory::Persister::InfraManager < ManageIQ def initialize_inventory_collections add_inventory_collections( infra, - %i(datacenters emsfolders emsclusters hosts resourcepools vm_or_templates vm + %i(datacenters emsfolders ems_clusters hosts resourcepools vm_or_templates vm miq_templates storages custom_attributes customization_specs disks guest_devices hardwars lans miq_scsi_luns miq_scsi_targets networks operating_systems snapshots switchs system_services) ) - # TODO: check what needs to be added here + add_inventory_collection( + infra.datacenter_children( + :dependency_attributes => { + :folders => [ + [collections[:ems_clusters]], + [collections[:vms]] + ] + } + ) + ) + + add_inventory_collection( + infra.resource_pool_children( + :dependency_attributes => { + :vms => [collections[:vms]], + } + ) + ) + + add_inventory_collection( + infra.ems_clusters_children( + :dependency_attributes => { + :resource_pools => [collections[:resource_pools]], + } + ) + ) 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 9d7e49042..aa330048b 100644 --- a/app/models/manageiq/providers/redhat/inventory/persister/target_collection.rb +++ b/app/models/manageiq/providers/redhat/inventory/persister/target_collection.rb @@ -3,31 +3,50 @@ def initialize_inventory_collections add_targeted_inventory_collections add_remaining_inventory_collections([infra], :strategy => :local_db_find_references) - # TODO: check what needs to be added here + add_inventory_collection( + infra.datacenter_children( + :dependency_attributes => { + :folders => [ + [collections[:ems_clusters]], + [collections[:vms]] + ] + } + ) + ) + + add_inventory_collection( + infra.resource_pool_children( + :dependency_attributes => { + :vms => [collections[:vms]], + } + ) + ) + + add_inventory_collection( + infra.ems_clusters_children( + :dependency_attributes => { + :resource_pools => [collections[:resource_pools]], + } + ) + ) end def references(collection) target.manager_refs_by_association.try(:[], collection).try(:[], :ems_ref).try(:to_a) || [] end - # TODO: check whether needed - def name_references(collection) - target.manager_refs_by_association.try(:[], collection).try(:[], :name).try(:to_a) || [] - end - def infra ManageIQ::Providers::Redhat::InventoryCollectionDefault::InfraManager end def add_targeted_inventory_collections add_vms_inventory_collections(references(:vms)) - add_miq_templates_inventory_collections(references(:miq_templates)) - add_vms_and_miq_templates_inventory_collections(references(:vms) + references(:miq_templates)) - - # TODO: check what needs to be added here + add_clusters_inventory_collections(references(:ems_clusters)) + add_datacenters_inventory_collections(references(:datacenters)) + add_storages_inventory_collcetions(references(:storagedomains)) + add_hosts_inventory_collections(references(:hosts)) end - # TODO: check correctness def add_vms_inventory_collections(manager_refs) return if manager_refs.blank? @@ -45,29 +64,99 @@ def add_vms_inventory_collections(manager_refs) :strategy => :local_db_find_missing_references ) ) + add_inventory_collection( + infra.nics( + :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.custom_attributes( + :arel => CustomAttribute.where(:resource => manager_refs, :source => "VC"), + :strategy => :local_db_find_missing_references + ) + ) end - # TODO: check correctness - def add_miq_templates_inventory_collections(manager_refs) + def add_clusters_inventory_collections(manager_refs) return if manager_refs.blank? add_inventory_collection( - infra.miq_templates( - :arel => manager.miq_templates.where(:ems_ref => manager_refs), + 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 - # TODO: check correctness - def add_vms_and_miq_templates_inventory_collections(manager_refs) + def add_datacenters_inventory_collections(manager_refs) return if manager_refs.blank? add_inventory_collection( - infra.hardwares( - :arel => manager.hardwares.joins(:vm_or_template).where( - 'vms' => {:ems_ref => manager_refs} - ), + infra.datacenters( + :arel => manager.ems_folders.where(:ems_ref => manager_refs, :type => Datacenter), + :strategy => :local_db_find_missing_references + ) + ) + end + + def add_storages_inventory_collcetions(manager_refs) + return if manager_refs.blank? + + add_inventory_collection( + infra.storages( + :arel => Storage.where(:ems_ref => manager_refs), + :strategy => :local_db_find_missing_references + ) + ) + end + + def add_hosts_inventory_collections(manager_refs) + return if manager_refs.blank? + + add_inventory_collection( + infra.hosts( + :arel => manager.hosts.where(:ems_ref => manager_refs), :strategy => :local_db_find_missing_references ) ) 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 index a95aa2a3a..424721871 100644 --- a/app/models/manageiq/providers/redhat/inventory_collection_default/infra_manager.rb +++ b/app/models/manageiq/providers/redhat/inventory_collection_default/infra_manager.rb @@ -1,54 +1,227 @@ -class ManageIQ::Providers::Redhat::InventoryCollectionDefault::InfraManager < ManagerRefresh::InventoryCollectionDefault::CloudManager - # TODO: above ManagerRefresh::InventoryCollectionDefault::CloudManager is wrong we need InfraManager here +class ManageIQ::Providers::Redhat::InventoryCollectionDefault::InfraManager < ManagerRefresh::InventoryCollectionDefault::InfraManager class << self - # TODO: check correctness def vms(extra_attributes = {}) attributes = { - :model_class => ::ManageIQ::Providers::Redhat::InfraManager::Vm, + :model_class => ::ManageIQ::Providers::Redhat::InfraManager::Vm, + :inventory_object_attributes => [ + :type, + :ems_ref, + :ems_ref_obj, + :uid_ems, + :connection_state, + :vendor, + :name, + :location, + :template, + :memory_reserve, + :raw_power_state, + :boot_time, + :host, + :ems_cluster, + :storages, + :storage, + :snapshots + ] + } + + super(attributes.merge!(extra_attributes)) + end + + def disks(extra_attributes = {}) + attributes = { + :model_class => ::Disk, + :inventory_object_attributes => [ + :device_name, + :device_type, + :controller_type, + :present, + :filename, + :location, + :size, + :size_on_disk, + :disk_type, + :mode, + :bootable, + :storage + ] + } + + super(attributes.merge!(extra_attributes)) + end + + def nics(extra_attributes = {}) + attributes = { + :model_class => ::Network, + :inventory_object_attributes => [ + :ipaddress, + :hostname + ] + } + + super(attributes.merge!(extra_attributes)) + end + + def guest_devices(extra_attributes = {}) + attributes = { + :model_class => ::GuestDevice, + :inventory_object_attributes => [ + :uid_ems, + :device_name, + :device_type, + :controller_type, + :address, + :lan, + :network + ] + } + + super(attributes.merge!(extra_attributes)) + end + + def hardwares(extra_attributes = {}) + attributes = { + :model_class => ::Hardware, + :inventory_object_attributes => [ + :guest_os, + :annotation, + :cpu_cores_per_socket, + :cpu_sockets, + :cpu_total_cores, + :memory_mb + ] + } + + super(attributes.merge!(extra_attributes)) + end + + def snapshots(extra_attributes = {}) + attributes = { + :model_class => ::Snapshot, + :inventory_object_attributes => [ + :uid_ems, + :uid, + :parent_uid, + :name, + :description, + :create_time, + :current, + ] + } + + super(attributes.merge!(extra_attributes)) + end + + def operating_systems(extra_attributes = {}) + attributes = { + :model_class => ::OperatingSystem, + :inventory_object_attributes => [ + :product_name, + :system_type + ] + } + + super(attributes.merge!(extra_attributes)) + end + + def custom_attributes(extra_attributes = {}) + attributes = { + :model_class => ::CustomAttribute, + :inventory_object_attributes => [ + :section, + :name, + :value, + :source, + ] } + super(attributes.merge!(extra_attributes)) end - # TODO: check correctness - def miq_templates(extra_attributes = {}) + def datacenters(extra_attributes = {}) attributes = { - :model_class => ::ManageIQ::Providers::Redhat::InfraManager::Template, + :model_class => ::Datacenter, :inventory_object_attributes => [ + :name, :type, - :ext_management_system, + :ems_ref, + :ems_ref_obj, + :uid_ems, + ] + } + + super(attributes.merge!(extra_attributes)) + end + + def resource_pools(extra_attributes = {}) + attributes = { + :model_class => ::ResourcePool, + :inventory_object_attributes => [ + :name, :uid_ems, + :is_default, + ] + } + + super(attributes.merge!(extra_attributes)) + end + + def ems_clusters(extra_attributes = {}) + attributes = { + :model_class => ::EmsCluster, + :inventory_object_attributes => [ :ems_ref, + :ems_ref_obj, + :uid_ems, :name, - :location, - :vendor, - :raw_power_state, - :template, - :publicly_available, + :datacenter_id, ] } super(attributes.merge!(extra_attributes)) end - # TODO: check correctness - def hardwares(extra_attributes = {}) + def storages(extra_attributes = {}) attributes = { + :model_class => ::Storage, :inventory_object_attributes => [ - :guest_os, - :bitness, - :virtualization_type, - :root_device_type, - :vm_or_template, + :ems_ref, + :ems_ref_obj, + :name, + :store_type, + :storage_domain_type, + :total_space, + :free_space, + :uncommitted, + :multiplehostaccess, + :location, + :master ] } super(attributes.merge!(extra_attributes)) end - # TODO: check correctness - def disks(extra_attributes = {}) + def hosts(extra_attributes = {}) attributes = { - :inventory_object_attributes => %i(hardware device_name location size backing), + :model_class => ::ManageIQ::Providers::Redhat::InfraManager::Host, + :inventory_object_attributes => [ + :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, + :storages + ] } super(attributes.merge!(extra_attributes)) diff --git a/spec/models/manageiq/providers/redhat/infra_manager/refresh/refresher/target_response_yamls/disks.yml b/spec/models/manageiq/providers/redhat/infra_manager/refresh/refresher/target_response_yamls/disks.yml new file mode 100644 index 000000000..999e3fd3d --- /dev/null +++ b/spec/models/manageiq/providers/redhat/infra_manager/refresh/refresher/target_response_yamls/disks.yml @@ -0,0 +1,103 @@ +--- +- !ruby/object:OvirtSDK4::Disk + href: "/ovirt-engine/api/disks/c413aff6-e988-4830-8b24-f74af66ced5a" + comment: + description: + id: c413aff6-e988-4830-8b24-f74af66ced5a + name: new_Disk1 + instance_type: + template: + vm: + vms: + active: true + actual_size: 0 + alias_: new_Disk1 + bootable: true + disk_profile: !ruby/object:OvirtSDK4::DiskProfile + href: "/ovirt-engine/api/diskprofiles/5ea19cf2-37fa-45fd-9034-87fe4c4d1c0a" + comment: + description: + id: 5ea19cf2-37fa-45fd-9034-87fe4c4d1c0a + name: + permissions: + qos: + storage_domain: + format: raw + image_id: 7c702634-a9e5-4107-8bb1-b0c3d900096d + initial_size: + interface: virtio_scsi + logical_name: + lun_storage: + openstack_volume_type: + permissions: !ruby/array:OvirtSDK4::List + internal: [] + ivars: + :@href: "/ovirt-engine/api/disks/c413aff6-e988-4830-8b24-f74af66ced5a/permissions" + propagate_errors: false + provisioned_size: 1073741824 + quota: !ruby/object:OvirtSDK4::Quota + href: + comment: + description: + id: 58ad9d24-00d3-00c3-01e2-00000000035b + name: + cluster_hard_limit_pct: + cluster_soft_limit_pct: + data_center: + disks: + permissions: + quota_cluster_limits: + quota_storage_limits: + storage_hard_limit_pct: + storage_soft_limit_pct: + users: + vms: + read_only: + sgio: + shareable: false + snapshot: + sparse: true + statistics: !ruby/array:OvirtSDK4::List + internal: [] + ivars: + :@href: "/ovirt-engine/api/disks/c413aff6-e988-4830-8b24-f74af66ced5a/statistics" + status: ok + storage_domain: + storage_domains: !ruby/array:OvirtSDK4::List + internal: + - !ruby/object:OvirtSDK4::StorageDomain + href: "/ovirt-engine/api/storagedomains/6cc26c9d-e1a7-43ba-95d3-c744442c7500" + comment: + description: + id: 6cc26c9d-e1a7-43ba-95d3-c744442c7500 + name: + available: + committed: + critical_space_action_blocker: + data_center: + data_centers: + disk_profiles: + disk_snapshots: + disks: + external_status: + files: + host: + images: + import: + master: + permissions: + status: + storage: + storage_connections: + storage_format: + templates: + type: + used: + vms: + warning_low_space_indicator: + wipe_after_delete: + ivars: + :@href: + storage_type: image + uses_scsi_reservation: + wipe_after_delete: false diff --git a/spec/models/manageiq/providers/redhat/infra_manager/refresh/refresher/target_response_yamls/nics.yml b/spec/models/manageiq/providers/redhat/infra_manager/refresh/refresher/target_response_yamls/nics.yml new file mode 100644 index 000000000..997068af9 --- /dev/null +++ b/spec/models/manageiq/providers/redhat/infra_manager/refresh/refresher/target_response_yamls/nics.yml @@ -0,0 +1,123 @@ +- !ruby/object:OvirtSDK4::Nic + href: "/ovirt-engine/api/vms/3a697bd0-7cea-42a1-95ef-fd292fcee721/nics/bec92d4f-9b9e-462f-9cd4-7d6b99948a81" + comment: + description: + id: bec92d4f-9b9e-462f-9cd4-7d6b99948a81 + name: nic1 + instance_type: + template: + vm: !ruby/object:OvirtSDK4::Vm + href: "/ovirt-engine/api/vms/3a697bd0-7cea-42a1-95ef-fd292fcee721" + comment: + description: + id: 3a697bd0-7cea-42a1-95ef-fd292fcee721 + name: + bios: + cluster: + console: + cpu: + cpu_profile: + cpu_shares: + creation_time: + custom_compatibility_version: + custom_cpu_model: + custom_emulated_machine: + custom_properties: + delete_protected: + display: + domain: + high_availability: + initialization: + io: + large_icon: + memory: + memory_policy: + migration: + migration_downtime: + origin: + os: + quota: + rng_device: + serial_number: + small_icon: + soundcard_enabled: + sso: + start_paused: + stateless: + storage_domain: + time_zone: + tunnel_migration: + type: + usb: + virtio_scsi: + affinity_labels: + applications: + cdroms: + disk_attachments: + external_host_provider: + floppies: + fqdn: + graphics_consoles: + guest_operating_system: + guest_time_zone: + host: + host_devices: + instance_type: + katello_errata: + next_run_configuration_exists: + nics: + numa_nodes: + numa_tune_mode: + payloads: + permissions: + placement_policy: + reported_devices: + run_once: + sessions: + snapshots: + start_time: + statistics: + status: + status_detail: + stop_reason: + stop_time: + tags: + template: + use_latest_template_version: + vm_pool: + watchdogs: + vms: + boot_protocol: + interface: virtio + linked: true + mac: !ruby/object:OvirtSDK4::Mac + href: + address: 00:1a:4a:16:01:51 + network: + network_attachments: + network_labels: + on_boot: + plugged: true + reported_devices: !ruby/array:OvirtSDK4::List + internal: [] + ivars: + :@href: "/ovirt-engine/api/vms/3a697bd0-7cea-42a1-95ef-fd292fcee721/nics/bec92d4f-9b9e-462f-9cd4-7d6b99948a81/reporteddevices" + statistics: !ruby/array:OvirtSDK4::List + internal: [] + ivars: + :@href: "/ovirt-engine/api/vms/3a697bd0-7cea-42a1-95ef-fd292fcee721/nics/bec92d4f-9b9e-462f-9cd4-7d6b99948a81/statistics" + virtual_function_allowed_labels: + virtual_function_allowed_networks: + vnic_profile: !ruby/object:OvirtSDK4::VnicProfile + href: "/ovirt-engine/api/vnicprofiles/0000000a-000a-000a-000a-000000000398" + comment: + description: + id: 0000000a-000a-000a-000a-000000000398 + name: + custom_properties: + network: + network_filter: + pass_through: + permissions: + port_mirroring: + qos: \ No newline at end of file diff --git a/spec/models/manageiq/providers/redhat/infra_manager/refresh/refresher/target_response_yamls/snapshots.yml b/spec/models/manageiq/providers/redhat/infra_manager/refresh/refresher/target_response_yamls/snapshots.yml new file mode 100644 index 000000000..6fc96c9fc --- /dev/null +++ b/spec/models/manageiq/providers/redhat/infra_manager/refresh/refresher/target_response_yamls/snapshots.yml @@ -0,0 +1,85 @@ +- !ruby/object:OvirtSDK4::Snapshot + href: "/ovirt-engine/api/vms/3a697bd0-7cea-42a1-95ef-fd292fcee721/snapshots/4af35c92-7c6e-4c23-b25e-e05cb29bed49" + comment: + description: Active VM + id: 4af35c92-7c6e-4c23-b25e-e05cb29bed49 + name: + bios: + cluster: + console: + cpu: + cpu_profile: + cpu_shares: + creation_time: + custom_compatibility_version: + custom_cpu_model: + custom_emulated_machine: + custom_properties: + delete_protected: + display: + domain: + high_availability: + initialization: + io: + large_icon: + memory: + memory_policy: + migration: + migration_downtime: + origin: + os: + quota: + rng_device: + serial_number: + small_icon: + soundcard_enabled: + sso: + start_paused: + stateless: + storage_domain: + time_zone: + tunnel_migration: + type: + usb: + virtio_scsi: + affinity_labels: + applications: + cdroms: + disk_attachments: + external_host_provider: + floppies: + fqdn: + graphics_consoles: + guest_operating_system: + guest_time_zone: + host: + host_devices: + instance_type: + katello_errata: + next_run_configuration_exists: + nics: + numa_nodes: + numa_tune_mode: + payloads: + permissions: + placement_policy: + reported_devices: + run_once: + sessions: + snapshots: + start_time: + statistics: + status: + status_detail: + stop_reason: + stop_time: + tags: + template: + use_latest_template_version: + vm_pool: + watchdogs: + date: !ruby/object:DateTime 2017-02-23 15:58:23.054000000 +01:00 + persist_memorystate: false + snapshot_status: ok + snapshot_type: active + vm: \ No newline at end of file diff --git a/spec/models/manageiq/providers/redhat/infra_manager/refresh/refresher/target_response_yamls/vms.yml b/spec/models/manageiq/providers/redhat/infra_manager/refresh/refresher/target_response_yamls/vms.yml index c3e287edf..1fc0a9354 100644 --- a/spec/models/manageiq/providers/redhat/infra_manager/refresh/refresher/target_response_yamls/vms.yml +++ b/spec/models/manageiq/providers/redhat/infra_manager/refresh/refresher/target_response_yamls/vms.yml @@ -28,7 +28,26 @@ cpu: cpu_profiles: custom_scheduling_policy_properties: - data_center: + data_center: !ruby/object:OvirtSDK4::DataCenter + href: "/ovirt-engine/api/datacenters/00000001-0001-0001-0001-000000000311" + comment: + description: + id: 00000001-0001-0001-0001-000000000311 + name: + clusters: + iscsi_bonds: + local: + mac_pool: + networks: + permissions: + qoss: + quota_mode: + quotas: + status: + storage_domains: + storage_format: + supported_versions: + version: display: error_handling: fencing_policy: diff --git a/spec/models/manageiq/providers/redhat/infra_manager/refresh/refresher_target_vm_4_spec.rb b/spec/models/manageiq/providers/redhat/infra_manager/refresh/refresher_target_vm_4_spec.rb index ed6605554..9794a1571 100644 --- a/spec/models/manageiq/providers/redhat/infra_manager/refresh/refresher_target_vm_4_spec.rb +++ b/spec/models/manageiq/providers/redhat/infra_manager/refresh/refresher_target_vm_4_spec.rb @@ -23,26 +23,39 @@ def load_response_mock_for(filename) end before(:each) do - inventory_wrapper_class = ManageIQ::Providers::Redhat::InfraManager::Inventory::Strategies::V4 - allow_any_instance_of(inventory_wrapper_class) - .to receive(:collect_clusters).and_return(load_response_mock_for('clusters')) - allow_any_instance_of(inventory_wrapper_class) - .to receive(:collect_datacenters).and_return(load_response_mock_for('datacenters')) - allow_any_instance_of(inventory_wrapper_class) - .to receive(:collect_vm_by_uuid).and_return(load_response_mock_for('vms')) - allow_any_instance_of(inventory_wrapper_class) - .to receive(:collect_storage).and_return(load_response_mock_for('storages')) - allow_any_instance_of(inventory_wrapper_class) - .to receive(:search_templates).and_return(load_response_mock_for('templates')) - allow_any_instance_of(inventory_wrapper_class).to receive(:api).and_return("4.2.0_master") - allow_any_instance_of(inventory_wrapper_class).to receive(:service) + @inventory_wrapper_class = ManageIQ::Providers::Redhat::InfraManager::Inventory::Strategies::V4 + allow_any_instance_of(@inventory_wrapper_class).to receive(:api).and_return("4.2.0_master") + allow_any_instance_of(@inventory_wrapper_class).to receive(:service) .and_return(OpenStruct.new(:version_string => '4.2.0_master')) + @root = FactoryGirl.create(:ems_folder, + :ext_management_system => @ems, + :name => "Datacenters") + + @host_folder = FactoryGirl.create(:ems_folder, + :ext_management_system => @ems, + :name => "host") + + @vm_folder = FactoryGirl.create(:ems_folder, + :ext_management_system => @ems, + :name => "vm") + + @dc = FactoryGirl.create(:datacenter, + :ems_ref => "/api/datacenters/00000001-0001-0001-0001-000000000311", + :ext_management_system => @ems, + :name => "Default", + :uid_ems => "00000001-0001-0001-0001-000000000311") + @cluster = FactoryGirl.create(:ems_cluster, - :ems_ref => "/api/clusters/00000002-0002-0002-0002-00000000017a", - :uid_ems => "00000002-0002-0002-0002-00000000017a", - :ems_id => @ems.id, - :name => "Default") + :ems_ref => "/api/clusters/00000002-0002-0002-0002-00000000017a", + :uid_ems => "00000002-0002-0002-0002-00000000017a", + :ext_management_system => @ems, + :name => "Default") + + @rp = FactoryGirl.create(:resource_pool, + :ext_management_system => @ems, + :name => "Default for Cluster Default", + :uid_ems => "00000002-0002-0002-0002-00000000017a_respool") @storage = FactoryGirl.create(:storage, :ems_ref => "/api/storagedomains/6cc26c9d-e1a7-43ba-95d3-c744442c7500", @@ -57,7 +70,7 @@ def load_response_mock_for(filename) @vm = FactoryGirl.create(:vm_redhat, :ext_management_system => @ems, :uid_ems => "3a697bd0-7cea-42a1-95ef-fd292fcee721", - :ems_cluster => @cluster, + :ems_cluster_id => @cluster.id, :ems_ref => "/api/vms/3a697bd0-7cea-42a1-95ef-fd292fcee721", :storage => @storage, :storages => [@storage], @@ -65,6 +78,17 @@ def load_response_mock_for(filename) end it "should refresh a vm" do + allow_any_instance_of(@inventory_wrapper_class) + .to receive(:collect_clusters).and_return(load_response_mock_for('clusters')) + allow_any_instance_of(@inventory_wrapper_class) + .to receive(:collect_datacenters).and_return(load_response_mock_for('datacenters')) + allow_any_instance_of(@inventory_wrapper_class) + .to receive(:collect_vm_by_uuid).and_return(load_response_mock_for('vms')) + allow_any_instance_of(@inventory_wrapper_class) + .to receive(:collect_storage).and_return(load_response_mock_for('storages')) + allow_any_instance_of(@inventory_wrapper_class) + .to receive(:search_templates).and_return(load_response_mock_for('templates')) + EmsRefresh.refresh(@vm) assert_table_counts @@ -77,14 +101,48 @@ def load_response_mock_for(filename) it "should collect a vm" do stub_settings_merge(:ems_refresh => { :rhevm => {:inventory_object_refresh => true }}) + cluster_service = double("cluster_service") + allow(cluster_service).to receive(:get).and_return(load_response_mock_for('clusters')) + allow_any_instance_of(OvirtSDK4::ClustersService).to receive(:cluster_service).and_return(cluster_service) + + data_center_service = double("data_center_service") + allow(data_center_service).to receive(:get).and_return(load_response_mock_for('datacenters')) + allow_any_instance_of(OvirtSDK4::DataCentersService).to receive(:data_center_service).and_return(data_center_service) + + storage_domain_service = double("storage_domain_service") + allow(storage_domain_service).to receive(:get).and_return(load_response_mock_for('storages')) + allow_any_instance_of(OvirtSDK4::StorageDomainsService).to receive(:storage_domain_service).and_return(storage_domain_service) + + collector_class = ManageIQ::Providers::Redhat::Inventory::Collector + allow_any_instance_of(collector_class).to receive(:collect_attached_disks).and_return(load_response_mock_for('disks')) + allow_any_instance_of(collector_class).to receive(:collect_vm_devices).and_return([]) + allow_any_instance_of(collector_class).to receive(:collect_nics).and_return(load_response_mock_for('nics')) + allow_any_instance_of(collector_class).to receive(:collect_snapshots).and_return(load_response_mock_for('snapshots')) + target_collector_class = ManageIQ::Providers::Redhat::Inventory::Collector::TargetCollection + allow_any_instance_of(target_collector_class).to receive(:templates).and_return(load_response_mock_for('templates')) + allow_any_instance_of(target_collector_class).to receive(:vms).and_return(load_response_mock_for('vms')) + + @rp.with_relationship_type("ems_metadata") { @rp.add_child(@vm) } + @vm.with_relationship_type("ems_metadata") { @vm.set_parent @rp } + + @cluster.with_relationship_type("ems_metadata") { @cluster.add_child @rp } + @rp.with_relationship_type("ems_metadata") { @rp.set_parent @cluster } + + @vm_folder.with_relationship_type("ems_metadata") { @vm_folder.add_child @vm } + @host_folder.with_relationship_type("ems_metadata") { @host_folder.add_child @cluster } + @dc.with_relationship_type("ems_metadata") { @dc.add_child @host_folder } + @dc.with_relationship_type("ems_metadata") { @dc.add_child @vm_folder } + + @root.with_relationship_type("ems_metadata") { @root.add_child @dc } + @ems.add_child @root + EmsRefresh.refresh(@vm) - # TODO: once implemented remove comments - # assert_table_counts - # assert_vm(@vm, @storage) - # assert_vm_rels(@vm, @hardware, @storage) - # assert_cluster(@vm) - # assert_storage(@storage, @vm) + assert_table_counts + assert_vm(@vm, @storage) + assert_vm_rels(@vm, @hardware, @storage) + assert_cluster(@vm) + assert_storage(@storage, @vm) end def assert_table_counts