Skip to content

Commit

Permalink
Merge branch 'vlan_range' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Sprygada committed Jul 22, 2015
2 parents 7c58dd7 + 7977119 commit 78464ea
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 3 deletions.
73 changes: 73 additions & 0 deletions pyeapi/api/switchports.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import re

from pyeapi.api import EntityCollection
from pyeapi.utils import make_iterable

class Switchports(EntityCollection):
"""The Switchports class provides a configuration resource for swichports
Expand All @@ -62,6 +63,7 @@ def get(self, name):
* access_vlan (str): The switchport access vlan value
* trunk_native_vlan (str): The switchport trunk native vlan vlaue
* trunk_allowed_vlans (str): The trunk allowed vlans value
* trunk_groups (list): The list of trunk groups configured
Args:
name (string): The interface identifier to get. Note: Switchports
Expand All @@ -82,6 +84,7 @@ def get(self, name):
resource.update(self._parse_access_vlan(config))
resource.update(self._parse_trunk_native_vlan(config))
resource.update(self._parse_trunk_allowed_vlans(config))
resource.update(self._parse_trunk_groups(config))
return resource


Expand All @@ -99,6 +102,19 @@ def _parse_mode(self, config):
value = re.search(r'switchport mode (\w+)', config, re.M)
return dict(mode=value.group(1))

def _parse_trunk_groups(self, config):
"""Scans the specified config and parses the trunk group values
Args:
config (str): The interface configuraiton blcok
Returns:
A dict object with the trunk group values that can be merged
into the resource dict
"""
values = re.findall(r'switchport trunk group ([^\s]+)', config, re.M)
return dict(trunk_groups=values)

def _parse_access_vlan(self, config):
"""Scans the specified config and parse the access-vlan value
Args:
Expand Down Expand Up @@ -311,6 +327,63 @@ def set_trunk_allowed_vlans(self, name, value=None, default=False):
command = self.command_builder(string, value=value, default=default)
return self.configure_interface(name, command)

def set_trunk_groups(self, intf, value=None, default=False):
"""Configures the switchport trunk group value
Args:
intf (str): The interface identifier to configure.
value (str): The set of values to configure the trunk group
default (bool): Configures the trunk group default value
Returns:
True if the config operation succeeds otherwise False
"""
if default:
cmd = 'default switchport trunk group'
return self.configure_interface(intf, cmd)

current_value = self.get(intf)['trunk_groups']
failure = False

value = make_iterable(value)

for name in set(value).difference(current_value):
if not self.add_trunk_group(intf, name):
failure = True

for name in set(current_value).difference(value):
if not self.remove_trunk_group(intf, name):
failure = True

return not failure

def add_trunk_group(self, intf, value):
"""Adds the specified trunk group to the interface
Args:
intf (str): The interface name to apply the trunk group to
value (str): The trunk group value to apply to the interface
Returns:
True if the operation as successfully applied otherwise false
"""
string = 'switchport trunk group {}'.format(value)
return self.configure_interface(intf, string)

def remove_trunk_group(self, intf, value):
"""Removes a specified trunk group to the interface
Args:
intf (str): The interface name to remove the trunk group from
value (str): The trunk group value
Returns:
True if the operation as successfully applied otherwise false
"""
string = 'no switchport trunk group {}'.format(value)
return self.configure_interface(intf, string)


def instance(node):
"""Returns an instance of Switchports
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/running_config.text
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,8 @@ interface Ethernet1
switchport trunk allowed vlan 1-4094
switchport mode access
switchport mac address learning
switchport trunk group foo
switchport trunk group bar
no switchport private-vlan mapping
switchport
default encapsulation dot1q vlan
Expand Down
27 changes: 24 additions & 3 deletions test/unit/test_api_switchports.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ def __init__(self, *args, **kwargs):

def test_get(self):
result = self.instance.get('Ethernet1')
values = dict(name='Ethernet1', mode='access', access_vlan='1',
trunk_native_vlan='1', trunk_allowed_vlans='1-4094')
self.assertEqual(result, values)
keys = ['name', 'mode', 'access_vlan', 'trunk_native_vlan',
'trunk_allowed_vlans', 'trunk_groups']
self.assertEqual(sorted(result.keys()), sorted(keys))

def test_getall(self):
result = self.instance.getall()
Expand Down Expand Up @@ -158,6 +158,27 @@ def test_set_allowed_vlans_with_default(self):
func = function('set_trunk_allowed_vlans', intf, default=True)
self.eapi_positive_config_test(func, cmds)

def test_set_trunk_groups_with_default(self):
for intf in self.INTERFACES:
cmds = ['interface %s' % intf,
'default switchport trunk group']
func = function('set_trunk_groups', intf, default=True)
self.eapi_positive_config_test(func, cmds)

def test_add_trunk_group(self):
for intf in self.INTERFACES:
cmds = ['interface %s' % intf,
'switchport trunk group foo']
func = function('add_trunk_group', intf, 'foo')
self.eapi_positive_config_test(func, cmds)

def test_remove_trunk_group(self):
for intf in self.INTERFACES:
cmds = ['interface %s' % intf,
'no switchport trunk group foo']
func = function('remove_trunk_group', intf, 'foo')
self.eapi_positive_config_test(func, cmds)

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

Expand Down

0 comments on commit 78464ea

Please sign in to comment.