Skip to content

Commit

Permalink
Update from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Mar 20, 2019
1 parent bc2e476 commit 7dbd0c1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
7 changes: 6 additions & 1 deletion src/lib/y2network/config.rb
Expand Up @@ -45,8 +45,13 @@ def initialize(interfaces:, routing_tables:)
@routing_tables = routing_tables
end

# Routes in the configuration
#
# Convenience method to iterate through the routes in all routing tables.
#
# @return [Array<Route>] List of routes which are defined in the configuration
def routes
routing_tables.flat_map(&:routes)
routing_tables.flat_map(&:to_a)
end
end
end
19 changes: 13 additions & 6 deletions src/lib/y2network/route.rb
Expand Up @@ -19,21 +19,28 @@
module Y2Network
# This class represents a network route
class Route
# @return [IPAddr]
# @return [IPAddr,:default] Destination; :default if it is the default route
attr_reader :to
# @return [Interface]
# @return [Interface,:any] Interface to associate the route to; :any if no interface is given
attr_reader :interface
# @return [IPAddr,nil]
# @return [IPAddr,nil] Source IP address ('src' in ip route)
attr_reader :source
# @return [IPAddr,nil]
# @return [IPAddr,nil] Gateway IP address ('via' in ip route)
attr_reader :gateway
# @return [String] Additional options
attr_reader :options

def initialize(to: :default, interface: :any, gateway: nil, source: nil, preference: nil)
# @param to [IPAddr,:default] Destination
# @param interface [Interface,:any] Interface to associate the root to
# @param gateway [IPAddr,nil] Gateway IP
# @param source [IPAddr,nil] Source IP
# @param options [String] Additional options
def initialize(to: :default, interface: :any, gateway: nil, source: nil, options: "")
@to = to
@interface = interface
@gateway = gateway
@source = source
@preference = preference
@options = options
end

# Determines whether it is the default route or not
Expand Down
24 changes: 17 additions & 7 deletions src/lib/y2network/routing_table.rb
Expand Up @@ -16,20 +16,30 @@
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.

require "forwardable"

module Y2Network
# Represents a routing table
#
# @example Adding routes
# table = Y2Network::RoutingTable.new
# route = Y2Network::Route.new(to: IPAddr.new("192.168.122.0/24"))
# table << route
#
# @example Iterating through routes
# table.map { |r| r.to } #=> [<IPAddr: IPv4:192.168.122.0/255.255.255.0>]
class RoutingTable
extend Forwardable
include Enumerable

# @return [Array<Route>] Routes included in the table
attr_reader :routes

# MAIN_TABLE_ID = 254
def_delegator :@routes, :each

def initialize(routes = [])
# @id = MAIN_TABLE_ID
@routes = routes
end

# @param route [Y2Network::Route] Route to add
def <<(route)
@routes << route
end
end
end

0 comments on commit 7dbd0c1

Please sign in to comment.