Skip to content

Commit

Permalink
Merge pull request #905 from yast/network-ng-use-connection-config-wr…
Browse files Browse the repository at this point in the history
…iters

network-ng: use connection config writers
  • Loading branch information
imobachgs committed Jul 31, 2019
2 parents c2bf2aa + 91c1265 commit 58e1de5
Show file tree
Hide file tree
Showing 18 changed files with 296 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/include/network/lan/cmdline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def EditHandler(options)
LanItems.current = getItem(options, config)

config = Lan.yast_config.copy
connection_config = config.connections.find { |c| c.name == LanItems.GetCurrentName }
connection_config = config.connections.by_name(LanItems.GetCurrentName)
builder = Y2Network::InterfaceConfigBuilder.for(LanItems.GetCurrentType(), config: connection_config)
builder.name = LanItems.GetCurrentName()
LanItems.SetItem(builder: builder)
Expand Down
6 changes: 3 additions & 3 deletions src/include/network/lan/complex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -310,14 +310,14 @@ def UpdateSlaves

current = LanItems.current
config = Lan.yast_config.copy
connection_config = config.connections.find { |c| c.name == LanItems.GetCurrentName }
connection_config = config.connections.by_name(LanItems.GetCurrentName)
Yast.y2milestone("update slaves for #{current}:#{LanItems.GetCurrentName}:#{LanItems.GetCurrentType}")
master_builder = Y2Network::InterfaceConfigBuilder.for(LanItems.GetCurrentType(), config: connection_config)
master_builder.name = LanItems.GetCurrentName()

Lan.autoconf_slaves.each do |dev|
if LanItems.FindAndSelect(dev)
connection_config = config.connections.find { |c| c.name == LanItems.GetCurrentName }
connection_config = config.connections.by_name(LanItems.GetCurrentName)
builder = Y2Network::InterfaceConfigBuilder.for(LanItems.GetCurrentType(), config: connection_config)
builder.name = LanItems.GetCurrentName()
LanItems.SetItem(builder: builder)
Expand Down Expand Up @@ -415,7 +415,7 @@ def handleOverview(_key, event)
when :edit
if LanItems.IsCurrentConfigured
config = Lan.yast_config.copy
connection_config = config.connections.find { |c| c.name == LanItems.GetCurrentName }
connection_config = config.connections.by_name(LanItems.GetCurrentName)
builder = Y2Network::InterfaceConfigBuilder.for(LanItems.GetCurrentType(), config: connection_config)
builder.name = LanItems.GetCurrentName()
LanItems.SetItem(builder: builder)
Expand Down
15 changes: 9 additions & 6 deletions src/lib/y2network/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
require "y2network/routing"
require "y2network/dns"
require "y2network/interfaces_collection"
require "y2network/connection_configs_collection"

module Y2Network
# This class represents the current network configuration including interfaces,
Expand All @@ -38,7 +39,7 @@ module Y2Network
class Config
# @return [InterfacesCollection]
attr_accessor :interfaces
# @return [Array<ConnectionConfig>]
# @return [ConnectionConfigsCollection]
attr_accessor :connections
# @return [Routing] Routing configuration
attr_accessor :routing
Expand Down Expand Up @@ -87,11 +88,13 @@ def configs

# Constructor
#
# @param interfaces [InterfacesCollection] List of interfaces
# @param routing [Routing] Object with routing configuration
# @param dns [DNS] Object with DNS configuration
# @param source [Symbol] Configuration source
def initialize(interfaces: InterfacesCollection.new, connections: [], routing: Routing.new, dns: DNS.new, source:)
# @param interfaces [InterfacesCollection] List of interfaces
# @param connections [ConnectionConfigsCollection] List of connection configurations
# @param routing [Routing] Object with routing configuration
# @param dns [DNS] Object with DNS configuration
# @param source [Symbol] Configuration source
def initialize(interfaces: InterfacesCollection.new, connections: ConnectionConfigsCollection.new,
routing: Routing.new, dns: DNS.new, source:)
@interfaces = interfaces
@connections = connections
@routing = routing
Expand Down
27 changes: 27 additions & 0 deletions src/lib/y2network/connection_config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# 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/connection_config/base"
require "y2network/connection_config/bonding"
require "y2network/connection_config/dummy"
require "y2network/connection_config/ethernet"
require "y2network/connection_config/infiniband"
require "y2network/connection_config/ip_config"
require "y2network/connection_config/vlan"
require "y2network/connection_config/wireless"
4 changes: 4 additions & 0 deletions src/lib/y2network/connection_config/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
# find current contact information at www.suse.com.

