Skip to content

Commit

Permalink
feat: add api MFE configuration (#745)
Browse files Browse the repository at this point in the history
* Merge pull request #30473 from eduNEXT/mfmz/mfe-config-api

feat: add mfe config api

* feat!: change names of dynamic MFE config Django settings

Formerly, the settings were:

* `MFE_CONFIG` for common config.
* `MFE_CONFIG_<APP_ID>` for app-specific overrides,
  with each app getting its own Django setting.

This commit changes it to:

* `MFE_CONFIG` for common config (unchanged)
* `MFE_CONFIG_OVERRIDES` for app-specific overrides,
  where each app gets a top-level key in the dictionary.

Why the change?

* We want common.py to have a complete list of overridable settings, as
  it helps operators reason about configuration and allows us to generate
  config documentation using toggle annotations. Dynamically generating
  setting names based on arbitrary APP_IDs makes this impossible.
* getattr(...) generally makes code more complicated bug prone. Tools
  like pylint and mypy cannot effectively analyze any code that uses
  dynamic attribute access.

* docs: add more detail to MFE Config API documentation

No functional changes here. This just uses the edx_api_doc_tools package
to add some additional documentation to the new API. The documentation
can be read from the code, or viewed by visiting
http://<LMS_ROOT>/api-docs and searching for "mfe_config".

* feat!: change /api/v1/mfe_config to /api/mfe_config/v1

* This changes the API's path. The reasoning is that this is Version 1 of
  the mfe_config API, not Version 1 of the LMS's entire API, so the v1
  should come after mfe_config.
* Why does this matter? Firstly, consistency. Secondly, it affects our
  generated API documentation. If you visited
  https://courses.edx.org/api-docs, you could see that the API was
  listed under "v1" instead of "mfe_config".

---------

Co-authored-by: Felipe Montoya <felipe.montoya@edunext.co>
Co-authored-by: Kyle McCormick <kdmc@pm.me>
  • Loading branch information
3 people authored Jun 17, 2023
1 parent 7e37b5a commit a91a5a8
Show file tree
Hide file tree
Showing 11 changed files with 405 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/pylint-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- module-name: lms-1
path: "lms/djangoapps/badges/ lms/djangoapps/branding/ lms/djangoapps/bulk_email/ lms/djangoapps/bulk_enroll/ lms/djangoapps/bulk_user_retirement/ lms/djangoapps/ccx/ lms/djangoapps/certificates/ lms/djangoapps/commerce/ lms/djangoapps/course_api/ lms/djangoapps/course_blocks/ lms/djangoapps/course_home_api/ lms/djangoapps/course_wiki/ lms/djangoapps/coursewarehistoryextended/ lms/djangoapps/debug/ lms/djangoapps/courseware/ lms/djangoapps/course_goals/ lms/djangoapps/rss_proxy/ lms/djangoapps/save_for_later/"
- module-name: lms-2
path: "lms/djangoapps/gating/ lms/djangoapps/grades/ lms/djangoapps/instructor/ lms/djangoapps/instructor_analytics/ lms/djangoapps/discussion/ lms/djangoapps/edxnotes/ lms/djangoapps/email_marketing/ lms/djangoapps/experiments/ lms/djangoapps/instructor_task/ lms/djangoapps/learner_dashboard/ lms/djangoapps/lms_initialization/ lms/djangoapps/lms_xblock/ lms/djangoapps/lti_provider/ lms/djangoapps/mailing/ lms/djangoapps/mobile_api/ lms/djangoapps/monitoring/ lms/djangoapps/ora_staff_grader/ lms/djangoapps/program_enrollments/ lms/djangoapps/rss_proxy lms/djangoapps/static_template_view/ lms/djangoapps/staticbook/ lms/djangoapps/support/ lms/djangoapps/survey/ lms/djangoapps/teams/ lms/djangoapps/tests/ lms/djangoapps/user_tours/ lms/djangoapps/verify_student/ lms/envs/ lms/lib/ lms/tests.py"
path: "lms/djangoapps/gating/ lms/djangoapps/grades/ lms/djangoapps/instructor/ lms/djangoapps/instructor_analytics/ lms/djangoapps/discussion/ lms/djangoapps/edxnotes/ lms/djangoapps/email_marketing/ lms/djangoapps/experiments/ lms/djangoapps/instructor_task/ lms/djangoapps/learner_dashboard/ lms/djangoapps/lms_initialization/ lms/djangoapps/lms_xblock/ lms/djangoapps/lti_provider/ lms/djangoapps/mailing/ lms/djangoapps/mobile_api/ lms/djangoapps/monitoring/ lms/djangoapps/ora_staff_grader/ lms/djangoapps/program_enrollments/ lms/djangoapps/rss_proxy lms/djangoapps/static_template_view/ lms/djangoapps/staticbook/ lms/djangoapps/support/ lms/djangoapps/survey/ lms/djangoapps/teams/ lms/djangoapps/tests/ lms/djangoapps/user_tours/ lms/djangoapps/verify_student/ lms/djangoapps/mfe_config_api/ lms/envs/ lms/lib/ lms/tests.py"
- module-name: openedx-1
path: "openedx/core/types/ openedx/core/djangoapps/ace_common/ openedx/core/djangoapps/agreements/ openedx/core/djangoapps/api_admin/ openedx/core/djangoapps/auth_exchange/ openedx/core/djangoapps/bookmarks/ openedx/core/djangoapps/cache_toolbox/ openedx/core/djangoapps/catalog/ openedx/core/djangoapps/ccxcon/ openedx/core/djangoapps/commerce/ openedx/core/djangoapps/common_initialization/ openedx/core/djangoapps/common_views/ openedx/core/djangoapps/config_model_utils/ openedx/core/djangoapps/content/ openedx/core/djangoapps/content_libraries/ openedx/core/djangoapps/contentserver/ openedx/core/djangoapps/cookie_metadata/ openedx/core/djangoapps/cors_csrf/ openedx/core/djangoapps/course_apps/ openedx/core/djangoapps/course_date_signals/ openedx/core/djangoapps/course_groups/ openedx/core/djangoapps/courseware_api/ openedx/core/djangoapps/crawlers/ openedx/core/djangoapps/credentials/ openedx/core/djangoapps/credit/ openedx/core/djangoapps/dark_lang/ openedx/core/djangoapps/debug/ openedx/core/djangoapps/demographics/ openedx/core/djangoapps/discussions/ openedx/core/djangoapps/django_comment_common/ openedx/core/djangoapps/embargo/ openedx/core/djangoapps/enrollments/ openedx/core/djangoapps/external_user_ids/ openedx/core/djangoapps/zendesk_proxy/ openedx/core/djangolib/ openedx/core/lib/ openedx/core/tests/ openedx/core/djangoapps/course_live/"
- module-name: openedx-2
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/unit-test-shards.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"lms/djangoapps/tests/",
"lms/djangoapps/user_tours/",
"lms/djangoapps/verify_student/",
"lms/djangoapps/mfe_config_api/",
"lms/envs/",
"lms/lib/",
"lms/tests.py"
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
0001 MFE CONFIG API
####################

Status
******

Accepted

Context
*******

Currently, MFE settings are set via command line environment variables or an .env file that is read during the build process, causing the operators to rebuild mfes each time when any variables are changed. The creation of the ``mfe_config_api`` allows configuration at runtime and avoids rebuilds.
`MFE Configuration during Runtime`_.

Decision
********

- A lightweight API will be created that returns the mfe configuration variables from the site configuration or django settings. `PR Discussion about django settings`_
- The API will be enabled or disabled using the setting ``ENABLE_MFE_CONFIG_API``.
- The API will take the mfe configuration in the ``MFE_CONFIG`` keyset in the site configuration (admin > site configuration > your domain) or in django settings.
- This API allows to consult the configurations by specific MFE. Making a request like ``/api/mfe_config/v1?mfe=mymfe`` will return the configuration defined in ``MFE_CONFIG_OVERRIDES["mymfe"]`` merged with the ``MFE_CONFIG`` configuration.
- The API will have a mechanism to cache the response with ``MFE_CONFIG_API_CACHE_TIMEOUT`` variable.
- The API will live in lms/djangoapps because this is not something Studio needs to serve and it is a lightweight API. `PR Discussion`_
- The API will not require authentication or authorization.
- The API request and response will be like:

Request::

GET http://lms.base.com/api/mfe_config/v1?mfe=learning

Response::

{
"BASE_URL": "https://name_of_mfe.example.com",
"LANGUAGE_PREFERENCE_COOKIE_NAME": "example-language-preference",
"CREDENTIALS_BASE_URL": "https://credentials.example.com",
"DISCOVERY_API_BASE_URL": "https://discovery.example.com",
"LMS_BASE_URL": "https://courses.example.com",
"LOGIN_URL": "https://courses.example.com/login",
"LOGOUT_URL": "https://courses.example.com/logout",
"STUDIO_BASE_URL": "https://studio.example.com",
"LOGO_URL": "https://courses.example.com/logo.png"

}

Consequences
************

- We have to change all the mfes so that they take the information from the API. `Issue MFE runtime configuration in frontend-wg`_
- Initialize the MFE could have a delay due to the HTTP method.
- `Site configuration is going to be deprecated`_ so later we have to clean the code that uses site configuration.
- The operator is responsible for configuring the settings in site configuration or django settings.
- We can have duplicate keys in site configuration (example: we can have a logo definition for each mfe).
- If the request is made from a domain that does not have a site configuration, it returns django settings.

Rejected Alternatives
**********************

- It was not made as a plugin or IDA because it is a lightweight implementation `PR Discussion`_

References
**********

.. _MFE Configuration during Runtime: https://docs.google.com/document/d/1-FHIQmyeQZu3311x8eYUNMru4JX7Yb3UlqjmJxvM8do/edit?usp=sharing

.. _PR Discussion: https://github.com/openedx/edx-platform/pull/30473#issuecomment-1146176151

.. _Site configuration is going to be deprecated: https://github.com/openedx/platform-roadmap/issues/21

.. _Issue MFE runtime configuration in frontend-wg: https://github.com/openedx/frontend-wg/issues/103

.. _PR Discussion about django settings: https://github.com/openedx/edx-platform/pull/30473#discussion_r916263245
Empty file.
164 changes: 164 additions & 0 deletions lms/djangoapps/mfe_config_api/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
"""
Test the use cases of the views of the mfe api.
"""

from unittest.mock import call, patch

import ddt
from django.conf import settings
from django.test import override_settings
from django.urls import reverse
from rest_framework import status
from rest_framework.test import APITestCase


@ddt.ddt
class MFEConfigTestCase(APITestCase):
"""
Test the use case that exposes the site configuration with the mfe api.
"""
def setUp(self):
self.mfe_config_api_url = reverse("mfe_config_api:config")
return super().setUp()

@patch("lms.djangoapps.mfe_config_api.views.configuration_helpers")
def test_get_mfe_config(self, configuration_helpers_mock):
"""Test the get mfe config from site configuration with the mfe api.
Expected result:
- The get_value method of the configuration_helpers in the views is called once with the
parameters ("MFE_CONFIG", settings.MFE_CONFIG)
- The status of the response of the request is a HTTP_200_OK.
- The json of the response of the request is equal to the mocked configuration.
"""
configuration_helpers_mock.get_value.return_value = {"EXAMPLE_VAR": "value"}
response = self.client.get(self.mfe_config_api_url)

configuration_helpers_mock.get_value.assert_called_once_with("MFE_CONFIG", settings.MFE_CONFIG)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.json(), {"EXAMPLE_VAR": "value"})

