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 delete_run(run_id):
    """Delete the run with the given identifier. The user has to be a member of
    the run submission in order to be authorized to delete 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).
    from robflask.service import service
    with service(access_token=ACCESS_TOKEN(request)) 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.
        api.runs().delete_run(run_id=run_id)
    return make_response(jsonify(dict()), 204)
Exemplo n.º 3
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.º 4
0
def service_descriptor():
    """Get the API service descriptor."""
    # If the request contains an access token we validate that the token is
    # still active. The access token is optional for the service descriptor.
    # Make sure not to raise an error if no token is present.
    from robflask.service import service
    with service(access_token=ACCESS_TOKEN(request, raise_error=False)) as api:
        return jsonify(api.server().to_dict()), 200
Exemplo n.º 5
0
def list_submission(workflow_id):
    """Get a list of all submissions for a given benchmark. The user has to be
    authenticated in order to be able to access the submission list.
    """
    # Get the access token first to raise an error immediately if no token is
    # present (to avoid unnecessarily instantiating the service API).
    from robflask.service import service
    with service(access_token=ACCESS_TOKEN(request)) as api:
        r = api.groups().list_groups(workflow_id=workflow_id)
    return make_response(jsonify(r), 200)
Exemplo n.º 6
0
def get_submission(group_id):
    """Get handle for the submission with the given identifier. The user has to
    be authenticated in order to access a submission.
    """
    # Get the access token first to raise an error immediately if no token is
    # present (to avoid unnecessarily instantiating the service API).
    from robflask.service import service
    with service(access_token=ACCESS_TOKEN(request)) as api:
        r = api.groups().get_group(group_id=group_id)
    return make_response(jsonify(r), 200)
Exemplo n.º 7
0
def delete_submission(group_id):
    """Delete the submission with the given identifier. The user has to be a
    submission member in order to be authorized to delete the submission.
    """
    # Get the access token first to raise an error immediately if no token is
    # present (to avoid unnecessarily instantiating the service API).
    from robflask.service import service
    with service(access_token=ACCESS_TOKEN(request)) as api:
        api.groups().delete_group(group_id=group_id)
    return make_response(jsonify(dict()), 204)
Exemplo n.º 8
0
def get_benchmark(workflow_id):
    """Get handle for given a benchmark. Benchmarks are available to everyone,
    independent of whether they are currently authenticated or not.
    """
    # Get the access token first. Do not raise raise an error if no token is
    # present.
    from robflask.service import service
    with service(access_token=ACCESS_TOKEN(request, raise_error=False)) as api:
        r = api.workflows().get_workflow(workflow_id=workflow_id)
    return make_response(jsonify(r), 200)
Exemplo n.º 9
0
def delete_file(group_id, file_id):
    """Delete a given file that was perviously uploaded for a submission. The
    user has to be a member of the submission in order to be allowed to delete
    files.
    """
    from robflask.service import service
    with service(access_token=ACCESS_TOKEN(request)) 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.
        api.uploads().delete_file(group_id=group_id, file_id=file_id)
    return make_response(jsonify(dict()), 204)
Exemplo n.º 10
0
def list_runs(group_id):
    """Get a listing of all runs for a given submission. The user has to be a
    submission member in order to be authorized to list the runs.
    """
    # Get the access token first to raise an error immediately if no token is
    # present (to avoid unnecessarily instantiating the service API).
    from robflask.service import service
    with service(access_token=ACCESS_TOKEN(request)) 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().list_runs(group_id=group_id)
    return make_response(jsonify(r), 200)
Exemplo n.º 11
0
def logout_user():
    """Logout user. Expects an access token for the authenticated user that is
    being logged out.

    Returns
    -------
    flask.response_class

    Raises
    ------
    flowserv.error.UnauthenticatedAccessError
    """
    from robflask.service import service
    with service() as api:
        r = api.users().logout_user(api_key=ACCESS_TOKEN(request))
    return make_response(jsonify(r), 200)
Exemplo n.º 12
0
def list_users():
    """Get listing of registered users. Only users that are registered and
    currently logged in are allowed to query the database.

    Returns
    -------
    flask.response_class

    Raises
    ------
    flowserv.error.UnauthenticatedAccessError
    """
    # Get the access token first to raise an error immediately if no token is
    # present (to avoid unnecessarily instantiating the service API).
    from robflask.service import service
    with service(access_token=ACCESS_TOKEN(request)) as api:
        r = api.users().list_users(query=request.args.get('query'))
    return make_response(jsonify(r), 200)
Exemplo n.º 13
0
def whoami_user():
    """Get information about user that is associated with the provided access
    token.

    Returns
    -------
    flask.response_class

    Raises
    ------
    flowserv.error.UnauthenticatedAccessError
    """
    # 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)
    from robflask.service import service
    with service() as api:
        r = api.users().whoami_user(api_key=token)
    return make_response(jsonify(r), 200)
Exemplo n.º 14
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.º 15
0
def update_submission(group_id):
    """Update the submission with the given identifier. The request body can
    contain a modified submission name and/or a modified list of submission
    members.
    """
    # 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 an
    # optional submission name and/or a list of member identifier.
    obj = jsonbody(request,
                   mandatory=[],
                   optional=[labels.GROUP_NAME, labels.GROUP_MEMBERS])
    name = obj.get(labels.GROUP_NAME)
    members = obj.get(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.
        r = api.groups().update_group(group_id=group_id,
                                      name=name,
                                      members=members)
    return make_response(jsonify(r), 200)
Exemplo n.º 16
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)