Skip to content

Commit 10c9551

Browse files
authored
Refactoring Ip.py to return proper boolean values (#1043)
* added bool to netaddr instances, removed redundant imports * created test file for ip.py * refactoring ip.py
1 parent 75fc06b commit 10c9551

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

nettacker/core/ip.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import netaddr
44
import requests
5-
from netaddr import iprange_to_cidrs, IPNetwork
65

76

87
def generate_ip_range(ip_range):
@@ -16,11 +15,11 @@ def generate_ip_range(ip_range):
1615
an array with CIDRs
1716
"""
1817
if "/" in ip_range:
19-
return [ip.format() for ip in [cidr for cidr in IPNetwork(ip_range)]]
18+
return [ip.format() for ip in [cidr for cidr in netaddr.IPNetwork(ip_range)]]
2019
else:
2120
ips = []
2221
for generator_ip_range in [
23-
cidr.iter_hosts() for cidr in iprange_to_cidrs(*ip_range.rsplit("-"))
22+
cidr.iter_hosts() for cidr in netaddr.iprange_to_cidrs(*ip_range.rsplit("-"))
2423
]:
2524
for ip in generator_ip_range:
2625
ips.append(ip.format())
@@ -68,7 +67,7 @@ def is_ipv4_range(ip_range):
6867
"/" in ip_range
6968
and "." in ip_range
7069
and "-" not in ip_range
71-
and netaddr.IPNetwork(ip_range)
70+
and bool(netaddr.IPNetwork(ip_range))
7271
)
7372
except Exception:
7473
return False
@@ -80,7 +79,7 @@ def is_ipv4_cidr(ip_range):
8079
"/" not in ip_range
8180
and "." in ip_range
8281
and "-" in ip_range
83-
and iprange_to_cidrs(*ip_range.split("-"))
82+
and bool(netaddr.iprange_to_cidrs(*ip_range.split("-")))
8483
)
8584
except Exception:
8685
return False
@@ -96,7 +95,7 @@ def is_single_ipv6(ip):
9695
Returns:
9796
True if it's IPv6 otherwise False
9897
"""
99-
return netaddr.valid_ipv6(str(ip))
98+
return netaddr.valid_ipv6(ip)
10099

101100

102101
def is_ipv6_range(ip_range):
@@ -105,7 +104,7 @@ def is_ipv6_range(ip_range):
105104
"/" not in ip_range
106105
and ":" in ip_range
107106
and "-" in ip_range
108-
and iprange_to_cidrs(*ip_range.split("-"))
107+
and bool(netaddr.iprange_to_cidrs(*ip_range.split("-")))
109108
)
110109
except Exception:
111110
return False
@@ -117,7 +116,7 @@ def is_ipv6_cidr(ip_range):
117116
"/" in ip_range
118117
and ":" in ip_range
119118
and "-" not in ip_range
120-
and netaddr.IPNetwork(ip_range)
119+
and bool(netaddr.IPNetwork(ip_range))
121120
)
122121
except Exception:
123122
return False

0 commit comments

Comments
 (0)