require "y2network/interface_type"
require "y2network/boot_protocol"
require "y2network/startmode"

module Y2Network
module ConnectionConfig
Expand Down Expand Up @@ -52,6 +54,8 @@ class Base
# Constructor
def initialize
@ip_configs = []
@bootproto = BootProtocol::STATIC
@startmode = Startmode.create("manual")
end

# Returns the connection type
Expand Down
80 changes: 80 additions & 0 deletions src/lib/y2network/connection_configs_collection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# 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 "yast"
require "forwardable"

module Y2Network
# A container for connection configurations objects.
#
# @example Create a new collection
# eth0 = Y2Network::ConnectionConfig::Ethernet.new
# collection = Y2Network::ConnectionConfigsCollection.new([eth0])
#
# @example Find a connection config using its name
# config = collection.by_name("eth0") #=> #<Y2Network::ConnectionConfig::Ethernet:0x...>
class ConnectionConfigsCollection
extend Forwardable
include Yast::Logger

attr_reader :connection_configs
alias_method :to_a, :connection_configs

def_delegators :@connection_configs, :each, :push, :<<, :reject!, :map, :flat_map, :any?, :size

# Constructor
#
# @param connection_configs [Array<ConnectionConfig>] List of connection configurations
def initialize(connection_configs = [])
@connection_configs = connection_configs
end

# Returns a connection configuration with the given name if present
#
# @param name [String] Connection name
# @return [ConnectionConfig::Base,nil] Connection config with the given name or nil if not found
def by_name(name)
connection_configs.find { |c| c.name == name }
end

# Adds or updates a connection configuration
#
# @note It uses the name to do the matching.
#
# @param connection_config [ConnectionConfig::Base] New connection configuration object
def add_or_update(connection_config)
idx = connection_configs.find_index { |c| c.name == connection_config.name }
if idx
connection_configs[idx] = connection_config
else
connection_configs << connection_config
end
end

# Removes a connection configuration
#
# @note It uses the name to do the matching.
#
# @param connection_config [ConnectionConfig::Base,String] Connection configuration object or name
def remove(connection_config)
name = connection_config.respond_to?(:name) ? connection_config.name : connection_config
connection_configs.reject! { |c| c.name == name }
end
end
end
23 changes: 18 additions & 5 deletions src/lib/y2network/interface_config_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
# find current contact information at www.suse.com.
require "yast"

require "y2network/connection_config/base"
require "y2network/connection_config/ip_config"
require "y2network/connection_config"
require "y2network/hwinfo"
require "y2network/startmode"
require "y2network/boot_protocol"
Expand Down Expand Up @@ -74,7 +73,7 @@ def initialize(type:, config: nil)
# edited with option for not yet created interface
@newly_added = config.nil?
# TODO: create specialized connection for type
@connection_config = config || ConnectionConfig::Base.new
@connection_config = config || connection_config_klass(type).new
end

def newly_added?
Expand All @@ -92,6 +91,9 @@ def save
Yast::LanItems.driver_options[driver] = driver_options
end

@connection_config.interface = name
Yast::Lan.yast_config.connections.add_or_update(@connection_config)

# create new instance as name can change
firewall_interface = Y2Firewall::Firewalld::Interface.new(name)
if Y2Firewall::Firewalld.instance.installed?
Expand Down Expand Up @@ -306,7 +308,8 @@ def ethtool_options=(value)
def ip_address
old = @config["IPADDR"]

default = @connection_config.ip_configs.find { |c| c.id == "" }
# FIXME: workaround to remove when primary ip config is separated from the rest
default = (@connection_config.ip_configs || []).find { |c| c.id == "" }
new_ = if default
default.address.address
else
Expand Down Expand Up @@ -335,7 +338,7 @@ def subnet_prefix
else
@config["NETMASK"] || ""
end
default = @connection_config.ip_configs.find { |c| c.id == "" }
default = (@connection_config.ip_configs || []).find { |c| c.id == "" }
new_ = if default
"/" + default.address.prefix.to_s
else
Expand Down Expand Up @@ -589,5 +592,15 @@ def select_backend(old, new)

old
end

# Returns the connection config class for a given type
#
# @param type [Y2Network::InterfaceType] type of device
def connection_config_klass(type)
ConnectionConfig.const_get(type.name)
rescue NameError
log.error "Could not find a class to handle '#{type.name}' connections"
ConnectionConfig::Base
end
end
end
31 changes: 14 additions & 17 deletions src/lib/y2network/interfaces_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,28 @@
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.

require "yast"
require "y2network/interface"
require "forwardable"

