Class: Proxy::DHCP::EfficientIp::Provider

Inherits:
Server
  • Object
show all
Defined in:
lib/smart_proxy_efficient_ip/main.rb

Instance Method Summary collapse

Constructor Details

#initialize(api, managed_subnets) ⇒ Provider

Returns a new instance of Provider.



12
13
14
15
16
# File 'lib/smart_proxy_efficient_ip/main.rb', line 12

def initialize(api, managed_subnets)
  @managed_subnets = managed_subnets
  @api = api
  super('efficient_ip', managed_subnets, nil)
end

Instance Method Details

#add_record(params) ⇒ Object



107
108
109
110
# File 'lib/smart_proxy_efficient_ip/main.rb', line 107

def add_record(params)
  logger.debug("Adding record with: #{params.to_s}")
  api.add_record(params)
end

#all_hosts(network_address) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/smart_proxy_efficient_ip/main.rb', line 46

def all_hosts(network_address)
  logger.debug("Fetching hosts for #{network_address}")
  hosts = api.hosts(network_address)
  return [] unless hosts

  subnet = find_subnet(network_address)
  hosts.map do |host|
    Proxy::DHCP::Reservation.new(
      host['name'], host['hostaddr'], host['mac_addr'], subnet
    )
  end
end

#all_leases(network_address) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/smart_proxy_efficient_ip/main.rb', line 59

def all_leases(network_address)
  logger.debug("Fetching leases for #{network_address}")
  leases = api.leases(network_address)
  return [] unless leases

  subnet = find_subnet(network_address)
  leases.map do |lease|
    Proxy::DHCP::Lease.new(
      lease['dhcplease_name'],
      lease['dhcplease_addr'],
      lease['dhcplease_mac_addr'].split(':')[1..6].join(':'),
      subnet,
      DateTime.strptime(lease['dhcplease_first_time'], '%s'),
      DateTime.strptime(lease['dhcplease_end_time'], '%s'),
      lease['time_to_expire'].to_i > 0 ? 'active' : 'free'
    )
  end
end

#del_record(record) ⇒ Object



112
113
114
115
# File 'lib/smart_proxy_efficient_ip/main.rb', line 112

def del_record(record)
  logger.debug("Deleting record: #{record.to_s}")
  api.delete_record(record)
end

#find_record(subnet_address, ip_or_mac_address) ⇒ Object



85
86
87
88
89
90
91
92
# File 'lib/smart_proxy_efficient_ip/main.rb', line 85

def find_record(subnet_address, ip_or_mac_address)
  logger.debug("Finding record for subnet:#{subnet_address} and address:#{ip_or_mac_address}")

  subnet = find_subnet(subnet_address)
  record = api.find_record(ip_or_mac_address)

  record ? build_reservation(subnet, record) : nil
end

#find_record_by_ipObject



10
# File 'lib/smart_proxy_efficient_ip/main.rb', line 10

alias_method :find_record_by_ip, :find_record

#find_record_by_macObject



9
# File 'lib/smart_proxy_efficient_ip/main.rb', line 9

alias_method :find_record_by_mac, :find_record

#find_records_by_ip(subnet_address, ip_or_mac) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/smart_proxy_efficient_ip/main.rb', line 94

def find_records_by_ip(subnet_address, ip_or_mac)
  logger.debug("Finding records by address: #{ip_or_mac}")

  records = api.find_records(ip_or_mac)
  return [] if records.empty?
  subnet = find_subnet(subnet_address)

  records.map do |record|
    reserv = build_reservation(subnet, record)
    reserv unless reserv.nil?
  end.compact
end

#find_subnet(network_address) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/smart_proxy_efficient_ip/main.rb', line 18

def find_subnet(network_address)
  logger.debug("Finding subnet #{network_address}")
  subnet = api.find_subnet(network_address)
  return nil unless subnet

  netmask = SIZE_TO_MASK[subnet['subnet_size'].to_i]
  ::Proxy::DHCP::Subnet.new(network_address, netmask)
end

#get_subnet(network_address) ⇒ Object



27
28
29
30
# File 'lib/smart_proxy_efficient_ip/main.rb', line 27

def get_subnet(network_address)
  find_subnet(network_address) ||
    raise(Proxy::DHCP::SubnetNotFound.new("No such subnet: %s" % [network_address]))
end

#subnetsObject



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/smart_proxy_efficient_ip/main.rb', line 32

def subnets
  result = api.subnets

  result.map do |subnet|
    address = subnet['start_hostaddr']
    subnet_size = subnet['subnet_size'].to_i
    netmask = SIZE_TO_MASK[subnet_size]

    if subnet_size >= 1 && managed_subnet?("#{address}/#{netmask}")
      Proxy::DHCP::Subnet.new(address, netmask)
    end
  end.compact
end

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



78
79
80
81
82
83
# File 'lib/smart_proxy_efficient_ip/main.rb', line 78

def unused_ip(network_address, _, from_ip_address, to_ip_address)
  logger.debug("Searching first unused ip from:#{from_ip_address} to:#{to_ip_address}")

  free_ip =  api.find_free(network_address, from_ip_address, to_ip_address)
  free_ip['hostaddr'] if free_ip
end