コード例 #1
0
def _check_call_report(call_report):
    """Inspect the given call report's ``error`` field.

    If the field is non-null, raise a ``CallReportError``.
    """
    if call_report["error"] is not None:
        raise exceptions.CallReportError(
            "A call report contains an error. Full call report: {}".format(
                call_report))
コード例 #2
0
def task_handler(client, response):
    """Wait for tasks to complete and then collect resources.

    Do the following:

    1. Call :meth:`json_handler` to handle 202 and get call_report.
    2. Raise error if response is not a task.
    3. Re-read the task by its _href to get the final state and metadata.
    4. Return the task's created or updated resource or task final state.

    :raises: ``ValueError`` if the target Pulp application under test is older
        than version 3 or at least version 4.

    Usage examples:

    Create a distribution using meth:`json_handler`::

        client = Client(cfg, api.json_handler)
        spawned_task = client.post(DISTRIBUTION_PATH, body)
        # json_handler returns the task call report not the created entity
        spawned_task == {'task': ...}
        # to have the distribution it is needed to get the task's resources

    Create a distribution using meth:`task_handler`::

        client = Client(cfg, api.task_handler)
        distribution = client.post(DISTRIBUTION_PATH, body)
        # task_handler resolves the created entity and returns its data
        distribution == {'_href': ..., 'base_path': ...}

    Having an existent client it is possible to use the shortcut::

       client.using_handler(api.task_handler).post(DISTRIBUTION_PATH, body)

    """
    check_pulp3_restriction(client)
    # JSON handler takes care of pooling tasks until it is done
    # If task errored then json_handler will raise the error
    response_dict = json_handler(client, response)
    if "task" not in response_dict:
        raise exceptions.CallReportError(
            "Response does not contains a task call_report: {}".format(
                response_dict))

    # Get the final state of the done task
    done_task = client.using_handler(json_handler).get(response_dict["task"])

    if response.request.method == "POST":
        # Task might have created new resources
        if "created_resources" in done_task:
            created = done_task["created_resources"]
            logger.debug("Task created resources: %s", created)
            if len(created) == 1:  # Single resource href
                return client.using_handler(json_handler).get(created[0])
            if len(created) > 1:  # Multiple resource hrefs
                return [
                    client.using_handler(json_handler).get(resource_href)
                    for resource_href in created
                ]
        else:
            return []

    if response.request.method in ["PUT", "PATCH"]:
        # Task might have updated resource so re-read and return it back
        logger.debug("Task updated resource: %s", response.request.url)
        return client.using_handler(json_handler).get(response.request.url)

    # response.request.method is one of ['DELETE', 'GET', 'HEAD', 'OPTION']
    # Returns the final state of the done task
    logger.debug("Task finished: %s", done_task)
    return done_task