Class: Y2Network::InterfacesCollection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
CanBeCopied, Yast::Logger
Defined in:
src/lib/y2network/interfaces_collection.rb

Overview

A container for network devices.

Objects of this class are able to keep a list of interfaces and perform simple queries on such a list. In the end should implement methods for mass operations over network interfaces like old LanItems::find_dhcp_ifaces.

Examples:

Finding an interface by its name

interfaces = Y2Network::InterfacesCollection.new([eth0, wlan0])
interfaces.by_name("wlan0") # => wlan0

Find an interface using its name

iface = collection.by_name("eth0") #=> #<Y2Network::Interface:0x...>

FIXME (not implemented yet). For the future, we are aiming at this kind of API.

interfaces = Y2Network::InterfacesCollection.new([eth0, wlan0])
interfaces.of_type(:eth).to_a # => [eth0]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CanBeCopied

#copy

Constructor Details

#initialize(interfaces = []) ⇒ InterfacesCollection

Constructor

Parameters:

  • interfaces (Array<Interface>) (defaults to: [])

    List of interfaces


56
57
58
# File 'src/lib/y2network/interfaces_collection.rb', line 56

def initialize(interfaces = [])
  @interfaces = interfaces
end

Instance Attribute Details

#interfacesArray<Interface> (readonly) Also known as: to_a

Returns List of interfaces.

Returns:


48
49
50
# File 'src/lib/y2network/interfaces_collection.rb', line 48

def interfaces
  @interfaces
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Compares InterfacesCollections

Returns:

  • (Boolean)

    true when both collections contain only equal interfaces, false otherwise


120
121
122
# File 'src/lib/y2network/interfaces_collection.rb', line 120

def ==(other)
  ((interfaces - other.interfaces) + (other.interfaces - interfaces)).empty?
end

#bond_indexObject


163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'src/lib/y2network/interfaces_collection.rb', line 163

def bond_index
  index = {}

  # TODO: should use by_type
  bond_devs = Yast::NetworkInterfaces.FilterDevices("netcard").fetch("bond", {})

  bond_devs.each do |bond_master, _value|
    bond_slaves(bond_master).each do |slave|
      index[slave] = bond_master
    end
  end

  log.debug("bond slaves index: #{index}")

  index
end

#bond_slaves(bond_iface) ⇒ Object

Creates list of devices enslaved in the bond device.

TODO: move to class of interface type bond, also change return type to InterfaceCollection

Parameters:

  • bond_iface (String)

    a name of an interface of bond type

Returns:

  • list of names of interfaces enslaved in the bond_iface


140
141
142
143
144
# File 'src/lib/y2network/interfaces_collection.rb', line 140

def bond_slaves(bond_iface)
  bond_map = Yast::NetworkInterfaces::FilterDevices("netcard").fetch("bond", {}).fetch(bond_iface, {})

  bond_map.select { |k, _| k.start_with?("BONDING_SLAVE") }.values
end

#bridge_indexObject

Creates a map where the keys are the interfaces enslaved and the values are the bridges where them are taking part.


148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'src/lib/y2network/interfaces_collection.rb', line 148

def bridge_index
  index = {}

  # TODO: should use by_type
  bridge_devs = Yast::NetworkInterfaces.FilterDevices("netcard").fetch("br", {})

  bridge_devs.each do |bridge_master, value|
    value["BRIDGE_PORTS"].to_s.split.each do |if_name|
      index[if_name] = bridge_master
    end
  end

  index
end

#bridge_slaves(master) ⇒ Array<String>

Returns the interfaces that are enslaved in the given bridge

TODO: move to class of interface type br, also change return type to InterfaceCollection

Parameters:

  • master (String)

    bridge name

Returns:

  • (Array<String>)

    a list of interface names


131
132
133
# File 'src/lib/y2network/interfaces_collection.rb', line 131

def bridge_slaves(master)
  bridge_index.select { |_k, v| v == master }.keys
end

#by_busid(busid) ⇒ Interface?

Returns an interface with the given hardware busid if present

Parameters:

  • busid (String)

    interface busid ("0.0.0700", "0000:00:19.0", ...)

Returns:

  • (Interface, nil)

    Interface with the given busid or nil if not found


77
78
79
80
81
# File 'src/lib/y2network/interfaces_collection.rb', line 77

def by_busid(busid)
  interfaces.find do |iface|
    iface.hardware && iface.hardware.busid == busid
  end
end

#by_name(name) ⇒ Interface?

Note:

It uses the hardware's name as a fallback if interface's name is not set

Returns an interface with the given name if present

Parameters:

  • name (String)

    interface name ("eth0", "br1", ...)

Returns:

  • (Interface, nil)

    Interface with the given name or nil if not found


66
67
68
69
70
71
# File 'src/lib/y2network/interfaces_collection.rb', line 66

def by_name(name)
  interfaces.find do |iface|
    iface_name = iface.name || iface.hardware.name
    iface_name == name
  end
end

#by_type(type) ⇒ InterfacesCollection

Returns list of interfaces of given type

Parameters:

Returns:


87
88
89
# File 'src/lib/y2network/interfaces_collection.rb', line 87

def by_type(type)
  InterfacesCollection.new(interfaces.select { |i| i.type == type })
end

#delete_if(&block) ⇒ InterfacesCollection

Deletes elements which meet a given condition


101
102
103
104
# File 'src/lib/y2network/interfaces_collection.rb', line 101

def delete_if(&block)
  interfaces.delete_if(&block)
  self
end

#free_name(prefix) ⇒ String

Returns free interface name for given prefix

Returns:

  • (String)

    returns free interface name for given prefix


181
182
183
# File 'src/lib/y2network/interfaces_collection.rb', line 181

def free_name(prefix)
  free_names(prefix, 1).first
end

#free_names(prefix, count) ⇒ Array<String>

Returns free interface name for given prefix

Returns:

  • (Array<String>)

    returns free interface name for given prefix


186
187
188
189
190
191
192
193
194
195
196
# File 'src/lib/y2network/interfaces_collection.rb', line 186

def free_names(prefix, count)
  result = []
  # TODO: when switch rubocop use endless range `(0..)`
  (0..100000).each do |i|
    candidate = prefix + i.to_s
    next if by_name(candidate)

    result << candidate
    return result if result.size == count
  end
end

#known_namesArray<String>

Returns all interfaces names

For those interfaces that are renamed, the new and old names are included in the list.

Returns:

  • (Array<String>)

    List of known interfaces


112
113
114
# File 'src/lib/y2network/interfaces_collection.rb', line 112

def known_names
  @interfaces.map { |i| [i.old_name, i.name] }.flatten.compact
end

#physicalInterfacesCollection

Returns the list of physical interfaces

Returns:


94
95
96
# File 'src/lib/y2network/interfaces_collection.rb', line 94

def physical
  interfaces.select { |i| i.is_a?(PhysicalInterface) }
end