Skip to content

Commit

Permalink
fixes #11
Browse files Browse the repository at this point in the history
This commit address an issue where an exception is raised if an ip interface
was not configured with an MTU value.  The config parser will now properly
parse the interface configuration and return the value of the mtu setting.
  • Loading branch information
Peter Sprygada committed May 3, 2015
1 parent d0fa10c commit 90eaae9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 15 deletions.
55 changes: 41 additions & 14 deletions pyeapi/api/ipinterfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,19 @@


SWITCHPORT_RE = re.compile(r'^\s{3}switchport$', re.M)
IPADDR_RE = re.compile(r'(?<=\s{3}ip\saddress\s)(?P<value>.+)$', re.M)
MTU_RE = re.compile(r'(?<=\s{3}mtu\s)(?P<value>\d+)$', re.M)


class Ipinterfaces(EntityCollection):

def get(self, name):
"""Returns the specific IP interface properties
Example
{
"name": <string>,
"address": <string>,
"mtu": <integer>
}
The Ipinterface resource returns the following:
* name (str): The name of the interface
* address (str): The IP address of the interface in the form
of A.B.C.D/E
* mtu (int): The configured value for IP MTU.
Args:
name (string): The interface identifier to retrieve the
Expand All @@ -81,14 +79,43 @@ def get(self, name):
if SWITCHPORT_RE.search(config, re.M):
return None

resp = dict(name=name)
resource = dict(name=name)
resource.update(self._parse_address(config))
resource.update(self._parse_mtu(config))
return resource

def _parse_address(self, config):
"""Parses the config block and returns the ip address value
The provided configuration block is scaned and the configured value
for the IP address is returned as a dict object. If the IP address
value is not configured, then None is returned for the value
Args:
config (str): The interface configuration block to parse
Return:
dict: A dict object intended to be merged into the resource dict
"""
match = re.search(r'ip address ([^\s]+)', config)
value = match.group(1) if match else None
return dict(address=value)

value = lambda x, y: x.group('value') if x else y
def _parse_mtu(self, config):
"""Parses the config block and returns the configured IP MTU value
resp['address'] = value(IPADDR_RE.search(config, re.M), '0.0.0.0')
resp['mtu'] = int(MTU_RE.search(config, re.M).group('value'))
The provided configuration block is scanned and the configured value
for the IP MTU is returned as a dict object. The IP MTU value is
expected to always be present in the provided config block
return resp
Args:
config (str): The interface configuration block to parse
Return:
dict: A dict object intended to be merged into the resource dict
"""
match = re.search(r'mtu (\d+)', config)
return dict(mtu=int(match.group(1)))

def getall(self):
""" Returns all of the IP interfaces found in the running-config
Expand Down
2 changes: 1 addition & 1 deletion test/system/test_api_ipinterfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def test_get_interface_wo_ip_adddress(self):
dut.config(['default interface %s' % intf, 'interface %s' % intf,
'no switchport'])
result = dut.api('ipinterfaces').get(intf)
self.assertEqual(result['address'], '0.0.0.0')
self.assertIsNone(result['address'])

def test_getall(self):
for dut in self.duts:
Expand Down

0 comments on commit 90eaae9

Please sign in to comment.