Ver código fonte

HUE-8758 [connectors] Start removing global connector list

Add test to initial connector load
Romain 6 anos atrás
pai
commit
b21bb2bd51

+ 33 - 27
desktop/core/src/desktop/lib/connectors/api.py

@@ -114,31 +114,8 @@ AVAILABLE_CONNECTORS = _group_category_connectors(CONNECTOR_TYPES)
 # TODO: type --> name, type --> SQL language, e.g. mysql
 
 # connector_type: category --> engine, is_sql --> engine_type: sql
-CONNECTOR_INSTANCES = []
-
-if has_connectors():
-  connector_config = CONNECTORS.get()
-
-  for i in connector_config:
-    connector_class = [
-      connector_type
-      for connector_type in CONNECTOR_TYPES
-          if connector_type['dialect'] == connector_config[i].DIALECT.get() and connector_type['interface'] == connector_config[i].INTERFACE.get()
-    ]
-    CONNECTOR_INSTANCES.append({
-        'nice_name': connector_config[i].NICE_NAME.get() or i,
-        'name': i,
-        'dialect': connector_config[i].DIALECT.get(),
-        'interface': connector_config[i].INTERFACE.get(),
-        'settings': connector_config[i].SETTINGS.get(),
-        # From Connector class
-        'is_sql': True,
-        'id': None,
-        'category': connector_class[0]['category'],
-        'description': connector_class[0]['description']
-      }
-    )
-
+CONNECTOR_INSTANCES = None
+CONNECTOR_IDS = 10
 
 def get_connector_classes(request):
   global AVAILABLE_CONNECTORS
@@ -152,7 +129,9 @@ def get_connector_classes(request):
 
 def get_installed_connectors(request):
   return JsonResponse({
-    'connectors': _group_category_connectors(CONNECTOR_INSTANCES),
+    'connectors': _group_category_connectors(
+      _get_installed_connectors()
+    ),
   })
 
 
@@ -169,7 +148,6 @@ def get_connector(request, id):
 
   return JsonResponse(instance)
 
-CONNECTOR_IDS = 10
 
 def update_connector(request):
   global CONNECTOR_IDS
@@ -219,6 +197,34 @@ def delete_connector(request):
     raise PopupException(_('No connector with the name %(name)s found.') % connector)
 
 
+def _get_installed_connectors():
+  global CONNECTOR_INSTANCES
+  connector_config = CONNECTORS.get()
+
+  if CONNECTOR_INSTANCES is None:
+    CONNECTOR_INSTANCES = []
+    for i in connector_config:
+      connector_class = [
+        connector_type
+        for connector_type in CONNECTOR_TYPES
+            if connector_type['dialect'] == connector_config[i].DIALECT.get() and connector_type['interface'] == connector_config[i].INTERFACE.get()
+      ]
+      CONNECTOR_INSTANCES.append({
+          'nice_name': connector_config[i].NICE_NAME.get() or i,
+          'name': i,
+          'dialect': connector_config[i].DIALECT.get(),
+          'interface': connector_config[i].INTERFACE.get(),
+          'settings': connector_config[i].SETTINGS.get(),
+          # From Connector class
+          'is_sql': True,
+          'id': None,
+          'category': connector_class[0]['category'],
+          'description': connector_class[0]['description']
+        }
+      )
+  return CONNECTOR_INSTANCES
+
+
 def _get_connector_by_id(id):
   global CONNECTOR_INSTANCES
 

+ 41 - 0
desktop/core/src/desktop/lib/connectors/tests.py

@@ -0,0 +1,41 @@
+#!/usr/bin/env python
+## -*- coding: utf-8 -*-
+# Licensed to Cloudera, Inc. under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  Cloudera, Inc. licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+from mock import patch, Mock, MagicMock
+from nose.tools import assert_equal, assert_true, assert_false
+
+from desktop.lib.connectors.api import _get_installed_connectors
+
+
+def test_get_installed_editor_connectors():
+
+  with patch('desktop.lib.connectors.api.CONNECTORS.get') as CONNECTORS:
+    CONNECTORS.return_value = {
+      'mysql-1': Mock(
+        NICE_NAME=Mock(get=Mock(return_value='MySql')),
+        DIALECT=Mock(get=Mock(return_value='mysql')),
+        INTERFACE=Mock(get=Mock(return_value='sqlalchemy')),
+        SETTINGS=Mock(get=Mock(return_value=[{"name": "url", "value": "mysql://hue:pwd@hue:3306/hue"}])),
+      )
+    }
+
+    connectors = _get_installed_connectors()
+
+    editor_category = [category for category in connectors if category['category'] == 'editor']
+    assert_true(len(editor_category), connectors)
+    assert_equal(1, len(editor_category), editor_category)

+ 2 - 4
desktop/libs/notebook/src/notebook/conf.py

@@ -47,9 +47,6 @@ def check_permissions(user, interpreter, user_apps=None):
 
 
 def get_ordered_interpreters(user=None):
-  from desktop.lib.connectors.api import CONNECTOR_INSTANCES
-  global CONNECTOR_INSTANCES
-
   if not INTERPRETERS.get():
     _default_interpreters(user)
 
@@ -69,6 +66,7 @@ def get_ordered_interpreters(user=None):
     raise ValueError("Interpreters from interpreters_shown_on_wheel is not in the list of Interpreters %s" % unknown_interpreters)
 
   if has_connectors():
+    from desktop.lib.connectors.api import _get_installed_connectors
     reordered_interpreters = [{
         'name': i['nice_name'],
         'type': i['name'],
@@ -77,7 +75,7 @@ def get_ordered_interpreters(user=None):
         'is_sql': i['is_sql'],
         'interface': i['interface'],
         'options': {setting['name']: setting['value'] for setting in i['settings']}
-      } for i in CONNECTOR_INSTANCES
+      } for i in _get_installed_connectors()
     ]
   else:
     reordered_interpreters = interpreters_shown_on_wheel + [i for i in user_interpreters if i not in interpreters_shown_on_wheel]