-
Notifications
You must be signed in to change notification settings - Fork 34
/
interface.rb
98 lines (85 loc) · 2.88 KB
/
interface.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# Copyright (c) [2019] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.
require "y2network/interface_type"
require "y2network/hwinfo"
module Y2Network
# Network interface.
class Interface
# @return [String] Device name ('eth0', 'wlan0', etc.)
attr_accessor :name
# @return [String] Interface description
attr_accessor :description
# @return [InterfaceType] Interface type
attr_accessor :type
# @return [Boolean]
attr_reader :configured
# @return [HwInfo]
attr_reader :hardware
# Shortcuts for accessing interfaces' ifcfg options
#
# TODO: this makes Interface class tighly couplet to netconfig backend
# once we have generic layer for accessing backends these methods has to be replaced
["STARTMODE", "BOOTPROTO"].each do |ifcfg_option|
method_name = ifcfg_option.downcase
define_method method_name do
# when switching to new backend we need as much guards as possible
if !configured || config.nil? || config.empty?
raise "Trying to read configuration of an unconfigured interface #{@name}"
end
config[ifcfg_option]
end
end
# Constructor
#
# @param name [String] Interface name (e.g., "eth0")
def initialize(name, type: InterfaceType::ETHERNET)
@name = name
@description = ""
@type = type
# @hardware and @name should not change during life of the object
@hardware = Hwinfo.new(name: name)
init(name)
end
# Determines whether two interfaces are equal
#
# @param other [Interface] Interface to compare with
# @return [Boolean]
def ==(other)
return false unless other.is_a?(Interface)
name == other.name
end
# eql? (hash key equality) should alias ==, see also
# https://ruby-doc.org/core-2.3.3/Object.html#method-i-eql-3F
alias_method :eql?, :==
# Complete configuration of the interface
#
# @return [Hash<String, String>] option, value hash map
def config
system_config(name)
end
private
def system_config(name)
Yast::NetworkInterfaces.devmap(name)
end
def init(name)
@configured = false
@configured = !system_config(name).nil? if !(name.nil? || name.empty?)
end
end
end