fix network.iface module related stack traces

This commit is contained in:
Justin Findlay 2015-03-23 20:55:00 -06:00
parent d3e14e51b0
commit a16039c5f1

View File

@ -718,25 +718,56 @@ def _ipv4_to_bits(ipaddr):
return ''.join([bin(int(x))[2:].rjust(8, '0') for x in ipaddr.split('.')])
def _get_iface_info(iface):
'''
If `iface` is available, return interface info and no error, otherwise
return no info and log and return an error
'''
iface_info = interfaces()
if iface in iface_info.keys():
return iface_info, False
else:
error_msg = ('Interface "{0}" not in available interfaces: "{1}"'
''.format(iface, '", "'.join(iface_info.keys())))
log.error(error_msg)
return None, error_msg
def hw_addr(iface):
'''
Return the hardware address (a.k.a. MAC address) for a given interface
'''
return interfaces().get(iface, {}).get('hwaddr', '')
iface_info, error = _get_iface_info(iface)
if error is False:
return iface_info.get(iface, {}).get('hwaddr', '')
else:
return error
def interface(iface):
'''
Return the the interface details
Return the details of `iface` or an error if it does not exist
'''
return interfaces().get(iface, {}).get('inet', '')
iface_info, error = _get_iface_info(iface)
if error is False:
return iface_info.get(iface, {}).get('inet', '')
else:
return error
def interface_ip(iface):
'''
Return the the interface details
Return `iface` IPv4 addr or an error if `iface` does not exist
'''
return interfaces().get(iface, {}).get('inet', {})[0].get('address', {})
iface_info, error = _get_iface_info(iface)
if error is False:
return iface_info.get(iface, {}).get('inet', {})[0].get('address', '')
else:
return error
def subnets():