Skip to content

Commit

Permalink
Add support for description
Browse files Browse the repository at this point in the history
  • Loading branch information
phil-dileo committed Oct 8, 2015
1 parent 638e30c commit dbfba32
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 7 deletions.
42 changes: 37 additions & 5 deletions pyeapi/api/routemaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,16 @@
name (string): The name given to the routemap clause
action (choices: permit/deny): How the clause will filter the route
seqno (integer): The sequence number of this clause
description (string): A description for the routemap clause
set (list): The list of set statements present in this clause
match (list): The list of match statements present in this clause.
continue (integer): The next sequence number to evaluate if the criteria
in this clause are met.
Notes:
The \'set\' and \'match\' attributes produce a list of strings with The
The set and match attributes produce a list of strings with The
corresponding configuration. These strings will omit the preceeding
\'set\' or \'match\' words, respectively.
set or match words, respectively.
"""

import re
Expand Down Expand Up @@ -91,14 +92,15 @@ def _get_instance(self, name, action, seqno):
resource.update(self._parse_match_statements(routemap))
resource.update(self._parse_set_statements(routemap))
resource.update(self._parse_continue_statement(routemap))
resource.update(self._parse_description(routemap))
return resource

def _parse_match_statements(self, config):
match_re = re.compile(r'^\s+match\s(.*)$', re.M)
match_re = re.compile(r'^\s+match\s(.+)$', re.M)
return dict(match=match_re.findall(config))

def _parse_set_statements(self, config):
set_re = re.compile(r'^\s+set\s(.*)$', re.M)
set_re = re.compile(r'^\s+set\s(.+)$', re.M)
return dict(set=set_re.findall(config))

def _parse_continue_statement(self, config):
Expand All @@ -107,6 +109,12 @@ def _parse_continue_statement(self, config):
value = int(match.group(1)) if match else None
return {'continue': value}

def _parse_description(self, config):
desc_re = re.compile(r'^\s+description\s\'(.+)\'$', re.M)
match = desc_re.search(config)
value = match.group(1) if match else None
return dict(description=value)

def create(self, name, action, seqno):
"""Creates a new routemap on the node
Expand Down Expand Up @@ -247,7 +255,7 @@ def set_set_statements(self, name, action, seqno, statements):
return self.configure(commands) if commands else True

def set_continue(self, name, action, seqno, value=None, default=False):
"""Configures the routemap \'continue\' value
"""Configures the routemap continue value
Args:
name (string): The full name of the routemap.
Expand All @@ -273,6 +281,30 @@ def set_continue(self, name, action, seqno, value=None, default=False):

return self.configure(commands)

def set_description(self, name, action, seqno, value=None, default=False):
"""Configures the routemap description
Args:
name (string): The full name of the routemap.
action (choices: permit,deny): The action to take for this routemap
clause.
seqno (integer): The sequence number for the routemap clause.
value (string): The value to configure for the routemap description
default (bool): Specifies to default the routemap continue value
Returns:
True if the operation succeeds otherwise False is returned
"""
commands = ['route-map %s %s %s' % (name, action, seqno)]
if default:
commands.append('default description')
elif value is not None:
commands.append('description %s' % value)
else:
commands.append('no description')

return self.configure(commands)

def instance(node):
"""Returns an instance of Routemaps
Expand Down
15 changes: 15 additions & 0 deletions test/system/test_api_routemaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
sys.path.append(os.path.join(os.path.dirname(__file__), '../lib'))

from systestlib import DutSystemTest
from testlib import random_string


class TestApiRoutemaps(DutSystemTest):
Expand Down Expand Up @@ -119,6 +120,20 @@ def test_default(self):
self.assertTrue(result)
self.assertIsNone(api.get('TEST', 'deny', 10))

def test_set_description(self):
for dut in self.duts:
text = random_string()
dut.config(['no route-map TEST deny 10',
'route-map TEST deny 10'])
api = dut.api('routemaps')
self.assertNotIn('description %s' % text,
api.get_block('route-map TEST deny 10'))
result = dut.api('routemaps').set_description('TEST', 'deny', 10,
text)
self.assertTrue(result)
self.assertIn('description %s' % text,
api.get_block('route-map TEST deny 10'))

def test_set_match_statements(self):
for dut in self.duts:
dut.config(['no route-map TEST deny 10',
Expand Down
10 changes: 8 additions & 2 deletions test/unit/test_api_routemaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

sys.path.append(os.path.join(os.path.dirname(__file__), '../lib'))

from testlib import get_fixture, function
from testlib import get_fixture, function, random_string
from testlib import EapiConfigUnitTest

import pyeapi.api.routemaps
Expand All @@ -53,7 +53,8 @@ def test_instance(self):

def test_get(self):
result = self.instance.get('TEST', 'permit', 10)
keys = ['name', 'action', 'seqno', 'set', 'match', 'continue']
keys = ['name', 'action', 'seqno', 'set', 'match', 'continue',
'description']
self.assertEqual(sorted(keys), sorted(result.keys()))

def test_get_not_configured(self):
Expand Down Expand Up @@ -109,6 +110,11 @@ def test_set_continue(self):
func = function('set_continue', 'TEST', 'permit', 10, 100)
self.eapi_positive_config_test(func, cmds)

def test_set_description_with_value(self):
value = random_string()
cmds = ['route-map TEST permit 10', 'description %s' % value]
func = function('set_description', 'TEST', 'permit', 10, value)
self.eapi_positive_config_test(func, cmds)

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

0 comments on commit dbfba32

Please sign in to comment.