Add IP address comprehension to reclass.storage.node repeat function
Issue: PROD-21241
Change-Id: I2dc2e58def568ca353e1fcb9fc93358df4bfd3bb
diff --git a/_modules/netutils.py b/_modules/netutils.py
new file mode 100644
index 0000000..2efd7e6
--- /dev/null
+++ b/_modules/netutils.py
@@ -0,0 +1,25 @@
+from netaddr import iter_iprange
+
+__virtualname__ = 'netutils'
+
+
+def __virtual__():
+ return __virtualname__
+
+
+def parse_network_ranges(ranges, needed):
+ '''
+ Takes comma seprated list of IP ranges and returns full list of IP addresses in these ranges.
+ Second argument is used to check if there is enough IP addresses to cover the required number of nodes.
+
+ >>> parse_network_ranges("192.168.1.101-192.168.1.103,192.168.2.101-192.168.2.103")
+ ["192.168.1.101", "192.168.1.102", "192.168.1.103", "192.168.2.101", "192.168.2.102", "192.168.2.103"]
+ '''
+ range_list = ranges.split(',')
+ ip_obj_list = []
+ for _range in ranges.split(','):
+ ip_obj_list += list(iter_iprange(*_range.split('-')))
+ ip_list = [str(ip) for ip in ip_obj_list]
+ if len(ip_list) < needed:
+ raise ValueError('There is not enough IP addresses in ranges: "{}". {} available, {} required.'.format(ranges, len(ip_list), needed))
+ return ip_list