[stable-4] docker_network: fix IP subnet and address idempotency (#1203)

* Fix IP subnet and address idempotency. (#1201)

(cherry picked from commit 3da2799e03)

* Add warning about missing normalization if ipaddress is not there on Python 2.

* Fix mistake.
This commit is contained in:
Felix Fontein
2025-11-16 11:18:47 +01:00
committed by GitHub
parent 99a81449c5
commit 8f50319434
3 changed files with 61 additions and 4 deletions
+18
View File
@@ -447,3 +447,21 @@ def normalize_ip_address(ip_address):
# If we don't have ipaddress, simply give up...
# This mainly affects Python 2.7.
return ip_address
def normalize_ip_network(network):
"""
Given a network in CIDR notation as a string, normalize it so that it can be
used to compare networks as strings.
"""
if network is None:
return None
if HAS_IPADDRESS:
try:
return ipaddress.ip_network(network).compressed
except ValueError:
# Fallback for invalid networks: simply return the input
return network
# If we don't have ipaddress, simply give up...
# This mainly affects Python 2.7.
return network