smart_proxy_dhcp_infoblox 0.0.5 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1f93a00fc3b3226cd13507406635a76d39fdf5bf
4
- data.tar.gz: 2af103b766051daba129f8a05918f704692f7362
3
+ metadata.gz: '085837132cc0862c9f3ffc24e0e2730f8e3adbb3'
4
+ data.tar.gz: 0f4eba506ee80571addcd1d4a567ba71642870e3
5
5
  SHA512:
6
- metadata.gz: 629785c6c921e5a356af8ab70703bc13d9db487dc3b4cdc2fb24ab1ff320d620e67b6762ff557a400cc1a77b4a73e7d7817c07591160e6d3de4011cf65abef48
7
- data.tar.gz: 442344c94cb0cf17e7e8915139149be7f50c545d771e570dcc7ae921c8ae98649a2f207c72e2c91c66bfdfc655fe73ab8b1b3a9ff1191c7ed2ecf470a4118fb7
6
+ metadata.gz: 42aa2e6637a6e1aaef0379d205e7f59df1e1a9aa477afc0e0b51ef723ed2b4a36560daa784f3b82d303d97e4c96acd010626ca5495f806615e244503973357c1
7
+ data.tar.gz: 6ee6869da932c9f8ef89a4996d10644cb68ed47c06ff7891e168c6c2d1cfc9948e7fb0e004b94da78e501c3286e3b867e93c7bf277946857bc8ee9b0b5c12742
@@ -48,11 +48,25 @@ module ::Proxy::DHCP::Infoblox
48
48
 
49
49
  def del_record(_, record)
50
50
  raise InvalidRecord, "#{record} is static - unable to delete" unless record.deleteable?
51
- found = find_host('ipv4addr' => record.ip)
51
+ found = find_hosts('ipv4addr' => record.ip).first
52
52
  return if found.nil?
53
53
  found.delete
54
54
  end
55
55
 
56
+ def del_records_by_ip(ip_address)
57
+ found = find_hosts({'ipv4addr' => ip_address}, 2147483646)
58
+ return if found.empty?
59
+ found.each {|record| record.delete}
60
+ nil
61
+ end
62
+
63
+ def del_record_by_mac(mac_address)
64
+ found = find_hosts('mac' => mac_address).first
65
+ return if found.nil?
66
+ found.delete
67
+ nil
68
+ end
69
+
56
70
  def build_reservation(name, host, full_subnet_address)
57
71
  return nil if host.nil?
58
72
  return nil if name.nil? || name.empty?
@@ -60,16 +74,14 @@ module ::Proxy::DHCP::Infoblox
60
74
  return nil if host.mac.nil? || host.mac.empty?
61
75
 
62
76
  opts = { :hostname => name }
63
- opts[:mac] = host.mac
64
- opts[:ip] = host.ipv4addr
65
77
  opts[:deleteable] = true
66
78
  # TODO: nextserver, use_nextserver, bootfile, and use_bootfile attrs exist but are not available in the Fixedaddress model
67
79
  # Might be useful to extend the model to include these
68
80
  opts[:nextServer] = host.nextserver if (host.respond_to?(:use_nextserver) && host.use_nextserver)
69
81
  opts[:filename] = host.bootfile if (host.respond_to?(:use_bootfile) && host.use_bootfile)
70
- opts[:subnet] = ::Proxy::DHCP::Subnet.new(full_subnet_address.split('/').first, cidr_to_ip_mask(cidr_to_i(full_subnet_address.split('/').last)))
82
+ subnet = ::Proxy::DHCP::Subnet.new(full_subnet_address.split('/').first, cidr_to_ip_mask(cidr_to_i(full_subnet_address.split('/').last)))
71
83
 
72
- Proxy::DHCP::Reservation.new(opts)
84
+ Proxy::DHCP::Reservation.new(name, host.ipv4addr, host.mac, subnet, opts)
73
85
  end
74
86
  end
75
87
  end
@@ -18,8 +18,6 @@ module Proxy::DHCP::Infoblox
18
18
  end
19
19
 
