Class: Furnish::IP

Inherits:
RangeSet show all
Defined in:
lib/furnish/ip.rb,
lib/furnish/ip/version.rb

Overview

Furnish::IP maintains a saved registry of IP allocations, and can be instantiated any time after Furnish is.

Constant Summary collapse

VERSION =

Version for furnish-ip

"0.2.1"

Instance Attribute Summary collapse

Attributes inherited from RangeSet

#allocated, #allocation_type, #groups, #name, #range

Instance Method Summary collapse

Methods inherited from RangeSet

#allocate, #allocated?, #assign_group_items, #deallocate, #decommission_group, #group_items, #remove_from_group, #replace_group, #unused

Constructor Details

#initialize(subnet = nil, name = "default") ⇒ IP

Instantiate a new IP registry.

Subnet is expected to be a String in CIDR notation or IPAddr object. If nil is passed, indicates no automated allocation logic is to be used and calls that do so will raise if referenced, allowing you to record allocations from another source.

The name is a string and is “default” by default. Can be used to create semantic relationships or refer to an existing collection with a newly constructed object.



27
28
29
30
31
32
33
34
# File 'lib/furnish/ip.rb', line 27

def initialize(subnet=nil, name="default")
  if subnet and !subnet.kind_of?(IPAddr)
    subnet = IPAddr.new(subnet) unless subnet.kind_of?(IPAddr)
  end

  @subnet = subnet
  super(subnet ? subnet.to_range : (0..0), "ip", name)
end

Instance Attribute Details

#subnetObject (readonly)

the subnet being worked over as an IPAddr object.



13
14
15
# File 'lib/furnish/ip.rb', line 13

def subnet
  @subnet
end

Instance Method Details

#allocate_network(gateway) ⇒ Object



62
63
64
65
66
# File 'lib/furnish/ip.rb', line 62

def allocate_network(gateway)
  allocate(range.first.to_s)
  allocate(range.last.to_s)
  allocate(gateway)
end

#gatewayObject



58
59
60
# File 'lib/furnish/ip.rb', line 58

def gateway
  allocated.to_a.sort[1]
end

#netmaskObject



53
54
55
56
# File 'lib/furnish/ip.rb', line 53

def netmask
  # FIXME this is pretty hacky but this is the only way to do it with IPAddr.
  subnet.send(:_to_string, subnet.instance_variable_get(:@mask_addr))
end

#unused_ip(name = nil) ⇒ Object

Determine, based on allocations already made, what the next available un-used IP is. Will raise ArgumentError if #new is not provided with an IPAddr object (see the documentation there for details).

If a name is supplied, it will add it to the group IP registry for that name.

Will raise RuntimeError if the subnet is exhausted.



45
46
47
48
49
50
51
# File 'lib/furnish/ip.rb', line 45

def unused_ip(name=nil)
  unless subnet
    raise ArgumentError, "#{self.class}#unused_ip requires an IPAddr object to work"
  end

  unused(name)
end