|
| 1 | +require 'net/http' |
| 2 | +require 'uri' |
| 3 | +require 'json' |
| 4 | + |
| 5 | +module Proxy::DHCP::Kea |
| 6 | + class KeaApiClient |
| 7 | + include Proxy::Log |
| 8 | + |
| 9 | + attr_reader :api_url, :username, :password, :verify_ssl |
| 10 | + |
| 11 | + def initialize(api_url, username = nil, password = nil, verify_ssl: true) |
| 12 | + @api_url = api_url.chomp('/') |
| 13 | + @username = username |
| 14 | + @password = password |
| 15 | + @verify_ssl = verify_ssl |
| 16 | + end |
| 17 | + |
| 18 | + def send_command(service, command, arguments = {}) |
| 19 | + payload = { |
| 20 | + 'command' => command, |
| 21 | + 'service' => [service], |
| 22 | + 'arguments' => arguments, |
| 23 | + } |
| 24 | + |
| 25 | + logger.debug "Sending KEA command: #{command} to service: #{service}" |
| 26 | + logger.debug "Arguments: #{arguments.inspect}" |
| 27 | + |
| 28 | + response = http_post('/', payload) |
| 29 | + |
| 30 | + result = response.first |
| 31 | + |
| 32 | + if result['result'] != 0 |
| 33 | + error_msg = "KEA command '#{command}' failed: #{result['text']}" |
| 34 | + logger.error error_msg |
| 35 | + raise error_msg |
| 36 | + end |
| 37 | + |
| 38 | + logger.debug "KEA command successful: #{result['text']}" |
| 39 | + result['arguments'] || {} |
| 40 | + end |
| 41 | + |
| 42 | + def config |
| 43 | + send_command('dhcp4', 'config-get') |
| 44 | + end |
| 45 | + |
| 46 | + def list_subnets |
| 47 | + conf = config |
| 48 | + conf.dig('Dhcp4', 'subnet4') || [] |
| 49 | + end |
| 50 | + |
| 51 | + def add_reservation(subnet_id, ip_address, hw_address, hostname = nil, options = {}) |
| 52 | + reservation = { |
| 53 | + 'subnet-id' => subnet_id.to_i, |
| 54 | + 'ip-address' => ip_address, |
| 55 | + 'hw-address' => hw_address, |
| 56 | + } |
| 57 | + |
| 58 | + reservation['hostname'] = hostname if hostname |
| 59 | + |
| 60 | + if options[:next_server] |
| 61 | + reservation['next-server'] = options[:next_server] |
| 62 | + end |
| 63 | + |
| 64 | + if options[:boot_file_name] |
| 65 | + reservation['boot-file-name'] = options[:boot_file_name] |
| 66 | + end |
| 67 | + |
| 68 | + logger.info "Adding KEA reservation: #{ip_address} for #{hw_address} in subnet #{subnet_id}" |
| 69 | + send_command('dhcp4', 'reservation-add', reservation) |
| 70 | + end |
| 71 | + |
| 72 | + def delete_reservation_by_ip(subnet_id, ip_address) |
| 73 | + logger.info "Deleting KEA reservation: #{ip_address} from subnet #{subnet_id}" |
| 74 | + send_command('dhcp4', 'reservation-del', { |
| 75 | + 'subnet-id' => subnet_id.to_i, |
| 76 | + 'ip-address' => ip_address, |
| 77 | + }) |
| 78 | + end |
| 79 | + |
| 80 | + def reservation_by_ip(subnet_id, ip_address) |
| 81 | + send_command('dhcp4', 'reservation-get', { |
| 82 | + 'subnet-id' => subnet_id.to_i, |
| 83 | + 'ip-address' => ip_address, |
| 84 | + }) |
| 85 | + rescue => e |
| 86 | + logger.debug "Reservation not found for #{ip_address}: #{e.message}" |
| 87 | + nil |
| 88 | + end |
| 89 | + |
| 90 | + def list_leases |
| 91 | + send_command('dhcp4', 'lease4-get-all') |
| 92 | + end |
| 93 | + |
| 94 | + def lease_by_ip(ip_address) |
| 95 | + result = send_command('dhcp4', 'lease4-get', { |
| 96 | + 'ip-address' => ip_address, |
| 97 | + }) |
| 98 | + result['leases']&.first |
| 99 | + rescue => e |
| 100 | + logger.debug "Lease not found for #{ip_address}: #{e.message}" |
| 101 | + nil |
| 102 | + end |
| 103 | + |
| 104 | + private |
| 105 | + |
| 106 | + def http_post(path, payload) |
| 107 | + uri = URI.parse("#{@api_url}#{path}") |
| 108 | + |
| 109 | + http = Net::HTTP.new(uri.host, uri.port) |
| 110 | + http.use_ssl = (uri.scheme == 'https') |
| 111 | + http.verify_mode = @verify_ssl ? OpenSSL::SSL::VERIFY_PEER : OpenSSL::SSL::VERIFY_NONE |
| 112 | + |
| 113 | + request = Net::HTTP::Post.new(uri.path, {'Content-Type' => 'application/json'}) |
| 114 | + request.body = payload.to_json |
| 115 | + |
| 116 | + if @username && @password |
| 117 | + request.basic_auth(@username, @password) |
| 118 | + end |
| 119 | + |
| 120 | + logger.debug "HTTP POST to #{uri}" |
| 121 | + response = http.request(request) |
| 122 | + |
| 123 | + unless response.is_a?(Net::HTTPSuccess) |
| 124 | + error_msg = "HTTP request failed: #{response.code} #{response.message}" |
| 125 | + logger.error error_msg |
| 126 | + raise error_msg |
| 127 | + end |
| 128 | + |
| 129 | + JSON.parse(response.body) |
| 130 | + rescue JSON::ParserError => e |
| 131 | + error_msg = "Failed to parse KEA response: #{e.message}" |
| 132 | + logger.error error_msg |
| 133 | + raise error_msg |
| 134 | + end |
| 135 | + end |
| 136 | +end |
0 commit comments