@patch("lms.djangoapps.mfe_config_api.views.configuration_helpers")
def test_get_mfe_config_with_queryparam(self, configuration_helpers_mock):
"""Test the get mfe config with a query param from site configuration.
Expected result:
- The get_value method of the configuration_helpers in the views is called twice, once with the
parameters ("MFE_CONFIG", settings.MFE_CONFIG)
and once with the parameters ("MFE_CONFIG_OVERRIDES", settings.MFE_CONFIG_OVERRIDES).
- The json of the response is the merge of both mocked configurations.
"""
configuration_helpers_mock.get_value.side_effect = [
{"EXAMPLE_VAR": "value", "OTHER": "other"},
{"mymfe": {"EXAMPLE_VAR": "mymfe_value"}},
]

response = self.client.get(f"{self.mfe_config_api_url}?mfe=mymfe")
self.assertEqual(response.status_code, status.HTTP_200_OK)
calls = [call("MFE_CONFIG", settings.MFE_CONFIG),
call("MFE_CONFIG_OVERRIDES", settings.MFE_CONFIG_OVERRIDES)]
configuration_helpers_mock.get_value.assert_has_calls(calls)
self.assertEqual(response.json(), {"EXAMPLE_VAR": "mymfe_value", "OTHER": "other"})

@ddt.unpack
@ddt.data(
dict(
mfe_config={},
mfe_config_overrides={},
expected_response={},
),
dict(
mfe_config={"EXAMPLE_VAR": "value"},
mfe_config_overrides={},
expected_response={"EXAMPLE_VAR": "value"},
),
dict(
mfe_config={},
mfe_config_overrides={"mymfe": {"EXAMPLE_VAR": "mymfe_value"}},
expected_response={"EXAMPLE_VAR": "mymfe_value"},
),
dict(
mfe_config={"EXAMPLE_VAR": "value"},
mfe_config_overrides={"mymfe": {"EXAMPLE_VAR": "mymfe_value"}},
expected_response={"EXAMPLE_VAR": "mymfe_value"},
),
dict(
mfe_config={"EXAMPLE_VAR": "value", "OTHER": "other"},
mfe_config_overrides={"mymfe": {"EXAMPLE_VAR": "mymfe_value"}},
expected_response={"EXAMPLE_VAR": "mymfe_value", "OTHER": "other"},
),
dict(
mfe_config={"EXAMPLE_VAR": "value"},
mfe_config_overrides={"yourmfe": {"EXAMPLE_VAR": "yourmfe_value"}},
expected_response={"EXAMPLE_VAR": "value"},
),
dict(
mfe_config={"EXAMPLE_VAR": "value"},
mfe_config_overrides={
"yourmfe": {"EXAMPLE_VAR": "yourmfe_value"},
"mymfe": {"EXAMPLE_VAR": "mymfe_value"},
},
expected_response={"EXAMPLE_VAR": "mymfe_value"},
),
)
@patch("lms.djangoapps.mfe_config_api.views.configuration_helpers")
def test_get_mfe_config_with_queryparam_multiple_configs(
self,
configuration_helpers_mock,
mfe_config,
mfe_config_overrides,
expected_response,
):
"""Test the get mfe config with a query param and different settings in mfe_config and mfe_config_overrides with
the site configuration to test that the merge of the configurations is done correctly and mymfe config take
precedence.
Expected result:
- The get_value method of the configuration_helpers in the views is called twice, once with the
parameters ("MFE_CONFIG", settings.MFE_CONFIG)
and once with the parameters ("MFE_CONFIG_OVERRIDES", settings.MFE_CONFIG_OVERRIDES).
- The json of the response is the expected_response passed by ddt.data.
"""
configuration_helpers_mock.get_value.side_effect = [mfe_config, mfe_config_overrides]

