Skip to content

Commit

Permalink
Merge fe4fe20 into 46525df
Browse files Browse the repository at this point in the history
  • Loading branch information
phil-dileo committed Sep 16, 2015
2 parents 46525df + fe4fe20 commit 8cacbb4
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
22 changes: 22 additions & 0 deletions pyeapi/api/bgp.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def get(self):
response = dict()
response.update(self._parse_bgp_as(config))
response.update(self._parse_router_id(config))
response.update(self._parse_max_paths(config))
response.update(self._parse_shutdown(config))
response.update(self._parse_networks(config))

Expand All @@ -85,6 +86,12 @@ def _parse_router_id(self, config):
value = match.group(1) if match else None
return dict(router_id=value)

def _parse_max_paths(self, config):
match = re.search(r'maximum-paths\s+(\d+)\s+ecmp\s+(\d+)', config)
paths = int(match.group(1)) if match else None
ecmp_paths = int(match.group(2)) if match else None
return dict(maximum_paths=paths, maximum_ecmp_paths=ecmp_paths)

def _parse_shutdown(self, config):
value = 'no shutdown' in config
return dict(shutdown=not value)
Expand Down Expand Up @@ -129,6 +136,21 @@ def set_router_id(self, value=None, default=False):
cmd = self.command_builder('router-id', value=value, default=default)
return self.configure_bgp(cmd)

def set_maximum_paths(self, max_path=None, max_ecmp_path=None, default=False):
# You cannot configure max_ecmp_path without max_paths
if not max_path and max_ecmp_path:
raise TypeError('Cannot use maximum_ecmp_paths without'
'providing max_path')
if default:
cmd = 'default maximum-paths'
elif max_path:
cmd = 'maximum-paths {}'.format(max_path)
if max_ecmp_path:
cmd += ' ecmp {}'.format(max_ecmp_path)
else:
cmd = 'no maximum-paths'
return self.configure_bgp(cmd)

def set_shutdown(self, value=None, default=False):
cmd = self.command_builder('shutdown', value=value, default=default)
return self.configure_bgp(cmd)
Expand Down
34 changes: 31 additions & 3 deletions test/unit/test_api_bgp.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ def __init__(self, *args, **kwargs):

def test_get(self):
result = self.instance.get()
keys = ['bgp_as', 'router_id', 'shutdown', 'neighbors', 'networks']
keys = ['bgp_as', 'router_id', 'maximum_paths', 'maximum_ecmp_paths',
'shutdown', 'neighbors', 'networks']
self.assertEqual(sorted(keys), sorted(result.keys()))

def test_create(self):
Expand Down Expand Up @@ -97,6 +98,35 @@ def test_set_router_id(self):
func = function('set_router_id', rid, True)
self.eapi_positive_config_test(func, cmds)

def test_maximum_paths_just_max_path(self):
for state in ['config', 'negate', 'default']:
max_paths = 20
if state == 'config':
cmds = ['router bgp 65000', 'maximum-paths 20']
func = function('set_maximum_paths', max_paths)
elif state == 'negate':
cmds = ['router bgp 65000', 'no maximum-paths']
func = function('set_maximum_paths')
elif state == 'default':
cmds = ['router bgp 65000', 'default maximum-paths']
func = function('set_maximum_paths', default=True)
self.eapi_positive_config_test(func, cmds)

def test_maximum_paths_max_path_and_ecmp(self):
for state in ['config', 'negate', 'default']:
max_paths = 20
max_ecmp_path = 20
if state == 'config':
cmds = ['router bgp 65000', 'maximum-paths 20 ecmp 20']
func = function('set_maximum_paths', max_paths, max_ecmp_path)
elif state == 'negate':
cmds = ['router bgp 65000', 'no maximum-paths']
func = function('set_maximum_paths')
elif state == 'default':
cmds = ['router bgp 65000', 'default maximum-paths']
func = function('set_maximum_paths', default=True)
self.eapi_positive_config_test(func, cmds)

def test_set_shutdown(self):
for state in ['config', 'negate', 'default']:
if state == 'config':
Expand Down Expand Up @@ -262,5 +292,3 @@ def test_set_description(self):

if __name__ == '__main__':
unittest.main()


0 comments on commit 8cacbb4

Please sign in to comment.