Class: Proxy::DHCP::KeaApi::Provider
- Inherits:
-
Server
- Object
- Server
- Proxy::DHCP::KeaApi::Provider
- Defined in:
- lib/smart_proxy_dhcp_kea_api/dhcp_kea_api_main.rb
Overview
The main provider class for the dhcp_kea_api
module. This class inherits
from the Foreman Smart Proxy's core DHCP::Server
and implements the
Kea-specific logic for adding and deleting DHCP reservations.
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#subnet_service ⇒ Object
readonly
Returns the value of attribute subnet_service.
Instance Method Summary collapse
-
#add_record(options = {}) ⇒ Proxy::DHCP::Reservation
Creates a new DHCP reservation in Kea.
-
#del_record(record) ⇒ Proxy::DHCP::Reservation
Deletes a DHCP reservation from Kea.
-
#initialize(subnet_service, client, free_ips) ⇒ Provider
constructor
Initialises the Kea API provider.
Constructor Details
#initialize(subnet_service, client, free_ips) ⇒ Provider
Initialises the Kea API provider.
20 21 22 23 24 25 |
# File 'lib/smart_proxy_dhcp_kea_api/dhcp_kea_api_main.rb', line 20 def initialize(subnet_service, client, free_ips) @subnet_service = subnet_service @client = client subnet_service.load! super('localhost', nil, subnet_service, free_ips) end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
13 14 15 |
# File 'lib/smart_proxy_dhcp_kea_api/dhcp_kea_api_main.rb', line 13 def client @client end |
#subnet_service ⇒ Object (readonly)
Returns the value of attribute subnet_service.
13 14 15 |
# File 'lib/smart_proxy_dhcp_kea_api/dhcp_kea_api_main.rb', line 13 def subnet_service @subnet_service end |
Instance Method Details
#add_record(options = {}) ⇒ Proxy::DHCP::Reservation
Creates a new DHCP reservation in Kea.
This method orchestrates the process by first calling the parent class's
add_record
to perform initial validation and IP selection, then building
the Kea-specific payload and sending it via the API client.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/smart_proxy_dhcp_kea_api/dhcp_kea_api_main.rb', line 62 def add_record( = {}) logger.debug "DHCP options received from Foreman: #{.inspect}" record = super reservation_args = build_base_reservation_args(record) (reservation_args, ) option_data = build_option_data() reservation_args['option-data'] = option_data unless option_data.empty? @client.post_command('dhcp4', 'reservation-add', { reservation: reservation_args }) subnet_service.add_host(record.subnet.network, record) logger.info "Successfully added reservation for MAC #{record.mac} and IP #{record.ip}" record end |
#del_record(record) ⇒ Proxy::DHCP::Reservation
Deletes a DHCP reservation from Kea.
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/smart_proxy_dhcp_kea_api/dhcp_kea_api_main.rb', line 99 def del_record(record) logger.debug "Deleting record; #{record.inspect}" unless record.is_a?(::Proxy::DHCP::Reservation) logger.warn "Attempted to delete a record for MAC '#{record.mac}' but it is not a Reservation (actual type: #{record.class.name}). No action taken." return record end subnet_id = @subnet_service.kea_id_map[record.subnet.network] raise Proxy::DHCP::Error, "Unable to find Kea subnet-id for network #{record.subnet.network}" unless subnet_id # Construct the arguments for the 'reservation-del' command using the identifier triplet. args = { 'subnet-id': subnet_id, 'identifier-type': 'hw-address', 'identifier' => record.mac } @client.post_command('dhcp4', 'reservation-del', args) subnet_service.delete_host(record) logger.info "Successfully deleted reservation for MAC #{record.mac} and IP #{record.ip}" record end |