Class: Net::DHCP::Record

Inherits:
Record
  • Object
show all
Defined in:
lib/net/dhcp/record.rb

Direct Known Subclasses

SparcRecord

Constant Summary

Constants included from Validations

Validations::IP_REGEXP, Validations::MAC_REGEXP

Instance Attribute Summary collapse

Attributes inherited from Record

#hostname, #logger, #proxy

Instance Method Summary collapse

Methods inherited from Record

#==, #conflicting?, #inspect, #reload!

Methods included from Validations

normalize_ip, normalize_mac, #validate_ip, #validate_mac, #validate_network

Constructor Details

#initialize(opts = { }) ⇒ Record


5
6
7
8
9
10
# File 'lib/net/dhcp/record.rb', line 5

def initialize opts = { }
  super(opts)
  self.mac     = validate_mac self.mac
  self.network = validate_network self.network
  self.ip      = validate_ip self.ip
end

Instance Attribute Details

#filenameObject

Returns the value of attribute filename


3
4
5
# File 'lib/net/dhcp/record.rb', line 3

def filename
  @filename
end

#ipObject

Returns the value of attribute ip


3
4
5
# File 'lib/net/dhcp/record.rb', line 3

def ip
  @ip
end

#macObject

Returns the value of attribute mac


3
4
5
# File 'lib/net/dhcp/record.rb', line 3

def mac
  @mac
end

#networkObject

Returns the value of attribute network


3
4
5
# File 'lib/net/dhcp/record.rb', line 3

def network
  @network
end

#nextServerObject

Returns the value of attribute nextServer


3
4
5
# File 'lib/net/dhcp/record.rb', line 3

def nextServer
  @nextServer
end

Instance Method Details

#attrsObject


50
51
52
53
54
# File 'lib/net/dhcp/record.rb', line 50

def attrs
  { :hostname   => hostname, :mac => mac, :ip => ip, :network => network,
    :nextServer => nextServer, :filename => filename
  }.delete_if { |k, v| v.nil? }
end

#conflictsObject

Returns an array of record objects which are conflicting with our own


40
41
42
# File 'lib/net/dhcp/record.rb', line 40

def conflicts
  @conflicts ||= [proxy.record(network, mac), proxy.record(network, ip)].delete_if { |c| c == self }.compact
end

#createObject

Create a DHCP entry


24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/net/dhcp/record.rb', line 24

def create
  logger.info "Create DHCP reservation for #{to_s}"
  begin
    proxy.set network, attrs
  rescue RestClient::Conflict
    logger.warn "Conflicting DHCP reservation for #{to_s} detected"
    e          = Net::Conflict.new
    e.type     = "dhcp"
    e.expected = to_s
    e.actual   = conflicts
    e.message  = "in DHCP detected - expected #{to_s}, found #{conflicts.map(&:to_s).join(', ')}"
    raise e
  end
end

#destroyObject

Deletes the DHCP entry


17
18
19
20
21
# File 'lib/net/dhcp/record.rb', line 17

def destroy
  logger.info "Delete DHCP reservation for #{to_s}"
  # it is safe to call destroy even if the entry does not exists, so we don't bother with validating anything here.
  proxy.delete network, mac
end

#to_sObject


12
13
14
# File 'lib/net/dhcp/record.rb', line 12

def to_s
  "#{hostname}-#{mac}/#{ip}"
end

#valid?Boolean

Verifies that are record already exists on the dhcp server


45
46
47
48
# File 'lib/net/dhcp/record.rb', line 45

def valid?
  logger.info "Fetching DHCP reservation for #{to_s}"
  self == proxy.record(network, mac)
end