Mini Shell

Direktori : /opt/saltstack/salt/extras-3.10/rads/
Upload File :
Current File : //opt/saltstack/salt/extras-3.10/rads/cpanel_api.py

"""backwards compat for rads.cpanel_api"""

import requests
from .compat import deprecated


@deprecated('the cpapis module')
def api_success(json_response):
    """deprecated"""
    try:
        return int(json_response['status']) == 1
    except (TypeError, KeyError, ValueError):
        pass
    try:
        return int(json_response['metadata']['result']) == 1
    except (TypeError, KeyError, ValueError):
        pass
    try:
        return int(json_response['result']['status']) == 1
    except (TypeError, KeyError, ValueError):
        try:
            return int(json_response['result'][0]['status']) == 1
        except (TypeError, IndexError, KeyError, ValueError):
            pass
    return False


def get_access_hash():
    """deprecated"""
    with open('/root/.accesshash', 'r') as hash_file:
        access_hash = hash_file.read().splitlines()
    return ''.join(access_hash)


@deprecated('cpapis.whmapi1')
def json_api_get(api_function, params=None, version=1, timeout=180):
    """deprecated"""
    return json_api_post(
        api_function, version=version, timeout=timeout, data=params
    )


@deprecated('cpapis.whmapi1')
def json_api_post(api_function, data=None, version=1, timeout=180):
    """deprecated"""
    if data is None:
        data = {'api.version': version}
    elif 'api.version' not in data:
        data['api.version'] = version
    try:
        access_hash = get_access_hash()
    except IOError:
        return None
    try:
        return requests.post(
            'https://127.0.0.1:2087/json-api/%s' % api_function,
            data=data,
            headers={'Authorization': 'WHM root:%s' % access_hash},
            timeout=timeout,
            verify=False,
        ).json()
    except (
        requests.exceptions.RequestException,
        ValueError,  # JSON issues
        TypeError,  # JSON issues
    ):
        return None


@deprecated('cpapis.whmapi1')
def whm_api(
    function,
    version,
    timeout=180,
    whmhost='127.0.0.1',
    access_hash=None,
    _data=None,
    **kwargs,
):
    """deprecated"""
    data = {} if _data is None else _data
    data['api.version'] = version
    for key, val in kwargs.items():
        data[key] = val
    if access_hash is None:
        try:
            access_hash = get_access_hash()
        except IOError:
            return None
    try:
        return requests.post(
            'https://{}:2087/json-api/{}'.format(whmhost, function),
            data=data,
            headers={'Authorization': 'WHM root:%s' % access_hash},
            timeout=timeout,
            verify=False,
        ).json()
    except (
        requests.exceptions.RequestException,
        ValueError,  # JSON issues
        TypeError,  # JSON issues
    ):
        return None


@deprecated('cpapis.cpapi3')
def cpanel_api(
    function, version, module, pos=None, timeout=180, _data=None, **kwargs
):
    """deprecated"""
    data = {} if _data is None else _data
    # required kwargs. All others are supplied inside data{}
    data['cpanel_jsonapi_module'] = module
    data['cpanel_jsonapi_func'] = function
    data['cpanel_jsonapi_apiversion'] = version
    for key, val in kwargs.items():
        data[key] = val

    # positional args are sent in as post variables, in format
    # &arg-0=SOMETHING&arg-1=SOMETHING
    # the pos (pun intended) variable holds them in the order
    # they are sent into the function
    if isinstance(pos, str):
        # even if there is only one arg, it should be a list
        raise ValueError('pos should be a list, received string')
    if isinstance(pos, list):
        for pos_index, pos_arg in enumerate(pos):
            data['arg-%d' % pos_index] = pos_arg
    try:
        access_hash = get_access_hash()
    except IOError:
        return None
    try:
        return requests.post(
            'https://127.0.0.1:2087/json-api/cpanel',
            data=data,
            headers={'Authorization': 'WHM root:%s' % access_hash},
            timeout=timeout,
            verify=False,
        ).json()
    except (
        requests.exceptions.RequestException,
        ValueError,  # JSON issues
        TypeError,  # JSON issues
    ):
        return None

Zerion Mini Shell 1.0