response = self.client.get(f"{self.mfe_config_api_url}?mfe=mymfe")
self.assertEqual(response.status_code, status.HTTP_200_OK)
calls = [call("MFE_CONFIG", settings.MFE_CONFIG),
call("MFE_CONFIG_OVERRIDES", settings.MFE_CONFIG_OVERRIDES)]
configuration_helpers_mock.get_value.assert_has_calls(calls)
self.assertEqual(response.json(), expected_response)

def test_get_mfe_config_from_django_settings(self):
"""Test that when there is no site configuration, the API takes the django settings.
Expected result:
- The status of the response of the request is a HTTP_200_OK.
- The json response is equal to MFE_CONFIG in lms/envs/test.py"""
response = self.client.get(self.mfe_config_api_url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.json(), settings.MFE_CONFIG)

def test_get_mfe_config_with_queryparam_from_django_settings(self):
"""Test that when there is no site configuration, the API with queryparam takes the django settings.
Expected result:
- The status of the response of the request is a HTTP_200_OK.
- The json response is equal to MFE_CONFIG merged with MFE_CONFIG_OVERRIDES['mymfe']
"""
response = self.client.get(f"{self.mfe_config_api_url}?mfe=mymfe")
self.assertEqual(response.status_code, status.HTTP_200_OK)
expected = {**settings.MFE_CONFIG, **settings.MFE_CONFIG_OVERRIDES["mymfe"]}
self.assertEqual(response.json(), expected)

