diff --git a/app/models/physical_network_port.rb b/app/models/physical_network_port.rb index ab903f31ab46..bbf92a82ea22 100644 --- a/app/models/physical_network_port.rb +++ b/app/models/physical_network_port.rb @@ -1,4 +1,17 @@ class PhysicalNetworkPort < ApplicationRecord belongs_to :guest_device belongs_to :physical_switch + + # + # Get the peer port connected. + # A connected port has the mac address of the pair in its peer_mac_address field. + # + # @return [PhysicalNetworkPort] - the connected port + # + def connected_port + port = PhysicalNetworkPort.find_by('peer_mac_address IS NOT NULL AND peer_mac_address = ?', mac_address) + port = PhysicalNetworkPort.find_by('mac_address IS NOT NULL AND mac_address = ?', peer_mac_address) if port.blank? + + port + end end diff --git a/app/models/physical_server.rb b/app/models/physical_server.rb index 5241ec3890a0..6e00027f39a0 100644 --- a/app/models/physical_server.rb +++ b/app/models/physical_server.rb @@ -104,4 +104,22 @@ def v_availability def v_host_os host.try(:vmm_product).nil? ? N_("") : host.vmm_product end + + # + # Searches the switches that are connected to the physical server + # it evaluates the server physical network ports that are connected with switch physical network ports + # + # @returns [Array] - a list of connected switches, and if it isn't connected to any, returns a empty list + # + def switches + switches = [] + + guest_devices.each do |device| + device.physical_network_ports&.each do |port| + switches << PhysicalSwitch.find(port.connected_port.switch_id) if port.connected_port.try(:switch_id).present? + end + end + + switches + end end