Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update host switch association #18427

Merged
merged 7 commits into from
Feb 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 20 additions & 38 deletions app/models/ems_refresh/save_inventory_infra.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
# - ems
# - storages
# - storage_profiles
# - distributed_virtual_switches
# - lans
# - ems_clusters
# - hosts
# - storages (link)
Expand All @@ -16,6 +18,8 @@
# - miq_scsi_luns
# - networks (if not already saved via guest_devices)
# - system_services
# - host_storages
# - host_switches
# - vms
# - storages (link)
# - operating_system
Expand Down Expand Up @@ -55,6 +59,7 @@ def save_ems_infra_inventory(ems, hashes, target = nil, disconnect = true)
child_keys = [
:storages,
:storage_profiles,
:distributed_virtual_switches,
:clusters,
:hosts,
:vms,
Expand All @@ -64,7 +69,7 @@ def save_ems_infra_inventory(ems, hashes, target = nil, disconnect = true)
:orchestration_templates,
:orchestration_stacks
]
old_switch_ids = ems.switches.pluck(:id)

# Save and link other subsections
save_child_inventory(ems, hashes, child_keys, target, disconnect)

Expand All @@ -78,7 +83,6 @@ def save_ems_infra_inventory(ems, hashes, target = nil, disconnect = true)

new_relats = hashes_relats(hashes)
link_ems_inventory(ems, target, prev_relats, new_relats, disconnect)
remove_obsolete_switches(ems, old_switch_ids)

ems
end
Expand Down Expand Up @@ -112,6 +116,14 @@ def save_storages_inventory(ems, hashes, target = nil, _disconnect = true)
end
end

def save_distributed_virtual_switches_inventory(ems, hashes, target = nil, disconnect = true)
target = ems if target.nil?
deletes = determine_deletes_using_association(ems, target, disconnect)

save_inventory_multi(ems.distributed_virtual_switches, hashes, deletes, [:uid_ems], [:lans])
store_ids_for_new_records(ems.distributed_virtual_switches, hashes, :uid_ems)
end

def save_hosts_inventory(ems, hashes, target = nil, disconnect = true)
target = ems if target.nil? && disconnect
log_header = "EMS: [#{ems.name}], id: [#{ems.id}]"
Expand All @@ -124,7 +136,7 @@ def save_hosts_inventory(ems, hashes, target = nil, disconnect = true)
[]
end

child_keys = [:operating_system, :switches, :hardware, :system_services, :host_storages]
child_keys = [:operating_system, :switches, :hardware, :system_services, :host_storages, :host_switches]
extra_keys = [:ems_cluster, :storages, :vms, :power_state, :ems_children]
remove_keys = child_keys + extra_keys

Expand Down Expand Up @@ -305,43 +317,13 @@ def save_miq_scsi_luns_inventory(miq_scsi_target, hashes)
end

def save_switches_inventory(host, hashes)
ems = host.ext_management_system
log_header = "EMS: [#{ems.name}], id: [#{ems.id}]"

already_saved, not_yet_saved = hashes.partition { |h| h[:id] }
save_inventory_multi(host.switches, not_yet_saved, [], [:uid_ems], :lans)
host_switches_hash = already_saved.collect { |switch| {:host_id => host.id, :switch_id => switch[:id]} }
save_inventory_multi(host.host_switches, host_switches_hash, [], [:host_id, :switch_id])
host.switches(true)

host.save!

# Collect the ids of switches and lans after saving
hashes.each do |sh|
switch = host.switches.detect { |s| s.uid_ems == sh[:uid_ems] }
sh[:id] = switch.id

next if sh[:lans].nil?
sh[:lans].each do |lh|
lan = switch.lans.detect { |l| l.uid_ems == lh[:uid_ems] }
if lan.nil?
_log.warn("#{log_header} Failed to find lan [#{lh[:uid_ems]}] for switch ID [#{switch.id}]")
next
end

lh[:id] = lan.id
end
end

# handle deletes here instead of inside #save_inventory_multi
switch_ids = Set.new(hashes.collect { |s| s[:id] })
deletes = host.switches.select { |s| !switch_ids.include?(s.id) }
host.switches.delete(deletes)
save_inventory_multi(host.host_virtual_switches, hashes, :use_association, [:uid_ems], [:lans])
store_ids_for_new_records(host.host_virtual_switches, hashes, :uid_ems)
end

def remove_obsolete_switches(ems, old_switch_ids)
obsolete_switch_ids = old_switch_ids - ems.switches.reload.pluck(:id)
Switch.where(:id => obsolete_switch_ids).destroy_all
def save_host_switches_inventory(host, switches)
hashes = switches.collect { |switch| {:host_id => host.id, :switch_id => switch[:id]} }
save_inventory_multi(host.host_switches, hashes, [], [:host_id, :switch_id])
end

def save_lans_inventory(switch, hashes)
Expand Down
1 change: 1 addition & 0 deletions app/models/host.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class Host < ApplicationRecord
has_many :storages, :through => :host_storages
has_many :writable_accessible_host_storages, -> { writable_accessible }, :class_name => "HostStorage"
has_many :writable_accessible_storages, :through => :writable_accessible_host_storages, :source => :storage
has_many :host_virtual_switches, :class_name => "Switch", :dependent => :destroy, :inverse_of => :host
has_many :host_switches, :dependent => :destroy
has_many :switches, :through => :host_switches
has_many :lans, :through => :switches
Expand Down
3 changes: 3 additions & 0 deletions app/models/manageiq/providers/infra_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ class InfraManager < BaseManager

include AvailabilityMixin

has_many :distributed_virtual_switches, :dependent => :destroy, :foreign_key => :ems_id, :inverse_of => :ext_management_system
has_many :host_virtual_switches, -> { distinct }, :through => :hosts

has_many :host_hardwares, :through => :hosts, :source => :hardware
has_many :host_operating_systems, :through => :hosts, :source => :operating_system
has_many :host_storages, :through => :hosts
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class ManageIQ::Providers::InfraManager::DistributedVirtualSwitch < ::Switch
belongs_to :ext_management_system, :foreign_key => :ems_id, :inverse_of => :distributed_virtual_switch
end
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,17 @@ def host_switches
)
end

def switches
def host_virtual_switches
add_properties(
:manager_ref => %i(uid_ems), # TODO looks like switches are using a bad association, this one is defined as through hosts
:manager_ref => %i(host uid_ems),
:model_class => Switch,
:parent_inventory_collections => %i(hosts)
)
end

def distributed_virtual_switches
add_properties(
:manager_ref => %i(uid_ems)
)
add_common_default_values
end
Expand Down
2 changes: 2 additions & 0 deletions app/models/switch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ class Switch < ApplicationRecord
include NewWithTypeStiMixin
include CustomActionsMixin

belongs_to :host, :inverse_of => :host_virtual_switches

has_many :host_switches, :dependent => :destroy
has_many :hosts, :through => :host_switches

Expand Down