Esempio n. 1
0
def wait_for_futures(futures, raise_on_error=True, log=None):
    '''Collect results or failures from a list of running future tasks.'''

    results = []
    retries = []

    # Check on each result as its thread finishes
    for completed in concurrent.futures.as_completed(futures):
        try:
            result = completed.result()
            # We have to do this here because munch_response doesn't
            # get called on async job results
            exc.raise_from_response(result)
            results.append(result)
        except (keystoneauth1.exceptions.RetriableConnectionFailure,
                exc.OpenStackCloudException) as e:
            if log:
                log.debug(
                    "Exception processing async task: {e}".format(e=str(e)),
                    exc_info=True)
            # If we get an exception, put the result into a list so we
            # can try again
            if raise_on_error:
                raise
            else:
                retries.append(result)
    return results, retries
Esempio n. 2
0
def wait_for_futures(futures, raise_on_error=True, log=None):
    '''Collect results or failures from a list of running future tasks.'''

    results = []
    retries = []

    # Check on each result as its thread finishes
    for completed in concurrent.futures.as_completed(futures):
        try:
            result = completed.result()
            # We have to do this here because munch_response doesn't
            # get called on async job results
            exc.raise_from_response(result)
            results.append(result)
        except (keystoneauth1.exceptions.RetriableConnectionFailure,
                exc.OpenStackCloudException) as e:
            if log:
                log.debug(
                    "Exception processing async task: {e}".format(
                        e=str(e)),
                    exc_info=True)
            # If we get an exception, put the result into a list so we
            # can try again
            if raise_on_error:
                raise
            else:
                retries.append(result)
    return results, retries
Esempio n. 3
0
    def _munch_response(self, response, result_key=None, error_message=None):
        exc.raise_from_response(response, error_message=error_message)

        if not response.content:
            # This doens't have any content
            return self._log_request_id(response)

        # Some REST calls do not return json content. Don't decode it.
        if 'application/json' not in response.headers.get('Content-Type'):
            return self._log_request_id(response)

        try:
            result_json = response.json()
        except Exception:
            return self._log_request_id(response)

        # Note(rods): this is just a temporary step needed until we
        # don't update all the other REST API calls
        if isinstance(result_json, dict):
            for key in ['volumes', 'volume', 'volumeAttachment', 'backups',
                        'volume_types', 'volume_type_access', 'snapshots',
                        'network', 'networks', 'subnet', 'subnets',
                        'router', 'routers', 'floatingip', 'floatingips',
                        'floating_ip', 'floating_ips', 'port', 'ports']:
                if key in result_json.keys():
                    self._log_request_id(response)
                    return result_json

        if isinstance(result_json, list):
            self._log_request_id(response)
            return meta.obj_list_to_munch(result_json)

        result = None
        if isinstance(result_json, dict):
            # Wrap the keys() call in list() because in python3 keys returns
            # a "dict_keys" iterator-like object rather than a list
            json_keys = list(result_json.keys())
            if len(json_keys) > 1 and result_key:
                result = result_json[result_key]
            elif len(json_keys) == 1:
                result = result_json[json_keys[0]]
        if result is None:
            # Passthrough the whole body - sometimes (hi glance) things
            # come through without a top-level container. Also, sometimes
            # you need to deal with pagination
            result = result_json

        self._log_request_id(response, result)

        if isinstance(result, list):
            return meta.obj_list_to_munch(result)
        elif isinstance(result, dict):
            return meta.obj_to_munch(result)
        return result
Esempio n. 4
0
    def _munch_response(self, response, result_key=None, error_message=None):
        exc.raise_from_response(response, error_message=error_message)

        if not response.content:
            # This doens't have any content
            return self._log_request_id(response)

        # Some REST calls do not return json content. Don't decode it.
        if 'application/json' not in response.headers.get('Content-Type'):
            return self._log_request_id(response)

        try:
            result_json = response.json()
            self._log_request_id(response, result_json)
        except Exception:
            return self._log_request_id(response)
        return result_json
Esempio n. 5
0
    def _munch_response(self, response, result_key=None):
        exc.raise_from_response(response)

        if not response.content:
            # This doens't have any content
            return self._log_request_id(response)

        # Some REST calls do not return json content. Don't decode it.
        if 'application/json' not in response.headers.get('Content-Type'):
            return self._log_request_id(response)

        try:
            result_json = response.json()
        except Exception:
            return self._log_request_id(response)

        if isinstance(result_json, list):
            self._log_request_id(response)
            return meta.obj_list_to_dict(result_json)

        result = None
        if isinstance(result_json, dict):
            # Wrap the keys() call in list() because in python3 keys returns
            # a "dict_keys" iterator-like object rather than a list
            json_keys = list(result_json.keys())
            if len(json_keys) > 1 and result_key:
                result = result_json[result_key]
            elif len(json_keys) == 1:
                result = result_json[json_keys[0]]
        if result is None:
            # Passthrough the whole body - sometimes (hi glance) things
            # come through without a top-level container. Also, sometimes
            # you need to deal with pagination
            result = result_json

        self._log_request_id(response, result)

        if isinstance(result, list):
            return meta.obj_list_to_dict(result)
        elif isinstance(result, dict):
            return meta.obj_to_dict(result)
        return result
Esempio n. 6
0
    def _munch_response(self, response, result_key=None, error_message=None):
        exc.raise_from_response(response, error_message=error_message)

        if not response.content:
            # This doens't have any content
            return self._log_request_id(response)

        # Some REST calls do not return json content. Don't decode it.
        if 'application/json' not in response.headers.get('Content-Type'):
            return self._log_request_id(response)

        try:
            result_json = response.json()
        except Exception:
            return self._log_request_id(response)

        # Note(rods): this is just a temporary step needed until we
        # don't update all the other REST API calls
        if isinstance(result_json, dict):
            for key in ['volumes', 'volume', 'volumeAttachment', 'backups',
                        'volume_types', 'volume_type_access', 'snapshots',
                        'network', 'networks', 'subnet', 'subnets',
                        'router', 'routers', 'floatingip', 'floatingips',
                        'floating_ip', 'floating_ips', 'port', 'ports',
                        'rule_types', 'policy', 'policies',
                        'stack', 'stacks', 'zones', 'events',
                        'security_group', 'security_groups',
                        'security_group_rule', 'security_group_rules',
                        'users', 'user', 'projects', 'tenants',
                        'project', 'tenant', 'servers', 'server',
                        'flavor', 'flavors', 'baymodels', 'aggregate',
                        'aggregates', 'availabilityZoneInfo',
                        'flavor_access', 'output', 'server_groups', 'domain',
                        'domains']:
                if key in result_json.keys():
                    self._log_request_id(response)
                    return result_json

        if isinstance(result_json, list):
            self._log_request_id(response)
            return meta.obj_list_to_munch(result_json)

        result = None
        if isinstance(result_json, dict):
            # Wrap the keys() call in list() because in python3 keys returns
            # a "dict_keys" iterator-like object rather than a list
            json_keys = list(result_json.keys())
            if len(json_keys) > 1 and result_key:
                result = result_json[result_key]
            elif len(json_keys) == 1:
                result = result_json[json_keys[0]]
        if result is None:
            # Passthrough the whole body - sometimes (hi glance) things
            # come through without a top-level container. Also, sometimes
            # you need to deal with pagination
            result = result_json

        self._log_request_id(response, result)

        if isinstance(result, list):
            return meta.obj_list_to_munch(result)
        elif isinstance(result, dict):
            return meta.obj_to_munch(result)
        return result