Skip to content

Commit a36d664

Browse files
committed
T6948: kea: Add helpers to interact with runtime
- `kea_add_lease` to add a lease to the running kea server - `kea_get_domain_from_subnet_id` to get the domain name from subnet id
1 parent 726dcee commit a36d664

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-1
lines changed

python/vyos/kea.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2023-2024 VyOS maintainers and contributors <maintainers@vyos.io>
1+
# Copyright 2023-2025 VyOS maintainers and contributors <maintainers@vyos.io>
22
#
33
# This library is free software; you can redistribute it and/or
44
# modify it under the terms of the GNU Lesser General Public
@@ -329,6 +329,32 @@ def kea_get_leases(inet):
329329

330330
return leases['arguments']['leases']
331331

332+
def kea_add_lease(inet, ip_address, host_name=None, mac_address=None, iaid=None, duid=None, subnet_id=None):
333+
args = {'ip-address': ip_address}
334+
335+
if host_name:
336+
args['hostname'] = host_name
337+
338+
if subnet_id:
339+
args['subnet-id'] = subnet_id
340+
341+
# IPv4 requires MAC address, IPv6 requires either MAC address or DUID
342+
if mac_address:
343+
args['hw-address'] = mac_address
344+
if duid:
345+
args['duid'] = duid
346+
347+
# IPv6 requires IAID
348+
if inet == '6' and iaid:
349+
args['iaid'] = iaid
350+
351+
result = _ctrl_socket_command(inet, f'lease{inet}-add', args)
352+
353+
if result and 'result' in result:
354+
return result['result'] == 0
355+
356+
return False
357+
332358
def kea_delete_lease(inet, ip_address):
333359
args = {'ip-address': ip_address}
334360

@@ -362,3 +388,26 @@ def kea_get_pool_from_subnet_id(config, inet, subnet_id):
362388
return network['name']
363389

364390
return None
391+
392+
def kea_get_domain_from_subnet_id(config, inet, subnet_id):
393+
shared_networks = dict_search_args(config, 'arguments', f'Dhcp{inet}', 'shared-networks')
394+
395+
if not shared_networks:
396+
return None
397+
398+
for network in shared_networks:
399+
if f'subnet{inet}' not in network:
400+
continue
401+
402+
for subnet in network[f'subnet{inet}']:
403+
if 'id' in subnet and int(subnet['id']) == int(subnet_id):
404+
for option in subnet['option-data']:
405+
if option['name'] == 'domain-name':
406+
return option['data']
407+
408+
# domain-name is not found in subnet, fallback to shared-network pool option
409+
for option in network['option-data']:
410+
if option['name'] == 'domain-name':
411+
return option['data']
412+
413+
return None

0 commit comments

Comments
 (0)