Skip to content

Commit

Permalink
add subnet id
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelkoetter committed Dec 5, 2022
1 parent 15c42ee commit 9f3b466
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
20 changes: 13 additions & 7 deletions kea-subnets.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import yaml
import jinja2
import dotenv
import zlib

dotenv.load_dotenv()

Expand Down Expand Up @@ -36,7 +37,7 @@ def main(url, token, parent_prefix, ip_range_role, config, template_path):
env.filters['ip'] = filter_ip

nb = pynetbox.api(url, token)
prefixes = nb.ipam.prefixes.filter(status='active', within=parent_prefix)
prefixes = nb.ipam.prefixes.filter(status='active', within_include=parent_prefix)
ip_ranges = list(nb.ipam.ip_ranges.filter(status='active', role=ip_range_role))
ip_addresses = list(nb.ipam.ip_addresses.filter(status='dhcp', parent=parent_prefix))
subnets = []
Expand All @@ -51,12 +52,17 @@ def main(url, token, parent_prefix, ip_range_role, config, template_path):
for ip_address in filter(lambda a : netaddr.IPNetwork(a.address).ip in _prefix, ip_addresses):
reservations.append(ip_address)

subnet_template = env.get_template('subnet.yaml.j2')
subnets.append(yaml.safe_load(subnet_template.render(
prefix=prefix,
pools=pools,
reservations=reservations
)))
if pools:
subnet_template = env.get_template('subnet.yaml.j2')
subnets.append(yaml.safe_load(subnet_template.render(
# The Kea subnet ID is a 32 bit unsigned int.
# We assume that CRC32 of the prefix is sufficiently unique.
id=zlib.crc32(bytes(_prefix.ip)) % (1<<32),

prefix=prefix,
pools=pools,
reservations=reservations
)))

if (config):
config_json = yaml.safe_load(open(config, 'r'))
Expand Down
2 changes: 2 additions & 0 deletions templates/subnet.yaml.j2
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{#
The following variables are available:
- id (Kea subnet ID, derived from prefix)
- prefix
- pools (list of IP Range)
- reservations (list of IP Address)
Expand Down Expand Up @@ -51,6 +52,7 @@
{%- endmacro %}

---
id: {{ id }}
subnet: "{{ prefix.prefix }}"
option-data: {{ option_data(prefix) }}
pools: [
Expand Down

0 comments on commit 9f3b466

Please sign in to comment.