Skip to content

Commit

Permalink
Graph refresh - vm target
Browse files Browse the repository at this point in the history
When calling vm target refresh the db is updated properly using
graph refresh code base.
  • Loading branch information
pkliczewski committed Jun 30, 2017
1 parent c5bb3f3 commit 802338f
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 110 deletions.
2 changes: 0 additions & 2 deletions app/models/manageiq/providers/redhat/infra_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 0 additions & 5 deletions app/models/manageiq/providers/redhat/inventory/collector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,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

Expand All @@ -27,7 +27,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

Expand All @@ -40,7 +40,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

Expand All @@ -53,7 +53,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

Expand All @@ -66,7 +66,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

Expand All @@ -79,7 +79,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

Expand All @@ -92,7 +92,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

Expand Down Expand Up @@ -142,24 +142,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

Expand Down Expand Up @@ -191,6 +191,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?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,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,
Expand Down Expand Up @@ -52,7 +52,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),
Expand Down Expand Up @@ -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
Expand All @@ -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!
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -439,6 +440,7 @@ def snapshots(persister_vm, vm)
)
parent_id = snapshot.id
end
snaps
end

def operating_systems(persister_vm, vm)
Expand All @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def vms(extra_attributes = {})
:ems_cluster,
:storages,
:storage,
:snapshots
]
}

Expand Down
Loading

0 comments on commit 802338f

Please sign in to comment.