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.



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

def initialize(connection)
  @connection = connection
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



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

def connection
  @connection
end

Instance Method Details

#add_record(options) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/smart_proxy_dhcp_infoblox/common_crud.rb', line 25

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



16
17
18
# File 'lib/smart_proxy_dhcp_infoblox/common_crud.rb', line 16

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

#build_reservation(name, host, full_subnet_address) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/smart_proxy_dhcp_infoblox/common_crud.rb', line 70

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)


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

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



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

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



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

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



20
21
22
23
# File 'lib/smart_proxy_dhcp_infoblox/common_crud.rb', line 20

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