Skip to content

Commit

Permalink
Simplify version checks, fix unittests for commands changed
Browse files Browse the repository at this point in the history
	modified:   pyeapi/api/abstract.py
	modified:   pyeapi/api/bgp.py
	modified:   pyeapi/api/interfaces.py
	modified:   pyeapi/api/ntp.py
	modified:   pyeapi/api/users.py
	modified:   pyeapi/api/vrfs.py
	modified:   pyeapi/api/vrrp.py
	modified:   pyeapi/client.py
	modified:   test/lib/testlib.py
	modified:   test/system/test_api_vrfs.py
	modified:   test/system/test_api_vrrp.py
	modified:   test/unit/test_api_vrrp.py
	modified:   test/unit/test_utils.py
  • Loading branch information
andriirr committed Jan 14, 2020
1 parent 9dfd1f7 commit 0f6ad04
Show file tree
Hide file tree
Showing 13 changed files with 94 additions and 104 deletions.
4 changes: 2 additions & 2 deletions pyeapi/api/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ def __init__(self, node):
self.node = node

@property
def version_id(self):
return self.node.version_id
def version_number(self):
return self.node.version_number

@property
def config(self):
Expand Down
6 changes: 3 additions & 3 deletions pyeapi/api/bgp.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ def getall(self):
return collection

def _parse_peer_group(self, config, name):
if self.version_id >= '4.23':
if self.version_number >= '4.23':
regexp = r'neighbor {} peer group ([^\s]+)'.format(name)
else:
regexp = r'neighbor {} peer-group ([^\s]+)'.format(name)
Expand Down Expand Up @@ -266,7 +266,7 @@ def create(self, name):
def delete(self, name):
response = self.configure('no neighbor {}'.format(name))
if not response:
if self.version_id >= '4.23':
if self.version_number >= '4.23':
response = self.configure('no neighbor {} '
'peer group'.format(name))
else:
Expand All @@ -288,7 +288,7 @@ def command_builder(self, name, cmd, value, default, disable):

