From 09ae1ec308d919f88e63280a507da624cd8f93d6 Mon Sep 17 00:00:00 2001 From: Trishna Guha Date: Thu, 2 Aug 2018 16:34:48 +0530 Subject: [PATCH] cliconf refactor eos, nxos fix (#43596) * cliconf refactor eos and nxos fix Signed-off-by: Trishna Guha * move nxos replace check to plugins Signed-off-by: Trishna Guha * address review comment * fix shippable --- lib/ansible/module_utils/network/nxos/nxos.py | 3 +++ lib/ansible/modules/network/nxos/nxos_config.py | 14 -------------- lib/ansible/plugins/cliconf/eos.py | 4 ++-- lib/ansible/plugins/cliconf/nxos.py | 6 +++++- lib/ansible/plugins/httpapi/nxos.py | 4 ++++ .../units/modules/network/nxos/test_nxos_config.py | 9 ++------- 6 files changed, 16 insertions(+), 24 deletions(-) diff --git a/lib/ansible/module_utils/network/nxos/nxos.py b/lib/ansible/module_utils/network/nxos/nxos.py index 2a4b3f0be31..50f8b2fbb11 100644 --- a/lib/ansible/module_utils/network/nxos/nxos.py +++ b/lib/ansible/module_utils/network/nxos/nxos.py @@ -392,6 +392,9 @@ class Nxapi: """Sends the ordered set of commands to the device """ if replace: + device_info = self.get_device_info() + if '9K' not in device_info.get('network_os_platform', ''): + self._module.fail_json(msg='replace is supported only on Nexus 9K devices') commands = 'config replace {0}'.format(replace) commands = to_list(commands) diff --git a/lib/ansible/modules/network/nxos/nxos_config.py b/lib/ansible/modules/network/nxos/nxos_config.py index 70a8e344388..a8af295df73 100644 --- a/lib/ansible/modules/network/nxos/nxos_config.py +++ b/lib/ansible/modules/network/nxos/nxos_config.py @@ -281,7 +281,6 @@ from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.connection import ConnectionError from ansible.module_utils.network.common.config import NetworkConfig, dumps from ansible.module_utils.network.nxos.nxos import get_config, load_config, run_commands, get_connection -from ansible.module_utils.network.nxos.nxos import get_capabilities from ansible.module_utils.network.nxos.nxos import nxos_argument_spec from ansible.module_utils.network.nxos.nxos import check_args as nxos_check_args from ansible.module_utils.network.common.utils import to_list @@ -392,19 +391,6 @@ def main(): config = None - try: - info = get_capabilities(module) - api = info.get('network_api') - device_info = info.get('device_info', {}) - os_platform = device_info.get('network_os_platform', '') - except ConnectionError: - api = '' - os_platform = '' - - if api == 'cliconf' and module.params['replace'] == 'config': - if '9K' not in os_platform: - module.fail_json(msg='replace: config is supported only on Nexus 9K series switches') - diff_ignore_lines = module.params['diff_ignore_lines'] path = module.params['parents'] connection = get_connection(module) diff --git a/lib/ansible/plugins/cliconf/eos.py b/lib/ansible/plugins/cliconf/eos.py index f5ce7a8a9d3..9531079b9fd 100644 --- a/lib/ansible/plugins/cliconf/eos.py +++ b/lib/ansible/plugins/cliconf/eos.py @@ -289,8 +289,8 @@ class Cliconf(CliconfBase): def get_capabilities(self): result = {} - result['rpc'] = self.get_base_rpc() - result['device_info'] = self.get_device_info() + rpc_list = ['commit', 'discard_changes', 'get_diff', 'run_commands', 'supports_sessions'] + result['rpc'] = self.get_base_rpc() + rpc_list result['device_info'] = self.get_device_info() result['device_operations'] = self.get_device_operations() result.update(self.get_option_values()) diff --git a/lib/ansible/plugins/cliconf/nxos.py b/lib/ansible/plugins/cliconf/nxos.py index 2c936cf1447..25e988d7f78 100644 --- a/lib/ansible/plugins/cliconf/nxos.py +++ b/lib/ansible/plugins/cliconf/nxos.py @@ -158,6 +158,9 @@ class Cliconf(CliconfBase): requests = [] if replace: + device_info = self.get_device_info() + if '9K' not in device_info.get('network_os_platform', ''): + raise ConnectionError(msg=u'replace is supported only on Nexus 9K devices') candidate = 'config replace {0}'.format(replace) if commit: @@ -244,8 +247,9 @@ class Cliconf(CliconfBase): def get_capabilities(self): result = {} - result['rpc'] = self.get_base_rpc() + result['rpc'] = self.get_base_rpc() + ['get_diff', 'run_commands'] result['device_info'] = self.get_device_info() + result['device_operations'] = self.get_device_operations() result.update(self.get_option_values()) if isinstance(self._connection, NetworkCli): diff --git a/lib/ansible/plugins/httpapi/nxos.py b/lib/ansible/plugins/httpapi/nxos.py index 1afc883b635..e5fdf5ec567 100644 --- a/lib/ansible/plugins/httpapi/nxos.py +++ b/lib/ansible/plugins/httpapi/nxos.py @@ -77,7 +77,11 @@ class HttpApi(HttpApiBase): operations = self.connection.get_device_operations() self.connection.check_edit_config_capabiltiy(operations, candidate, commit, replace, comment) + if replace: + device_info = self.connection.get_device_info() + if '9K' not in device_info.get('network_os_platform', ''): + raise ConnectionError(msg=u'replace is supported only on Nexus 9K devices') candidate = 'config replace {0}'.format(replace) responses = self.send_request(candidate, output='config') diff --git a/test/units/modules/network/nxos/test_nxos_config.py b/test/units/modules/network/nxos/test_nxos_config.py index d06121d61c8..de476734a4f 100644 --- a/test/units/modules/network/nxos/test_nxos_config.py +++ b/test/units/modules/network/nxos/test_nxos_config.py @@ -38,10 +38,6 @@ class TestNxosConfigModule(TestNxosModule): self.mock_load_config = patch('ansible.modules.network.nxos.nxos_config.load_config') self.load_config = self.mock_load_config.start() - self.mock_get_capabilities = patch('ansible.modules.network.nxos.nxos_config.get_capabilities') - self.get_capabilities = self.mock_get_capabilities.start() - self.get_capabilities.return_value = {'device_info': {'network_os_platform': 'N9K-NXOSV'}} - self.mock_save_config = patch('ansible.modules.network.nxos.nxos_config.save_config') self.save_config = self.mock_save_config.start() @@ -61,7 +57,6 @@ class TestNxosConfigModule(TestNxosModule): super(TestNxosConfigModule, self).tearDown() self.mock_get_config.stop() self.mock_load_config.stop() - self.mock_get_capabilities.stop() self.mock_run_commands.stop() self.mock_get_connection.stop() @@ -89,10 +84,10 @@ class TestNxosConfigModule(TestNxosModule): self.assertEqual(sorted(config), sorted(result['commands']), result['commands']) def test_nxos_config_replace_src(self): - set_module_args(dict(replace_src='config.txt', replace='config')) + set_module_args(dict(replace_src='bootflash:config', replace='config')) self.conn.get_diff = MagicMock(return_value=self.cliconf_obj.get_diff(self.running_config, self.running_config, diff_replace='config')) result = self.execute_module(changed=True) - self.assertEqual(result['commands'], ['config replace config.txt']) + self.assertEqual(result['commands'], ['config replace bootflash:config']) def test_nxos_config_lines(self): lines = ['hostname switch01', 'ip domain-name eng.ansible.com']