Skip to content

Commit

Permalink
Adapt interface to support renaming virtual/physical interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Aug 27, 2019
1 parent a38455a commit f2b0939
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 25 deletions.
28 changes: 21 additions & 7 deletions src/lib/y2network/dialogs/rename_interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,14 @@ def initialize(builder)
textdomain "network"

@builder = builder
interface = @builder.interface
@old_name = interface.name
end

# Runs the dialog
def run
ret = super
return unless ret == :ok
renaming_mechanism, _hwinfo = rename_hwinfo_widget.value
renaming_mechanism = rename_hwinfo_widget.value unless virtual_interface?
@builder.rename_interface(name_widget.value, renaming_mechanism)
name_widget.value
end
Expand All @@ -56,14 +55,17 @@ def run
def contents
VBox(
Left(name_widget),
VSpacing(0.5),
Frame(
_("Base Udev Rule On"),
rename_hwinfo_widget
)
*hardware_info
)
end

private

# Elements to display the hardware information
def hardware_info
virtual_interface? ? [Empty()] : [VSpacing(0.5), rename_hwinfo_widget]
end

# Interface's name widget
#
# @return [Y2Network::Widgets::CustomInterfaceName]
Expand All @@ -77,6 +79,18 @@ def name_widget
def rename_hwinfo_widget
@rename_hwinfo_widget ||= Y2Network::Widgets::RenameHwinfo.new(@builder)
end

# Returns the interface
#
# @return [Y2Network::Interface]
def interface
@builder.interface
end

# Determines whether it is a virtual interface
def virtual_interface?
interface.is_a?(Y2Network::VirtualInterface)
end
end
end
end
4 changes: 2 additions & 2 deletions src/lib/y2network/interface_config_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def renamed_interface?
#
# @param new_name [String] New interface's name
# @param renaming_mechanism [Symbol,nil] Mechanism to rename the interface
# (nil -no rename-, :mac or :bus_id)
# (no hardware based, :mac or :bus_id)
def rename_interface(new_name, renaming_mechanism)
@old_name ||= name
self.name = new_name
Expand Down Expand Up @@ -309,7 +309,7 @@ def aliases=(value)

# gets interface name that will be assigned by udev
def udev_name
# cannot cache as EditNicName dialog can change it
# cannot cache as it can be changed
Yast::LanItems.current_udev_name
end

Expand Down
26 changes: 13 additions & 13 deletions src/lib/y2network/widgets/rename_hwinfo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def initialize(builder)
@hwinfo = interface.hardware
@mac = @hwinfo.mac
@bus_id = @hwinfo.busid
@renaming_mechanism = interface.renaming_mechanism
@renaming_mechanism = builder.renaming_mechanism || :mac
end

# @see CWM::AbstractWidget
Expand All @@ -52,6 +52,10 @@ def store
@value = current_value
end

def value
@value ||= current_value
end

# @see CWM::CustomWidget
def contents
Frame(
Expand All @@ -61,18 +65,7 @@ def contents
VBox(
# make sure there is enough space (#367239)
HSpacing(30),
Left(
RadioButton(
Id(:mac),
_("MAC address: %s") % @mac
),
),
Left(
RadioButton(
Id(:bus_id),
_("BusID: %s") % @bus_id
)
)
*radio_buttons
)
)
)
Expand All @@ -83,6 +76,13 @@ def contents
def current_value
Yast::UI.QueryWidget(Id(:udev_type), :Value)
end

def radio_buttons
buttons = []
buttons << Left(RadioButton(Id(:mac), _("MAC address: %s") % @mac)) if @mac
buttons << Left(RadioButton(Id(:bus_id), _("BusID: %s") % @bus_id)) if @bus_id
buttons
end
end
end
end
8 changes: 7 additions & 1 deletion src/lib/y2network/widgets/udev_rules.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def contents
_("Udev Rules"),
HBox(
InputField(Id(:udev_rules_name), Opt(:hstretch, :disabled), _("Device Name"), ""),
PushButton(Id(:udev_rules_change), _("Change"))
@settings.interface.can_be_renamed? ? change_button : Empty()
)
)
end
Expand Down Expand Up @@ -70,6 +70,12 @@ def help
"example, eth1, wlan0 ) and assures a persistent device name upon reboot.\n"
)
end

private

def change_button
PushButton(Id(:udev_rules_change), _("Change"))
end
end
end
end
2 changes: 1 addition & 1 deletion test/y2network/widgets/rename_hwinfo_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
subject { described_class.new(builder) }

let(:builder) do
instance_double(Y2Network::InterfaceConfigBuilder, interface: interface)
instance_double(Y2Network::InterfaceConfigBuilder, interface: interface, renaming_mechanism: mechanism)
end

let(:interface) { Y2Network::PhysicalInterface.new("eth0") }
Expand Down
7 changes: 6 additions & 1 deletion test/y2network/widgets/udev_rules_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,14 @@

require "y2network/widgets/udev_rules"
require "y2network/dialogs/rename_interface"
require "y2network/interface_config_builder"

describe Y2Network::Widgets::UdevRules do
subject { described_class.new({}) }
subject { described_class.new(builder) }

let(:builder) do
instance_double(Y2Network::InterfaceConfigBuilder, interface: double(can_be_renamed?: true))
end

before do
allow(Yast::LanItems).to receive(:current_udev_name).and_return("hell666")
Expand Down

0 comments on commit f2b0939

Please sign in to comment.