Class: Y2Network::InterfacesCollection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
CanBeCopied, Yast2::Equatable, 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

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



61
62
63
# File 'src/lib/y2network/interfaces_collection.rb', line 61

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

Instance Attribute Details

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

Returns List of interfaces.

Returns:



50
51
52
# File 'src/lib/y2network/interfaces_collection.rb', line 50

def interfaces
  @interfaces
end

Instance Method Details

#+(other) ⇒ InterfacesCollection

Returns a new collection including elements from both collections

Parameters:

Returns:



150
151
152
# File 'src/lib/y2network/interfaces_collection.rb', line 150

def +(other)
  self.class.new(to_a + other.to_a)
end

#-(other) ⇒ InterfacesCollection

Returns a new collection including only the elements that are not in the given collection

Parameters:

Returns:



158
159
160
# File 'src/lib/y2network/interfaces_collection.rb', line 158

def -(other)
  self.class.new(to_a - other.to_a)
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



88
89
90
91
92
# File 'src/lib/y2network/interfaces_collection.rb', line 88

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



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

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:

  • type (InterfaceType, String, Symbol)

    device type or its short name

Returns:



98
99
100
101
# File 'src/lib/y2network/interfaces_collection.rb', line 98

def by_type(type)
  type = InterfaceType.from_short_name(type.to_s) unless type.is_a?(InterfaceType)
  InterfacesCollection.new(interfaces.select { |i| i.type == type })
end

#delete_if(&block) ⇒ InterfacesCollection

Deletes elements which meet a given condition



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

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

#eql_hashObject



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

def eql_hash
  h = super
  h[:interfaces] = h[:interfaces].sort_by(&:hash) if h.keys.include?(:interfaces)
  h
end

#free_name(prefix) ⇒ String

Returns free interface name for given prefix

Returns:

  • (String)

    returns free interface name for given prefix



129
130
131
# File 'src/lib/y2network/interfaces_collection.rb', line 129

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



134
135
136
137
138
139
140
141
142
143
144
# File 'src/lib/y2network/interfaces_collection.rb', line 134

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



124
125
126
# File 'src/lib/y2network/interfaces_collection.rb', line 124

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

#physicalInterfacesCollection

Returns the list of physical interfaces

Returns:



106
107
108
# File 'src/lib/y2network/interfaces_collection.rb', line 106

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