Skip to content

Commit

Permalink
Merge pull request #35 from pkliczewski/graph_refresh
Browse files Browse the repository at this point in the history
Graph refresh - target vm
  • Loading branch information
oourfali authored Jul 5, 2017
2 parents 9329b15 + c743ab6 commit 12639ef
Show file tree
Hide file tree
Showing 13 changed files with 1,447 additions and 148 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down
78 changes: 71 additions & 7 deletions app/models/manageiq/providers/redhat/inventory/collector.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
class ManageIQ::Providers::Redhat::Inventory::Collector < ManagerRefresh::Inventory::Collector
# TODO: review the changes here and find common parts with ManageIQ::Providers::Redhat::InfraManager::Inventory::Strategies::V4
require_nested :InfraManager
require_nested :TargetCollection

attr_reader :clusters
attr_reader :vmpools
attr_reader :ems_clusters
attr_reader :networks
attr_reader :storagedomains
attr_reader :datacenters
attr_reader :hosts
attr_reader :vms
attr_reader :templates

def initialize(_manager, _target)
VERSION_HASH = {:version => 4}.freeze

def initialize(manager, _target)
super

initialize_inventory_sources
end

def initialize_inventory_sources
@clusters = []
@vmpools = []
@ems_clusters = []
@networks = []
@storagedomains = []
@datacenters = []
Expand All @@ -28,7 +29,70 @@ def initialize_inventory_sources
@templates = []
end

def hash_collection
::ManageIQ::Providers::Redhat::Inventory::HashCollection
def collect_ems_clusters
manager.with_provider_connection(VERSION_HASH) do |connection|
connection.system_service.clusters_service.list
end
end

def collect_networks
manager.with_provider_connection(VERSION_HASH) do |connection|
connection.system_service.networks_service.list
end
end

def collect_storagedomains
manager.with_provider_connection(VERSION_HASH) do |connection|
connection.system_service.storage_domains_service.list
end
end

def collect_datacenters
manager.with_provider_connection(VERSION_HASH) do |connection|
connection.system_service.data_centers_service.list
end
end

def collect_attached_disks(disks_owner)
manager.with_provider_connection(VERSION_HASH) do |connection|
attachments = connection.follow_link(disks_owner.disk_attachments)
attachments.map do |attachment|
res = connection.follow_link(attachment.disk)
res.interface = attachment.interface
res.bootable = attachment.bootable
res.active = attachment.active
res
end
end
end

def collect_nics(nic_owner)
manager.with_provider_connection(VERSION_HASH) do |connection|
connection.follow_link(nic_owner.nics)
end
end

def collect_vm_devices(vm)
manager.with_provider_connection(VERSION_HASH) do |connection|
connection.follow_link(vm.reported_devices)
end
end

def collect_snapshots(vm)
manager.with_provider_connection(VERSION_HASH) do |connection|
connection.follow_link(vm.snapshots)
end
end

def collect_host_nics(host)
manager.with_provider_connection(VERSION_HASH) do |connection|
connection.follow_link(host.nics)
end
end

def collect_dc_domains(dc)
manager.with_provider_connection(VERSION_HASH) do |connection|
connection.follow_link(dc.storage_domains)
end
end
end
Original file line number Diff line number Diff line change
@@ -1,33 +1,41 @@
class ManageIQ::Providers::Redhat::Inventory::Collector::InfraManager < ManageIQ::Providers::Redhat::Inventory::Collector
def clusters
# TODO
end

def vmpools
# TODO
def ems_clusters
collect_ems_clusters
end

def networks
# TODO
collect_networks
end

def storagedomains
# TODO
collect_storagedomains
end

def datacenters
# TODO
collect_datacenters
end

def hosts
# TODO
manager.with_provider_connection(VERSION_HASH) do |connection|
connection.system_service.hosts_service.list
end
end

def host_stats(host)
manager.with_provider_connection(VERSION_HASH) do |connection|
connection.link?(host.statistics) ? connection.follow_link(host.statistics) : host.statistics
end
end

def vms
# TODO
manager.with_provider_connection(VERSION_HASH) do |connection|
connection.system_service.vms_service.list
end
end

def templates
# TODO
manager.with_provider_connection(VERSION_HASH) do |connection|
connection.system_service.templates_service.list
end
end
end
Original file line number Diff line number Diff line change
@@ -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!
Expand All @@ -8,42 +9,105 @@ def initialize(_manager, _target)
target.manager_refs_by_association_reset
end

def clusters
# TODO
end
def ems_clusters
clusters = []
return clusters if references(:ems_clusters).blank?

manager.with_provider_connection(VERSION_HASH) do |connection|
references(:ems_clusters).each do |id|
clusters += connection.system_service.clusters_service.cluster_service(id).get
end
end

def vmpools
# TODO
clusters
end

def networks
# TODO
nets = []
return domains if references(:networks).blank?