module Y2Network
# A container for network devices. In the end should implement methods for mass operations over
# network interfaces like old LanItems::find_dhcp_ifaces.
# A container for network devices.
#
# @example Create a new collection
# eth0 = Y2Network::Interface.new("eth0")
# collection = Y2Network::InterfacesCollection.new(eth0)
# 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.
#
# @example Finding an interface by its name
# interfaces = Y2Network::InterfacesCollection.new([eth0, wlan0])
# interfaces.by_name("wlan0") # => wlan0
#
# @example Find an interface using its name
# iface = collection.by_name("eth0") #=> #<Y2Network::Interface:0x...>
#
# @example 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]
class InterfacesCollection
# Objects of this class are able to keep a list of interfaces and perform simple queries
# on such a list.
#
# @example Finding an interface by its name
# interfaces = Y2Network::InterfacesCollection.new([eth0, wlan0])
# interfaces.by_name("wlan0") # => wlan0
#
# @example 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]

extend Forwardable
include Yast::Logger

Expand All @@ -60,7 +57,7 @@ def initialize(interfaces = [])

# Returns an interface with the given name if present
#
# @todo It uses the hardware's name as a fallback if interface's name is not set
# @note It uses the hardware's name as a fallback if interface's name is not set
#
# @param name [String] interface name ("eth0", "br1", ...)
# @return [Interface,nil] Interface with the given name or nil if not found
Expand Down
2 changes: 1 addition & 1 deletion src/lib/y2network/sysconfig/interface_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ def type_from_keys
#
# @return [Array<String>] name of keys that are included in the file
def defined_variables
@defined_variables ||= Yast::SCR.Dir(Yast::Path.new(".network.value.\"#{interface}\""))
@defined_variables ||= Yast::SCR.Dir(Yast::Path.new(".network.value.\"#{interface}\"")) || []
end

# Fetches the value for a given key
Expand Down
4 changes: 3 additions & 1 deletion src/lib/y2network/sysconfig/interfaces_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
require "y2network/physical_interface"
require "y2network/fake_interface"
require "y2network/sysconfig/connection_config_reader"
require "y2network/interfaces_collection"
require "y2network/connection_configs_collection"

Yast.import "LanItems"
Yast.import "NetworkInterfaces"
Expand Down Expand Up @@ -81,7 +83,7 @@ def find_physical_interfaces
# Finds the connections configurations
def find_connections
@connections ||=
configured_devices.each_with_object([]) do |name, conns|
configured_devices.each_with_object(ConnectionConfigsCollection.new([])) do |name, conns|
interface = @interfaces.by_name(name)
connection = ConnectionConfigReader.new.read(
name,
Expand Down
5 changes: 1 addition & 4 deletions src/modules/LanItems.rb
Original file line number Diff line number Diff line change
Expand Up @@ -736,9 +736,6 @@ def write
end

LanItems.WriteUdevRules if !Stage.cont && InstallInfConvertor.instance.AllowUdevModify

# FIXME: hack: no "netcard" filter as biosdevname names it diferently (bnc#712232)
NetworkInterfaces.Write("")
end

# Exports configuration for use in AY profile
Expand Down Expand Up @@ -2388,7 +2385,7 @@ def move_routes(from, to)
return unless config && config.routing
routing = config.routing
add_device_to_routing(to)
target_interface = config.interfaces.find { |i| i.name == to }
target_interface = config.interfaces.by_name(to)
return unless target_interface
routing.routes.select { |r| r.interface && r.interface.name == from }
.each { |r| r.interface = target_interface }
Expand Down
8 changes: 8 additions & 0 deletions test/default_route_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,17 @@
require "network/install_inf_convertor"
require "y2network/interface_config_builder"

Yast.import "Lan"

describe "Yast::LanItemsClass" do
subject { Yast::LanItems }

let(:config) { Y2Network::Config.new(source: :sysconfig) }

before do
allow(Yast::Lan).to receive(:yast_config).and_return(config)
end

before do
Yast.import "LanItems"

Expand Down
3 changes: 2 additions & 1 deletion test/lan_items_helpers_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -761,8 +761,9 @@ def mock_dhcp_setup(ifaces, global)
end
let(:eth0) { Y2Network::Interface.new("eth0") }
let(:br0) { Y2Network::Interface.new("br0") }
let(:interfaces) { Y2Network::InterfacesCollection.new([eth0, br0]) }
let(:yast_config) do
instance_double(Y2Network::Config, interfaces: [eth0, br0], routing: routing)
instance_double(Y2Network::Config, interfaces: interfaces, routing: routing)
end

before do
Expand Down
Loading

0 comments on commit 58e1de5

Please sign in to comment.