Class: ProxyAPI::DHCP

Inherits:
Resource show all
Defined in:
lib/proxy_api/dhcp.rb

Instance Attribute Summary

Attributes inherited from Resource

#url

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ DHCP


3
4
5
6
# File 'lib/proxy_api/dhcp.rb', line 3

def initialize args
  @url  = args[:url] + "/dhcp"
  super args
end

Instance Method Details

#delete(subnet, mac) ⇒ Object

Deletes a DHCP entry

subnet

: String in dotted decimal format

mac

: String in coloned sextuplet format

Returns : Boolean status


63
64
65
66
67
68
# File 'lib/proxy_api/dhcp.rb', line 63

def delete subnet, mac
  parse super("#{subnet}/#{mac}")
rescue RestClient::ResourceNotFound
  # entry doesn't exists anyway
  return true
end

#record(subnet, mac) ⇒ Object

Retrieves a DHCP entry

subnet

: String in dotted decimal format

mac

: String in coloned sextuplet format

Returns : Hash or false


36
37
38
39
40
41
42
43
44
45
46
# File 'lib/proxy_api/dhcp.rb', line 36

def record subnet, mac
  response = parse(get("#{subnet}/#{mac}"))
  attrs = response.merge(:network => subnet, :proxy => self)
  if response.keys.grep(/Sun/i).empty?
    Net::DHCP::Record.new attrs
  else
    Net::DHCP::SparcRecord.new attrs
  end
rescue RestClient::ResourceNotFound
  nil
end

#set(subnet, args) ⇒ Object

Sets a DHCP entry

subnet

: String in dotted decimal format

mac

: String in coloned sextuplet format

args

: Hash containing DHCP values. The :mac key is taken from the mac parameter

Returns : Boolean status


53
54
55
56
57
# File 'lib/proxy_api/dhcp.rb', line 53

def set subnet, args
  raise "Must define a subnet" if subnet.empty?
  raise "Must provide arguments" unless args.is_a?(Hash)
  parse(post(args, subnet.to_s))
end

#subnet(subnet) ⇒ Object


15
16
17
# File 'lib/proxy_api/dhcp.rb', line 15

def subnet subnet
  parse get(subnet)
end

#subnetsObject

Retrieve the Server's subnets Returns: Array of Hashes or false Example [ProxyAPI::DHCP.“network”:“192.168.11.0”,“netmask”:“255“network”:“192.168.11.0”,“netmask”:“255.255“network”:“192.168.11.0”,“netmask”:“255.255.255“network”:“192.168.11.0”,“netmask”:“255.255.255.0”,ProxyAPI::DHCP.“network”:“192.168.122.0”,“netmask”:“255“network”:“192.168.122.0”,“netmask”:“255.255“network”:“192.168.122.0”,“netmask”:“255.255.255“network”:“192.168.122.0”,“netmask”:“255.255.255.0”]


11
12
13
# File 'lib/proxy_api/dhcp.rb', line 11

def subnets
  parse get
end

#unused_ip(subnet, mac = nil) ⇒ Object


19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/proxy_api/dhcp.rb', line 19

def unused_ip subnet, mac = nil
  params = {}
  params.merge!({:mac => mac}) if mac.present?

  params.merge!({:from => subnet.from, :to => subnet.to}) if subnet.from.present? and subnet.to.present?
  if params.any?
    params = "?" + params.map{|e| e.join("=")}.join("&")
  else
    params = ""
  end
  parse get("#{subnet.network}/unused_ip#{params}")
end