Class: Proxy::DHCP::Device42::Provider

Inherits:
Server
  • Object
show all
Includes:
Log, Util
Defined in:
lib/smart_proxy_dhcp_device42/dhcp_device42_main.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, managed_subnets) ⇒ Provider

Returns a new instance of Provider.



10
11
12
13
14
# File 'lib/smart_proxy_dhcp_device42/dhcp_device42_main.rb', line 10

def initialize(connection, managed_subnets)
  @connection = connection
  @managed_subnets = managed_subnets
  super('device42', managed_subnets, nil)
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



8
9
10
# File 'lib/smart_proxy_dhcp_device42/dhcp_device42_main.rb', line 8

def connection
  @connection
end

Instance Method Details

#add_record(options) ⇒ Object



85
86
87
# File 'lib/smart_proxy_dhcp_device42/dhcp_device42_main.rb', line 85

def add_record(options)
  @connection.add_host(options)
end

#all_hosts(network_address) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/smart_proxy_dhcp_device42/dhcp_device42_main.rb', line 49

def all_hosts(network_address)
  records = @connection.get_hosts(network_address)
  return [] if records.empty?
  reservs = []
  records.each do |record|
    reserv = build_reservation(record)
    reservs.push(reserv) if !reserv.nil?
  end
  reservs
end

#all_leases(network_address) ⇒ Object



60
61
62
# File 'lib/smart_proxy_dhcp_device42/dhcp_device42_main.rb', line 60

def all_leases(network_address)
  [] # device42 doesn't support leases
end

#build_reservation(record) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/smart_proxy_dhcp_device42/dhcp_device42_main.rb', line 21

def build_reservation(record)
  return nil if record.empty?
  return nil if record['name'].nil? || record['name'].empty?
  return nil if record['hwaddress'].nil? || record['hwaddress'].empty?

  mac = record['hwaddress']
  mac = mac.gsub(/(.{2})/, '\1:')[0...-1]
  opts = {:hostname => record['name']}
  subnet = ::Proxy::DHCP::Subnet.new(record['network'], cidr_to_ip_mask(record['mask_bits'].to_i))
  Proxy::DHCP::Reservation.new(record['name'], record['ip_address'], mac, subnet, opts)
end

#cidr_to_ip_mask(prefix_length) ⇒ Object



16
17
18
19
# File 'lib/smart_proxy_dhcp_device42/dhcp_device42_main.rb', line 16

def cidr_to_ip_mask(prefix_length)
  bitmask = 0xFFFFFFFF ^ (2 ** (32-prefix_length) - 1)
  (0..3).map {|i| (bitmask >> i*8) & 0xFF}.reverse.join('.')
end

#del_record(record) ⇒ Object



89
90
91
# File 'lib/smart_proxy_dhcp_device42/dhcp_device42_main.rb', line 89

def del_record(record)
  @connection.remove_host(record.name)
end

#find_record_by_mac(subnet_address, mac_address) ⇒ Object



79
80
81
82
83
# File 'lib/smart_proxy_dhcp_device42/dhcp_device42_main.rb', line 79

def find_record_by_mac(subnet_address, mac_address)
  record = @connection.get_host_by_mac(mac_address)
  return nil if record.empty?
  build_reservation(record[0])
end

#find_records_by_ip(subnet_address, ip) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/smart_proxy_dhcp_device42/dhcp_device42_main.rb', line 68

def find_records_by_ip(subnet_address, ip)
  records = @connection.get_hosts_by_ip(ip)
  return [] if records.empty?
  reservs = []
  records.each do |record|
    reserv = build_reservation(record)
    reservs.push(reserv) if !reserv.nil?
  end
  reservs
end

#subnetsObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/smart_proxy_dhcp_device42/dhcp_device42_main.rb', line 33

def subnets
  difined_subnets = []
  @connection.get_subnets().each do |subnet|
    address = subnet['network']
    if !['::'].include? address
      if subnet['mask_bits'].to_i <= 32
        netmask = cidr_to_ip_mask(subnet['mask_bits'].to_i)
        if managed_subnet?("#{address}/#{netmask}")
          difined_subnets.push(Proxy::DHCP::Subnet.new(address, netmask, {}))
        end
      end
    end
  end.compact
  difined_subnets
end

#unused_ip(subnet, _, from_ip_address, to_ip_address) ⇒ Object



64
65
66
# File 'lib/smart_proxy_dhcp_device42/dhcp_device42_main.rb', line 64

def unused_ip(subnet, _, from_ip_address, to_ip_address)
  @connection.get_next_ip(subnet, from_ip_address, to_ip_address)
end