20
20
  def find_subnet(address);::Proxy::DHCP::Subnet.new(address, '255.255.255.0'); end
21
- def load_subnets; end
22
- def load_subnet_data(_); end
23
21
 
24
22
  def subnets
25
23
  ::Infoblox::Network.all(connection).map do |network|
@@ -41,20 +39,40 @@ module Proxy::DHCP::Infoblox
41
39
  crud.find_record(full_network_address(subnet_address), an_address)
42
40
  end
43
41
 
42
+ def find_record_by_mac(subnet_address, mac_address)
43
+ crud.find_record_by_mac(full_network_address(subnet_address), mac_address)
44
+ end
45
+
46
+ def find_records_by_ip(subnet_address, ip_address)
47
+ crud.find_records_by_ip(full_network_address(subnet_address), ip_address)
48
+ end
49
+
44
50
  def add_record(options)
45
51
  crud.add_record(options)
46
52
  logger.debug("Added DHCP reservation for #{options[:ip]}/#{options[:mac]}")
47
53
  restart_grid.try_restart
48
54
  end
49
55
 
50
- def del_record(subnet, record)
51
- crud.del_record(full_network_address(subnet.network), record)
56
+ def del_record(record)
57
+ crud.del_record(full_network_address(record.subnet_address), record)
52
58
  logger.debug("Removed DHCP reservation for #{record.ip} => #{record}")
53
59
  restart_grid.try_restart
54
60
  end
55
61
 
62
+ def del_record_by_mac(_, mac_address)
63
+ crud.del_record_by_mac(mac_address)
64
+ logger.debug("Removed DHCP reservation for #{mac_address}")
65
+ restart_grid.try_restart
66
+ end
67
+
68
+ def del_records_by_ip(_, ip_address)
69
+ crud.del_records_by_ip(ip_address)
70
+ logger.debug("Removed DHCP reservation(s) for #{ip_address}")
71
+ restart_grid.try_restart
72
+ end
73
+
56
74
  def unused_ip(subnet, _, from_ip_address, to_ip_address)
57
- unused_ips.unused_ip(subnet.network, from_ip_address, to_ip_address)
75
+ unused_ips.unused_ip(subnet, from_ip_address, to_ip_address)
58
76
  end
59
77
 
60
78
  def find_network(network_address)
@@ -1,7 +1,7 @@
1
1
  module Proxy
2
2
  module DHCP
3
3
  module Infoblox
4
- VERSION = '0.0.5'
4
+ VERSION = '0.0.6'
5
5
  end
6
6
  end
7
7
  end
@@ -3,7 +3,7 @@ require 'smart_proxy_dhcp_infoblox/common_crud'
3
3
  module ::Proxy::DHCP::Infoblox
4
4
  class FixedAddressCRUD < CommonCRUD
5
5
  def initialize(connection)
6
- @memoized_host = nil
6
+ @memoized_hosts = []
7
7
  @memoized_condition = nil
8
8
  super
9
9
  end
@@ -14,26 +14,32 @@ module ::Proxy::DHCP::Infoblox
14
14
  end
15
15
 
16
16
  def find_record_by_ip(subnet_address, ip_address)
17
- found = find_host('ipv4addr' => ip_address)
17
+ found = find_hosts('ipv4addr' => ip_address).first
18
18
  return nil if found.nil?
19
19
  build_reservation(found.name, found, subnet_address)
20
20
  end
21
21
 
22
+ def find_records_by_ip(subnet_address, ip_address)
23
+ found = find_hosts({'ipv4addr' => ip_address}, 2147483646)
24
+ return [] if found.empty?
25
+ found.map {|record| build_reservation(record.name, record, subnet_address)}
26
+ end
27
+
22
28
  def find_record_by_mac(subnet_address, mac_address)
23
- found = find_host('mac' => mac_address)
29
+ found = find_hosts('mac' => mac_address).first
24
30
  return nil if found.nil?
25
31
  build_reservation(found.name, found, subnet_address)
26
32
  end
27
33
 
