Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def testVal_IOSIntfLine_ip_network_object01():
"""Ensure we raise an error for intf.ip_network_object if the intf uses dhcp"""
lines = ['!',
'interface GigabitEthernet 1/1',
' ip address dhcp',
'!',
]
cfg = CiscoConfParse(lines, factory=True)
with pytest.raises(DynamicAddressException):
cfg.find_objects('^interface')[0].ip_network_object
def testVal_IOSIntfLine_ipv4_addr_object02():
"""Ensure we raise an error for intf.ipv4_addr_object if the intf uses dhcp"""
lines = ['!',
'interface GigabitEthernet 1/1',
' ip address dhcp',
'!',
]
cfg = CiscoConfParse(lines, factory=True)
with pytest.raises(DynamicAddressException):
cfg.find_objects('^interface')[0].ipv4_addr_object
def ipv4_addr(self):
"""Return a string with the interface's IPv4 address, or '' if there is none"""
retval = self.re_match_iter_typed(
r'^\s+ip\s+address\s+(\d+\.\d+\.\d+\.\d+)\s+\d+\.\d+\.\d+\.\d+\s*$',
result_type=str,
default='')
condition1 = self.re_match_iter_typed(
r'^\s+ip\s+address\s+(dhcp)\s*$',
result_type=str,
default='')
if condition1.lower()=='dhcp':
error = "Cannot parse address from a dhcp interface: {0}".format(
self.name)
raise DynamicAddressException(error)
else:
return retval
def __repr__(self):
if not self.is_switchport:
try:
ipv4_addr_object = self.ipv4_addr_object
except DynamicAddressException:
# Interface uses dhcp
ipv4_addr_object = None
if ipv4_addr_object is None:
addr = "IPv4 dhcp"
elif ipv4_addr_object == self.default_ipv4_addr_object:
addr = "No IPv4"
else:
ip = str(self.ipv4_addr_object.ip)
prefixlen = str(self.ipv4_addr_object.prefixlen)
addr = "{0}/{1}".format(ip, prefixlen)
return "<%s # %s '%s' info: '%s'>" % (self.classname, self.linenum,
self.name, addr)
else:
return "<%s # %s '%s' info: 'switchport'>" % (
self.classname, self.linenum, self.name)
def __init__(self, msg=""):
super(DynamicAddressException, self).__init__(msg)
self.msg = msg
def ipv4_addr_object(self):
"""Return a ccp_util.IPv4Obj object representing the address on this interface; if there is no address, return IPv4Obj('127.0.0.1/32')"""
try:
return IPv4Obj('%s/%s' % (self.ipv4_addr, self.ipv4_masklength))
except DynamicAddressException as e:
raise DynamicAddressException(e)
except:
return self.default_ipv4_addr_object
def __repr__(self):
if not self.is_switchport:
try:
ipv4_addr_object = self.ipv4_addr_object
except DynamicAddressException:
ipv4_addr_object = None
if ipv4_addr_object is None:
addr = "IPv4 dhcp"
elif ipv4_addr_object == self.default_ipv4_addr_object:
addr = "No IPv4"
else:
ip = str(self.ipv4_addr_object.ip)
prefixlen = str(self.ipv4_addr_object.prefixlen)
addr = "{0}/{1}".format(ip, prefixlen)
return "<%s # %s '%s' info: '%s'>" % (self.classname, self.linenum,
self.name, addr)
else:
return "<%s # %s '%s' info: 'switchport'>" % (
self.classname, self.linenum, self.name)
def ip_network_object(self):
# Simplified on 2014-12-02
try:
return IPv4Obj(
'{0}/{1}'.format(self.ipv4_addr, self.ipv4_netmask),
strict=False)
except DynamicAddressException as e:
raise DynamicAddressException(e)
except (Exception) as e:
return self.default_ipv4_addr_object
def ipv4_addr(self):
"""Return a string with the interface's IPv4 address, or '' if there is none"""
retval = self.re_match_iter_typed(
r'^\s+ip\s+address\s+(\d+\.\d+\.\d+\.\d+)\s*\/\d+\s*$',
result_type=str,
default='')
condition1 = self.re_match_iter_typed(
r'^\s+ip\s+address\s+(dhcp)\s*$',
result_type=str,
default='')
if condition1.lower()=='dhcp':
error = "Cannot parse address from a dhcp interface: {0}".format(
self.name)
raise DynamicAddressException(error)
else:
return retval
def ipv4_addr_object(self):
"""Return a ccp_util.IPv4Obj object representing the address on this interface; if there is no address, return IPv4Obj('127.0.0.1/32')"""
try:
return IPv4Obj('%s/%s' % (self.ipv4_addr, self.ipv4_netmask))
except DynamicAddressException as e:
raise DynamicAddressException(e)
except:
return self.default_ipv4_addr_object