Class: Proxy::DHCP::Infoblox::Provider

Inherits:
Server
  • Object
show all
Includes:
IpAddressArithmetic, Log, Util
Defined in:
lib/smart_proxy_dhcp_infoblox/dhcp_infoblox_main.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from IpAddressArithmetic

#cidr_to_bitmask, #cidr_to_i, #cidr_to_ip_mask, #i_to_ipv4, #ipv4_to_i, #network_cidr_to_range

Constructor Details

#initialize(connection, crud, restart_grid, unused_ips, managed_subnets, network_view) ⇒ Provider

Returns a new instance of Provider.



12
13
14
15
16
17
18
# File 'lib/smart_proxy_dhcp_infoblox/dhcp_infoblox_main.rb', line 12

def initialize(connection, crud, restart_grid, unused_ips, managed_subnets, network_view)
  @connection = connection
  @crud = crud
  @restart_grid = restart_grid
  @network_view = network_view
  super('infoblox', managed_subnets, nil, unused_ips)
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



10
11
12
# File 'lib/smart_proxy_dhcp_infoblox/dhcp_infoblox_main.rb', line 10

def connection
  @connection
end

#crudObject (readonly)

Returns the value of attribute crud.



10
11
12
# File 'lib/smart_proxy_dhcp_infoblox/dhcp_infoblox_main.rb', line 10

def crud
  @crud
end

#network_viewObject (readonly)

Returns the value of attribute network_view.



10
11
12
# File 'lib/smart_proxy_dhcp_infoblox/dhcp_infoblox_main.rb', line 10

def network_view
  @network_view
end

#restart_gridObject (readonly)

Returns the value of attribute restart_grid.



10
11
12
# File 'lib/smart_proxy_dhcp_infoblox/dhcp_infoblox_main.rb', line 10

def restart_grid
  @restart_grid
end

Instance Method Details

#add_record(options) ⇒ Object



50
51
52
53
54
# File 'lib/smart_proxy_dhcp_infoblox/dhcp_infoblox_main.rb', line 50

def add_record(options)
  crud.add_record(options)
  logger.debug("Added DHCP reservation for #{options[:ip]}/#{options[:mac]}")
  restart_grid.try_restart
end

#all_hosts(network_address) ⇒ Object



30
31
32
# File 'lib/smart_proxy_dhcp_infoblox/dhcp_infoblox_main.rb', line 30

def all_hosts(network_address)
  crud.all_hosts(full_network_address(network_address))
end

#all_leases(network_address) ⇒ Object



34
35
36
# File 'lib/smart_proxy_dhcp_infoblox/dhcp_infoblox_main.rb', line 34

def all_leases(network_address)
  crud.all_leases(full_network_address(network_address), find_subnet(network_address))
end

#del_record(record) ⇒ Object



56
57
58
59
60
# File 'lib/smart_proxy_dhcp_infoblox/dhcp_infoblox_main.rb', line 56

def del_record(record)
  crud.del_record(full_network_address(record.subnet_address), record)
  logger.debug("Removed DHCP reservation for #{record.ip} => #{record}")
  restart_grid.try_restart
end

#del_record_by_mac(_, mac_address) ⇒ Object



62
63
64
65
66
# File 'lib/smart_proxy_dhcp_infoblox/dhcp_infoblox_main.rb', line 62

def del_record_by_mac(_, mac_address)
  crud.del_record_by_mac(mac_address)
  logger.debug("Removed DHCP reservation for #{mac_address}")
  restart_grid.try_restart
end

#del_records_by_ip(_, ip_address) ⇒ Object



68
69
70
71
72
# File 'lib/smart_proxy_dhcp_infoblox/dhcp_infoblox_main.rb', line 68

def del_records_by_ip(_, ip_address)
  crud.del_records_by_ip(ip_address)
  logger.debug("Removed DHCP reservation(s) for #{ip_address}")
  restart_grid.try_restart
end

#find_ip_by_mac_address_and_range(subnet, mac_address, from_address, to_address) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'lib/smart_proxy_dhcp_infoblox/dhcp_infoblox_main.rb', line 81

def find_ip_by_mac_address_and_range(subnet, mac_address, from_address, to_address)
  r = crud.find_record_by_mac("#{subnet.network}/#{subnet.cidr}", mac_address)

  if r && (IPAddr.new(from_address)..IPAddr.new(to_address)).cover?(IPAddr.new(r.ip))
    logger.debug "Found an existing DHCP record #{r}, reusing..."
    return r.ip
  end

  nil
end

#find_network(network_address) ⇒ Object



92
93
94
95
96
97
# File 'lib/smart_proxy_dhcp_infoblox/dhcp_infoblox_main.rb', line 92

def find_network(network_address)
  network = ::Infoblox::Network.find(connection, 'network' => network_address, 'network_view' => network_view,
                                     '_max_results' => 1).first
  raise "Subnet #{network_address} not found in network view #{network_view}" if network.nil?
  network
end

#find_record(subnet_address, an_address) ⇒ Object



38
39
40
# File 'lib/smart_proxy_dhcp_infoblox/dhcp_infoblox_main.rb', line 38

def find_record(subnet_address, an_address)
  crud.find_record(full_network_address(subnet_address), an_address)
end

#find_record_by_mac(subnet_address, mac_address) ⇒ Object



42
43
44
# File 'lib/smart_proxy_dhcp_infoblox/dhcp_infoblox_main.rb', line 42

def find_record_by_mac(subnet_address, mac_address)
  crud.find_record_by_mac(full_network_address(subnet_address), mac_address)
end

#find_records_by_ip(subnet_address, ip_address) ⇒ Object



46
47
48
# File 'lib/smart_proxy_dhcp_infoblox/dhcp_infoblox_main.rb', line 46

def find_records_by_ip(subnet_address, ip_address)
  crud.find_records_by_ip(full_network_address(subnet_address), ip_address)
end

#find_subnet(address) ⇒ Object



20
# File 'lib/smart_proxy_dhcp_infoblox/dhcp_infoblox_main.rb', line 20

def find_subnet(address); ::Proxy::DHCP::Subnet.new(address, '255.255.255.0'); end

#full_network_address(network_address) ⇒ Object



99
100
101
# File 'lib/smart_proxy_dhcp_infoblox/dhcp_infoblox_main.rb', line 99

def full_network_address(network_address)
  find_network(network_address).network
end

#get_subnet(subnet_address) ⇒ Object



75
76
77
78
79
# File 'lib/smart_proxy_dhcp_infoblox/dhcp_infoblox_main.rb', line 75

def get_subnet(subnet_address)
  address, prefix_length = full_network_address(subnet_address).split("/")
  netmask = cidr_to_ip_mask(prefix_length.to_i)
  ::Proxy::DHCP::Subnet.new(address, netmask)
end

#subnetsObject



22
23
24
25
26
27
28
# File 'lib/smart_proxy_dhcp_infoblox/dhcp_infoblox_main.rb', line 22

def subnets
  ::Infoblox::Network.all(connection).map do |network|
    address, prefix_length = network.network.split("/")
    netmask = cidr_to_ip_mask(prefix_length.to_i)
    managed_subnet?("#{address}/#{netmask}") ? Proxy::DHCP::Subnet.new(address, netmask, {}) : nil
  end.compact
end