28
- def find_host(condition)
29
- return @memoized_host if (!@memoized_host.nil? && @memoized_condition == condition)
34
+ def find_hosts(condition, max_results = 1)
35
+ return @memoized_hosts if (!@memoized_host.empty? && @memoized_condition == condition)
30
36
  @memoized_condition = condition
31
- @memoized_host = ::Infoblox::Fixedaddress.find(@connection, condition.merge('_max_results' => 1)).first
37
+ @memoized_hosts = ::Infoblox::Fixedaddress.find(@connection, condition.merge('_max_results' => max_results))
32
38
  end
33
39
 
34
40
  def find_host_and_name_by_ip(ip_address)
35
- h = find_host('ipv4addr' => ip_address)
36
- [h.name, h]
41
+ h = find_hosts('ipv4addr' => ip_address).first
42
+ h.nil? ? [nil, nil] : [h.name, h]
37
43
  end
38
44
 
39
45
  def build_host(options)
@@ -41,12 +47,10 @@ module ::Proxy::DHCP::Infoblox
41
47
  host.name = options[:hostname]
42
48
  host.ipv4addr = options[:ip]
43
49
  host.mac = options[:mac]
44
- # TODO: nextserver, use_nextserver, bootfile, and use_bootfile attrs exist but are not available in the model
45
- # Might be useful to extend the model to include these
46
- #host.nextserver = options[:nextServer]
47
- #host.use_nextserver = true
48
- #host.bootfile = options[:filename]
49
- #host.use_bootfile = true
50
+ host.nextserver = options[:nextServer]
51
+ host.use_nextserver = true
52
+ host.bootfile = options[:filename]
53
+ host.use_bootfile = true
50
54
  host
51
55
  end
52
56
  end
@@ -4,7 +4,7 @@ require 'smart_proxy_dhcp_infoblox/network_address_range_regex_generator'
4
4
  module ::Proxy::DHCP::Infoblox
5
5
  class HostIpv4AddressCRUD < CommonCRUD
6
6
  def initialize(connection)
7
- @memoized_host = nil
7
+ @memoized_hosts = []
8
8
  @memoized_condition = nil
9
9
  super
10
10
  end
@@ -22,26 +22,32 @@ module ::Proxy::DHCP::Infoblox
22
22
  end
23
23
 
24
24
  def find_record_by_ip(subnet_address, ip_address)
25
- found = find_host('ipv4addr' => ip_address)
25
+ found = find_hosts('ipv4addr' => ip_address).first
26
26
  return nil if found.nil?
27
27
  build_reservation(found.name, found.ipv4addrs.find {|ip| ip.ipv4addr == ip_address}, subnet_address)
28
28
  end
29
29
 
30
+ def find_records_by_ip(subnet_address, ip_address)
31
+ found = find_hosts({'ipv4addr' => ip_address}, 2147483646)
32
+ return [] if found.empty?
33
+ found.map {|record| build_reservation(record.name, record.ipv4addrs.find {|ip| ip.ipv4addr == ip_address}, subnet_address)}
34
+ end
35
+
30
36
  def find_record_by_mac(subnet_address, mac_address)
31
- found = find_host('mac' => mac_address)
37
+ found = find_hosts('mac' => mac_address).first
32
38
  return nil if found.nil?
33
39
  build_reservation(found.name, found.ipv4addrs.find {|ip| ip.mac == mac_address}, subnet_address)
34
40
  end
35
41
 
36
42
  def find_host_and_name_by_ip(ip_address)
37
- h = find_host('ipv4addr' => ip_address)
38
- [h.name, h.ipv4addrs.find {|ip| ip.ipv4addr == ip_address}]
43
+ h = find_hosts('ipv4addr' => ip_address).first
44
+ h.nil? ? [nil, nil] : [h.name, h.ipv4addrs.find {|ip| ip.ipv4addr == ip_address}]
39
45
  end
40
46
 
41
- def find_host(condition)
42
- return @memoized_host if (!@memoized_host.nil? && @memoized_condition == condition)
47
+ def find_hosts(condition, max_results = 1)
48
+ return @memoized_hosts if (!@memoized_hosts.empty? && @memoized_condition == condition)
43
49
  @memoized_condition = condition
