88import yaml
99import jinja2
1010import dotenv
11+ import zlib
1112
1213dotenv .load_dotenv ()
1314
@@ -36,7 +37,7 @@ def main(url, token, parent_prefix, ip_range_role, config, template_path):
3637 env .filters ['ip' ] = filter_ip
3738
3839 nb = pynetbox .api (url , token )
39- prefixes = nb .ipam .prefixes .filter (status = 'active' , within = parent_prefix )
40+ prefixes = nb .ipam .prefixes .filter (status = 'active' , within_include = parent_prefix )
4041 ip_ranges = list (nb .ipam .ip_ranges .filter (status = 'active' , role = ip_range_role ))
4142 ip_addresses = list (nb .ipam .ip_addresses .filter (status = 'dhcp' , parent = parent_prefix ))
4243 subnets = []
@@ -51,12 +52,17 @@ def main(url, token, parent_prefix, ip_range_role, config, template_path):
5152 for ip_address in filter (lambda a : netaddr .IPNetwork (a .address ).ip in _prefix , ip_addresses ):
5253 reservations .append (ip_address )
5354
54- subnet_template = env .get_template ('subnet.yaml.j2' )
55- subnets .append (yaml .safe_load (subnet_template .render (
56- prefix = prefix ,
57- pools = pools ,
58- reservations = reservations
59- )))
55+ if pools :
56+ subnet_template = env .get_template ('subnet.yaml.j2' )
57+ subnets .append (yaml .safe_load (subnet_template .render (
58+ # The Kea subnet ID is a 32 bit unsigned int.
59+ # We assume that CRC32 of the prefix is sufficiently unique.
60+ id = zlib .crc32 (bytes (_prefix .ip )) % (1 << 32 ),
61+
62+ prefix = prefix ,
63+ pools = pools ,
64+ reservations = reservations
65+ )))
6066
6167 if (config ):
6268 config_json = yaml .safe_load (open (config , 'r' ))
0 commit comments