From 7bb193ce644ea73e4acaa345c84326cb5e0ef78e Mon Sep 17 00:00:00 2001 From: Christian Poessinger Date: Thu, 2 Jan 2020 23:01:06 +0100 Subject: ifconfig: T1557: refactor apply_vlan_config() function instead of providing three copies of the same method in bonding, ethernet and wireless, make a common function in vyos.ifconfig_vlan.apply_vlan_config(). --- src/conf_mode/interfaces-bonding.py | 65 ++---------------------------------- src/conf_mode/interfaces-ethernet.py | 65 ++---------------------------------- src/conf_mode/interfaces-wireless.py | 38 ++------------------- 3 files changed, 6 insertions(+), 162 deletions(-) (limited to 'src') diff --git a/src/conf_mode/interfaces-bonding.py b/src/conf_mode/interfaces-bonding.py index 8a0f9f84d..6c5362956 100755 --- a/src/conf_mode/interfaces-bonding.py +++ b/src/conf_mode/interfaces-bonding.py @@ -20,7 +20,8 @@ from copy import deepcopy from sys import exit from netifaces import interfaces -from vyos.ifconfig import BondIf, VLANIf +from vyos.ifconfig import BondIf +from vyos.ifconfig_vlan import apply_vlan_config from vyos.configdict import list_diff, vlan_to_dict from vyos.config import Config from vyos import ConfigError @@ -74,68 +75,6 @@ def get_bond_mode(mode): else: raise ConfigError('invalid bond mode "{}"'.format(mode)) - -def apply_vlan_config(vlan, config): - """ - Generic function to apply a VLAN configuration from a dictionary - to a VLAN interface - """ - - if type(vlan) != type(VLANIf("lo")): - raise TypeError() - - # get DHCP config dictionary and update values - opt = vlan.get_dhcp_options() - - if config['dhcp_client_id']: - opt['client_id'] = config['dhcp_client_id'] - - if config['dhcp_hostname']: - opt['hostname'] = config['dhcp_hostname'] - - if config['dhcp_vendor_class_id']: - opt['vendor_class_id'] = config['dhcp_vendor_class_id'] - - # store DHCP config dictionary - used later on when addresses are aquired - vlan.set_dhcp_options(opt) - - # get DHCPv6 config dictionary and update values - opt = vlan.get_dhcpv6_options() - - if config['dhcpv6_prm_only']: - opt['dhcpv6_prm_only'] = True - - if config['dhcpv6_temporary']: - opt['dhcpv6_temporary'] = True - - # store DHCPv6 config dictionary - used later on when addresses are aquired - vlan.set_dhcpv6_options(opt) - - # update interface description used e.g. within SNMP - vlan.set_alias(config['description']) - # ignore link state changes - vlan.set_link_detect(config['disable_link_detect']) - # Maximum Transmission Unit (MTU) - vlan.set_mtu(config['mtu']) - # Change VLAN interface MAC address - if config['mac']: - vlan.set_mac(config['mac']) - - # enable/disable VLAN interface - if config['disable']: - vlan.set_state('down') - else: - vlan.set_state('up') - - # Configure interface address(es) - # - not longer required addresses get removed first - # - newly addresses will be added second - for addr in config['address_remove']: - vlan.del_addr(addr) - for addr in config['address']: - vlan.add_addr(addr) - - def get_config(): # initialize kernel module if not loaded if not os.path.isfile('/sys/class/net/bonding_masters'): diff --git a/src/conf_mode/interfaces-ethernet.py b/src/conf_mode/interfaces-ethernet.py index a9ed6bfb6..cd75e1257 100755 --- a/src/conf_mode/interfaces-ethernet.py +++ b/src/conf_mode/interfaces-ethernet.py @@ -19,7 +19,8 @@ import os from copy import deepcopy from sys import exit -from vyos.ifconfig import EthernetIf, VLANIf +from vyos.ifconfig import EthernetIf +from vyos.ifconfig_vlan import apply_vlan_config from vyos.configdict import list_diff, vlan_to_dict from vyos.config import Config from vyos import ConfigError @@ -57,68 +58,6 @@ default_config_data = { 'vif_remove': [] } - -def apply_vlan_config(vlan, config): - """ - Generic function to apply a VLAN configuration from a dictionary - to a VLAN interface - """ - - if type(vlan) != type(VLANIf("lo")): - raise TypeError() - - # get DHCP config dictionary and update values - opt = vlan.get_dhcp_options() - - if config['dhcp_client_id']: - opt['client_id'] = config['dhcp_client_id'] - - if config['dhcp_hostname']: - opt['hostname'] = config['dhcp_hostname'] - - if config['dhcp_vendor_class_id']: - opt['vendor_class_id'] = config['dhcp_vendor_class_id'] - - # store DHCP config dictionary - used later on when addresses are aquired - vlan.set_dhcp_options(opt) - - # get DHCPv6 config dictionary and update values - opt = vlan.get_dhcpv6_options() - - if config['dhcpv6_prm_only']: - opt['dhcpv6_prm_only'] = True - - if config['dhcpv6_temporary']: - opt['dhcpv6_temporary'] = True - - # store DHCPv6 config dictionary - used later on when addresses are aquired - vlan.set_dhcpv6_options(opt) - - # update interface description used e.g. within SNMP - vlan.set_alias(config['description']) - # ignore link state changes - vlan.set_link_detect(config['disable_link_detect']) - # Maximum Transmission Unit (MTU) - vlan.set_mtu(config['mtu']) - # Change VLAN interface MAC address - if config['mac']: - vlan.set_mac(config['mac']) - - # enable/disable VLAN interface - if config['disable']: - vlan.set_state('down') - else: - vlan.set_state('up') - - # Configure interface address(es) - # - not longer required addresses get removed first - # - newly addresses will be added second - for addr in config['address_remove']: - vlan.del_addr(addr) - for addr in config['address']: - vlan.add_addr(addr) - - def get_config(): eth = deepcopy(default_config_data) conf = Config() diff --git a/src/conf_mode/interfaces-wireless.py b/src/conf_mode/interfaces-wireless.py index 17b0876a0..0df0b3ba4 100755 --- a/src/conf_mode/interfaces-wireless.py +++ b/src/conf_mode/interfaces-wireless.py @@ -26,7 +26,8 @@ from grp import getgrnam from subprocess import Popen, PIPE from psutil import pid_exists -from vyos.ifconfig import EthernetIf, VLANIf +from vyos.ifconfig import EthernetIf +from vyos.ifconfig_vlan import apply_vlan_config from vyos.configdict import list_diff, vlan_to_dict from vyos.config import Config from vyos import ConfigError @@ -884,41 +885,6 @@ def subprocess_cmd(command): p = Popen(command, stdout=PIPE, shell=True) p.communicate() - -def apply_vlan_config(vlan, config): - """ - Generic function to apply a VLAN configuration from a dictionary - to a VLAN interface - """ - - if type(vlan) != type(VLANIf("lo")): - raise TypeError() - - # update interface description used e.g. within SNMP - vlan.set_alias(config['description']) - # ignore link state changes - vlan.set_link_detect(config['disable_link_detect']) - # Maximum Transmission Unit (MTU) - vlan.set_mtu(config['mtu']) - # Change VLAN interface MAC address - if config['mac']: - vlan.set_mac(config['mac']) - - # enable/disable VLAN interface - if config['disable']: - vlan.set_state('down') - else: - vlan.set_state('up') - - # Configure interface address(es) - # - not longer required addresses get removed first - # - newly addresses will be added second - for addr in config['address_remove']: - vlan.del_addr(addr) - for addr in config['address']: - vlan.add_addr(addr) - - def get_config(): wifi = deepcopy(default_config_data) conf = Config() -- cgit v1.2.3