Skip to content

Commit

Permalink
Merge pull request #898 from yast/connection_config_in_builder
Browse files Browse the repository at this point in the history
set more connection config attributes
  • Loading branch information
jreidinger committed Jul 24, 2019
2 parents 60dfdaa + 92efab0 commit 6db2778
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
57 changes: 55 additions & 2 deletions src/lib/y2network/interface_config_builder.rb
Expand Up @@ -19,9 +19,11 @@
require "yast"

require "y2network/connection_config/base"
require "y2network/connection_config/ip_config"
require "y2network/hwinfo"
require "y2network/startmode"
require "y2network/boot_protocol"
require "y2network/ip_address"
require "y2firewall/firewalld"
require "y2firewall/firewalld/interface"

Expand Down Expand Up @@ -58,12 +60,17 @@ def self.for(type)
#
# Load with reasonable defaults
# @param type [Y2Network::InterfaceType] type of device
def initialize(type:)
# @param config [Y2Network::ConnectionConfig::Base, nil] existing configuration of device or nil
# for newly created
def initialize(type:, config: nil)
@type = type
@config = init_device_config({})
@s390_config = init_device_s390_config({})
# TODO: also config need to store it, as newly added can be later
# edited with option for not yet created interface
@newly_added = config.nil?
# TODO: create specialized connection for type
@connection_config = ConnectionConfig::Base.new
@connection_config = config || ConnectionConfig::Base.new
end

def newly_added?
Expand Down Expand Up @@ -148,6 +155,7 @@ def boot_protocol
def boot_protocol=(value)
value = value.name if value.is_a?(Y2Network::BootProtocol)
@config["BOOTPROTO"] = value
@connection_config.bootproto = Y2Network::BootProtocol.from_name(value)
end

# @return [Startmode]
Expand Down Expand Up @@ -232,6 +240,23 @@ def aliases
# @param value [Array<Hash>] see #aliases for hash values
def aliases=(value)
@aliases = value

# connection config
# keep only default as aliases does not handle default ip config
@connection_config.ip_configs.delete_if { |c| c.id != "" }
value.each_with_index do |h, i|
ip_addr = IPAddress.from_string(h[:ip])
if h[:prefixlen] && !h[:prefixlen].empty?
ip_addr.prefix = h[:prefixlen].delete("/").to_i
elsif h[:mask] && !h[:mask].empty?
ip.netmask = h[:mask]
end
@connection_config.ip_configs << ConnectionConfig::IPConfig.new(
ip_addr,
label: h[:label],
id: "_#{i}" # TODO: remember original prefixes
)
end
end

# gets interface name that will be assigned by udev
Expand Down Expand Up @@ -259,6 +284,14 @@ def ip_address
# @param [String] value
def ip_address=(value)
@config["IPADDR"] = value

# connection_config
if value.nil? || value.empty?
# in such case remove default config
@connection_config.ip_configs.delete_if { |c| c.id == "" }
else
ip_config_default.address.address = value
end
end

# @return [String] returns prefix or netmask. prefix in format "/<prefix>"
Expand All @@ -275,13 +308,21 @@ def subnet_prefix=(value)
if value.empty?
@config["PREFIXLEN"] = ""
@config["NETMASK"] = ""
ip_config_default.address.prefix = nil
elsif value.start_with?("/")
@config["PREFIXLEN"] = value[1..-1]
ip_config_default.address.prefix = value[1..-1].to_i
elsif value.size < 3 # one or two digits can be only prefixlen
@config["PREFIXLEN"] = value
ip_config_default.address.prefix = value.to_i
else
param = Yast::Netmask.Check6(value) ? "PREFIXLEN" : "NETMASK"
@config[param] = value
if param == "PREFIXLEN"
ip_config_default.address.prefix = value.to_i
else
ip_config_default.address.netmask = value
end
end
end

Expand All @@ -304,6 +345,7 @@ def remote_ip
# @param [String] value
def remote_ip=(value)
@config["REMOTEIP"] = value
ip_config_default.remote_address = IPAddress.from_string(value)
end

# Gets Maximum Transition Unit
Expand All @@ -316,6 +358,8 @@ def mtu
# @param [String] value
def mtu=(value)
@config["MTU"] = value

@connection_config.mtu = value.to_i
end

# @return [Array(2)<String,String>] user and group of tunnel
Expand Down Expand Up @@ -475,5 +519,14 @@ def save_aliases
Yast::NetworkInterfaces.DeleteAlias(Yast::NetworkInterfaces.Name, a) if v
end
end

def ip_config_default
default = @connection_config.ip_configs.find { |c| c.id == "" }
if !default
default = ConnectionConfig::IPConfig.new(IPAddress.new("0.0.0.0")) # fake ip as it will be replaced soon
@connection_config.ip_configs << default
end
default
end
end
end
7 changes: 7 additions & 0 deletions src/lib/y2network/ip_address.rb
Expand Up @@ -80,6 +80,13 @@ def netmask=(netmask)
self.prefix = IPAddr.new("#{netmask}/#{netmask}").prefix
end

# Sets the address from the string
#
# @param value [String] String representation of the address
def address=(value)
@address = IPAddr.new(value)
end

# Determines whether two addresses are equivalent
#
# @param other [IPAddress] The address to compare with
Expand Down

0 comments on commit 6db2778

Please sign in to comment.