def set_peer_group(self, name, value=None, default=False, disable=False):
if not self.ispeergroup(name):
if self.version_id >= '4.23':
if self.version_number >= '4.23':
cmd = self.command_builder(name, 'peer group', value, default,
disable)
else:
Expand Down
2 changes: 1 addition & 1 deletion pyeapi/api/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ def set_vrf(self, name, vrf, default=False, disable=False):
True if the operation succeeds otherwise False is returned
"""
commands = ['interface %s' % name]
if self.version_id >= '4.23':
if self.version_number >= '4.23':
commands.append(self.command_builder('vrf', vrf,
default=default,
disable=disable))
Expand Down
8 changes: 4 additions & 4 deletions pyeapi/api/ntp.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def get(self):
return response

def _parse_source_interface(self, config):
if self.version_id >= '4.23':
if self.version_number >= '4.23':
match = re.search(r'^ntp local-interface ([^\s]+)', config, re.M)
else:
match = re.search(r'^ntp source ([^\s]+)', config, re.M)
Expand Down Expand Up @@ -121,7 +121,7 @@ def delete(self):
Returns:
True if the operation succeeds, otherwise False.
"""
if self.version_id >= '4.23':
if self.version_number >= '4.23':
cmd = self.command_builder('ntp local-interface', disable=True)
else:
cmd = self.command_builder('ntp source', disable=True)
Expand All @@ -133,7 +133,7 @@ def default(self):
Returns:
True if the operation succeeds, otherwise False.
"""
if self.version_id >= '4.23':
if self.version_number >= '4.23':
cmd = self.command_builder('ntp local-interface', default=True)
else:
cmd = self.command_builder('ntp source', default=True)
Expand All @@ -148,7 +148,7 @@ def set_source_interface(self, name):
Returns:
True if the operation succeeds, otherwise False.
"""
if self.version_id >= '4.23':
if self.version_number >= '4.23':
cmd = self.command_builder('ntp local-interface', value=name)
else:
cmd = self.command_builder('ntp source', value=name)
Expand Down
6 changes: 3 additions & 3 deletions pyeapi/api/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def getall(self):
Returns:
dict: A dict of usernames with a nested resource dict object
"""
if self.version_id >= '4.23':
if self.version_number >= '4.23':
self.users_re = re.compile(r'username (?P<user>[^\s]+) '
r'privilege (\d+)'
r'(?: role ([^\s]+))?'
Expand Down Expand Up @@ -142,7 +142,7 @@ def _parse_username(self, config):
resource['nopassword'] = nopass == 'nopassword'
resource['format'] = fmt
resource['secret'] = secret
if self.version_id >= '4.23':
if self.version_number >= '4.23':
resource['ssh-key'] = sshkey
else:
resource['sshkey'] = sshkey
Expand Down Expand Up @@ -301,7 +301,7 @@ def set_sshkey(self, name, value=None, default=False, disable=False):
Returns:
True if the operation was successful otherwise False
"""
if self.version_id >= '4.23':
if self.version_number >= '4.23':
cmd = self.command_builder('username %s ssh-key' % name,
value=value,
default=default, disable=disable)
Expand Down
14 changes: 7 additions & 7 deletions pyeapi/api/vrfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def get(self, value):
key/value pairs.
"""
if self.version_id >= '4.23':
if self.version_number >= '4.23':
config = self.get_block('vrf instance %s' % value)
else:
config = self.get_block('vrf definition %s' % value)
Expand Down Expand Up @@ -139,7 +139,7 @@ def getall(self):
A dict object of VRF attributes
"""
if self.version_id >= '4.23':
if self.version_number >= '4.23':
vrfs_re = re.compile(r'(?<=^vrf instance\s)(\w+)', re.M)
else:
vrfs_re = re.compile(r'(?<=^vrf definition\s)(\w+)', re.M)
Expand All @@ -166,7 +166,7 @@ def create(self, vrf_name, rd=None):
Returns:
True if create was successful otherwise False
"""
if self.version_id >= '4.23':
if self.version_number >= '4.23':
commands = ['vrf instance %s' % vrf_name]
else:
commands = ['vrf definition %s' % vrf_name]
Expand All @@ -183,7 +183,7 @@ def delete(self, vrf_name):
Returns:
True if the operation was successful otherwise False
"""
if self.version_id >= '4.23':
if self.version_number >= '4.23':
command = 'no vrf instance %s' % vrf_name
else:
command = 'no vrf definition %s' % vrf_name
Expand All @@ -198,7 +198,7 @@ def default(self, vrf_name):
Returns:
True if the operation was successful otherwise False
"""
if self.version_id >= '4.23':
if self.version_number >= '4.23':
command = 'default vrf instance %s' % vrf_name
else:
command = 'default vrf definition %s' % vrf_name
Expand All @@ -215,7 +215,7 @@ def configure_vrf(self, vrf_name, commands):
True if the commands completed successfully
"""
commands = make_iterable(commands)
if self.version_id >= '4.23':
if self.version_number >= '4.23':
commands.insert(0, 'vrf instance %s' % vrf_name)
else:
commands.insert(0, 'vrf definition %s' % vrf_name)
Expand Down Expand Up @@ -320,7 +320,7 @@ def set_interface(self, vrf_name, interface, default=False, disable=False):
True if the operation was successful otherwise False
"""
cmds = ['interface %s' % interface]
if self.version_id >= '4.23':
if self.version_number >= '4.23':
cmds.append(self.command_builder('vrf', value=vrf_name,
default=default, disable=disable))
else:
Expand Down
46 changes: 23 additions & 23 deletions pyeapi/api/vrrp.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def getall(self):
return vrrps

def _parse_enable(self, config, vrid):
if self.version_id >= '4.21.3':
if self.version_number >= '4.21.3':
match = re.search(r'^\s+vrrp %s disabled$' % vrid, config, re.M)
else:
match = re.search(r'^\s+vrrp %s shutdown$' % vrid, config, re.M)
Expand All @@ -260,7 +260,7 @@ def _parse_enable(self, config, vrid):
return dict(enable=True)

def _parse_primary_ip(self, config, vrid):
if self.version_id >= '4.21.3':
if self.version_number >= '4.21.3':
match = re.search(r'^\s+vrrp %s ipv4 (\d+\.\d+\.\d+\.\d+)$' %
vrid, config, re.M)
else:
Expand All @@ -270,7 +270,7 @@ def _parse_primary_ip(self, config, vrid):
return dict(primary_ip=value)

def _parse_priority(self, config, vrid):
if self.version_id >= '4.21.3':
if self.version_number >= '4.21.3':
match = re.search(r'^\s+vrrp %s priority-level (\d+)$' %
vrid, config, re.M)
else:
Expand All @@ -280,7 +280,7 @@ def _parse_priority(self, config, vrid):
return dict(priority=value)

def _parse_timers_advertise(self, config, vrid):
if self.version_id >= '4.21.3':
if self.version_number >= '4.21.3':
match = re.search(r'^\s+vrrp %s advertisement interval (\d+)$' %
vrid, config, re.M)
else:
Expand All @@ -296,7 +296,7 @@ def _parse_preempt(self, config, vrid):
return dict(preempt=False)

def _parse_secondary_ip(self, config, vrid):
if self.version_id >= '4.21.3':
if self.version_number >= '4.21.3':
matches = re.findall(r'^\s+vrrp %s ipv4 (\d+\.\d+\.\d+\.\d+) '
r'secondary$' % vrid, config, re.M)
else:
Expand All @@ -306,7 +306,7 @@ def _parse_secondary_ip(self, config, vrid):
return dict(secondary_ip=value)

def _parse_description(self, config, vrid):
if self.version_id >= '4.21.3':
if self.version_number >= '4.21.3':
match = re.search(r'^\s+vrrp %s session description(.*)$' %
vrid, config, re.M)
else:
Expand Down Expand Up @@ -343,7 +343,7 @@ def _parse_bfd_ip(self, config, vrid):
return dict(bfd_ip='')

def _parse_ip_version(self, config, vrid):
if self.version_id >= '4.21.3':
if self.version_number >= '4.21.3':
match = re.search(r'^\s+vrrp %s ipv4 version (\d+)$' %
vrid, config, re.M)
else:
Expand All @@ -353,7 +353,7 @@ def _parse_ip_version(self, config, vrid):
return dict(ip_version=value)

def _parse_delay_reload(self, config, vrid):
if self.version_id >= '4.21.3':
if self.version_number >= '4.21.3':
match = re.search(r'^\s+vrrp %s timers delay reload (\d+)$' %
vrid, config, re.M)
else:
Expand All @@ -363,7 +363,7 @@ def _parse_delay_reload(self, config, vrid):
return dict(delay_reload=value)

def _parse_track(self, config, vrid):
if self.version_id >= '4.21.3':
if self.version_number >= '4.21.3':
matches = re.findall(r'^\s+vrrp %s tracked-object (\S+) '
r'(decrement|shutdown)(?:( \d+$|$))' %
vrid, config, re.M)
Expand Down Expand Up @@ -478,12 +478,12 @@ def set_enable(self, name, vrid, value=False, run=True):
"""