@patch("lms.djangoapps.mfe_config_api.views.configuration_helpers")
@override_settings(ENABLE_MFE_CONFIG_API=False)
def test_404_get_mfe_config(self, configuration_helpers_mock):
"""Test the 404 not found response from get mfe config.
Expected result:
- The get_value method of configuration_helpers is not called.
- The status of the response of the request is a HTTP_404_NOT_FOUND.
"""
response = self.client.get(self.mfe_config_api_url)
configuration_helpers_mock.get_value.assert_not_called()
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
10 changes: 10 additions & 0 deletions lms/djangoapps/mfe_config_api/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
""" URLs configuration for the mfe api."""

from django.urls import path

from lms.djangoapps.mfe_config_api.views import MFEConfigView

app_name = 'mfe_config_api'
urlpatterns = [
path('', MFEConfigView.as_view(), name='config'),
]
71 changes: 71 additions & 0 deletions lms/djangoapps/mfe_config_api/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
"""
MFE API Views for useful information related to mfes.
"""

import edx_api_doc_tools as apidocs
from django.conf import settings
from django.http import HttpResponseNotFound, JsonResponse
from django.utils.decorators import method_decorator
from django.views.decorators.cache import cache_page
from rest_framework import status
from rest_framework.views import APIView

from openedx.core.djangoapps.site_configuration import helpers as configuration_helpers


