这种代码有什么好的重构吗?
def _assign_network_from_db
begin
@nodes.each do |node_name, node|
next if node["topology_resource_id"].nil?
topology_resource = TopologyResource.find(node["topology_resource_id"])
@interfaces[node_name].each do |interface_name,interface|
iface = topology_resource.topology_resource_interfaces.find_by_name interface_name
if !iface.nil?
if !iface.topology_vlan_id.nil?
# Find the vlan details from DB and set them
topology_vlan = TopologyVlan.find(iface.topology_vlan_id)
vlan = topology_vlan.vlan
physnet = topology_vlan.physnet
external_network = topology_vlan.external_network
if interface["ip"].nil?
interface["ip"] = iface.ip
end
if interface["network"].nil?
interface["network"] = iface.network
end
if interface["share_network"].nil?
interface["vlan"] = vlan
end
interface["physnet"] = physnet
interface["external_network"] = external_network
interface["switch_id"] = iface.switch_id
interface["switch_port"] = iface.switch_port
topology_vlan.status = 'INUSE'
topology_vlan.save!
elsif !iface.nil? && !iface.ip.nil?
if interface["ip"].nil?
interface["ip"] = iface.ip
end
if interface["network"].nil?
interface["network"] = iface.network
end
interface["skip_network"] = true
end
elsif !interface_name.match(/X\d:\d+/i).nil?
# deal with the subinterface(like: X1:1) of UTM by Rex
iface_parent_name = interface_name.split(":")[0]
iface_parent = topology_resource.topology_resource_interfaces.find_by_name iface_parent_name
vlan_result = _get_private_vlan
vlan = vlan_result["vlan"]
physnet = vlan_result["physnet"]
external_network = vlan_result["external_network"]
interface["physnet"] = physnet
interface["external_network"] = external_network
interface["switch_id"] = iface_parent.switch_id
interface["switch_port"] = iface_parent.switch_port
if interface["share_network"].nil? then
interface["vlan"] = vlan
end
end
if !interface["remote_node"].nil? && interface["routed_connection"].nil? then
remote_node_name = interface["remote_node"]
remote_interface_name = interface["remote_interface"]
interface["network"] = nil if interface["network"].to_s.match(/\d/).nil?
@interfaces[remote_node_name][remote_interface_name]["network"] = interface["network"]
@interfaces[remote_node_name][remote_interface_name]["vlan"] = vlan
@interfaces[remote_node_name][remote_interface_name]["physnet"] = physnet
@interfaces[remote_node_name][remote_interface_name]["external_network"] = external_network
end
end
end
rescue => e
self.status = "error"
self.message = "Error occured while assigning network details from db. #{e}"
self.backtrace = e.backtrace
return
end
end