Class: Proxy::DHCP::Infoblox::CommonCRUD

Inherits:
Object
  • Object
show all
Includes:
IpAddressArithmetic, Validations
Defined in:
lib/smart_proxy_dhcp_infoblox/common_crud.rb

Direct Known Subclasses

FixedAddressCRUD, HostIpv4AddressCRUD

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) ⇒ CommonCRUD

Returns a new instance of CommonCRUD.



13
14
15
# File 'lib/smart_proxy_dhcp_infoblox/common_crud.rb', line 13

def initialize(connection)
  @connection = connection
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



11
12
13
# File 'lib/smart_proxy_dhcp_infoblox/common_crud.rb', line 11

def connection
  @connection
end

Instance Method Details

#add_record(options) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/smart_proxy_dhcp_infoblox/common_crud.rb', line 38

def add_record(options)
  validate_ip(options[:ip])
  validate_mac(options[:mac])
  raise(Proxy::DHCP::Error, "Must provide hostname") unless options[:hostname]

  build_host(options).post
  # TODO: DELETE ME needed for testing on infoblox ipam express
  #host.configure_for_dns = false
rescue Infoblox::Error => e
  raise e unless e.message.include?("IB.Data.Conflict") # not a conflict

  begin
    existing_name, existing_host = find_host_and_name_by_ip(options[:ip])
  rescue Exception
    raise e
  end
  raise e if existing_host.nil? # something weird going on, re-raise the original exception

  if options[:mac] != existing_host.mac || options[:hostname] != existing_name
    raise Proxy::DHCP::Collision, "Record #{options[:ip]} conflicts with an existing record."
  end
  raise Proxy::DHCP::AlreadyExists, "Record #{options[:ip]} already exists."
end

#all_leases(network_address, subnet) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/smart_proxy_dhcp_infoblox/common_crud.rb', line 17

def all_leases(network_address, subnet)
  address_range_regex = NetworkAddressesRegularExpressionGenerator.new.generate_regex(network_address)
  ::Infoblox::Lease.find(@connection, 'address~' => address_range_regex).map do |lease|
    Proxy::DHCP::Lease.new(
      lease.client_hostname,
      lease.address,
      lease.hardware,
      subnet,
      lease.starts,
      lease.ends,
      lease.binding_state,
      :hostname => lease.client_hostname
    )
  end
end

#build_reservation(name, host, full_subnet_address) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/smart_proxy_dhcp_infoblox/common_crud.rb', line 83

def build_reservation(name, host, full_subnet_address)
  return nil if host.nil?
  return nil if name.nil? || name.empty?
  return nil if (host.respond_to?(:configure_for_dhcp) && !host.configure_for_dhcp)
  return nil if host.mac.nil? || host.mac.empty?

  opts = { :hostname => name }
  opts[:deleteable] = true
  # TODO: nextserver, use_nextserver, bootfile, and use_bootfile attrs exist but are not available in the Fixedaddress model
  # Might be useful to extend the model to include these
  opts[:nextServer] = host.nextserver if (host.respond_to?(:use_nextserver) && host.use_nextserver)
  opts[:filename] = host.bootfile if (host.respond_to?(:use_bootfile) && host.use_bootfile)
  subnet = ::Proxy::DHCP::Subnet.new(full_subnet_address.split('/').first, cidr_to_ip_mask(cidr_to_i(full_subnet_address.split('/').last)))

  Proxy::DHCP::Reservation.new(name, host.ipv4addr, host.mac, subnet, opts)
end

#del_record(_, record) ⇒ Object

Raises:

  • (InvalidRecord)


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

def del_record(_, record)
  raise InvalidRecord, "#{record} is static - unable to delete" unless record.deleteable?
  found = find_hosts('ipv4addr' => record.ip).first
  return if found.nil?
  found.delete
end

#del_record_by_mac(mac_address) ⇒ Object



76
77
78
79
80
81
# File 'lib/smart_proxy_dhcp_infoblox/common_crud.rb', line 76

def del_record_by_mac(mac_address)
  found = find_hosts('mac' => mac_address).first
  return if found.nil?
  found.delete
  nil
end

#del_records_by_ip(ip_address) ⇒ Object



69
70
71
72
73
74
# File 'lib/smart_proxy_dhcp_infoblox/common_crud.rb', line 69

def del_records_by_ip(ip_address)
  found = find_hosts({ 'ipv4addr' => ip_address }, 2147483646)
  return if found.empty?
  found.each { |record| record.delete }
  nil
end

#find_record(subnet_address, an_address) ⇒ Object



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

def find_record(subnet_address, an_address)
  return find_record_by_ip(subnet_address, an_address) if Resolv::IPv4::Regex =~ an_address
  find_record_by_mac(subnet_address, an_address)
end