diff --git a/aiida/manage/configuration/schema/config-v9.schema.json b/aiida/manage/configuration/schema/config-v9.schema.json index 9ba239d267..b9fb971428 100644 --- a/aiida/manage/configuration/schema/config-v9.schema.json +++ b/aiida/manage/configuration/schema/config-v9.schema.json @@ -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": { @@ -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, diff --git a/aiida/restapi/resources.py b/aiida/restapi/resources.py index c99a64975e..579f47ec3a 100644 --- a/aiida/restapi/resources.py +++ b/aiida/restapi/resources.py @@ -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 @@ -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 diff --git a/docs/source/howto/share_data.rst b/docs/source/howto/share_data.rst index f707c50165..1a5314688c 100644 --- a/docs/source/howto/share_data.rst +++ b/docs/source/howto/share_data.rst @@ -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 `. +.. 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