@@ -0,0 +1,2969 @@
+# -*- encoding: utf-8 -*-%0A'''%0AFlexible Data Gathering: curl%0A=============================%0A%0AThis fdg module allows for querying against URLs%0A%0ANote that this module doesn't actually shell out to curl. Instead, it uses%0Athe requests library, primarily for performance concerns.%0A%0AAlso note that this module doesn't support chaining from other fdg modules.%0AThis is due to security concerns -- because fdg can collect arbitrary data from%0Aa system, we don't want an attacker to be able to send that data to arbitrary%0Aendpoints.%0A'''%0Afrom __future__ import absolute_import%0Aimport json%0Aimport logging%0Aimport requests%0A%0Afrom salt.exceptions import CommandExecutionError%0A%0Alog = logging.getLogger(__name__)%0A%0A%0Adef request(url,%0A            port=80,%0A            function='GET',%0A            params=None,%0A            data=None,%0A            username=None,%0A            password=None,%0A            timeout=9,%0A            chained=None):%0A    '''%0A    Given a series of arguments, make a request using %60%60requests%60%60.%0A%0A    Note that this function doesn't support chained values (they are thrown%0A    away) due to security concerns.%0A%0A    Returns will be a dict with 'status' (the http status code from the%0A    request) and 'response' (the json response from the server). The status%0A    piece of the fdg return will be based on the http status.%0A%0A    url%0A        The endpoint to which the request will be sent%0A%0A    port%0A        The port to use. 80 by default%0A%0A    function%0A        GET, PUT, or POST%0A%0A    params%0A        Optional. Parameters that will be included in the url.%0A%0A    data%0A        Payload to include for POST/PUT%0A%0A    username%0A        Used for auth%0A%0A    password%0A        Used for auth%0A%0A    timeout%0A        How long to wait for a request%0A%0A    chained%0A        Ignored%0A    '''%0A    if chained:%0A        log.warn('Chained value detected in curl.request module. Chained '%0A                 'values are unsupported in the curl fdg module.')%0A%0A    # Data validation and preparation%0A    kwargs = %7B%7D%0A    kwargs%5B'port'%5D = port%0A    if params is not None:%0A        kwargs%5B'params'%5D = params%0A    if data is not None:%0A        kwargs%5B'data'%5D = data%0A    if username is not None:%0A        kwargs%5B'auth'%5D = (username, password)%0A    kwargs%5B'timeout'%5D = int(timeout)%0A    if function not in ('GET', 'PUT', 'POST'):%0A        log.error('Invalid request type %7B0%7D'.format(function))%0A        return False, %7B%7D%0A%0A    # Make the request%0A    try:%0A        if function == 'GET':%0A            r = requests.get(url, **kwargs)%0A        elif function == 'PUT':%0A            r = requests.put(url, **kwargs)%0A        elif function == 'POST':%0A            r = requests.post(url, **kwargs)%0A    except Exception as e:%0A        return False, str(e)%0A%0A    # Pull out the pieces we want%0A    ret = %7B%7D%0A    ret%5B'status'%5D = r.status_code%0A    ret%5B'response'%5D = r.json%0A%0A    # Status in the return is based on http status%0A    try:%0A        r.raise_for_status()%0A        return True, ret%0A    except requests.exceptions.HTTPError as e:%0A        return False, ret%0A

Add fdg curl module