44
- @memoized_host = ::Infoblox::Host.find(@connection, condition.merge('_max_results' => 1)).first
50
+ @memoized_hosts = ::Infoblox::Host.find(@connection, condition.merge('_max_results' => max_results))
45
51
  end
46
52
 
47
53
  def build_host(options)
@@ -9,6 +9,7 @@ module Proxy::DHCP::Infoblox
9
9
  def initialize(connection, use_ranges)
10
10
  @connection = connection
11
11
  @use_ranges = use_ranges
12
+ @memoized_network = nil
12
13
  end
13
14
 
14
15
  def unused_ip(network_address, from_ip_address, to_ip_address)
@@ -16,6 +16,11 @@ module CommoncrudTests
16
16
  assert_nil @crud.find_record_by_ip('192.168.42.0/24', '192.168.42.1')
17
17
  end
18
18
 
19
+ def test_find_records_using_ip_returns_empty_arrays_if_records_not_found
20
+ @entity.expects(:find).with(@connection, 'ipv4addr' => '192.168.42.1', '_max_results' => 2147483646).returns([])
21
+ assert @crud.find_records_by_ip('192.168.42.0/24', '192.168.42.1').empty?
22
+ end
23
+
19
24
  def test_find_record_using_mac
20
25
  @entity.expects(:find).with(@connection, 'mac' => '00:01:02:03:05:06', '_max_results' => 1).returns([@host])
21
26
  assert_equal @reservation, @crud.find_record_by_mac('192.168.42.0/24', '00:01:02:03:05:06')
@@ -46,16 +51,29 @@ module CommoncrudTests
46
51
  def test_add_record_with_already_existing_host
47
52
  @crud.expects(:build_host).with(:ip => @ip, :mac => @mac, :hostname => @hostname).returns(@host)
48
53
  @host.expects(:post).raises(Infoblox::Error.new("IB.Data.Conflict"))
49
- @crud.expects(:find_host).with('ipv4addr' => @ip).returns(@host)
54
+ @crud.expects(:find_hosts).with('ipv4addr' => @ip).returns([@host])
50
55
 
51
56
  assert_raises(Proxy::DHCP::AlreadyExists) { @crud.add_record(:ip => @ip, :mac => @mac, :hostname => @hostname) }
52
57
  end
53
58
 
54
59
  def test_del_record
55
- @crud.expects(:find_host).with('ipv4addr' => @ip).returns(@host)
60
+ @crud.expects(:find_hosts).with('ipv4addr' => @ip).returns([@host])
56
61
  @host.expects(:delete)
57
62
  @crud.del_record('unused', @reservation)
58
63
  end
64
+
65
+ def test_del_records_by_ip
66
+ @crud.expects(:find_hosts).with({'ipv4addr' => @ip}, 2147483646).returns([@host, @host1])
67
+ @host.expects(:delete)
68
+ @host1.expects(:delete)
69
+ @crud.del_records_by_ip(@ip)
70
+ end
71
+
72
+ def test_del_record_by_mac
73
+ @crud.expects(:find_hosts).with('mac' => @mac).returns([@host])
74
+ @host.expects(:delete)
75
+ @crud.del_record_by_mac(@mac)
76
+ end
59
77
  end
60
78
 
61
79
  class HostCrudTest < Test::Unit::TestCase
@@ -72,16 +90,20 @@ class HostCrudTest < Test::Unit::TestCase
72
90
  @nextserver = '192.168.42.1'
73
91
  @filename = '/tftpboot.img'
74
92
  @ip = '192.168.42.1'
93
+ @subnet_ip = '192.168.42.0'
75
94
 
76
95
  @host = ::Infoblox::Host.new(
77
96
  :name => @hostname,
78
97
  :ipv4addrs => [{:ipv4addr => @ip, :mac => @mac, :nextserver => @nextserver, :use_nextserver => true,
79
98
  :bootfile => @filename, :use_bootfile => true, :configure_for_dhcp => true}])
