From 45fabbab6a4107df8a32f09045cf955afeb2e4ac Mon Sep 17 00:00:00 2001 From: Dan Radez Date: Mon, 20 Jun 2016 05:56:34 -0400 Subject: Syntax updates and new tests - syntax updates to match pep8 standards - tests to cover apex_python_utils.py Change-Id: Ifac06fdbb97266f1b574b20610979b6965d6dd55 Signed-off-by: Dan Radez --- lib/python/apex/ip_utils.py | 41 ++++++++++------------------------------- 1 file changed, 10 insertions(+), 31 deletions(-) (limited to 'lib/python/apex/ip_utils.py') diff --git a/lib/python/apex/ip_utils.py b/lib/python/apex/ip_utils.py index f51f227a..b039e26b 100644 --- a/lib/python/apex/ip_utils.py +++ b/lib/python/apex/ip_utils.py @@ -34,11 +34,11 @@ def get_ip_range(start_offset=None, count=None, end_offset=None, space, and end_offset will be calculated from the interface IP. 2 of start_offset, end_offset and count options must be provided: - - If start_offset and end_offset are provided, a range from start_offset - to end_offset will be returned. - - If count is provided, a range from either start_offset to (start_offset - +count) or (end_offset-count) to end_offset will be returned. The - IP range returned will be of size . + - If start_offset and end_offset are provided, a range from + start_offset to end_offset will be returned. + - If count is provided, a range from either start_offset to + (start_offset+count) or (end_offset-count) to end_offset will be + returned. The IP range returned will be of size . Both start_offset and end_offset must be greater than 0. Returns IP range in the format of "first_addr,second_addr" or exception @@ -47,7 +47,7 @@ def get_ip_range(start_offset=None, count=None, end_offset=None, if cidr: if count and start_offset and not end_offset: start_index = start_offset - end_index = start_offset + count -1 + end_index = start_offset + count - 1 elif count and end_offset and not start_offset: end_index = -1 - end_offset start_index = -1 - end_index - count + 1 @@ -81,7 +81,7 @@ def get_ip_range(start_offset=None, count=None, end_offset=None, else: if count and start_offset and not end_offset: start_ip = network[start_offset] - end_ip = start_ip + count -1 + end_ip = start_ip + count - 1 elif count and end_offset and not start_offset: end_ip = interface.ip - end_offset start_ip = end_ip - count + 1 @@ -135,27 +135,6 @@ def get_ip(offset, cidr=None, interface=None): return str(ip) -def generate_ip_range(args): - """ - Generate IP range in string format for given CIDR. - This function works for both IPv4 and IPv6. - - args is expected to contain the following members: - CIDR: any valid CIDR representation. - start_position: starting index, default to first address in subnet (1) - end_position: ending index, default to last address in subnet (-1) - - Returns IP range in string format. A single IP is returned if start and - end IPs are identical. - """ - cidr = ipaddress.ip_network(args.CIDR) - (start_index, end_index) = (args.start_position, args.end_position) - if cidr[start_index] == cidr[end_index]: - return str(cidr[start_index]) - else: - return ','.join(sorted([str(cidr[start_index]), str(cidr[end_index])])) - - def get_interface(nic, address_family=4): """ Returns interface object for a given NIC name in the system @@ -183,7 +162,7 @@ def get_interface(nic, address_family=4): return ipaddress.ip_interface(match.group()) else: logging.info("interface ip not found! ip address output:\n{}" - .format(output)) + .format(output)) return None @@ -228,8 +207,8 @@ def _validate_ip_range(start_ip, end_ip, cidr): """ ip_range = "{},{}".format(start_ip, end_ip) if end_ip <= start_ip: - logging.warning("IP range {} is invalid: end_ip should be greater than " - "starting ip".format(ip_range)) + logging.warning("IP range {} is invalid: end_ip should be greater " + "than starting ip".format(ip_range)) return False if start_ip not in ipaddress.ip_network(cidr): logging.warning('start_ip {} is not in network {}' -- cgit 1.2.3-korg