Skip to content

Commit

Permalink
Fixes #18395 - better error message with ms dhcp on win2008.
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitri-d authored and domcleal committed Feb 10, 2017
1 parent d9acf4e commit f6d461a
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion bundler.d/windows.rb
@@ -1,5 +1,5 @@
group :windows do
gem 'highline', :platforms => [:mingw, :x64_mingw]
gem 'win32-service', :platforms => [:mingw, :x64_mingw]
gem 'dhcpsapi', '~> 0.0'
gem 'dhcpsapi', '>= 0.0.11', '< 1.0.0'
end
2 changes: 2 additions & 0 deletions modules/dhcp/dhcp_api.rb
Expand Up @@ -34,6 +34,8 @@ class Proxy::DhcpApi < ::Sinatra::Base
{:ip => server.unused_ip(params[:network], params[:mac], params[:from], params[:to])}.to_json
rescue ::Proxy::DHCP::SubnetNotFound
log_halt 404, "Subnet #{params[:network]} could not found"
rescue ::Proxy::DHCP::NotImplemented => e
log_halt 501, e
rescue => e
log_halt 400, e
end
Expand Down
1 change: 1 addition & 0 deletions modules/dhcp_common/dhcp_common.rb
Expand Up @@ -27,6 +27,7 @@ class SubnetNotFound < RuntimeError; end
class Collision < RuntimeError; end
class InvalidRecord < RuntimeError; end
class AlreadyExists < RuntimeError; end
class NotImplemented < RuntimeError; end

def self.ipv4_to_i(ipv4_address)
ipv4_address.split('.', 4).inject(0) { |i, octet| (i << 8) | octet.to_i }
Expand Down
4 changes: 4 additions & 0 deletions modules/dhcp_native_ms/dhcp_native_ms_main.rb
Expand Up @@ -74,6 +74,10 @@ def unused_ip(subnet_address, mac_address, from_address, to_address)
client = dhcpsapi.get_client_by_mac_address(subnet_address, mac_address) rescue nil
return client[:client_ip_address] unless client.nil?

if dhcpsapi.api_level == DhcpsApi::Server::DHCPS_WIN2008_API || dhcpsapi.api_level == DhcpsApi::Server::DHCPS_NONE
raise Proxy::DHCP::NotImplemented.new("DhcpsApi::Server#get_free_ip_address is not available on Windows Server 2008 and earlier versions.")
end

return dhcpsapi.get_free_ip_address(subnet_address, from_address, to_address).first
end

Expand Down
6 changes: 6 additions & 0 deletions test/dhcp/dhcp_api_test.rb
Expand Up @@ -112,6 +112,12 @@ def test_get_unused_ip_for_nonexistent_network
assert_equal 404, last_response.status
end

def test_get_unused_when_not_inmplemented
@server.expects(:unused_ip).raises(::Proxy::DHCP::NotImplemented)
get "/192.168.122.0/unused_ip?mac=01:02:03:04:05:06&from=192.168.122.10&to=192.168.122.20"
assert_equal 501, last_response.status
end

def test_get_record
@server.expects(:find_record).with("192.168.122.0", "192.168.122.1").returns(@reservations.first)

Expand Down
1 change: 1 addition & 0 deletions test/dhcp_ms_native/server_ms_test.rb
Expand Up @@ -73,6 +73,7 @@ def test_should_return_all_leases
end

def test_should_return_free_ip_address
@dhcpsapi.expects(:api_level).twice.returns(DhcpsApi::Server::DHCPS_WIN2012_API)
@dhcpsapi.expects(:get_client_by_mac_address).raises(RuntimeError)
@dhcpsapi.expects(:get_free_ip_address).with(@network, nil, nil).returns(['192.168.42.20'])
assert_equal '192.168.42.20', @server.unused_ip(@network, '00:01:02:03:04:05', nil, nil)
Expand Down

0 comments on commit f6d461a

Please sign in to comment.