From 0abcb771b7593f960fc3e2a631a3ce3b8091f9a0 Mon Sep 17 00:00:00 2001 From: Piotr Kliczewski Date: Fri, 12 May 2017 15:33:31 +0200 Subject: [PATCH 1/4] graph refresh: collector and parser implementations --- .../providers/redhat/inventory/collector.rb | 74 ++- .../inventory/collector/infra_manager.rb | 30 +- .../inventory/collector/target_collection.rb | 117 +++- .../redhat/inventory/parser/infra_manager.rb | 502 ++++++++++++++++-- .../inventory/persister/infra_manager.rb | 27 +- .../inventory/persister/target_collection.rb | 55 +- .../infra_manager.rb | 113 +++- 7 files changed, 811 insertions(+), 107 deletions(-) diff --git a/app/models/manageiq/providers/redhat/inventory/collector.rb b/app/models/manageiq/providers/redhat/inventory/collector.rb index 270b0c8f7..42869e3f3 100644 --- a/app/models/manageiq/providers/redhat/inventory/collector.rb +++ b/app/models/manageiq/providers/redhat/inventory/collector.rb @@ -3,7 +3,6 @@ class ManageIQ::Providers::Redhat::Inventory::Collector < ManagerRefresh::Invent require_nested :TargetCollection attr_reader :clusters - attr_reader :vmpools attr_reader :networks attr_reader :storagedomains attr_reader :datacenters @@ -11,7 +10,9 @@ 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 @@ -19,7 +20,6 @@ def initialize(_manager, _target) def initialize_inventory_sources @clusters = [] - @vmpools = [] @networks = [] @storagedomains = [] @datacenters = [] @@ -29,6 +29,74 @@ def initialize_inventory_sources end def hash_collection + # TODO: check whether needed ::ManageIQ::Providers::Redhat::Inventory::HashCollection end + + def collect_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..226f97d07 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 + collect_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..f77c2db7a 100644 --- a/app/models/manageiq/providers/redhat/inventory/collector/target_collection.rb +++ b/app/models/manageiq/providers/redhat/inventory/collector/target_collection.rb @@ -9,41 +9,88 @@ def initialize(_manager, _target) end def clusters - # TODO - end + if target.kind_of? VmOrTemplate + return collect_clusters + end - def vmpools - # TODO + [] end def networks - # TODO + if target.kind_of? Host + return collect_networks + end + + [] end def storagedomains - # TODO + if target.kind_of? VmOrTemplate + domains = if target.storages.empty? + collect_storagedomains + else + collect_storagedomain(target.storages.map { |s| uuid_from_target(s) }) + end + return domains + end + + [] + end + + def collect_storagedomain(uuids) + manager.with_provider_connection(VERSION_HASH) do |connection| + uuids.collect do |uuid| + connection.system_service.storage_domains_service.storage_domain_service(uuid).get + end + end end def datacenters - # TODO + if target.kind_of? VmOrTemplate + return collect_datacenters + end + + [] end def hosts - # TODO + if target.kind_of? Host + manager.with_provider_connection(VERSION_HASH) do |connection| + return connection.system_service.hosts_service.host_service(uuid_from_target(target)).get + end + end + + [] end def vms - # TODO + if target.kind_of? VmOrTemplate + manager.with_provider_connection(VERSION_HASH) do |connection| + return connection.system_service.vms_service.vm_service(uuid_from_target(target)).get + end + end + + [] end def templates - # TODO + if target.kind_of? VmOrTemplate + manager.with_provider_connection(VERSION_HASH) do |connection| + connection.system_service.templates_service.list(:search => "vm.id=#{uuid_from_target(target)}") + end + end + + [] 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 +110,55 @@ 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| + # TODO + end end def infer_related_vm_ems_refs_api! - # TODO + vms.each do |vm| + # TODO + end + end + + def infer_related_host_ems_refs_db! + changed_hosts = manager.hosts.where(:ems_ref => references(:hosts)) + + changed_hosts.each do |host| + # TODO + end + end + + def infer_related_host_ems_refs_api! + hosts.each do |host| + # TODO + end + end + + private + + def uuid_from_target(t) + URI(t.ems_ref).path.split('/').last + 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..50d258825 100644 --- a/app/models/manageiq/providers/redhat/inventory/parser/infra_manager.rb +++ b/app/models/manageiq/providers/redhat/inventory/parser/infra_manager.rb @@ -3,81 +3,513 @@ 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}...") + datacenters clusters - vmpools - networks storagedomains - datacenters hosts vms - templates $rhevm_log.info("#{log_header}...Complete") end - private - def clusters - parse_clusters(collector.clusters) - end + collector.clusters.each do |cluster| + persister.resource_pools.find_or_build(r_id).assign_attributes( + :name => "Default for Cluster #{cluster.name}", + :uid_ems => "#{cluster.id}_respool", + :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.clusters.find_or_build(cluster.id).assign_attributes( + :ems_ref => ems_ref, + :ems_ref_obj => ems_ref, + :uid_ems => cluster.id, + :name => cluster.name, + :datacenter_id => persister.datacenters.lazy_find(cluster.data_center.id).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 + uncommitted = total - committed + + ems_ref = ManageIQ::Providers::Redhat::InfraManager.make_ems_ref(storagedomain.try(:href)) + + persister.datacenters.find_or_build(storagedomain.id).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 => uncommitted, + :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) + + clusters = persister.clusters.lazy_find(host.dig(:cluster, :id)) + dc_ids = clusters.collect { |c| c[:datacenter_id] }.uniq + storage_ids = [] + dcs.each do |dc| + if dc_ids.include? dc.id + 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 => clusters, + :ipmi_address => ipmi_address, + :storages => storage_ids + ) + + host_operating_systems(persister_host, host, hostname) + networks = collector.collect_networks + switchs(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) + + if network + switch_uid = network.id + else + switch_uid = network_name unless network_name.nil? + end + + 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.switchs.lazy_find(switch_uid), + :network => persister.host_nics.lazy_find(network.id) + ) + end + end + + def switchs(persister_host, host, nics, networks) + nics.to_miq_a.each do |nic| + network = network_from_nic(nic, host, networks) + + persister_switch = persister.switchs.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 + 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.clusters.lazy_find(vm.cluster.id), + :storages => storages, + :storage => storages.first, + ) + + vm_hardware(persister_vm, vm) + snapshots(persister_vm, vm) + operating_systems(persister_vm, vm) + custom_attributes(persister_vm, vm) + end + end + + def storages(vm) + storages = [] + collector.attached_disks(vm).to_miq_a.each do |disk| + disk.storage_domains.to_miq_a.each do |sd| + storages << persister.storages.lazy_find(:ems_ref => sd.id) + end + end + storages.compact! + storages.uniq! + + storages + end + + def vm_hardware(presister_vm, vm) + topology = vm.cpu.topology + cpu_socks = topology.try(:sockets) || 1 + cpu_cores = topology.try(:cores) || 1 + + result[:memory_mb] = vm.memory / 1.megabyte + + 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 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 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_id = storage_domain && storage_domain.id + + 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(:ems_ref => storage_id) + ) + end + end end - def templates - parse_templates(collector.templates) + def hardware_guest_devices(persister_hardware, vm, addresses) + 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 => persister.nics.lazy_find(addresses[0]) + ) + end end - def parse_clusters(cluster) - # TODO + def snapshots(persister_vm, vm) + snapshots = collector.collect_snapshots(vm) + parent_id = nil + snapshots.each_with_index do |snapshot, idx| + name = description = snapshot.description + name = "Active Image" if name[0, 13] == '_ActiveImage_' + + persister.snapshots.find_or_build_by(persister_vm).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 end - def parse_vmpools(pools) - # TODO + def operating_systems(persister_vm, vm) + guest_os = vm.dig(:os, :type) + + persister.operating_systems.find_or_build_by(persister_vm).assign_attributes( + :product_name => guest_os.blank? ? "Other" : guest_os, + :system_type => vm.type + ) end - def parse_networks(networks) - # TODO + def custom_attributes(persister_vm, vm) + custom_attrs = vm.try(:custom_properties) + return result if custom_attrs.nil? + + custom_attrs.each do |ca| + persister.custom_attributes.find_or_build_by(persister_vm).assign_attributes( + :section => 'custom_field', + :name => ca.name, + :value => ca.value.try(:truncate, 255), + :source => "VC", + ) + end end - def parse_storagedomains(domains) - # 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 parse_datacenters(datacenters) - # TODO + 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 parse_hosts(hosts) - # TODO + 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..1ccaac51d 100644 --- a/app/models/manageiq/providers/redhat/inventory/persister/infra_manager.rb +++ b/app/models/manageiq/providers/redhat/inventory/persister/infra_manager.rb @@ -8,6 +8,31 @@ def initialize_inventory_collections operating_systems snapshots switchs system_services) ) - # TODO: check what needs to be added here + add_inventory_collection( + infra.datacenter_children( + :dependency_attributes => { + :folders => [ + [collections[:clusters]], + [collections[:vms]] + ] + } + ) + ) + + add_inventory_collection( + infra.resource_pool_children( + :dependency_attributes => { + :vms => [collections[:vms]], + } + ) + ) + + add_inventory_collection( + infra.cluster_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..cde91b376 100644 --- a/app/models/manageiq/providers/redhat/inventory/persister/target_collection.rb +++ b/app/models/manageiq/providers/redhat/inventory/persister/target_collection.rb @@ -3,7 +3,32 @@ 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[:clusters]], + # [collections[:vms]] + # ] + # } + # ) + # ) + + add_inventory_collection( + infra.resource_pool_children( + :dependency_attributes => { + :vms => [collections[:vms]], + } + ) + ) + + add_inventory_collection( + infra.cluster_children( + :dependency_attributes => { + :resource_pools => [collections[:resource_pools]], + } + ) + ) end def references(collection) @@ -21,8 +46,8 @@ def infra 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)) + add_resource_pools_inventory_collections(references(:resource_pools)) + add_clusters_inventory_collections(references(:clusters)) # TODO: check what needs to be added here end @@ -45,29 +70,33 @@ 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 + ) + ) end - # TODO: check correctness - def add_miq_templates_inventory_collections(manager_refs) + def add_resource_pools_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.resource_pools( + :arel => manager.resource_pools.where(:ems_ref => manager_refs), :strategy => :local_db_find_missing_references ) ) end - # TODO: check correctness - def add_vms_and_miq_templates_inventory_collections(manager_refs) + def add_clusters_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.clusters( + :arel => manager.clusters.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..436a830f4 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,119 @@ -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::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, + ] } + super(attributes.merge!(extra_attributes)) end - # TODO: check correctness - def miq_templates(extra_attributes = {}) + def disks(extra_attributes = {}) attributes = { - :model_class => ::ManageIQ::Providers::Redhat::InfraManager::Template, + :model_class => ::Disk, :inventory_object_attributes => [ - :type, - :ext_management_system, - :uid_ems, - :ems_ref, - :name, + :device_name, + :device_type, + :controller_type, + :present, + :filename, :location, - :vendor, - :raw_power_state, - :template, - :publicly_available, + :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 - # TODO: check correctness def hardwares(extra_attributes = {}) attributes = { + :model_class => ::Hardware, :inventory_object_attributes => [ :guest_os, - :bitness, - :virtualization_type, - :root_device_type, - :vm_or_template, + :annotation, + :cpu_cores_per_socket, + :cpu_sockets, + :cpu_total_cores, + :memory_mb ] } super(attributes.merge!(extra_attributes)) end - # TODO: check correctness - def disks(extra_attributes = {}) + def datacenters(extra_attributes = {}) attributes = { - :inventory_object_attributes => %i(hardware device_name location size backing), + :model_class => ::Datacenter, + :inventory_object_attributes => [ + :name, + :type, + :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 clusters(extra_attributes = {}) + attributes = { + :model_class => ::EmsCluster, + :inventory_object_attributes => [ + :ems_ref, + :ems_ref_obj, + :uid_ems, + :name, + :datacenter_id, + ] } super(attributes.merge!(extra_attributes)) From 53e60e342a1219309d28db74f73fd38552ccb0f0 Mon Sep 17 00:00:00 2001 From: Piotr Kliczewski Date: Wed, 17 May 2017 11:29:02 +0200 Subject: [PATCH 2/4] Initial persister implementation --- .../providers/redhat/inventory/collector.rb | 6 +-- .../inventory/collector/infra_manager.rb | 4 +- .../inventory/collector/target_collection.rb | 2 +- .../redhat/inventory/parser/infra_manager.rb | 6 +-- .../inventory/persister/infra_manager.rb | 4 +- .../inventory/persister/target_collection.rb | 40 ++++++++++++------- .../infra_manager.rb | 2 +- 7 files changed, 38 insertions(+), 26 deletions(-) diff --git a/app/models/manageiq/providers/redhat/inventory/collector.rb b/app/models/manageiq/providers/redhat/inventory/collector.rb index 42869e3f3..2c92ef5df 100644 --- a/app/models/manageiq/providers/redhat/inventory/collector.rb +++ b/app/models/manageiq/providers/redhat/inventory/collector.rb @@ -2,7 +2,7 @@ class ManageIQ::Providers::Redhat::Inventory::Collector < ManagerRefresh::Invent require_nested :InfraManager require_nested :TargetCollection - attr_reader :clusters + attr_reader :emsclusters attr_reader :networks attr_reader :storagedomains attr_reader :datacenters @@ -19,7 +19,7 @@ def initialize(manager, _target) end def initialize_inventory_sources - @clusters = [] + @emsclusters = [] @networks = [] @storagedomains = [] @datacenters = [] @@ -33,7 +33,7 @@ def hash_collection ::ManageIQ::Providers::Redhat::Inventory::HashCollection end - def collect_clusters + def collect_emsclusters manager.with_provider_connection(VERSION_HASH) do |connection| connection.system_service.clusters_service.list 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 226f97d07..683b01cd8 100644 --- a/app/models/manageiq/providers/redhat/inventory/collector/infra_manager.rb +++ b/app/models/manageiq/providers/redhat/inventory/collector/infra_manager.rb @@ -1,6 +1,6 @@ class ManageIQ::Providers::Redhat::Inventory::Collector::InfraManager < ManageIQ::Providers::Redhat::Inventory::Collector - def clusters - collect_clusters + def emsclusters + collect_emsclusters end def networks 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 f77c2db7a..5c7e5f182 100644 --- a/app/models/manageiq/providers/redhat/inventory/collector/target_collection.rb +++ b/app/models/manageiq/providers/redhat/inventory/collector/target_collection.rb @@ -10,7 +10,7 @@ def initialize(_manager, _target) def clusters if target.kind_of? VmOrTemplate - return collect_clusters + return collect_emsclusters 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 50d258825..edef428a7 100644 --- a/app/models/manageiq/providers/redhat/inventory/parser/infra_manager.rb +++ b/app/models/manageiq/providers/redhat/inventory/parser/infra_manager.rb @@ -13,7 +13,7 @@ def parse end def clusters - collector.clusters.each do |cluster| + collector.emsclusters.each do |cluster| persister.resource_pools.find_or_build(r_id).assign_attributes( :name => "Default for Cluster #{cluster.name}", :uid_ems => "#{cluster.id}_respool", @@ -22,7 +22,7 @@ def clusters ems_ref = ManageIQ::Providers::Redhat::InfraManager.make_ems_ref(cluster.href) - persister.clusters.find_or_build(cluster.id).assign_attributes( + persister.emsclusters.find_or_build(cluster.id).assign_attributes( :ems_ref => ems_ref, :ems_ref_obj => ems_ref, :uid_ems => cluster.id, @@ -51,7 +51,7 @@ def storagedomains ems_ref = ManageIQ::Providers::Redhat::InfraManager.make_ems_ref(storagedomain.try(:href)) - persister.datacenters.find_or_build(storagedomain.id).assign_attributes( + persister.storagedomains.find_or_build(storagedomain.id).assign_attributes( :ems_ref => ems_ref, :ems_ref_obj => ems_ref, :name => storagedomain.try(:name), 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 1ccaac51d..4d153715d 100644 --- a/app/models/manageiq/providers/redhat/inventory/persister/infra_manager.rb +++ b/app/models/manageiq/providers/redhat/inventory/persister/infra_manager.rb @@ -12,7 +12,7 @@ def initialize_inventory_collections infra.datacenter_children( :dependency_attributes => { :folders => [ - [collections[:clusters]], + [collections[:ems_clusters]], [collections[:vms]] ] } @@ -28,7 +28,7 @@ def initialize_inventory_collections ) add_inventory_collection( - infra.cluster_children( + infra.ems_clusters_children( :dependency_attributes => { :resource_pools => [collections[:resource_pools]], } 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 cde91b376..b4fde814a 100644 --- a/app/models/manageiq/providers/redhat/inventory/persister/target_collection.rb +++ b/app/models/manageiq/providers/redhat/inventory/persister/target_collection.rb @@ -3,16 +3,16 @@ def initialize_inventory_collections add_targeted_inventory_collections add_remaining_inventory_collections([infra], :strategy => :local_db_find_references) - # add_inventory_collection( - # infra.datacenter_children( - # :dependency_attributes => { - # :folders => [ - # [collections[:clusters]], - # [collections[:vms]] - # ] - # } - # ) - # ) + add_inventory_collection( + infra.datacenter_children( + :dependency_attributes => { + :folders => [ + [collections[:ems_clusters]], + [collections[:vms]] + ] + } + ) + ) add_inventory_collection( infra.resource_pool_children( @@ -23,7 +23,7 @@ def initialize_inventory_collections ) add_inventory_collection( - infra.cluster_children( + infra.emsclusters_children( :dependency_attributes => { :resource_pools => [collections[:resource_pools]], } @@ -47,7 +47,8 @@ def infra def add_targeted_inventory_collections add_vms_inventory_collections(references(:vms)) add_resource_pools_inventory_collections(references(:resource_pools)) - add_clusters_inventory_collections(references(:clusters)) + add_clusters_inventory_collections(references(:emsclusters)) + add_datacenters_inventory_collections(references(:datacenters)) # TODO: check what needs to be added here end @@ -95,8 +96,19 @@ def add_clusters_inventory_collections(manager_refs) return if manager_refs.blank? add_inventory_collection( - infra.clusters( - :arel => manager.clusters.where(:ems_ref => manager_refs), + infra.ems_clusters( + :arel => manager.ems_clusters.where(:ems_ref => manager_refs), + :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 ) ) 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 436a830f4..926d9e9a0 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 @@ -104,7 +104,7 @@ def resource_pools(extra_attributes = {}) super(attributes.merge!(extra_attributes)) end - def clusters(extra_attributes = {}) + def ems_clusters(extra_attributes = {}) attributes = { :model_class => ::EmsCluster, :inventory_object_attributes => [ From 8cf3210104a741f97c85146df98ab24bca1c9991 Mon Sep 17 00:00:00 2001 From: Piotr Kliczewski Date: Tue, 23 May 2017 09:47:49 +0200 Subject: [PATCH 3/4] Further improvments --- .../providers/redhat/infra_manager.rb | 4 +- .../providers/redhat/inventory/collector.rb | 6 +- .../inventory/collector/infra_manager.rb | 4 +- .../inventory/collector/target_collection.rb | 117 +++++++++++------ .../redhat/inventory/parser/infra_manager.rb | 67 +++++----- .../inventory/persister/infra_manager.rb | 2 +- .../inventory/persister/target_collection.rb | 100 ++++++++++++-- .../infra_manager.rb | 109 +++++++++++++++- .../refresher/target_response_yamls/disks.yml | 103 +++++++++++++++ .../refresher/target_response_yamls/nics.yml | 123 ++++++++++++++++++ .../target_response_yamls/snapshots.yml | 85 ++++++++++++ .../refresher/target_response_yamls/vms.yml | 21 ++- .../refresh/refresher_target_vm_4_spec.rb | 69 +++++++--- 13 files changed, 699 insertions(+), 111 deletions(-) create mode 100644 spec/models/manageiq/providers/redhat/infra_manager/refresh/refresher/target_response_yamls/disks.yml create mode 100644 spec/models/manageiq/providers/redhat/infra_manager/refresh/refresher/target_response_yamls/nics.yml create mode 100644 spec/models/manageiq/providers/redhat/infra_manager/refresh/refresher/target_response_yamls/snapshots.yml diff --git a/app/models/manageiq/providers/redhat/infra_manager.rb b/app/models/manageiq/providers/redhat/infra_manager.rb index 8158fb2ec..31e4f8537 100644 --- a/app/models/manageiq/providers/redhat/infra_manager.rb +++ b/app/models/manageiq/providers/redhat/infra_manager.rb @@ -18,6 +18,8 @@ class ManageIQ::Providers::Redhat::InfraManager < ManageIQ::Providers::InfraMana supports :provisioning supports :refresh_new_target + has_many :ems_custom_attributes, :through => :vms_and_templates + def refresher Refresh::RefresherBuilder.new(self).build end @@ -76,7 +78,7 @@ def self.provision_class(via) def vm_reconfigure(vm, options = {}) ovirt_services_for_reconfigure = ManageIQ::Providers::Redhat::InfraManager::OvirtServices::Builder.new(self) - .build(:use_highest_supported_version => true).new(:ems => self) + .build(:use_highest_supported_version => true).new(:ems => self) ovirt_services_for_reconfigure.vm_reconfigure(vm, options) end diff --git a/app/models/manageiq/providers/redhat/inventory/collector.rb b/app/models/manageiq/providers/redhat/inventory/collector.rb index 2c92ef5df..3275f9ea3 100644 --- a/app/models/manageiq/providers/redhat/inventory/collector.rb +++ b/app/models/manageiq/providers/redhat/inventory/collector.rb @@ -2,7 +2,7 @@ class ManageIQ::Providers::Redhat::Inventory::Collector < ManagerRefresh::Invent require_nested :InfraManager require_nested :TargetCollection - attr_reader :emsclusters + attr_reader :ems_clusters attr_reader :networks attr_reader :storagedomains attr_reader :datacenters @@ -19,7 +19,7 @@ def initialize(manager, _target) end def initialize_inventory_sources - @emsclusters = [] + @ems_clusters = [] @networks = [] @storagedomains = [] @datacenters = [] @@ -33,7 +33,7 @@ def hash_collection ::ManageIQ::Providers::Redhat::Inventory::HashCollection end - def collect_emsclusters + def collect_ems_clusters manager.with_provider_connection(VERSION_HASH) do |connection| connection.system_service.clusters_service.list 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 683b01cd8..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,6 +1,6 @@ class ManageIQ::Providers::Redhat::Inventory::Collector::InfraManager < ManageIQ::Providers::Redhat::Inventory::Collector - def emsclusters - collect_emsclusters + def ems_clusters + collect_ems_clusters end def networks 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 5c7e5f182..2a2c9a356 100644 --- a/app/models/manageiq/providers/redhat/inventory/collector/target_collection.rb +++ b/app/models/manageiq/providers/redhat/inventory/collector/target_collection.rb @@ -8,79 +8,95 @@ def initialize(_manager, _target) target.manager_refs_by_association_reset end - def clusters - if target.kind_of? VmOrTemplate - return collect_emsclusters + 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 - [] + clusters end def networks - if target.kind_of? Host - return collect_networks + 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 - if target.kind_of? VmOrTemplate - domains = if target.storages.empty? - collect_storagedomains - else - collect_storagedomain(target.storages.map { |s| uuid_from_target(s) }) - end - return domains - end - - [] - end + domains = [] + return domains if references(:storagedomains).blank? - def collect_storagedomain(uuids) manager.with_provider_connection(VERSION_HASH) do |connection| - uuids.collect do |uuid| - connection.system_service.storage_domains_service.storage_domain_service(uuid).get + references(:storagedomains).each do |id| + domains + connection.system_service.storage_domains_service.storage_domain_service(id).get end end + + domains end def datacenters - if target.kind_of? VmOrTemplate - return collect_datacenters + 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 - if target.kind_of? Host - manager.with_provider_connection(VERSION_HASH) do |connection| - return connection.system_service.hosts_service.host_service(uuid_from_target(target)).get + 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 - if target.kind_of? VmOrTemplate - manager.with_provider_connection(VERSION_HASH) do |connection| - return connection.system_service.vms_service.vm_service(uuid_from_target(target)).get + 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 - if target.kind_of? VmOrTemplate - manager.with_provider_connection(VERSION_HASH) do |connection| - connection.system_service.templates_service.list(:search => "vm.id=#{uuid_from_target(target)}") + 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) @@ -126,13 +142,24 @@ def infer_related_vm_ems_refs_db! changed_vms = manager.vms.where(:ems_ref => references(:vms)) changed_vms.each do |vm| - # TODO + add_simple_target!(:ems_clusters, uuid_from_target(vm.ems_cluster)) + vm.storages.collect(&:ems_ref).compact.each { |ems_ref| add_simple_target!(:storagedomains, uuid_from_ems_ref(ems_ref)) } + add_simple_target!(:datacenters, uuid_from_target(vm.parent_datacenter)) + add_simple_target!(:templates, uuid_from_target(vm)) end end def infer_related_vm_ems_refs_api! vms.each do |vm| - # TODO + add_simple_target!(:ems_clusters, vm.cluster.id) + disks = collect_attached_disks(vm) + disks.each do |disk| + disk.storage_domains.to_miq_a.each do |sd| + add_simple_target!(:storagedomains, sd.id) + end + end + add_simple_target!(:datacenters, vm.cluster.data_center.id) + add_simple_target!(:templates, vm.id) end end @@ -140,20 +167,28 @@ def infer_related_host_ems_refs_db! changed_hosts = manager.hosts.where(:ems_ref => references(:hosts)) changed_hosts.each do |host| - # TODO + 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| - # TODO + 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) - URI(t.ems_ref).path.split('/').last + uuid_from_ems_ref(t.ems_ref) + end + + def uuid_from_ems_ref(ems_ref) + URI(ems_ref).path.split('/').last end def add_simple_target!(association, ems_ref) 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 edef428a7..92be1bf17 100644 --- a/app/models/manageiq/providers/redhat/inventory/parser/infra_manager.rb +++ b/app/models/manageiq/providers/redhat/inventory/parser/infra_manager.rb @@ -3,8 +3,8 @@ 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}...") + ems_clusters datacenters - clusters storagedomains hosts vms @@ -12,22 +12,24 @@ def parse $rhevm_log.info("#{log_header}...Complete") end - def clusters - collector.emsclusters.each do |cluster| + def ems_clusters + collector.ems_clusters.each do |cluster| + r_id = "#{cluster.id}_respool" + persister.resource_pools.find_or_build(r_id).assign_attributes( :name => "Default for Cluster #{cluster.name}", - :uid_ems => "#{cluster.id}_respool", + :uid_ems => r_id, :is_default => true, ) ems_ref = ManageIQ::Providers::Redhat::InfraManager.make_ems_ref(cluster.href) - persister.emsclusters.find_or_build(cluster.id).assign_attributes( + 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 => persister.datacenters.lazy_find(cluster.data_center.id).id, + :datacenter_id => cluster.data_center.id, ) end end @@ -47,11 +49,10 @@ def storagedomains used = storagedomain.try(:used).to_i total = free + used committed = storagedomain.try(:committed).to_i - uncommitted = total - committed ems_ref = ManageIQ::Providers::Redhat::InfraManager.make_ems_ref(storagedomain.try(:href)) - persister.storagedomains.find_or_build(storagedomain.id).assign_attributes( + persister.storages.find_or_build(storagedomain.id).assign_attributes( :ems_ref => ems_ref, :ems_ref_obj => ems_ref, :name => storagedomain.try(:name), @@ -59,7 +60,7 @@ def storagedomains :storage_domain_type => storagedomain.dig(:type, :downcase), :total_space => total, :free_space => free, - :uncommitted => uncommitted, + :uncommitted => total - committed, :multiplehostaccess => true, :location => location, :master => storagedomain.try(:master) @@ -110,11 +111,12 @@ def hosts host_os_version = host.dig(:os, :version) ems_ref = ManageIQ::Providers::Redhat::InfraManager.make_ems_ref(host_inv.href) - clusters = persister.clusters.lazy_find(host.dig(:cluster, :id)) - dc_ids = clusters.collect { |c| c[:datacenter_id] }.uniq + cluster = persister.ems_clusters.lazy_find(host.dig(:cluster, :id)) + dc_id = cluster[:datacenter_id] storage_ids = [] dcs.each do |dc| - if dc_ids.include? dc.id + 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 @@ -133,14 +135,14 @@ def hosts :vmm_buildnumber => (host_os_version.build if host_os_version), :connection_state => connection_state, :power_state => power_state, - :ems_cluster => clusters, + :ems_cluster => cluster, :ipmi_address => ipmi_address, :storages => storage_ids ) host_operating_systems(persister_host, host, hostname) networks = collector.collect_networks - switchs(persister_host, host, nics, networks) + switches(persister_host, host, nics, networks) host_hardware(persister_host, host, networks, nics) end end @@ -187,11 +189,7 @@ def host_guest_devices(persister_hardware, nics, networks) nics.to_miq_a.each do |nic| network = network_from_nic(nic, host, networks) - if network - switch_uid = network.id - else - switch_uid = network_name unless network_name.nil? - end + switch_uid = network.try(:id) || network_name location = nil location = $1 if nic.name =~ /(\d+)$/ @@ -203,17 +201,17 @@ def host_guest_devices(persister_hardware, nics, networks) :location => location, :present => true, :controller_type => 'ethernet', - :switch => persister.switchs.lazy_find(switch_uid), + :switch => persister.switches.lazy_find(switch_uid), :network => persister.host_nics.lazy_find(network.id) ) end end - def switchs(persister_host, host, nics, networks) + def switches(persister_host, host, nics, networks) nics.to_miq_a.each do |nic| network = network_from_nic(nic, host, networks) - persister_switch = persister.switchs.find_or_build(persister_host).assign_attributes( + persister_switch = persister.switches.find_or_build(persister_host).assign_attributes( :uid_ems => uid, :name => name, ) @@ -225,12 +223,14 @@ def switchs(persister_host, host, nics, networks) 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) + # TODO: get rid of detect network = networks.detect { |n| n.name == network_name && n.dig(:data_center, :id) == datacenter_id } end @@ -300,7 +300,7 @@ def vms :raw_power_state => template ? "never" : vm.status, :boot_time => vm.try(:start_time), :host => persister.hosts.lazy_find(host_id), - :ems_cluster => persister.clusters.lazy_find(vm.cluster.id), + :ems_cluster => persister.ems_clusters.lazy_find(vm.cluster.id), :storages => storages, :storage => storages.first, ) @@ -314,9 +314,9 @@ def vms def storages(vm) storages = [] - collector.attached_disks(vm).to_miq_a.each do |disk| + 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(:ems_ref => sd.id) + storages << persister.storages.lazy_find(sd.id) end end storages.compact! @@ -330,8 +330,6 @@ def vm_hardware(presister_vm, vm) cpu_socks = topology.try(:sockets) || 1 cpu_cores = topology.try(:cores) || 1 - result[:memory_mb] = vm.memory / 1.megabyte - persister_hardware = persister.hardwares.find_or_build(presister_vm).assign_attributes( :guest_os => vm.dig(:os, :type), :annotation => vm.try(:description), @@ -398,13 +396,15 @@ def hardware_disks(persister_hardware, vm) :disk_type => device.sparse == true ? 'thin' : 'thick', :mode => 'persistent', :bootable => device.try(:bootable), - :storage => persister.storages.lazy_find(:ems_ref => storage_id) + :storage => persister.storages.lazy_find(storage_id) ) 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, @@ -416,7 +416,7 @@ def hardware_guest_devices(persister_hardware, vm, addresses) :controller_type => 'ethernet', :address => nic.dig(:mac, :address), :lan => nic.dig(:network, :id), - :network => persister.nics.lazy_find(addresses[0]) + :network => network ) end end @@ -428,7 +428,7 @@ def snapshots(persister_vm, vm) name = description = snapshot.description name = "Active Image" if name[0, 13] == '_ActiveImage_' - persister.snapshots.find_or_build_by(persister_vm).assign_attributes( + persister.snapshots.find_or_build(persister_vm).assign_attributes( :uid_ems => snapshot.id, :uid => snapshot.id, :parent_uid => parent_id, @@ -444,7 +444,7 @@ def snapshots(persister_vm, vm) def operating_systems(persister_vm, vm) guest_os = vm.dig(:os, :type) - persister.operating_systems.find_or_build_by(persister_vm).assign_attributes( + persister.operating_systems.find_or_build(persister_vm).assign_attributes( :product_name => guest_os.blank? ? "Other" : guest_os, :system_type => vm.type ) @@ -452,10 +452,9 @@ def operating_systems(persister_vm, vm) def custom_attributes(persister_vm, vm) custom_attrs = vm.try(:custom_properties) - return result if custom_attrs.nil? - custom_attrs.each do |ca| - persister.custom_attributes.find_or_build_by(persister_vm).assign_attributes( + custom_attrs.to_a.each do |ca| + persister.custom_attributes.find_or_build(persister_vm).assign_attributes( :section => 'custom_field', :name => ca.name, :value => ca.value.try(:truncate, 255), 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 4d153715d..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,7 +2,7 @@ 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) 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 b4fde814a..dcc8010de 100644 --- a/app/models/manageiq/providers/redhat/inventory/persister/target_collection.rb +++ b/app/models/manageiq/providers/redhat/inventory/persister/target_collection.rb @@ -23,7 +23,7 @@ def initialize_inventory_collections ) add_inventory_collection( - infra.emsclusters_children( + infra.ems_clusters_children( :dependency_attributes => { :resource_pools => [collections[:resource_pools]], } @@ -35,11 +35,6 @@ 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 @@ -47,13 +42,16 @@ def infra def add_targeted_inventory_collections add_vms_inventory_collections(references(:vms)) add_resource_pools_inventory_collections(references(:resource_pools)) - add_clusters_inventory_collections(references(:emsclusters)) + add_clusters_inventory_collections(references(:ems_clusters)) add_datacenters_inventory_collections(references(:datacenters)) - - # TODO: check what needs to be added here + add_storages_inventory_collcetions(references(:storages)) + add_hosts_inventory_collections(references(:hosts)) + add_guest_devices_inventory_collections(references(:guest_devices)) + add_snapshots_inventory_collections(references(:snapshots)) + add_operating_systems_inventory_collections(references(:operating_systems)) + add_custom_attributes_inventory_collections(references(:custom_attributes)) end - # TODO: check correctness def add_vms_inventory_collections(manager_refs) return if manager_refs.blank? @@ -79,6 +77,14 @@ def add_vms_inventory_collections(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 + ) + ) end def add_resource_pools_inventory_collections(manager_refs) @@ -113,4 +119,78 @@ def add_datacenters_inventory_collections(manager_refs) ) ) end + + def add_storages_inventory_collcetions(manager_refs) + return if manager_refs.blank? + + add_inventory_collection( + infra.storages( + :arel => manager.storages.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.storages( + :arel => manager.hosts.where(:ems_ref => manager_refs), + :strategy => :local_db_find_missing_references + ) + ) + end + + def add_guest_devices_inventory_collections(manager_refs) + return if manager_refs.blank? + + 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 + ) + ) + end + + def add_snapshots_inventory_collections(manager_refs) + return if manager_refs.blank? + + add_inventory_collection( + infra.snapshots( + :arel => Snapshot.joins(:vm_or_template).where( + 'vms' => {:ems_ref => manager_refs} + ), + :strategy => :local_db_find_missing_references + ) + ) + end + + def add_operating_systems_inventory_collections(manager_refs) + return if manager_refs.blank? + + add_inventory_collection( + infra.operating_systems( + :arel => OperatingSystem.joins(:vm_or_template).where( + 'vms' => {:ems_ref => manager_refs} + ), + :strategy => :local_db_find_missing_references + ) + ) + end + + def add_custom_attributes_inventory_collections(manager_refs) + return if manager_refs.blank? + + add_inventory_collection( + infra.custom_attributes( + :arel => manager.ems_custom_attributes.joins(:vm_or_template).where( + 'vms' => {:ems_ref => manager_refs} + ), + :strategy => :local_db_find_missing_references + ) + ) + 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 index 926d9e9a0..298a53763 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 @@ -2,7 +2,7 @@ class ManageIQ::Providers::Redhat::InventoryCollectionDefault::InfraManager < Ma class << self def vms(extra_attributes = {}) attributes = { - :model_class => ::ManageIQ::Providers::InfraManager::Vm, + :model_class => ::ManageIQ::Providers::Redhat::InfraManager::Vm, :inventory_object_attributes => [ :type, :ems_ref, @@ -60,6 +60,23 @@ def nics(extra_attributes = {}) 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, @@ -76,6 +93,49 @@ def hardwares(extra_attributes = {}) 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 + def datacenters(extra_attributes = {}) attributes = { :model_class => ::Datacenter, @@ -118,5 +178,52 @@ def ems_clusters(extra_attributes = {}) super(attributes.merge!(extra_attributes)) end + + def storages(extra_attributes = {}) + attributes = { + :model_class => ::Storage, + :inventory_object_attributes => [ + :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 + + def hosts(extra_attributes = {}) + attributes = { + :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)) + end end end 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..6d321d2eb 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,17 @@ 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')) @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", + # :ems_id => @ems.id, + :ext_management_system => @ems, + :name => "Default") @storage = FactoryGirl.create(:storage, :ems_ref => "/api/storagedomains/6cc26c9d-e1a7-43ba-95d3-c744442c7500", @@ -65,6 +56,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,6 +79,39 @@ 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')) + + allow(@vm).to receive(:ems_cluster).and_return(@cluster) + + dc = FactoryGirl.create(:datacenter, + :ems_ref => "/ovirt-engine/api/datacenters/00000001-0001-0001-0001-000000000311", + :name => "Default", + :uid_ems => "00000001-0001-0001-0001-000000000311") + + allow(@vm).to receive(:parent_datacenter).and_return(dc) + vms = double("vms") + allow(vms).to receive(:where).and_return([@vm]) + allow(@ems).to receive(:vms).and_return(vms) + EmsRefresh.refresh(@vm) # TODO: once implemented remove comments From c743ab60f30fcd68059e1fc0744dfa3e050dccef Mon Sep 17 00:00:00 2001 From: Piotr Kliczewski Date: Thu, 8 Jun 2017 12:05:19 +0200 Subject: [PATCH 4/4] Graph refresh - vm target When calling vm target refresh the db is updated properly using graph refresh code base. --- .../providers/redhat/infra_manager.rb | 4 +- .../redhat/infra_manager/refresh/refresher.rb | 1 - .../providers/redhat/inventory/collector.rb | 6 +- .../inventory/collector/target_collection.rb | 35 ++++---- .../redhat/inventory/parser/infra_manager.rb | 29 +++--- .../inventory/persister/target_collection.rb | 90 ++++++------------- .../infra_manager.rb | 1 + .../refresh/refresher_target_vm_4_spec.rb | 57 ++++++++---- 8 files changed, 109 insertions(+), 114 deletions(-) diff --git a/app/models/manageiq/providers/redhat/infra_manager.rb b/app/models/manageiq/providers/redhat/infra_manager.rb index 31e4f8537..8158fb2ec 100644 --- a/app/models/manageiq/providers/redhat/infra_manager.rb +++ b/app/models/manageiq/providers/redhat/infra_manager.rb @@ -18,8 +18,6 @@ class ManageIQ::Providers::Redhat::InfraManager < ManageIQ::Providers::InfraMana supports :provisioning supports :refresh_new_target - has_many :ems_custom_attributes, :through => :vms_and_templates - def refresher Refresh::RefresherBuilder.new(self).build end @@ -78,7 +76,7 @@ def self.provision_class(via) def vm_reconfigure(vm, options = {}) ovirt_services_for_reconfigure = ManageIQ::Providers::Redhat::InfraManager::OvirtServices::Builder.new(self) - .build(:use_highest_supported_version => true).new(:ems => self) + .build(:use_highest_supported_version => true).new(:ems => self) ovirt_services_for_reconfigure.vm_reconfigure(vm, options) end 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 3275f9ea3..7e42bc046 100644 --- a/app/models/manageiq/providers/redhat/inventory/collector.rb +++ b/app/models/manageiq/providers/redhat/inventory/collector.rb @@ -1,4 +1,5 @@ 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 @@ -28,11 +29,6 @@ def initialize_inventory_sources @templates = [] end - def hash_collection - # TODO: check whether needed - ::ManageIQ::Providers::Redhat::Inventory::HashCollection - end - def collect_ems_clusters manager.with_provider_connection(VERSION_HASH) do |connection| connection.system_service.clusters_service.list 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 2a2c9a356..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! @@ -14,7 +15,7 @@ def ems_clusters manager.with_provider_connection(VERSION_HASH) do |connection| references(:ems_clusters).each do |id| - clusters + connection.system_service.clusters_service.cluster_service(id).get + clusters += connection.system_service.clusters_service.cluster_service(id).get end end @@ -27,7 +28,7 @@ def networks manager.with_provider_connection(VERSION_HASH) do |connection| references(:networks).each do |id| - nets + connection.system_service.networks_service.network_service(id).get + nets += connection.system_service.networks_service.network_service(id).get end end @@ -40,7 +41,7 @@ def storagedomains 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 + domains += connection.system_service.storage_domains_service.storage_domain_service(id).get end end @@ -53,7 +54,7 @@ def datacenters 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 + dcs += connection.system_service.data_centers_service.data_center_service(id).get end end @@ -66,7 +67,7 @@ def hosts 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 + h += connection.system_service.hosts_service.host_service(uuid_from_ems_ref(ems_ref)).get end end @@ -79,7 +80,7 @@ def vms 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 + v += connection.system_service.vms_service.vm_service(uuid_from_ems_ref(ems_ref)).get end end @@ -92,7 +93,7 @@ def templates manager.with_provider_connection(VERSION_HASH) do |connection| references(:templates).each do |id| - temp + connection.system_service.templates_service.list(:search => "vm.id=#{id}") + temp += connection.system_service.templates_service.list(:search => "vm.id=#{id}") end end @@ -142,24 +143,24 @@ def infer_related_vm_ems_refs_db! changed_vms = manager.vms.where(:ems_ref => references(:vms)) changed_vms.each do |vm| - add_simple_target!(:ems_clusters, uuid_from_target(vm.ems_cluster)) - vm.storages.collect(&:ems_ref).compact.each { |ems_ref| add_simple_target!(:storagedomains, uuid_from_ems_ref(ems_ref)) } - add_simple_target!(:datacenters, uuid_from_target(vm.parent_datacenter)) - add_simple_target!(:templates, uuid_from_target(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! vms.each do |vm| - add_simple_target!(:ems_clusters, vm.cluster.id) + 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, sd.id) + add_simple_target!(:storagedomains, ems_ref_from_sdk(sd)) end end - add_simple_target!(:datacenters, vm.cluster.data_center.id) - add_simple_target!(:templates, vm.id) + add_simple_target!(:datacenters, ems_ref_from_sdk(vm.cluster.data_center)) + add_simple_target!(:templates, ems_ref_from_sdk(vm)) end end @@ -191,6 +192,10 @@ 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? 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 92be1bf17..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,4 +1,5 @@ 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}...") @@ -16,7 +17,7 @@ def ems_clusters collector.ems_clusters.each do |cluster| r_id = "#{cluster.id}_respool" - persister.resource_pools.find_or_build(r_id).assign_attributes( + 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, @@ -52,7 +53,7 @@ def storagedomains ems_ref = ManageIQ::Providers::Redhat::InfraManager.make_ems_ref(storagedomain.try(:href)) - persister.storages.find_or_build(storagedomain.id).assign_attributes( + persister.storages.find_or_build(ems_ref).assign_attributes( :ems_ref => ems_ref, :ems_ref_obj => ems_ref, :name => storagedomain.try(:name), @@ -230,7 +231,6 @@ def network_for_nic(nic, host, networks) cluster_id = host.dig(:cluster, :id) cluster = persister.clusters.lazy_find(cluster_id) datacenter_id = cluster.dig(:data_center, :id) - # TODO: get rid of detect network = networks.detect { |n| n.name == network_name && n.dig(:data_center, :id) == datacenter_id } end @@ -251,7 +251,7 @@ def lans(persister_switch, network) return end - persister.lans..find_or_build_by( + persister.lans.find_or_build_by( :switch => persister_switch, :uid_ems => uid ).assign_attributes( @@ -300,13 +300,13 @@ def vms :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(vm.cluster.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) - snapshots(persister_vm, vm) operating_systems(persister_vm, vm) custom_attributes(persister_vm, vm) end @@ -316,7 +316,7 @@ 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(sd.id) + storages << persister.storages.lazy_find(ManageIQ::Providers::Redhat::InfraManager.make_ems_ref(sd.href)) end end storages.compact! @@ -379,7 +379,7 @@ def hardware_disks(persister_hardware, vm) disks.each do |interface, devices| devices.each_with_index do |device, index| storage_domain = device.storage_domains && device.storage_domains.first - storage_id = storage_domain && storage_domain.id + storage_ref = storage_domain && storage_domain.href persister.disks.find_or_build_by( :hardware => persister_hardware, @@ -396,7 +396,7 @@ def hardware_disks(persister_hardware, vm) :disk_type => device.sparse == true ? 'thin' : 'thick', :mode => 'persistent', :bootable => device.try(:bootable), - :storage => persister.storages.lazy_find(storage_id) + :storage => persister.storages.lazy_find(ManageIQ::Providers::Redhat::InfraManager.make_ems_ref(storage_ref)) ) end end @@ -421,14 +421,15 @@ def hardware_guest_devices(persister_hardware, vm, addresses) end end - def snapshots(persister_vm, vm) + 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_' - persister.snapshots.find_or_build(persister_vm).assign_attributes( + snaps << persister.snapshots.find_or_build(:uid => snapshot.id).assign_attributes( :uid_ems => snapshot.id, :uid => snapshot.id, :parent_uid => parent_id, @@ -439,6 +440,7 @@ def snapshots(persister_vm, vm) ) parent_id = snapshot.id end + snaps end def operating_systems(persister_vm, vm) @@ -454,7 +456,10 @@ 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(persister_vm).assign_attributes( + 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), 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 dcc8010de..aa330048b 100644 --- a/app/models/manageiq/providers/redhat/inventory/persister/target_collection.rb +++ b/app/models/manageiq/providers/redhat/inventory/persister/target_collection.rb @@ -41,15 +41,10 @@ def infra def add_targeted_inventory_collections add_vms_inventory_collections(references(:vms)) - add_resource_pools_inventory_collections(references(:resource_pools)) add_clusters_inventory_collections(references(:ems_clusters)) add_datacenters_inventory_collections(references(:datacenters)) - add_storages_inventory_collcetions(references(:storages)) + add_storages_inventory_collcetions(references(:storagedomains)) add_hosts_inventory_collections(references(:hosts)) - add_guest_devices_inventory_collections(references(:guest_devices)) - add_snapshots_inventory_collections(references(:snapshots)) - add_operating_systems_inventory_collections(references(:operating_systems)) - add_custom_attributes_inventory_collections(references(:custom_attributes)) end def add_vms_inventory_collections(manager_refs) @@ -85,110 +80,83 @@ def add_vms_inventory_collections(manager_refs) :strategy => :local_db_find_missing_references ) ) - end - - def add_resource_pools_inventory_collections(manager_refs) - return if manager_refs.blank? - add_inventory_collection( - infra.resource_pools( - :arel => manager.resource_pools.where(:ems_ref => manager_refs), + infra.guest_devices( + :arel => GuestDevice.joins(:hardware => :vm_or_template).where( + :hardware => {'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), + infra.snapshots( + :arel => Snapshot.joins(:vm_or_template).where( + 'vms' => {:ems_ref => manager_refs} + ), :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), + infra.operating_systems( + :arel => OperatingSystem.joins(:vm_or_template).where( + 'vms' => {:ems_ref => manager_refs} + ), :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 => manager.storages.where(:ems_ref => manager_refs), + infra.custom_attributes( + :arel => CustomAttribute.where(:resource => manager_refs, :source => "VC"), :strategy => :local_db_find_missing_references ) ) end - def add_hosts_inventory_collections(manager_refs) + def add_clusters_inventory_collections(manager_refs) return if manager_refs.blank? add_inventory_collection( - infra.storages( - :arel => manager.hosts.where(:ems_ref => manager_refs), + infra.ems_clusters( + :arel => manager.ems_clusters.where(:ems_ref => manager_refs), :strategy => :local_db_find_missing_references ) ) - end - - def add_guest_devices_inventory_collections(manager_refs) - return if manager_refs.blank? - add_inventory_collection( - infra.guest_devices( - :arel => GuestDevice.joins(:hardware => :vm_or_template).where( - :hardware => {'vms' => {:ems_ref => manager_refs}} - ), + 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_snapshots_inventory_collections(manager_refs) + def add_datacenters_inventory_collections(manager_refs) return if manager_refs.blank? add_inventory_collection( - infra.snapshots( - :arel => Snapshot.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_operating_systems_inventory_collections(manager_refs) + def add_storages_inventory_collcetions(manager_refs) return if manager_refs.blank? add_inventory_collection( - infra.operating_systems( - :arel => OperatingSystem.joins(:vm_or_template).where( - 'vms' => {:ems_ref => manager_refs} - ), + infra.storages( + :arel => Storage.where(:ems_ref => manager_refs), :strategy => :local_db_find_missing_references ) ) end - def add_custom_attributes_inventory_collections(manager_refs) + def add_hosts_inventory_collections(manager_refs) return if manager_refs.blank? add_inventory_collection( - infra.custom_attributes( - :arel => manager.ems_custom_attributes.joins(:vm_or_template).where( - 'vms' => {:ems_ref => manager_refs} - ), + 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 298a53763..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 @@ -20,6 +20,7 @@ def vms(extra_attributes = {}) :ems_cluster, :storages, :storage, + :snapshots ] } 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 6d321d2eb..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 @@ -28,13 +28,35 @@ def load_response_mock_for(filename) 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, :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", :location => "192.168.1.107:/export/data") @@ -48,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], @@ -100,26 +122,27 @@ def load_response_mock_for(filename) 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')) - allow(@vm).to receive(:ems_cluster).and_return(@cluster) + @rp.with_relationship_type("ems_metadata") { @rp.add_child(@vm) } + @vm.with_relationship_type("ems_metadata") { @vm.set_parent @rp } - dc = FactoryGirl.create(:datacenter, - :ems_ref => "/ovirt-engine/api/datacenters/00000001-0001-0001-0001-000000000311", - :name => "Default", - :uid_ems => "00000001-0001-0001-0001-000000000311") + @cluster.with_relationship_type("ems_metadata") { @cluster.add_child @rp } + @rp.with_relationship_type("ems_metadata") { @rp.set_parent @cluster } - allow(@vm).to receive(:parent_datacenter).and_return(dc) - vms = double("vms") - allow(vms).to receive(:where).and_return([@vm]) - allow(@ems).to receive(:vms).and_return(vms) + @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