Prechádzať zdrojové kódy

[api] Add new check_config API endpoint to validate Hue configuration (#4018)

Harsh Gupta 9 mesiacov pred
rodič
commit
4d008df9ca

+ 12 - 1
desktop/core/src/desktop/api2.py

@@ -71,7 +71,7 @@ from desktop.models import (
   set_user_preferences,
   uuid_default,
 )
-from desktop.views import get_banner_message, serve_403_error
+from desktop.views import _get_config_errors, get_banner_message, serve_403_error
 from filebrowser.conf import (
   CONCURRENT_MAX_CONNECTIONS,
   ENABLE_EXTRACT_UPLOADED_ARCHIVE,
@@ -1478,3 +1478,14 @@ def _setup_search_examples(request):
 
   if data == 'log_analytics_demo':
     search_setup.Command().handle()
+
+
+@api_error_handler
+def check_config(request):
+  """Returns the configuration directory and the list of validation errors."""
+  response = {
+    'hue_config_dir': os.path.realpath(os.getenv("HUE_CONF_DIR", get_desktop_root("conf"))),
+    'config_error_list': _get_config_errors(request, cache=False),
+  }
+
+  return JsonResponse(response)

+ 28 - 2
desktop/core/src/desktop/api2_tests.py

@@ -19,12 +19,12 @@
 import re
 import json
 from builtins import object
-from unittest.mock import MagicMock, Mock, patch
+from unittest.mock import Mock, patch
 
 import pytest
 
 from beeswax.conf import HIVE_SERVER_HOST
-from desktop.api2 import _setup_hive_impala_examples, _setup_notebook_examples, _setup_search_examples, install_app_examples
+from desktop.api2 import _setup_hive_impala_examples, _setup_notebook_examples, _setup_search_examples, check_config, install_app_examples
 from desktop.conf import ENABLE_GIST_PREVIEW
 from desktop.lib.django_test_util import make_logged_in_client
 from desktop.models import Directory, Document2
@@ -848,3 +848,29 @@ class TestInstallAppExampleAPI:
 
         mock_indexer_setup_command.assert_called_once_with(data='twitter_demo')
         assert not mock_search_setup_command.called
+
+
+class TestCheckConfigAPI:
+  def test_check_config_success(self):
+    with patch('desktop.api2.os.path.realpath') as mock_hue_conf_dir:
+      with patch('desktop.api2._get_config_errors') as mock_get_config_errors:
+        request = Mock(method='POST')
+        mock_hue_conf_dir.return_value = '/test/hue/conf'
+        mock_get_config_errors.return_value = [
+          {"name": "Hive", "message": "The application won't work without a running HiveServer2."},
+          {"name": "Impala", "message": "No available Impalad to send queries to."},
+          {"name": "Spark", "message": "The app won't work without a running Livy Spark Server"},
+        ]
+
+        response = check_config(request)
+        response_data = json.loads(response.content)
+
+        assert response.status_code == 200
+        assert response_data == {
+          "hue_config_dir": "/test/hue/conf",
+          "config_error_list": [
+            {"name": "Hive", "message": "The application won't work without a running HiveServer2."},
+            {"name": "Impala", "message": "No available Impalad to send queries to."},
+            {"name": "Spark", "message": "The app won't work without a running Livy Spark Server"},
+          ],
+        }

+ 6 - 0
desktop/core/src/desktop/api_public.py

@@ -71,6 +71,12 @@ def download_hue_logs(request):
   return logs_api.download_hue_logs(django_request)
 
 
+@api_view(["POST"])
+def check_config(request):
+  django_request = get_django_request(request)
+  return desktop_api.check_config(django_request)
+
+
 @api_view(["POST"])
 def install_app_examples(request):
   django_request = get_django_request(request)

+ 1 - 0
desktop/core/src/desktop/api_public_urls_v1.py

@@ -35,6 +35,7 @@ urlpatterns += [
   re_path(r'^logs/download/?$', api_public.download_hue_logs, name='core_download_hue_logs'),
   re_path(r'^install_app_examples/?$', api_public.install_app_examples, name='core_install_app_examples'),
   re_path(r'^get_config/?$', api_public.get_config),
+  re_path(r'^check_config/?$', api_public.check_config, name='core_check_config'),
   re_path(r'^get_namespaces/(?P<interface>[\w\-]+)/?$', api_public.get_context_namespaces),  # To remove
 ]