class DopCommon::Network

Attributes

name[R]

Public Class Methods

new(name, hash) click to toggle source
# File lib/dop_common/network.rb, line 12
def initialize(name, hash)
  @name = name
  @hash = deep_symbolize_keys(hash)
end

Public Instance Methods

ip_defgw() click to toggle source
# File lib/dop_common/network.rb, line 27
def ip_defgw
  @ip_defgw ||= ip_defgw_valid? ? (@hash[:ip_defgw] == false ? false : IPAddr.new(@hash[:ip_defgw])) : nil
end
ip_netmask() click to toggle source
# File lib/dop_common/network.rb, line 23
def ip_netmask
  @ip_netmask ||= ip_netmask_valid? ? IPAddr.new(@hash[:ip_netmask]) : nil
end
ip_pool() click to toggle source
# File lib/dop_common/network.rb, line 31
def ip_pool
  @ip_pool ||= ip_pool_valid? ? create_ip_pool : {}
end
validate() click to toggle source
# File lib/dop_common/network.rb, line 17
def validate
  log_validation_method(:ip_pool_valid?)
  log_validation_method(:ip_defgw_valid?)
  log_validation_method(:ip_netmask_valid?)
end

Private Instance Methods

create_ip_pool() click to toggle source
# File lib/dop_common/network.rb, line 81
def create_ip_pool
  Hash[@hash[:ip_pool].collect { |k,v| [k.to_sym, IPAddr.new(v)] }]
end
ip_defgw_valid?() click to toggle source
# File lib/dop_common/network.rb, line 45
def ip_defgw_valid?
  return false if @hash.empty?
  return true if @hash[:ip_defgw] == false
  IPAddr.new(@hash[:ip_defgw])
  true
rescue ArgumentError
  raise PlanParsingError, "Network #{@name}: Invalid default gateway definition"
end
ip_netmask_valid?() click to toggle source
# File lib/dop_common/network.rb, line 37
def ip_netmask_valid?
  return false if @hash.empty?
  IPAddr.new(@hash[:ip_netmask])
  true
rescue ArgumentError
  raise PlanParsingError, "Network #{@name}: Invalid network mask definition"
end
ip_pool_valid?() click to toggle source
# File lib/dop_common/network.rb, line 54
def ip_pool_valid?
  return false if @hash.empty? # An empty network specification is valid
  # It must be a hash with from and to keys if defined
  raise PlanParsingError, "Network #{@name}: An IP pool must be a hash with 'from' and 'to' keys" unless
    @hash[:ip_pool].kind_of?(Hash) and
    @hash[:ip_pool].has_key?(:from) and
    @hash[:ip_pool].has_key?(:to)
  # The IP defined by from must be lower than the IP defined by to keyword
  ip_from = IPAddr.new(@hash[:ip_pool][:from])
  ip_to   = IPAddr.new(@hash[:ip_pool][:to])
  raise PlanParsingError, "Network #{@name}: The IP defined by 'from' must to be lower than the IP defined by in 'to'" unless ip_from < ip_to
  if ip_defgw
    # The IP of default gateway must be out of IP pool range
    raise PlanParsingError, "Network #{@name}: The default gateway must be out of IP pool range" unless ip_defgw < ip_from || ip_defgw > ip_to
    # IPs specified by IP pool and the default gateway must belong to the same network
    net = ip_defgw.mask(ip_netmask.to_s)
    raise PlanParsingError, "Network #{@name}: IPs specified by IP pool and the default gateway must belong to the same network" unless
      net.include?(ip_from) && net.include?(ip_to)
  elsif ip_defgw.nil?
    raise PlanParsingError, "Network #{@name}: The default gateway must either be a valid IP or false if there is no default gateway"
  end
  true
rescue ArgumentError
  # Invalide IP/Netmasl definition
  raise PlanParsingError, "Network #{@name}: Invalid IP and/or netmask definition"
end