Exemplo n.º 1
0
def make_sempv1_post_request(solace_config, xml_data):
    headers = {
        'Content-Type': 'application/xml',
        'x-broker-name': solace_config.x_broker
    }
    resp = requests.post(
                solace_config.vmr_url + "/SEMP",
                data=xml_data,
                auth=solace_config.vmr_auth,
                timeout=solace_config.vmr_timeout,
                headers=headers,
                params=None
            )
    if sc.ENABLE_LOGGING:
        sc.log_http_roundtrip(resp)
    if resp.status_code != 200:
        raise AnsibleError("SEMP v1 call not successful. Pls check the log and raise an issue.")
    # SEMP v1 always returns 200 (it seems)
    # error: rpc-reply.execute-result.@code != ok or missing
    # if error: rpc-reply ==> display
    resp_body = xmltodict.parse(resp.text)
    try:
        code = resp_body['rpc-reply']['execute-result']['@code']
    except KeyError:
        return False, resp_body
    if code != "ok":
        return False, resp_body
    return True, resp_body
def _parse_response(solace_config, resp):
    if sc.ENABLE_LOGGING:
        sc.log_http_roundtrip(resp)
    # Solace Cloud API returns 202: accepted if long running request
    if resp.status_code == 202 and is_broker_solace_cloud(solace_config):
        return _wait_solace_cloud_request_completed(solace_config, resp)
    elif resp.status_code != 200:
        return False, parse_bad_response(resp)
    return True, parse_good_response(resp)
Exemplo n.º 3
0
def _parse_response(solace_config, resp):
    if sc.ENABLE_LOGGING:
        sc.log_http_roundtrip(resp)
    # POST: https://api.solace.cloud/api/v0/services: returns 201
    if resp.status_code == 201:
        return True, _parse_good_response(resp)
    if resp.status_code != 200:
        return False, _parse_bad_response(resp)
    return True, _parse_good_response(resp)
def _wait_solace_cloud_request_completed(solace_config, request_resp):
    # GET https://api.solace.cloud/api/v0/services/{paste-your-serviceId-here}/requests/{{requestId}}
    request_resp_body = json.loads(request_resp.text)
    request_id = request_resp_body['data']['id']
    path_array = [
        SOLACE_CLOUD_API_SERVICES_BASE_PATH,
        solace_config.solace_cloud_config['service_id'], 'requests', request_id
    ]
    url = compose_path(path_array)
    auth = BearerAuth(solace_config.solace_cloud_config['api_token'])
    is_completed = False
    try_count = 0
    retries = 12
    delay = 5  # seconds

    while not is_completed and try_count < retries:
        try:
            resp = requests.get(
                url,
                json=None,
                auth=auth,
                timeout=solace_config.vmr_timeout,
                headers={'x-broker-name': solace_config.x_broker},
                params=None)
            if sc.ENABLE_LOGGING:
                sc.log_http_roundtrip(resp)
            if resp.status_code != 200:
                return False, resp
        except (requests.exceptions.ConnectionError,
                requests.exception.Timeout) as e:
            raise AnsibleError(
                "Solace Cloud: GET request status error: {}".format(str(e)))

        if resp.text:
            resp_body = json.loads(resp.text)
            is_completed = (resp_body['data']['adminProgress'] == 'completed')
            if is_completed:
                return True, resp_body
            else:
                ok, err = scu.parse_resp_body_for_errs(resp_body)
                if not ok:
                    return False, err
        else:
            raise AnsibleError(
                "Solace Cloud: GET request status error: no body found in response"
            )
        try_count += 1
        time.sleep(delay)
    # never gets here
    return True, None
Exemplo n.º 5
0
def make_get_request(solace_config, path_array):

    path = su.compose_path(path_array)

    try:
        resp = requests.get(solace_config.vmr_url + path,
                            json=None,
                            auth=solace_config.vmr_auth,
                            timeout=solace_config.vmr_timeout,
                            headers={'x-broker-name': solace_config.x_broker},
                            params=None)
        if sc.ENABLE_LOGGING:
            sc.log_http_roundtrip(resp)
        if resp.status_code != 200:
            return False, su.parse_bad_response(resp)
        return True, su.parse_good_response(resp)

    except (requests.exceptions.ConnectionError,
            requests.exceptions.Timeout) as e:
        return False, str(e)
    def execute_get_list(self, path_array):

        query = self.get_list_default_query_params()
        if query is None:
            query = ''

        query_params = self.module.params['query_params']
        if query_params:
            if ("select" in query_params and query_params['select'] is not None
                    and len(query_params['select']) > 0):
                query += ('&' if query != '' else '')
                query += "select=" + ','.join(query_params['select'])
            if ("where" in query_params and query_params['where'] is not None
                    and len(query_params['where']) > 0):
                where_array = []
                for i, where_elem in enumerate(query_params['where']):
                    where_array.append(where_elem.replace('/', '%2F'))
                query += ('&' if query != '' else '')
                query += "where=" + ','.join(where_array)

        api_path = SEMP_V2_CONFIG
        if self.module.params['api'] == 'monitor':
            api_path = SEMP_V2_MONITOR
        path_array = [api_path] + path_array

        path = compose_path(path_array)

        url = self.solace_config.vmr_url + path + ("?" + query if query
                                                   is not None else '')

        result_list = []

        hasNextPage = True

        while hasNextPage:

            try:
                resp = requests.get(
                    url,
                    json=None,
                    auth=self.solace_config.vmr_auth,
                    timeout=self.solace_config.vmr_timeout,
                    headers={'x-broker-name': self.solace_config.x_broker},
                    params=None)

                if sc.ENABLE_LOGGING:
                    sc.log_http_roundtrip(resp)

                if resp.status_code != 200:
                    return False, parse_bad_response(resp)
                else:
                    body = resp.json()
                    if "data" in body.keys():
                        result_list.extend(body['data'])

            except (requests.exceptions.ConnectionError,
                    requests.exceptions.Timeout) as e:
                return False, str(e)

            if "meta" not in body:
                hasNextPage = False
            elif "paging" not in body["meta"]:
                hasNextPage = False
            elif "nextPageUri" not in body["meta"]["paging"]:
                hasNextPage = False
            else:
                url = body["meta"]["paging"]["nextPageUri"]

        return True, result_list