99
+ @host1 = ::Infoblox::Host.new(
100
+ :name => 'another.test.com',
101
+ :ipv4addrs => [{:ipv4addr => @ip, :mac => '00:01:02:03:05:07', :nextserver => @nextserver, :use_nextserver => true,
102
+ :bootfile => @filename, :use_bootfile => true, :configure_for_dhcp => true}])
80
103
 
81
- @reservation = ::Proxy::DHCP::Reservation.new(
82
- :hostname => @hostname, :mac => @mac, :ip => @ip, :nextServer => @nextserver,
83
- :filename => @filename, :deleteable => true,
84
- :subnet => ::Proxy::DHCP::Subnet.new('192.168.42.0', '255.255.255.0'))
104
+ @reservation = ::Proxy::DHCP::Reservation.new(@hostname, @ip, @mac, ::Proxy::DHCP::Subnet.new(@subnet_ip, '255.255.255.0'),
105
+ :hostname => @hostname, :nextServer => @nextserver, :filename => @filename,
106
+ :deleteable => true)
85
107
  end
86
108
 
87
109
  def test_all_hosts
@@ -111,8 +133,8 @@ class HostCrudTest < Test::Unit::TestCase
111
133
  def test_add_record_with_collision
112
134
  @crud.expects(:build_host).with(:ip => @ip, :mac => @mac, :hostname => @hostname).returns(@host)
113
135
  @host.expects(:post).raises(Infoblox::Error.new("IB.Data.Conflict"))
114
- @crud.expects(:find_host).with('ipv4addr' => @ip).returns(
115
- ::Infoblox::Host.new(:name => @hostname, :ipv4addrs => [{:ipv4addr => @ip, :mac => '11:22:33:44:55:66', :configure_for_dhcp => true}]))
136
+ @crud.expects(:find_hosts).with('ipv4addr' => @ip).returns(
137
+ [::Infoblox::Host.new(:name => @hostname, :ipv4addrs => [{:ipv4addr => @ip, :mac => '11:22:33:44:55:66', :configure_for_dhcp => true}])])
116
138
 
117
139
  assert_raises(Proxy::DHCP::Collision) { @crud.add_record(:ip => @ip, :mac => @mac, :hostname => @hostname) }
118
140
  end
@@ -130,14 +152,17 @@ class FixedaddressCrudTest < Test::Unit::TestCase
130
152
  @nextserver = '192.168.42.1'
131
153
  @filename = '/tftpboot.img'
132
154
  @ip = '192.168.42.1'
155
+ @subnet_ip = '192.168.42.0'
133
156
 
134
157
  @host = ::Infoblox::Fixedaddress.new(
135
158
  :name => @hostname,
136
159
  :ipv4addr => @ip, :mac => @mac) # :ipv4addr => @ip, :mac => @mac) #
160
+ @host1 = ::Infoblox::Fixedaddress.new(
161
+ :name => 'another.test.com',
162
+ :ipv4addr => @ip, :mac => '00:01:02:03:05:07')
137
163
 
138
- @reservation = ::Proxy::DHCP::Reservation.new(
139
- :hostname => @hostname, :mac => @mac, :ip => @ip, :deleteable => true,
140
- :subnet => ::Proxy::DHCP::Subnet.new('192.168.42.0', '255.255.255.0'))
164
+ @reservation = ::Proxy::DHCP::Reservation.new(@hostname, @ip, @mac, ::Proxy::DHCP::Subnet.new(@subnet_ip, '255.255.255.0'),
165
+ :deleteable => true, :hostname => @hostname)
141
166
  end
142
167
 
143
168
  def test_all_hosts
@@ -152,17 +177,17 @@ class FixedaddressCrudTest < Test::Unit::TestCase
152
177
  assert_equal @host.name, built.name
153
178
  assert_equal @host.ipv4addr, built.ipv4addr
154
179
  assert_equal @host.mac, built.mac
155
- # assert @host.nextserver, built.nextserver
156
- # assert built.use_nextserver
157
- # assert_equal @host.bootfile, built.bootfile
158
- # assert built.use_bootfile
180
+ assert_equal @nextserver, built.nextserver
181
+ assert built.use_nextserver
182
+ assert_equal @filename, built.bootfile
183
+ assert built.use_bootfile
159
184
  end
