Skip to content

Commit

Permalink
Add global config option rest_api.profile_switching
Browse files Browse the repository at this point in the history
This global config option is set to `False` by default. When set to
`True`, the REST API will allow requests to specify the profile and it
will switch the loaded profile when necessary. If a request specifies
the profile query parameter and profile switching is turned off, a 400
Bad Request response is returned.
  • Loading branch information
sphuber committed Jul 15, 2022
1 parent 2f87d74 commit 9e95849
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
8 changes: 7 additions & 1 deletion aiida/manage/configuration/schema/config-v9.schema.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "http://json-schema.org/draft-07/schema",
"description": "Schema for AiiDA configuration files, format version 8",
"description": "Schema for AiiDA configuration files, format version 9",
"type": "object",
"definitions": {
"options": {
Expand Down Expand Up @@ -124,6 +124,12 @@
"minimum": 1,
"description": "Maximum number of transport task attempts before a Process is Paused."
},
"rest_api.profile_switching": {
"type": "boolean",
"default": false,
"description": "Toggle whether the profile can be specified in requests submitted to the REST API",
"global_only": true
},
"rmq.task_timeout": {
"type": "integer",
"default": 10,
Expand Down
11 changes: 8 additions & 3 deletions aiida/restapi/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from flask_restful import Resource

from aiida.common.lang import classproperty
from aiida.manage import load_profile
from aiida.manage import get_config_option, load_profile
from aiida.restapi.common.exceptions import RestInputValidationError
from aiida.restapi.common.utils import Utils, close_thread_connection
from aiida.restapi.translator.nodes.node import NodeTranslator
Expand Down Expand Up @@ -153,13 +153,18 @@ def _load_and_verify(self, node_id=None):

return node

def load_profile(self, profile):
def load_profile(self, profile=None):
"""Load the required profile.
This will load the profile specified by the ``profile`` keyword in the query parameters, and if not specified it
will default to the profile defined in the constructor.
"""
load_profile(profile, allow_switch=True)
profile_switching_enabled = get_config_option('rest_api.profile_switching')

if profile is not None and not profile_switching_enabled:
raise RestInputValidationError('specifying an explicit profile is not enabled for this REST API.')

load_profile(profile or self.profile, allow_switch=True)

def get(self, id=None, page=None): # pylint: disable=redefined-builtin,invalid-name,unused-argument
# pylint: disable=too-many-locals
Expand Down
23 changes: 23 additions & 0 deletions docs/source/howto/share_data.rst
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,29 @@ Here are some examples to try::
For an extensive user documentation of the endpoints, the query string as well as the format of the responses, see the :ref:`AiiDA REST API reference <reference:rest-api>`.

.. versionadded:: 2.1.0

It is possible to allow a request to declare a specific profile for which to run the profile.
This makes it possible to use a single REST API to serve the content of all configured profiles.
The profile switching functionality is disabled by default but can be enabled through the config:

.. code-block:: console
verdi config set rest_api.profile_switching True
After the REST API is restarted, it will now accept the `profile` query parameter, for example:

.. code-block:: console
http://127.0.0.1:5000/api/v4/computers?profile=some-profile-name
If the specified is already loaded, the REST API functions exactly as without profile switching enabled.
If another profile is specified, the REST API will first switch profiles before executing the request.

.. note::

If the profile parameter is specified in a request and the REST API does not have profile switching enabled, a 400 response is returned.

.. _how-to:share:serve:deploy:

Deploying a REST API server
Expand Down

0 comments on commit 9e95849

Please sign in to comment.