class MFEConfigView(APIView):
"""
Provides an API endpoint to get the MFE configuration from settings (or site configuration).
"""

@method_decorator(cache_page(settings.MFE_CONFIG_API_CACHE_TIMEOUT))
@apidocs.schema(
parameters=[
apidocs.query_parameter(
'mfe',
str,
description="Name of an MFE (a.k.a. an APP_ID).",
),
],
)
def get(self, request):
"""
Return the MFE configuration, optionally including MFE-specific overrides.
**Usage**
Get common config:
GET /api/mfe_config/v1
Get app config (common + app-specific overrides):
GET /api/mfe_config/v1?mfe=name_of_mfe
**GET Response Values**
```
{
"BASE_URL": "https://name_of_mfe.example.com",
"LANGUAGE_PREFERENCE_COOKIE_NAME": "example-language-preference",
"CREDENTIALS_BASE_URL": "https://credentials.example.com",
"DISCOVERY_API_BASE_URL": "https://discovery.example.com",
"LMS_BASE_URL": "https://courses.example.com",
"LOGIN_URL": "https://courses.example.com/login",
"LOGOUT_URL": "https://courses.example.com/logout",
"STUDIO_BASE_URL": "https://studio.example.com",
"LOGO_URL": "https://courses.example.com/logo.png",
... and so on
}
```
"""

if not settings.ENABLE_MFE_CONFIG_API:
return HttpResponseNotFound()

mfe_config = configuration_helpers.get_value('MFE_CONFIG', settings.MFE_CONFIG)
if request.query_params.get('mfe'):
mfe = str(request.query_params.get('mfe'))
app_config = configuration_helpers.get_value(
'MFE_CONFIG_OVERRIDES',
settings.MFE_CONFIG_OVERRIDES,
)
mfe_config.update(app_config.get(mfe, {}))
return JsonResponse(mfe_config, status=status.HTTP_200_OK)
Loading

0 comments on commit a91a5a8

Please sign in to comment.