Exemplo n.º 1
0
def create_submission(workflow_id):
    """Create a new submission for a given benchmark. The user has to be
    authenticated in order to be able to create a new submission.
    """
    # Get the access token first to raise an error immediately if no token is
    # present (to avoid unnecessarily instantiating the service API).
    token = ACCESS_TOKEN(request)
    # Verify that the request contains a valid Json object that contains the
    # submission name and an optional list of member identifier.
    obj = jsonbody(request,
                   mandatory=[labels.GROUP_NAME],
                   optional=[labels.GROUP_MEMBERS])
    name = obj[labels.GROUP_NAME]
    members = obj.get(labels.GROUP_MEMBERS)
    if members is not None and not isinstance(members, list):
        raise err.InvalidRequestError('{} not a list'.format(
            labels.GROUP_MEMBERS))
    from robflask.service import service
    with service(access_token=token) as api:
        # Authentication of the user from the expected api_token in the header
        # will fail if no token is given or if the user is not logged in.
        try:
            r = api.groups().create_group(workflow_id=workflow_id,
                                          name=name,
                                          members=members)
        except UnknownUserError as ex:
            # Change error type from unknown object to invalid request if a
            # user in the member list is unknown
            raise err.InvalidRequestError(str(ex))
    return make_response(jsonify(r), 201)
Exemplo n.º 2
0
def upload_file(group_id):
    """Upload a new file as part of a given submission. The user has to be a
    member of the submission in order to be allowed to upload files.
    """
    # Get the access token first to raise an error immediately if no token is
    # present (to avoid unnecessarily instantiating the service API).
    token = ACCESS_TOKEN(request)
    # Ensure that the upload request contains a file object
    if request.files and 'file' in request.files:
        file = request.files['file']
        # A browser may submit a empty part without filename
        if file.filename == '':
            raise err.InvalidRequestError('empty file name')
        # Save uploaded file to a bytes buffer.
        filename = secure_filename(file.filename)
        from robflask.service import service
        with service(access_token=token) as api:
            # Authentication of the user from the expected api_token in the
            # header will fail if the user is not logged in.
            r = api.uploads().upload_file(group_id=group_id,
                                          file=FlaskFile(file),
                                          name=filename)
        return make_response(jsonify(r), 201)
    else:
        raise err.InvalidRequestError('no file request')
Exemplo n.º 3
0
def jsonbody(request, mandatory=None, optional=None) -> Dict:
    """Get Json object from the body of an API request. Validates the object
    based on the given (optional) lists of mandatory and optional labels.

    Returns the JSON object (dictionary). Raises an error if an invalid request
    or body is given.

    Parameters
    ----------
    request: flask.request
        HTTP request
    mandatory: list(string)
        List of mandatory labels for the dictionary serialization
    optional: list(string), optional
        List of optional labels for the dictionary serialization

    Returns
    -------
    dict

    Raises
    ------
    robflask.error.InvalidRequest
    """
    try:
        return validate_doc(request.json,
                            mandatory=mandatory,
                            optional=optional)
    except (AttributeError, TypeError, ValueError) as ex:
        raise err.InvalidRequestError(str(ex))
Exemplo n.º 4
0
def start_run(group_id):
    """Start a new run. Expects argument values for each mandatory benchmark
    parameter in the request body. The user has to be a submission member in
    order to be authorized to start new submission runs.
    """
    # Get the access token first to raise an error immediately if no token is
    # present (to avoid unnecessarily instantiating the service API).
    token = ACCESS_TOKEN(request)
    # Verify that the request contains a valid Json object that contains a
    # optional list of workflow arguments.
    obj = jsonbody(request, optional=[labels.RUN_ARGUMENTS])
    args = obj[labels.RUN_ARGUMENTS] if labels.RUN_ARGUMENTS in obj else dict()
    from robflask.service import service
    with service(access_token=token) as api:
        # Authentication of the user from the expected api_token in the header
        # will fail if no token is given or if the user is not logged in.
        try:
            r = api.runs().start_run(group_id=group_id, arguments=args)
        except UnknownParameterError as ex:
            # Convert unknown parameter errors into invalid request errors
            # to avoid sending a 404 response
            raise err.InvalidRequestError(str(ex))
    return make_response(jsonify(r), 201)
Exemplo n.º 5
0
def cancel_run(run_id):
    """Get handle for a given run. The user has to be a member of the run
    submission in order to be authorized to access the run.

    Parameters
    ----------
    run_id: string
        Unique run identifier

    Returns
    -------
    flask.response_class

    Raises
    ------
    flowserv.error.UnauthenticatedAccessError
    flowserv.error.UnauthorizedAccessError
    flowserv.error.UnknownWorkflowGroupError
    """
    # Get the access token first to raise an error immediately if no token is
    # present (to avoid unnecessarily instantiating the service API).
    token = ACCESS_TOKEN(request)
    # If the body contains a Json object verify that the object has the
    # mandatory element 'reason'
    reason = None
    if request.json:
        try:
            obj = util.validate_doc(request.json, mandatory=['reason'])
            reason = obj['reason']
        except ValueError as ex:
            raise err.InvalidRequestError(str(ex))
    from robflask.service import service
    with service(access_token=token) as api:
        # Authentication of the user from the expected api_token in the header
        # will fail if no token is given or if the user is not logged in.
        r = api.runs().cancel_run(run_id=run_id, reason=reason)
    return make_response(jsonify(r), 200)