Skip to content

Commit

Permalink
CA comments addressed
Browse files Browse the repository at this point in the history
  • Loading branch information
itdependsnetworks committed Aug 1, 2023
1 parent dd1ef96 commit f71f678
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 36 deletions.
66 changes: 31 additions & 35 deletions netutils/ip.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,11 @@ def is_ip(ip: str) -> bool:
def is_ip_range(ip_range: str) -> bool:
"""Verifies whether or not a string is a valid IP address range.
An `ip_range` is in the format of `{ip_start}-{ip_end}`, IPs in str format, same IP version, and
ip_start is before ip_end.
Args:
ip_range: An IP address range in string format that is properly formatter.
ip_range: An IP address range in string format that is properly formatted.
Returns:
The result as to whether or not the string is a valid IP address.
Expand Down Expand Up @@ -270,31 +273,6 @@ def is_ip_range(ip_range: str) -> bool:
return True


def get_range_ips(ip_range: str) -> t.Tuple[IPAddress, IPAddress]:
"""Get's the two IPs as a tuple of IPAddress objects.
Args:
ip_range: An IP address range in string format that is properly formatter.
Returns:
The start and end IP address of the range provided.
Examples:
>>> from netutils.ip import get_range_ips
>>> get_range_ips("10.100.100.255-10.100.101.1")
(IPv4Address('10.100.100.255'), IPv4Address('10.100.101.1'))
>>> get_range_ips("2001::1-2001::10")
(IPv6Address('2001::1'), IPv6Address('2001::10'))
>>>
"""
if not is_ip_range(ip_range):
raise ValueError("Not a valid IP range format of $start_ip-$end_ip")
start_ip, end_ip = ip_range.split("-")
start_ip_obj = ipaddress.ip_address(start_ip)
end_ip_obj = ipaddress.ip_address(end_ip)
return start_ip_obj, end_ip_obj


def is_ip_within(ip: str, ip_compare: t.Union[str, t.List[str]]) -> bool:
"""
Check if an IP address, IP subnet, or IP range is within the range of a list of IP addresses, IP subnets, and IP ranges.
Expand Down Expand Up @@ -331,15 +309,8 @@ def _convert_ip(ip: str) -> str:
return f"{ip}/{mask}"
return ip

def _convert_range(ip: str) -> t.Tuple[IPAddress, IPAddress]:
start_ip, end_ip = ip.split("-")
start_ip_obj = ipaddress.ip_address(start_ip.strip())
end_ip_obj = ipaddress.ip_address(end_ip.strip())
assert type(start_ip_obj) == type(end_ip_obj) # nosec # pylint: disable=unidiomatic-typecheck
return start_ip_obj, end_ip_obj

if "-" in ip:
ip_obj_start, ip_obj_end = _convert_range(ip)
ip_obj_start, ip_obj_end = get_range_ips(ip)
else:
ip_obj = ipaddress.ip_network(_convert_ip(ip))
ip_obj_start = ip_obj[0]
Expand All @@ -350,7 +321,7 @@ def _convert_range(ip: str) -> t.Tuple[IPAddress, IPAddress]:

for item in ip_compare:
if "-" in item:
item_obj_start, item_obj_end = _convert_range(item)
item_obj_start, item_obj_end = get_range_ips(item)

else:
item_obj = ipaddress.ip_network(_convert_ip(item))
Expand Down Expand Up @@ -574,6 +545,31 @@ def get_peer_ip(ip_interface: str) -> str:
return val[0]


def get_range_ips(ip_range: str) -> t.Tuple[IPAddress, IPAddress]:
"""Get's the two IPs as a tuple of IPAddress objects.
Args:
ip_range: An IP address range in string format that is properly formatted.
Returns:
The start and end IP address of the range provided.
Examples:
>>> from netutils.ip import get_range_ips
>>> get_range_ips("10.100.100.255-10.100.101.1")
(IPv4Address('10.100.100.255'), IPv4Address('10.100.101.1'))
>>> get_range_ips("2001::1-2001::10")
(IPv6Address('2001::1'), IPv6Address('2001::10'))
>>>
"""
if not is_ip_range(ip_range):
raise ValueError(r"Not a valid IP range format of `{start_ip}-{end_ip}`")
start_ip, end_ip = ip_range.split("-")
start_ip_obj = ipaddress.ip_address(start_ip)
end_ip_obj = ipaddress.ip_address(end_ip)
return start_ip_obj, end_ip_obj


def get_usable_range(ip_network: str) -> str:
"""Given a network, return the string of usable IP addresses.
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_ip.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ def test_is_ip_within(data):


def test_is_ip_within_fail():
with pytest.raises(AssertionError):
with pytest.raises(ValueError):
data = {"ip": "10.1.100.100", "ip_compare": "10.1.100.10-2001::1"}
ip.is_ip_within(**data)

Expand Down

0 comments on commit f71f678

Please sign in to comment.