Module: Bosh::Director::IpUtil

Instance Method Summary collapse

Instance Method Details

#each_ip(ranges, &block) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/bosh/director/ip_util.rb', line 6

def each_ip(ranges, &block)
  if ranges.kind_of?(Array)
    ranges.each do |range|
      process_range(range, &block)
    end
  elsif ranges.kind_of?(String)
    process_range(ranges, &block)
  elsif !ranges.nil?
    raise ArgumentError,
          "Unknown range type, must be list or a string: " +
          "#{ranges.class} #{ranges}"
  end
end

#format_ip(ip) ⇒ String

Returns Human-readable IP representation.

Parameters:

  • ip (Integer)

    Integer IP representation

Returns:

  • (String)

    Human-readable IP representation



62
63
64
# File 'lib/bosh/director/ip_util.rb', line 62

def format_ip(ip)
  ip_to_netaddr(ip).ip
end

#ip_to_i(ip) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/bosh/director/ip_util.rb', line 43

def ip_to_i(ip)
  unless ip.kind_of?(Integer)
    unless ip.kind_of?(NetAddr::CIDR)
      ip = NetAddr::CIDR.create(ip)
    end
    ip = ip.to_i
  end
  ip
end

#ip_to_netaddr(ip) ⇒ Object



53
54
55
56
57
58
# File 'lib/bosh/director/ip_util.rb', line 53

def ip_to_netaddr(ip)
  unless ip.kind_of?(NetAddr::CIDR)
    ip = NetAddr::CIDR.create(ip)
  end
  ip
end

#process_range(range) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/bosh/director/ip_util.rb', line 20

def process_range(range)
  parts = range.split("-")
  parts.each { |part| part.strip! }
  if parts.size == 1
    range    = NetAddr::CIDR.create(parts[0])
    first_ip = range.first(:Objectify => true).to_i
    last_ip  = range.last(:Objectify => true).to_i
    (first_ip .. last_ip).each do |ip|
      yield ip
    end
  elsif parts.size == 2
    first_ip = NetAddr::CIDR.create(parts[0])
    last_ip  = NetAddr::CIDR.create(parts[1])
    raise ArgumentError unless first_ip.size == 1
    raise ArgumentError unless last_ip.size == 1
    (first_ip.to_i .. last_ip.to_i).each do |ip|
      yield ip
    end
  else
    raise ArgumentError
  end
end