160
185
 
161
186
  def test_add_record_with_collision
162
187
  @crud.expects(:build_host).with(:ip => @ip, :mac => @mac, :hostname => @hostname).returns(@host)
163
188
  @host.expects(:post).raises(Infoblox::Error.new("IB.Data.Conflict"))
164
- @crud.expects(:find_host).with('ipv4addr' => @ip).returns(
165
- ::Infoblox::Fixedaddress.new(:name => @hostname, :ipv4addr => @ip, :mac => '11:22:33:44:55:66'))
189
+ @crud.expects(:find_hosts).with('ipv4addr' => @ip).returns(
190
+ [::Infoblox::Fixedaddress.new(:name => @hostname, :ipv4addr => @ip, :mac => '11:22:33:44:55:66')])
166
191
 
167
192
  assert_raises(Proxy::DHCP::Collision) { @crud.add_record(:ip => @ip, :mac => @mac, :hostname => @hostname) }
168
193
  end
@@ -52,10 +52,19 @@ class InfobloxProviderTest < Test::Unit::TestCase
52
52
  @crud.stubs(:del_record)
53
53
  @provider.stubs(:full_network_address)
54
54
  @restart_grid.expects(:try_restart)
55
- @provider.del_record(@subnet,
56
- ::Proxy::DHCP::Record.new(:name => 'test',
57
- :ip => '192.168.42.1',
58
- :mac => '00:01:02:03:04:05',
59
- :subnet => ::Proxy::DHCP::Subnet.new('192.168.42.0', '255.255.255.0')))
55
+ @provider.del_record(::Proxy::DHCP::Record.new('192.168.42.1', '00:01:02:03:04:05',
56
+ ::Proxy::DHCP::Subnet.new('192.168.42.0', '255.255.255.0')))
57
+ end
58
+
59
+ def test_del_record_by_mac_restarts_grid
60
+ @crud.stubs(:del_record_by_mac)
61
+ @restart_grid.expects(:try_restart)
62
+ @provider.del_record_by_mac(@subnet.network, '00:01:02:03:04:05')
63
+ end
64
+
65
+ def test_del_record_by_ip_restarts_grid
66
+ @crud.stubs(:del_records_by_ip)
67
+ @restart_grid.expects(:try_restart)
68
+ @provider.del_records_by_ip(@subnet.network, '192.168.42.1')
60
69
  end
61
70
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smart_proxy_dhcp_infoblox
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Klaas Demter
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-12 00:00:00.000000000 Z
11
+ date: 2017-06-12 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Infoblox DHCP provider plugin for Foreman's smart proxy
14
14
  email:
@@ -43,7 +43,7 @@ files:
43
43
  - test/unused_ip_test.rb
44
44
  homepage: https://github.com/theforeman/smart_proxy_dhcp_infoblox
45
45
  licenses:
46
- - GPLv3
46
+ - GPL-3.0
47
47
  metadata: {}
48
48
  post_install_message:
49
49
  rdoc_options: []
@@ -61,15 +61,15 @@ required_rubygems_version: !ruby/object:Gem::Requirement
61
61
  version: '0'
62
62
  requirements: []
63
63
  rubyforge_project:
64
- rubygems_version: 2.2.2
64
+ rubygems_version: 2.6.12
65
65
  signing_key:
66
66
  specification_version: 4
67
67
  summary: Infoblox DHCP provider plugin for Foreman's smart proxy
68
68
  test_files:
69
- - test/host_and_fixedaddress_crud_test.rb
70
- - test/infoblox_provider_test.rb
71
69
  - test/plugin_configuration_test.rb
70
+ - test/unused_ip_test.rb
71
+ - test/host_and_fixedaddress_crud_test.rb
72
72
  - test/record_type_validator_test.rb
73
- - test/regex_generator_test.rb
74
73
  - test/test_helper.rb
75
- - test/unused_ip_test.rb
74
+ - test/infoblox_provider_test.rb
75
+ - test/regex_generator_test.rb