Dre4m Shell
Server IP : 85.214.239.14  /  Your IP : 3.16.50.164
Web Server : Apache/2.4.62 (Debian)
System : Linux h2886529.stratoserver.net 4.9.0 #1 SMP Mon Sep 30 15:36:27 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/community/zabbix/plugins/module_utils/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ HOME SHELL ]     

Current File : /usr/lib/python3/dist-packages/ansible_collections/community/zabbix/plugins/module_utils/helpers.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# 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

from ansible.module_utils.basic import env_fallback


def require_creds_params(module):
    if module._socket_path is None:
        # ansible_connection = local
        if ((not module.params.get('server_url', None)) or (not module.params.get('login_user', None)) or (not module.params.get('login_password', None))):
            module.fail_json(msg="server_url, login_user, login_password are mandatory parameters when httpapi connection is not used")


def zabbix_common_argument_spec():
    """
    Return a dictionary with connection options.
    The options are commonly used by most of Zabbix modules.
    """
    return dict(
        server_url=dict(
            type='str',
            required=False,
            aliases=['url'],
            fallback=(env_fallback, ['ZABBIX_SERVER'])
        ),
        login_user=dict(
            type='str', required=False,
            fallback=(env_fallback, ['ZABBIX_USERNAME'])
        ),
        login_password=dict(
            type='str',
            required=False,
            no_log=True,
            fallback=(env_fallback, ['ZABBIX_PASSWORD'])
        ),
        http_login_user=dict(
            type='str',
            required=False,
            default=None
        ),
        http_login_password=dict(
            type='str',
            required=False,
            default=None,
            no_log=True
        ),
        timeout=dict(
            type='int'
        ),
        validate_certs=dict(
            type='bool',
            required=False,
            fallback=(env_fallback, ['ZABBIX_VALIDATE_CERTS'])
        ),
    )


def helper_cleanup_data(obj):
    """
    Removes the None values from the object and returns the object
    Args:
        obj: object to cleanup

    Returns:
       object: cleaned object
    """
    if isinstance(obj, (list, tuple, set)):
        return type(obj)(helper_cleanup_data(x) for x in obj if x is not None)
    elif isinstance(obj, dict):
        return type(obj)((helper_cleanup_data(k), helper_cleanup_data(v))
                         for k, v in obj.items() if k is not None and v is not None)
    else:
        return obj


def helper_to_numeric_value(elements, value):
    """Converts string values to integers

    Parameters:
        elements: list of elements to enumerate
        value: string value

    Returns:
        int: converted integer
    """
    if value is None:
        return None
    for index, element in enumerate(elements):
        if isinstance(element, str) and element.lower() == value.lower():
            return index
        if isinstance(element, list):
            for deep_element in element:
                if isinstance(deep_element, str) and deep_element.lower() == value.lower():
                    return index


def helper_convert_unicode_to_str(data):
    """Converts unicode objects to strings in dictionary

    Parameters:
        data: unicode object

    Returns:
        dict: strings in dictionary
    """
    if isinstance(data, dict):
        return dict(map(helper_convert_unicode_to_str, data.items()))
    elif isinstance(data, (list, tuple, set)):
        return type(data)(map(helper_convert_unicode_to_str, data))
    elif data is None:
        return data
    else:
        return str(data)


def helper_compare_lists(l1, l2, diff_dict):
    """
    Compares l1 and l2 lists and adds the items that are different
    to the diff_dict dictionary.
    Used in recursion with helper_compare_dictionaries() function.

    Parameters:
        l1: first list to compare
        l2: second list to compare
        diff_dict: dictionary to store the difference

    Returns:
        dict: items that are different
    """
    if len(l1) != len(l2):
        diff_dict.append(l1)
        return diff_dict
    for i, item in enumerate(l1):
        if isinstance(item, dict):
            for item2 in l2:
                diff_dict2 = {}
                diff_dict2 = helper_compare_dictionaries(item, item2, diff_dict2)
                if len(diff_dict2) == 0:
                    break
            if len(diff_dict2) != 0:
                diff_dict.insert(i, item)
        else:
            if item != l2[i]:
                diff_dict.append(item)
    while {} in diff_dict:
        diff_dict.remove({})
    return diff_dict


def helper_compare_dictionaries(d1, d2, diff_dict):
    """
    Compares d1 and d2 dictionaries and adds the items that are different
    to the diff_dict dictionary.
    Used in recursion with helper_compare_lists() function.

    Parameters:
        d1: first dictionary to compare
        d2: second dictionary to compare
        diff_dict: dictionary to store the difference

    Returns:
        dict: items that are different
    """
    for k, v in d1.items():
        if k not in d2:
            diff_dict[k] = v
            continue
        if isinstance(v, dict):
            diff_dict[k] = {}
            helper_compare_dictionaries(v, d2[k], diff_dict[k])
            if diff_dict[k] == {}:
                del diff_dict[k]
            else:
                diff_dict[k] = v
        elif isinstance(v, list):
            diff_dict[k] = []
            helper_compare_lists(v, d2[k], diff_dict[k])
            if diff_dict[k] == []:
                del diff_dict[k]
            else:
                diff_dict[k] = v
        else:
            if v != d2[k]:
                diff_dict[k] = v
    return diff_dict


def helper_normalize_data(data, del_keys=None):
    """
    Delete None parameter or specified keys from data.

    Parameters:
        data: dictionary

    Returns:
        data: falsene parameter removed data
        del_keys: deleted keys
    """
    if del_keys is None:
        del_keys = []

    for key, value in data.items():
        if value is None:
            del_keys.append(key)

    for key in del_keys:
        if key in data.keys():
            del data[key]

    return data, del_keys

Anon7 - 2022
AnonSec Team