Browse Source

Use sorted's key to sort subnets by version

pull/1070/head
Viranch Mehta 2 years ago
parent
commit
a728a7ca4c
No known key found for this signature in database GPG Key ID: D83D1392AE9F93B4
1 changed files with 7 additions and 12 deletions
  1. +7
    -12
      octodns/record/dynamic.py

+ 7
- 12
octodns/record/dynamic.py View File

@ -293,20 +293,15 @@ class _DynamicMixin(object):
# process only valid ones and skip invalid ones # process only valid ones and skip invalid ones
pass pass
# subnets of type IPv4 and IPv6 can't be sorted together
# separately sort them and then combine them
networks_by_type = defaultdict(list)
for network in networks:
networks_by_type[network.__class__].append(network)
sorted_networks = []
for _, networks_of_type in networks_by_type.items():
sorted_networks.extend(sorted(networks_of_type))
# sort subnets from largest to smallest so that we can
# detect rule that have needlessly targeted a more specific # detect rule that have needlessly targeted a more specific
# subnet along with a larger subnet that already contains it # subnet along with a larger subnet that already contains it
sorted_networks = sorted(
networks, key=lambda n: (n.version, n)
)
for subnet in sorted_networks: for subnet in sorted_networks:
subnets_seen_of_type = subnets_seen[subnet.__class__]
for seen, where in subnets_seen_of_type.items():
subnets_seen_version = subnets_seen[subnet.version]
for seen, where in subnets_seen_version.items():
if subnet == seen: if subnet == seen:
reasons.append( reasons.append(
f'rule {rule_num} targets subnet {subnet} which has previously been seen in rule {where}' f'rule {rule_num} targets subnet {subnet} which has previously been seen in rule {where}'
@ -316,7 +311,7 @@ class _DynamicMixin(object):
f'rule {rule_num} targets subnet {subnet} which is more specific than the previously seen {seen} in rule {where}' f'rule {rule_num} targets subnet {subnet} which is more specific than the previously seen {seen} in rule {where}'
) )
subnets_seen_of_type[subnet] = rule_num
subnets_seen_version[subnet] = rule_num
if not isinstance(geos, (list, tuple)): if not isinstance(geos, (list, tuple)):
reasons.append(f'rule {rule_num} geos must be a list') reasons.append(f'rule {rule_num} geos must be a list')


Loading…
Cancel
Save