Skip to content

Commit

Permalink
Merge pull request #558 from agrare/set_opaque_network_ems_ref
Browse files Browse the repository at this point in the history
Set the opaqueNetwork ems_ref and uid_ems and link VMs to opaqueNetworks

(cherry picked from commit 298b968)

https://bugzilla.redhat.com/show_bug.cgi?id=1816278
  • Loading branch information
Fryguy authored and simaishi committed Mar 24, 2020
1 parent ecc6e9b commit 3297c72
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ def parse_license_manager(_object, kind, props)

def parse_network(object, kind, props)
end
alias parse_opaque_networks parse_network

def parse_distributed_virtual_portgroup(object, kind, props)
return if kind == "leave"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -452,13 +452,22 @@ def parse_host_system_lans(host, switches, props)
end

network[:opaqueNetwork].to_a.each do |opaque_network|
switch_key = cache["HostSystem"][host.ems_ref]&.dig(:config, :network, :opaqueSwitch)&.pluck(:key)&.sort
switch_key = cache["HostSystem"][host.ems_ref]&.dig(:config, :network, :opaqueSwitch)&.pluck(:key)&.sort&.first
next if switch_key.nil?

extra_config = Hash[opaque_network.extraConfig.to_a.map { |ec| [ec.key, ec.value] }]
nsx_network_uuid = extra_config["com.vmware.opaquenetwork.segment.path"]&.split("/")&.last

name = opaque_network.opaqueNetworkName
ems_ref = nsx_network_uuid || name
uid_ems = opaque_network.opaqueNetworkId
switch = persister.host_virtual_switches.lazy_find(:host => host, :uid_ems => switch_key)

persister.host_virtual_lans.build(
:switch => persister.host_virtual_switches.lazy_find(:host => host, :uid_ems => switch_key),
:uid_ems => opaque_network.opaqueNetworkName,
:name => opaque_network.opaqueNetworkName
:switch => switch,
:ems_ref => ems_ref,
:uid_ems => uid_ems,
:name => name
)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,32 +351,27 @@ def parse_virtual_machine_snapshot(vm, snapshot, current, parent_uid = nil)
end

def parse_virtual_machine_guest_device_lan(vm, nic)
backing = nic.backing
return if backing.nil?

if backing.kind_of?(RbVmomi::VIM::VirtualEthernetCardDistributedVirtualPortBackingInfo)
collection = :distributed_virtual_lans
lan_uid = backing.port.portgroupKey
persister_switch = persister.distributed_virtual_switches.lazy_find({:switch_uuid => backing.port.switchUuid}, :ref => :by_switch_uuid)
else
collection = :host_virtual_lans
lan_uid = backing.deviceName

host_ref = find_vm_host_ref(vm)
return if host_ref.nil?

portgroup = find_host_portgroup_by_lan_name(host_ref, lan_uid)
return if portgroup.nil?

host = persister.hosts.lazy_find(host_ref)
switch_uid = portgroup.spec.vswitchName

persister_switch = persister.host_virtual_switches.lazy_find(:host => host, :uid_ems => switch_uid)
host_ref = find_vm_host_ref(vm)
return if host_ref.nil?

persister_host = persister.hosts.lazy_find(host_ref)

case nic.backing
when RbVmomi::VIM::VirtualEthernetCardDistributedVirtualPortBackingInfo
lan_uid = nic.backing.port.portgroupKey
persister_switch = persister.distributed_virtual_switches.lazy_find({:switch_uuid => nic.backing.port.switchUuid}, {:ref => :by_switch_uuid})
persister.distributed_virtual_lans.lazy_find({:switch => persister_switch, :uid_ems => lan_uid}, {:transform_nested_lazy_finds => true})
when RbVmomi::VIM::VirtualEthernetCardOpaqueNetworkBackingInfo
lan_uid = nic.backing.opaqueNetworkId
switch_uid = find_host_opaque_switch(host_ref)
persister_switch = persister.host_virtual_switches.lazy_find(:host => persister_host, :uid_ems => switch_uid)
persister.host_virtual_lans.lazy_find({:switch => persister_switch, :uid_ems => lan_uid}, {:transform_nested_lazy_finds => true})
when RbVmomi::VIM::VirtualEthernetCardNetworkBackingInfo
lan_uid = nic.backing.deviceName
switch_uid = find_host_vswitch(host_ref, lan_uid)
persister_switch = persister.host_virtual_switches.lazy_find(:host => persister_host, :uid_ems => switch_uid)
persister.host_virtual_lans.lazy_find({:switch => persister_switch, :uid_ems => lan_uid}, {:transform_nested_lazy_finds => true})
end

return if lan_uid.nil? || persister_switch.nil?

persister.send(collection).lazy_find({:switch => persister_switch, :uid_ems => lan_uid}, {:transform_nested_lazy_finds => true})
end

def find_vm_host_ref(persister_vm)
Expand All @@ -386,12 +381,13 @@ def find_vm_host_ref(persister_vm)
host[:ems_ref]
end

def find_host_portgroup_by_lan_name(host_ref, lan_name)
cached_host = cache["HostSystem"][host_ref]
return if cached_host.nil?
def find_host_vswitch(host_ref, lan_name)
portgroups = cache["HostSystem"][host_ref]&.dig(:config, :network, :portgroup) || []
portgroups.detect { |portgroup| portgroup.spec.name == lan_name }
end

host_portgroups = cached_host.dig(:config, :network, :portgroup) || []
host_portgroups.detect { |portgroup| portgroup.spec.name == lan_name }
def find_host_opaque_switch(host_ref)
cache["HostSystem"][host_ref]&.dig(:config, :network, :opaqueSwitch)&.pluck(:key)&.sort&.first
end
end
end

0 comments on commit 3297c72

Please sign in to comment.