From f4fa4e4e0ef0ac0ec57dfa8f479ced461d62c52a Mon Sep 17 00:00:00 2001 From: anas-shami <54317149+anas-shami@users.noreply.github.com> Date: Wed, 28 Aug 2019 17:11:56 +0300 Subject: [PATCH] Feature/onyx wjh upstream (#61269) * support wjh module config with testing file * Fix version, fix enable/disable all groups * refactor get current group status * fix indentation and change command_generate function, add test cases and check them * Update onyx_wjh.py fix description * Update onyx_wjh.py remove extra spaces, add type to choices after run validate-modules * fix format string - pytest use 2.6.6 format function * Remove unused imports * chenge onyx_wjh file permessions * Fix version, fix enable/disable all groups * fix indentation and change command_generate function, add test cases and check them * support wjh module config with testing file * Fix version, fix enable/disable all groups * refactor get current group status * fix indentation and change command_generate function, add test cases and check them * Update onyx_wjh.py fix description * Update onyx_wjh.py remove extra spaces, add type to choices after run validate-modules * fix format string - pytest use 2.6.6 format function * fix format string - pytest use 2.6.6 format function * chenge onyx_wjh file permessions --- lib/ansible/modules/network/onyx/onyx_wjh.py | 224 ++++++++++++++++++ .../network/onyx/fixtures/onyx_wjh_show.cfg | 3 + .../modules/network/onyx/test_onyx_wjh.py | 66 ++++++ 3 files changed, 293 insertions(+) create mode 100644 lib/ansible/modules/network/onyx/onyx_wjh.py create mode 100644 test/units/modules/network/onyx/fixtures/onyx_wjh_show.cfg create mode 100644 test/units/modules/network/onyx/test_onyx_wjh.py diff --git a/lib/ansible/modules/network/onyx/onyx_wjh.py b/lib/ansible/modules/network/onyx/onyx_wjh.py new file mode 100644 index 00000000000..1953c65125d --- /dev/null +++ b/lib/ansible/modules/network/onyx/onyx_wjh.py @@ -0,0 +1,224 @@ +#!/usr/bin/python +# +# Copyright: Ansible Project +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import absolute_import, division, print_function +__metaclass__ = type + +ANSIBLE_METADATA = {'metadata_version': '1.1', + 'status': ['preview'], + 'supported_by': 'community'} + +DOCUMENTATION = """ +--- +module: onyx_wjh +version_added: "2.9" +author: "Anas Shami (@anass)" +short_description: Configure what-just-happend module +description: + - This module provides declarative management of wjh + on Mellanox ONYX network devices. +notes: +options: + group: + description: + - Name of wjh group. + choices: ['all', 'forwarding', 'acl'] + type: str + enabled: + description: + - wjh group status + type: bool + auto_export: + description: + - wjh group auto export pcap file status + type: bool + export_group: + description: + - wjh group auto export group + choices: ['all', 'forwarding', 'acl'] + type: str + clear_group: + description: + - clear pcap file by group + choices: ['all', 'user', 'auto-export'] + type: str +""" + +EXAMPLES = """ +- name: enable wjh + onyx_wjh: + group: forwarding + enabled: True + +- name: disable wjh + onyx_wjh: + group: forwarding + enabled: False + +- name: enable auto-export + onyx_wjh: + auto_export: True + export_group: forwarding +- name: disable auto-export + onyx_wjh: + auto_export: False + export_group: forwarding +- name: clear pcap file + onyx_wjh: + clear_group: auto-export +""" + +RETURN = """ +commands: + description: The list of configuration mode commands to send to the device. + returned: always + type: list + sample: + - what-just-happend forwarding enable + - what-just-happend auto-export forwarding enable + - clear what-just-happend pcap-file user +""" +import re + +from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils.network.onyx.onyx import BaseOnyxModule, show_cmd + + +class OnyxWJHModule(BaseOnyxModule): + WJH_DISABLED_REGX = re.compile(r'^no what-just-happened ([a-z]+) enable.*') + WJH_DISABLED_AUTO_EXPORT_REGX = re.compile(r'^no what-just-happened auto-export ([a-z]+) enable.*') + + WJH_CMD_FMT = '{0}what-just-happened {1} enable' + WJH_EXPORT_CMD_FMT = '{0}what-just-happened auto-export {1} enable' + WJH_CLEAR_CMD_FMT = 'clear what-just-happened pcap-files {0}' + + WJH_GROUPS = ['all', 'forwarding', 'acl'] + CLEAR_GROUPS = ['all', 'user', 'auto-export'] + + def init_module(self): + """ + module initialization + """ + element_spec = dict(group=dict(choices=self.WJH_GROUPS), + enabled=dict(type='bool'), + auto_export=dict(type='bool'), + export_group=dict(choices=self.WJH_GROUPS), + clear_group=dict(choices=self.CLEAR_GROUPS)) + + argument_spec = dict() + argument_spec.update(element_spec) + self._module = AnsibleModule( + argument_spec=argument_spec, + supports_check_mode=True, + required_together=[ + ['group', 'enabled'], + ['auto_export', 'export_group'] + ]) + + def get_required_config(self): + self._required_config = dict() + module_params = self._module.params + group = module_params.get('group') + export_group = module_params.get('export_group') + clear_group = module_params.get('clear_group') + + params = dict() + if group: + enabled = module_params.get('enabled') + params.update({ + 'group': group, + 'enabled': enabled + }) + + if export_group: + auto_export = module_params.get('auto_export') + params.update({ + 'export_group': export_group, + 'auto_export': auto_export + }) + + if clear_group: + params.update({ + 'clear_group': clear_group + }) + + self.validate_param_values(params) + self._required_config = params + + def _get_wjh_config(self): + return show_cmd(self._module, "show running-config | include .*what-just-happened.*", json_fmt=False, fail_on_error=False) + + def _set_current_config(self, config): + if not config: + return + current_config = self._current_config + lines = config.split('\n') + for line in lines: + if line.startswith('#'): + continue + match = self.WJH_DISABLED_REGX.match(line) + if match: + # wjh is disabled + group = match.group(1) + current_config[group] = False + + match = self.WJH_DISABLED_AUTO_EXPORT_REGX.match(line) + if match: + # wjh auto export is disabled + export_group = match.group(1) + '_export' + current_config[export_group] = False + + ''' + show running config will contains [no wjh * group enable] if disabled - default config is enabled + ''' + def load_current_config(self): + self._current_config = dict() + config_lines = self._get_wjh_config() + if config_lines: + self._set_current_config(config_lines) + + def wjh_group_status(self, current_config, group_value, suffix=''): + current_enabled = False + if group_value == 'all': + # no disabled group so all would be false + current_enabled = not all([ + (group + suffix) in current_config for group in self.WJH_GROUPS]) + else: + # if no current-value its enabled + current_enabled = current_config[group_value + suffix] if((group_value + suffix) in current_config) else True + return current_enabled + + ''' + wjh is enabled "by default" + when wjh disable we will find no wjh commands in running config + ''' + def generate_commands(self): + current_config, required_config = self._current_config, self._required_config + group = required_config.get('group') + export_group = required_config.get('export_group') + clear_group = required_config.get('clear_group') + if group: + current_enabled = self.wjh_group_status(current_config, group) + if(required_config['enabled'] != current_enabled): + self._commands.append(self.WJH_CMD_FMT + .format(('' if required_config['enabled'] else 'no '), group)) + if export_group: + current_enabled = self.wjh_group_status(current_config, required_config['export_group'], '_export') + if(required_config['auto_export'] != current_enabled): + self._commands.append(self.WJH_EXPORT_CMD_FMT + .format(('' if required_config['auto_export'] else 'no '), export_group)) + if clear_group: + # clear pcap files + self._commands.append(self.WJH_CLEAR_CMD_FMT.format(clear_group)) + + +def main(): + """ main entry point for module execution + """ + OnyxWJHModule.main() + + +if __name__ == '__main__': + main() diff --git a/test/units/modules/network/onyx/fixtures/onyx_wjh_show.cfg b/test/units/modules/network/onyx/fixtures/onyx_wjh_show.cfg new file mode 100644 index 00000000000..d50f497607c --- /dev/null +++ b/test/units/modules/network/onyx/fixtures/onyx_wjh_show.cfg @@ -0,0 +1,3 @@ + +no what-just-happened auto-export forwarding enable +no what-just-happened forwarding enable diff --git a/test/units/modules/network/onyx/test_onyx_wjh.py b/test/units/modules/network/onyx/test_onyx_wjh.py new file mode 100644 index 00000000000..e98c8841ebd --- /dev/null +++ b/test/units/modules/network/onyx/test_onyx_wjh.py @@ -0,0 +1,66 @@ +# +# Copyright: Ansible Project +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +# Make coding more python3-ish +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +from units.compat.mock import patch +from ansible.modules.network.onyx import onyx_wjh +from units.modules.utils import set_module_args +from .onyx_module import TestOnyxModule, load_fixture + + +class TestOnyxWJHModule(TestOnyxModule): + + module = onyx_wjh + + def setUp(self): + self.enabled = False + super(TestOnyxWJHModule, self).setUp() + self.mock_get_config = patch.object( + onyx_wjh.OnyxWJHModule, "_get_wjh_config") + self.get_config = self.mock_get_config.start() + + self.mock_load_config = patch( + 'ansible.module_utils.network.onyx.onyx.load_config') + self.load_config = self.mock_load_config.start() + + def tearDown(self): + super(TestOnyxWJHModule, self).tearDown() + self.mock_get_config.stop() + self.mock_load_config.stop() + + def load_fixtures(self, commands=None, transport='cli'): + config_file = 'onyx_wjh_show.cfg' + self.get_config.return_value = load_fixture(config_file) + self.load_config.return_value = None + + def test_wjh_no_change(self): + set_module_args(dict(group='forwarding', enabled=False)) + self.execute_module(changed=False) + + def test_wjh_enable(self): + set_module_args(dict(group='forwarding', enabled=True)) + commands = ['what-just-happened forwarding enable'] + self.execute_module(changed=True, commands=commands) + + def test_wjh_export_no_change(self): + set_module_args(dict(export_group='forwarding', auto_export=False)) + self.execute_module(changed=False) + + def test_wjh_export_enable(self): + set_module_args(dict(export_group='forwarding', auto_export=True)) + commands = ['what-just-happened auto-export forwarding enable'] + self.execute_module(changed=True, commands=commands) + + def test_wjh_export_disable(self): + set_module_args(dict(export_group='all', auto_export=False)) + commands = ['no what-just-happened auto-export all enable'] + self.execute_module(changed=True, commands=commands) + + def test_wjh_clear(self): + set_module_args(dict(clear_group='all')) + commands = ['clear what-just-happened pcap-files all'] + self.execute_module(changed=True, commands=commands)