Skip to content

Commit

Permalink
Changes based on code review
Browse files Browse the repository at this point in the history
  • Loading branch information
teclator committed Jan 21, 2021
1 parent 9fabc33 commit 0b0f084
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
11 changes: 10 additions & 1 deletion src/lib/y2network/network_manager/config_writer.rb
Expand Up @@ -35,9 +35,18 @@ class ConfigWriter < Y2Network::ConfigWriter
def write_connections(config, _old_config)
writer = Y2Network::NetworkManager::ConnectionConfigWriter.new
config.connections.each do |conn|
writer.write(conn, nil, config.routing.routes) # FIXME
writer.write(conn, nil, routes_for(conn, config.routing.routes)) # FIXME
end
end

# Finds routes for a given connection
#
# @param conn [ConnectionConfig::Base] Connection configuration
# @param routes [Array<Route>] List of routes to search in
# @return [Array<Route>] List of routes for the given connection
def routes_for(conn, routes)
routes.select { |r| r.interface&.name == conn.name }
end
end
end
end
14 changes: 13 additions & 1 deletion src/lib/y2network/network_manager/connection_config_writer.rb
Expand Up @@ -40,6 +40,12 @@ def initialize(basedir = Yast::Installation.destdir)
@basedir = basedir
end

# @param conn [ConnectionConfig::Base] Connection configuration to be
# written
# @param old_conn [ConnectionConfig::Base] Original connection
# configuration
# @param routes [Array<Routes>] routes associated with the connection to be
# written
def write(conn, old_conn = nil, routes = [])
return if conn == old_conn

Expand All @@ -56,12 +62,18 @@ def write(conn, old_conn = nil, routes = [])

private

# Convenience method to obtain the path for writing the given connection
# configuration
#
# @param conn [ConnectionConfig::Base] Connection config to be written
def path_for(conn)
Yast.import "Installation"
conn_file_path = SYSTEM_CONNECTIONS_PATH.join(conn.name).sub_ext(FILE_EXT)
Pathname.new(File.join(basedir, conn_file_path))
end

# Convenience method to ensure the new configuration file permissions
#
# @param path [Pathname] connection configuration file path
def ensure_permissions(path)
::FileUtils.touch(path)
::File.chmod(0o600, path)
Expand Down
Expand Up @@ -40,26 +40,29 @@ def initialize(file)
# Writes connection information to the interface configuration file
#
# @param conn [Y2Network::ConnectionConfig::Base] Connection to take settings from
# @param routes [<Array<Route>]
# @param routes [<Array<Y2Network::Route>] routes associated with the connection
def write(conn, routes = [])
file.connection["id"] = conn.name
file.connection["autoconnect"] = "false" if ["manual", "off"].include? conn.startmode.name
file.connection["permissions"] = nil
file.connection["interface-name"] = conn.interface
file.connection["zone"] = conn.firewall_zone unless ["", nil].include? conn.firewall_zone
conn.bootproto.dhcp? ? configure_dhcp(conn) : configure_ips(conn)
configure_routes(conn, routes)
configure_routes(routes)
update_file(conn)
end

private

def configure_routes(conn, routes)
routes.select { |r| (r.interface&.name == conn.name) && r.is_default? }.each do |route|
configure_gateway(route)
end
# Convenience method to write routing configuration associated with the
# connection config to be written
#
# @param routes [<Array<Y2Network::Route>] routes associated with the connection
def configure_routes(routes)
routes.select(&:default?).each { |r| configure_gateway(r) }
end

# @param route [Y2Network::Route] route to be written
def configure_gateway(route)
section = route.gateway.ipv4? ? file.ipv4 : file.ipv6
section["gateway"] = route.gateway.to_s
Expand Down

0 comments on commit 0b0f084

Please sign in to comment.