8
8
import yaml
9
9
import jinja2
10
10
import dotenv
11
+ import zlib
11
12
12
13
dotenv .load_dotenv ()
13
14
@@ -36,7 +37,7 @@ def main(url, token, parent_prefix, ip_range_role, config, template_path):
36
37
env .filters ['ip' ] = filter_ip
37
38
38
39
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 )
40
41
ip_ranges = list (nb .ipam .ip_ranges .filter (status = 'active' , role = ip_range_role ))
41
42
ip_addresses = list (nb .ipam .ip_addresses .filter (status = 'dhcp' , parent = parent_prefix ))
42
43
subnets = []
@@ -51,12 +52,17 @@ def main(url, token, parent_prefix, ip_range_role, config, template_path):
51
52
for ip_address in filter (lambda a : netaddr .IPNetwork (a .address ).ip in _prefix , ip_addresses ):
52
53
reservations .append (ip_address )
53
54
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
+ )))
60
66
61
67
if (config ):
62
68
config_json = yaml .safe_load (open (config , 'r' ))
0 commit comments