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.0"

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

#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