if value is False:
if self.version_id >= '4.21.3':
if self.version_number >= '4.21.3':
cmd = "vrrp %d disabled" % vrid
else:
cmd = "vrrp %d shutdown" % vrid
elif value is True:
if self.version_id >= '4.21.3':
if self.version_number >= '4.21.3':
cmd = "no vrrp %d disabled" % vrid
else:
cmd = "no vrrp %d shutdown" % vrid
Expand Down Expand Up @@ -527,19 +527,19 @@ def set_primary_ip(self, name, vrid, value=None, disable=False,
if default is True:
vrrps = self.get(name)
primary_ip = vrrps[vrid]['primary_ip']
if self.version_id >= '4.21.3':
if self.version_number >= '4.21.3':
cmd = "default vrrp %d ipv4 %s" % (vrid, primary_ip)
else:
cmd = "default vrrp %d ip %s" % (vrid, primary_ip)
elif disable is True or value is None:
vrrps = self.get(name)
primary_ip = vrrps[vrid]['primary_ip']
if self.version_id >= '4.21.3':
if self.version_number >= '4.21.3':
cmd = "no vrrp %d ipv4 %s" % (vrid, primary_ip)
else:
cmd = "no vrrp %d ip %s" % (vrid, primary_ip)
elif re.match(r'^\d+\.\d+\.\d+\.\d+$', str(value)):
if self.version_id >= '4.21.3':
if self.version_number >= '4.21.3':
cmd = "vrrp %d ipv4 %s" % (vrid, value)
else:
cmd = "vrrp %d ip %s" % (vrid, value)
Expand Down Expand Up @@ -584,7 +584,7 @@ def set_priority(self, name, vrid, value=None, disable=False,
if not str(value).isdigit() or value < 1 or value > 254:
raise ValueError("vrrp property 'priority' must be "
"an integer in the range 1-254")
if self.version_id >= '4.21.3':
if self.version_number >= '4.21.3':
cmd = self.command_builder('vrrp %d priority-level' %
vrid, value=value,
default=default, disable=disable)
Expand Down Expand Up @@ -625,7 +625,7 @@ def set_description(self, name, vrid, value=None, disable=False,
be passed to the node
"""
if self.version_id >= '4.21.3':
if self.version_number >= '4.21.3':
cmd = self.command_builder('vrrp %d session description' %
vrid, value=value,
default=default, disable=disable)
Expand Down Expand Up @@ -670,7 +670,7 @@ def set_ip_version(self, name, vrid, value=None, disable=False,
if not default and not disable:
if value not in (2, 3):
raise ValueError("vrrp property 'ip_version' must be 2 or 3")
if self.version_id >= '4.21.3':
if self.version_number >= '4.21.3':
cmd = self.command_builder('vrrp %d ipv4 version' %
vrid, value=value,
default=default, disable=disable)
Expand Down Expand Up @@ -742,13 +742,13 @@ def set_secondary_ips(self, name, vrid, secondary_ips, run=True):

# Build the commands to add and remove the secondary ip addresses
for sec_ip in remove:
if self.version_id >= '4.21.3':
if self.version_number >= '4.21.3':
cmds.append("no vrrp %d ipv4 %s secondary" % (vrid, sec_ip))
else:
cmds.append("no vrrp %d ip %s secondary" % (vrid, sec_ip))

for sec_ip in add:
if self.version_id >= '4.21.3':
if self.version_number >= '4.21.3':
cmds.append("vrrp %d ipv4 %s secondary" % (vrid, sec_ip))
else:
cmds.append("vrrp %d ip %s secondary" % (vrid, sec_ip))
Expand Down Expand Up @@ -792,7 +792,7 @@ def set_timers_advertise(self, name, vrid, value=None, disable=False,
if not int(value) or int(value) < 1 or int(value) > 255:
raise ValueError("vrrp property 'timers_advertise' must be"
"in the range 1-255")
if self.version_id >= '4.21.3':
if self.version_number >= '4.21.3':
cmd = self.command_builder('vrrp %d advertisement interval' %
vrid,
value=value, default=default,
Expand Down Expand Up @@ -1011,7 +1011,7 @@ def set_delay_reload(self, name, vrid, value=None, disable=False,
if not int(value) or int(value) < 1 or int(value) > 3600:
raise ValueError("vrrp property 'delay_reload' must be"
"in the range 0-3600 %r" % value)
if self.version_id >= '4.21.3':
if self.version_number >= '4.21.3':
cmd = self.command_builder('vrrp %d timers delay reload' %
vrid, value=value,
default=default, disable=disable)
Expand Down Expand Up @@ -1152,7 +1152,7 @@ def set_tracks(self, name, vrid, tracks, run=True):

if amount == unset:
amount = ''
if self.version_id >= '4.21.3':
if self.version_number >= '4.21.3':
t_cmd = ("no vrrp %d tracked-object %s %s %s"
% (vrid, tr_obj, action, amount))
else:
Expand All @@ -1168,7 +1168,7 @@ def set_tracks(self, name, vrid, tracks, run=True):

if amount == unset:
amount = ''
if self.version_id >= '4.21.3':
if self.version_number >= '4.21.3':
t_cmd = ("vrrp %d tracked-object %s %s %s"
% (vrid, tr_obj, action, amount))
else:
Expand Down
Loading

0 comments on commit 0f6ad04

Please sign in to comment.