Adding reservations with Kea API?

Started by krizzo, September 09, 2024, 08:28:46 PM

Previous topic - Next topic
I am trying to add a bunch of DHCP reservations in Kea and wanted to use the API. I've generated a key/secret for the user and can query the service status just fine at /api/kea/service/status

I tried multiple ways to post some json to the API for adding the reservations but with no luck. I keep getting a status 200 result failed message. The simple curl I've tried to add one reservation is below. What am I doing wrong?


curl -k --header "Content-Type: application/json" \
 --request POST \
 -d '{"description": "DEVICE", "hostname": "device-001", "hw_address": "01:23:45:67:89:ab", "subnet": "192.168.0.0/24", "ip_address": "192.168.0.10"}' \
 -u "key":"secret" \
 https://192.168.0.1/api/kea/dhcpv4/addReservation

Well, admittedly I never tried any API stuff with Kea, but 200 sounds like "it worked", not "failed".

Quote from: doktornotor on September 09, 2024, 08:58:00 PM
Well, admittedly I never tried any API stuff with Kea, but 200 sounds like "it worked", not "failed".

200 might just mean it received the request and the API didn't know how to process the data so it reported back that it failed to create the reservation. When I look in the UI I don't see the reservation that it should create from the API request. I'm wondering if the data structure is missing something.

Posting  the exact response instead of describing it would be awesome...

Hi,
I just needed this, and your question helped me to make it work.
You need to add a "reservation" entry to your json and you also need to use the subnet uuid. To get your subnet uuid, you can call the searchSubnet command.


curl -k --request GET \
-u "your_key":"your_secret" \
https://192.168.0.1/api/kea/dhcpv4/searchSubnet

{"rows":[{"uuid":"d4a3aa6d-e4e2-4b31-a54b-a9184f353ebc","subnet":"192.168.0.0\/24","next_server":"","option_data_autocollect":"1","option_data":"","pools":"","description":"My LAN"}],"rowCount":1,"total":1,"current":1}%   


And then to add your reservation:

curl -k --request POST \
--header "Content-Type: application/json"  \
-u "your_key":"your_secret" \
-d '{"reservation":{"subnet":"d4a3aa6d-e4e2-4b31-a54b-a9184f353ebc","ip_address":"192.168.0.10","hw_address":"01:23:45:67:89:ab","hostname":"device-001","description":"DEVICE"}}'  \
https://192.168.0.1/api/kea/dhcpv4/addReservation

{"result":"saved","uuid":"fe4312c4-1c4f-4ec2-943c-6ebd112c2433"}