manager.with_provider_connection(VERSION_HASH) do |connection|
references(:networks).each do |id|
nets += connection.system_service.networks_service.network_service(id).get
end
end

nets
end

def storagedomains
# TODO
domains = []
return domains if references(:storagedomains).blank?

manager.with_provider_connection(VERSION_HASH) do |connection|
references(:storagedomains).each do |id|
domains += connection.system_service.storage_domains_service.storage_domain_service(id).get
end
end

domains
end

def datacenters
# TODO
dcs = []
return dcs if references(:datacenters).blank?

manager.with_provider_connection(VERSION_HASH) do |connection|
references(:datacenters).each do |id|
dcs += connection.system_service.data_centers_service.data_center_service(id).get
end
end

dcs
end

def hosts
# TODO
h = []
return h if references(:hosts).blank?

manager.with_provider_connection(VERSION_HASH) do |connection|
references(:vms).each do |ems_ref|
h += connection.system_service.hosts_service.host_service(uuid_from_ems_ref(ems_ref)).get
end
end

h
end

def vms
# TODO
v = []
return v if references(:vms).blank?

manager.with_provider_connection(VERSION_HASH) do |connection|
references(:vms).each do |ems_ref|
v += connection.system_service.vms_service.vm_service(uuid_from_ems_ref(ems_ref)).get
end
end

v
end

def templates
# TODO
temp = []
return temp if references(:templates).blank?

manager.with_provider_connection(VERSION_HASH) do |connection|
references(:templates).each do |id|
temp += connection.system_service.templates_service.list(:search => "vm.id=#{id}")
end
end

temp
end

def references(collection)
target.manager_refs_by_association.try(:[], collection).try(:[], :ems_ref).try(:to_a) || []
end

def name_references(collection)
target.manager_refs_by_association.try(:[], collection).try(:[], :name).try(:to_a) || []
end

def parse_targets!
target.targets.each do |t|
case t
Expand All @@ -63,25 +127,78 @@ def parse_host_target!(t)
add_simple_target!(:hosts, t.ems_ref)
end

def add_simple_target!(association, ems_ref)
return if ems_ref.blank?

target.add_target(:association => association, :manager_ref => {:ems_ref => ems_ref})
end

def infer_related_ems_refs!
# TODO: check whether we can do it for either vms or hosts
unless references(:vms).blank? || references(:hosts).blank?
unless references(:vms).blank?
infer_related_vm_ems_refs_db!
infer_related_vm_ems_refs_api!
end

unless references(:hosts).blank?
infer_related_host_ems_refs_db!
infer_related_host_ems_refs_api!
end
end

def infer_related_vm_ems_refs_db!
# TODO
changed_vms = manager.vms.where(:ems_ref => references(:vms))

changed_vms.each do |vm|
add_simple_target!(:ems_clusters, vm.ems_cluster.ems_ref)
vm.storages.collect(&:ems_ref).compact.each { |ems_ref| add_simple_target!(:storagedomains, ems_ref) }
add_simple_target!(:datacenters, vm.parent_datacenter.ems_ref)
add_simple_target!(:templates, vm.ems_ref)
end
end

def infer_related_vm_ems_refs_api!
# TODO
vms.each do |vm|
add_simple_target!(:ems_clusters, ems_ref_from_sdk(vm.cluster))
disks = collect_attached_disks(vm)
disks.each do |disk|
disk.storage_domains.to_miq_a.each do |sd|
add_simple_target!(:storagedomains, ems_ref_from_sdk(sd))
end
end
add_simple_target!(:datacenters, ems_ref_from_sdk(vm.cluster.data_center))
add_simple_target!(:templates, ems_ref_from_sdk(vm))
end
end

def infer_related_host_ems_refs_db!
changed_hosts = manager.hosts.where(:ems_ref => references(:hosts))

changed_hosts.each do |host|
add_simple_target!(:ems_clusters, uuid_from_target(host.ems_cluster))
# TODO: host.hardware.networks do not have ems_ref nor ems_uid
end
end

def infer_related_host_ems_refs_api!
hosts.each do |host|
add_simple_target!(:ems_clusters, host.cluster.id)
host.network_attachments.each do |attachement|
add_simple_target!(:networks, attachement.network.id)
end
end
end

private

def uuid_from_target(t)
uuid_from_ems_ref(t.ems_ref)
end

def uuid_from_ems_ref(ems_ref)
URI(ems_ref).path.split('/').last
end

def ems_ref_from_sdk(object)
ManageIQ::Providers::Redhat::InfraManager.make_ems_ref(object.href)
end

def add_simple_target!(association, ems_ref)
return if ems_ref.blank?

target.add_target(:association => association, :manager_ref => {:ems_ref => ems_ref})
end
end
Loading

0 comments on commit 12639ef

Please sign in to comment.