示例#1
0
def regenerate_bundle():
    """
    Submit a request to regenerate an operator bundle image.

    :rtype: flask.Response
    :raise ValidationError: if required parameters are not supplied
    """
    payload = flask.request.get_json()
    if not isinstance(payload, dict):
        raise ValidationError('The input data must be a JSON object')

    request = RequestRegenerateBundle.from_json(payload)
    db.session.add(request)
    db.session.commit()
    messaging.send_message_for_state_change(request, new_batch_msg=True)

    error_callback = failed_request_callback.s(request.id)
    handle_regenerate_bundle_request.apply_async(
        args=[
            payload['from_bundle_image'],
            payload.get('organization'), request.id
        ],
        link_error=error_callback,
        queue=_get_user_queue(),
    )

    flask.current_app.logger.debug('Successfully scheduled request %d',
                                   request.id)
    return flask.jsonify(request.to_json()), 201
示例#2
0
def regenerate_bundle_batch():
    """
    Submit a batch of requests to regenerate operator bundle images.

    :rtype: flask.Response
    :raise ValidationError: if required parameters are not supplied
    """
    payload = flask.request.get_json()
    Batch.validate_batch_request_params(payload)

    batch = Batch(annotations=payload.get('annotations'))
    db.session.add(batch)

    requests = []
    # Iterate through all the build requests and verify that the requests are valid before
    # committing them and scheduling the tasks
    for build_request in payload['build_requests']:
        try:
            request = RequestRegenerateBundle.from_json(build_request, batch)
        except ValidationError as e:
            # Rollback the transaction if any of the build requests are invalid
            db.session.rollback()
            raise ValidationError(
                f'{str(e).rstrip(".")}. This occurred on the build request in '
                f'index {payload["build_requests"].index(build_request)}.')
        db.session.add(request)
        requests.append(request)

    db.session.commit()
    messaging.send_messages_for_new_batch_of_requests(requests)

    request_jsons = []
    # This list will be used for the log message below and avoids the need of having to iterate
    # through the list of requests another time
    request_id_strs = []
    for build_request, request in zip(payload['build_requests'], requests):
        request_jsons.append(request.to_json())
        request_id_strs.append(str(request.id))

        error_callback = failed_request_callback.s(request.id)
        handle_regenerate_bundle_request.apply_async(
            args=[
                build_request['from_bundle_image'],
                build_request.get('organization'),
                request.id,
            ],
            link_error=error_callback,
            queue=_get_user_queue(),
        )

    flask.current_app.logger.debug(
        'Successfully scheduled the batch %d with requests: %s',
        batch.id,
        ', '.join(request_id_strs),
    )
    return flask.jsonify(request_jsons), 201