diff --git a/lib/ansible/modules/net_tools/nmcli.py b/lib/ansible/modules/net_tools/nmcli.py index d286804924..3d2682da1e 100644 --- a/lib/ansible/modules/net_tools/nmcli.py +++ b/lib/ansible/modules/net_tools/nmcli.py @@ -1049,12 +1049,60 @@ class Nmcli(object): def create_connection_vlan(self): cmd = [self.nmcli_bin] - # format for creating ethernet interface + cmd.append('con') + cmd.append('add') + cmd.append('type') + cmd.append('vlan') + cmd.append('con-name') + + if self.conn_name is not None: + cmd.append(self.conn_name) + elif self.ifname is not None: + cmd.append(self.ifname) + else: + cmd.append('vlan%s' % self.vlanid) + + cmd.append('ifname') + if self.ifname is not None: + cmd.append(self.ifname) + elif self.conn_name is not None: + cmd.append(self.conn_name) + else: + cmd.append('vlan%s' % self.vlanid) + + params = {'dev': self.vlandev, + 'id': self.vlanid, + 'ip4': self.ip4, + 'gw4': self.gw4, + 'ip6': self.ip6, + 'gw6': self.gw6, + 'autoconnect': self.bool_to_string(self.autoconnect) + } + for k, v in params.items(): + cmd.extend([k, v]) + return cmd def modify_connection_vlan(self): cmd = [self.nmcli_bin] - # format for modifying ethernet interface + cmd.append('con') + cmd.append('mod') + cmd.append('con-name') + + params = {'vlan.parent': self.vlandev, + 'vlan.id': self.vlanid, + 'ipv4.address': self.ip4, + 'ipv4.geteway': self.gw4, + 'ipv4.dns': self.dns4, + 'ipv6.address': self.ip6, + 'ipv6.gateway': self.gw6, + 'ipv6.dns': self.dns6, + 'autoconnect': self.bool_to_string(self.autoconnect) + } + + for k, v in params.items(): + cmd.extend([k, v]) + return cmd def create_connection(self): diff --git a/test/units/modules/net_tools/test_nmcli.py b/test/units/modules/net_tools/test_nmcli.py index 3025e6049a..39d54ee962 100644 --- a/test/units/modules/net_tools/test_nmcli.py +++ b/test/units/modules/net_tools/test_nmcli.py @@ -1,9 +1,10 @@ # Copyright: (c) 2017 Ansible Project # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) -import pytest import json +import pytest + from ansible.modules.net_tools import nmcli pytestmark = pytest.mark.usefixtures('patch_ansible_module') @@ -118,6 +119,18 @@ TESTCASE_BRIDGE_SLAVE = [ } ] +TESTCASE_VLAN = [ + { + 'type': 'vlan', + 'conn_name': 'non_existent_nw_device', + 'ifname': 'vlan_not_exists', + 'ip4': '10.10.10.10', + 'gw4': '10.10.10.1', + 'state': 'present', + '_ansible_check_mode': False, + } +] + def mocker_set(mocker, connection_exists=False): """ @@ -360,3 +373,37 @@ def test_mod_bridge_slave(mocked_generic_connection_modify): for param in ['bridge-port.path-cost', '100']: assert param in args[0] + + +@pytest.mark.parametrize('patch_ansible_module', TESTCASE_VLAN, indirect=['patch_ansible_module']) +def test_create_vlan_con(mocked_generic_connection_create): + """ + Test if Bridge_slave created + """ + + with pytest.raises(SystemExit): + nmcli.main() + + assert nmcli.Nmcli.execute_command.call_count == 1 + arg_list = nmcli.Nmcli.execute_command.call_args_list + args, kwargs = arg_list[0] + + for param in ['vlan']: + assert param in args[0] + + +@pytest.mark.parametrize('patch_ansible_module', TESTCASE_VLAN, indirect=['patch_ansible_module']) +def test_mod_vlan_conn(mocked_generic_connection_modify): + """ + Test if Bridge_slave modified + """ + + with pytest.raises(SystemExit): + nmcli.main() + + assert nmcli.Nmcli.execute_command.call_count == 1 + arg_list = nmcli.Nmcli.execute_command.call_args_list + args, kwargs = arg_list[0] + + for param in ['vlan.id']: + assert param in args[0]