浏览代码

HUE-8758 [connectors] Prepare the DB model

Romain 6 年之前
父节点
当前提交
df8b13ae70

+ 4 - 0
apps/useradmin/src/useradmin/models2.py

@@ -115,6 +115,10 @@ class OrganizationUser(AbstractUser):
     token = models.CharField(_t('token'), max_length=128, default=None, null=True)
     customer_id = models.CharField(_t('Customer id'), max_length=128, default=None, null=True)
     organization = models.ForeignKey(Organization, on_delete=models.CASCADE)
+    # settings = json
+    #   connector_whitelist
+    #   connector_blacklist
+    #   auths: login/password, Google SSO, SAML, LDAP, Github
 
     groups = models.ManyToManyField(
         OrganizationGroup,

+ 1 - 85
desktop/core/src/desktop/lib/connectors/api.py

@@ -20,36 +20,15 @@ import logging
 
 from django.utils.translation import ugettext as _
 
-from desktop.conf import has_connectors, CONNECTORS
 from desktop.lib.django_util import JsonResponse, render
 from desktop.lib.exceptions_renderable import PopupException
+from desktop.lib.connectors.models import AVAILABLE_CONNECTORS, _get_connector_by_id, _get_installed_connectors
 from desktop.lib.connectors.types import CONNECTOR_TYPES, CATEGORIES
 
 
 LOG = logging.getLogger(__name__)
 
 
-def _group_category_connectors(connectors):
-  return [{
-    'category': category['type'],
-    'category_name': category['name'],
-    'description': category['description'],
-    'values': [_connector for _connector in connectors if _connector['category'] == category['type']],
-  } for category in CATEGORIES
-]
-
-AVAILABLE_CONNECTORS = _group_category_connectors(CONNECTOR_TYPES)
-
-
-# TODO: persist in DB
-# TODO: remove installed connectors that don't have a connector or are blacklisted
-# TODO: load back from DB and apply Category properties, e.g. defaults, interface, category, category_name...
-# TODO: connector groups: if we want one type (e.g. Hive) to show-up with multiple computes and the same saved query.
-
-# connector_type: category --> engine, is_sql --> engine_type: sql
-CONNECTOR_INSTANCES = None
-CONNECTOR_IDS = 1
-
 def get_connector_types(request):
   global AVAILABLE_CONNECTORS
   global CATEGORIES
@@ -126,66 +105,3 @@ def delete_connector(request):
     return JsonResponse({})
   else:
     raise PopupException(_('No connector with the name %(name)s found.') % connector)
-
-
-def _get_installed_connectors(category=None, categories=None, dialect=None, interface=None):
-  global CONNECTOR_INSTANCES
-  global CONNECTOR_IDS
-  config_connectors = CONNECTORS.get()
-
-  if CONNECTOR_INSTANCES is None:
-    CONNECTOR_INSTANCES = []
-
-    for i in config_connectors:
-      connector_types = []
-
-      for connector_type in CONNECTOR_TYPES:
-        if connector_type['dialect'] == config_connectors[i].DIALECT.get():
-          connector_types.insert(0, connector_type)
-        elif connector_type.get('interface') == config_connectors[i].INTERFACE.get():
-          connector_types.append(connector_type)
-
-      if not connector_types:
-        LOG.warn('Skipping connector %s as connector dialect %s or interface %s are not installed' % (
-            i, config_connectors[i].DIALECT.get(), config_connectors[i].INTERFACE.get()
-          )
-        )
-      else:
-        connector_type = connector_types[0]
-        connector = {
-          'nice_name': config_connectors[i].NICE_NAME.get() or i,
-          'name': i,
-          'dialect': config_connectors[i].DIALECT.get(),
-          'interface': config_connectors[i].INTERFACE.get(),
-          'settings': config_connectors[i].SETTINGS.get(),
-          'id': CONNECTOR_IDS,
-          'category': connector_type['category'],
-          'description': connector_type['description']
-        }
-        connector.update(connector_type['properties'])
-        CONNECTOR_INSTANCES.append(connector)
-        CONNECTOR_IDS += 1
-
-  connectors = CONNECTOR_INSTANCES
-
-  if categories is not None:
-    connectors = [connector for connector in connectors if connector['category'] in categories]
-  if category is not None:
-    connectors = [connector for connector in connectors if connector['category'] == category]
-  if dialect is not None:
-    connectors = [connector for connector in connectors if connector['dialect'] == dialect]
-  if interface is not None:
-    connectors = [connector for connector in connectors if connector['interface'] == interface]
-
-  return connectors
-
-
-def _get_connector_by_id(id):
-  global CONNECTOR_INSTANCES
-
-  instance = [connector for connector in CONNECTOR_INSTANCES if connector['id'] == id]
-
-  if instance:
-    return instance[0]
-  else:
-    raise PopupException(_('No connector with the id %s found.') % id)

+ 98 - 8
desktop/core/src/desktop/lib/connectors/models.py

@@ -22,19 +22,109 @@ from django.db.models import Q
 from django.db.models.query import QuerySet
 from django.utils.translation import ugettext as _, ugettext_lazy as _t
 
+from desktop.conf import CONNECTORS
+from desktop.lib.connectors.types import CONNECTOR_TYPES, CATEGORIES
 
-# TODO: persistence and migrations
 
-# class Connectors(models.Model):
-#   type = models.CharField(max_length=32, db_index=True, help_text=_t('Type of connector, e.g. hive-tez, '))  # Must be in lib
+LOG = logging.getLogger(__name__)
 
+
+# TODO: persist in DB and migrations
+# TODO: connector groups: if we want one dialect (e.g. hive) to show-up with multiple/transient computes and the same saved query
+
+CONNECTOR_INSTANCES = None
+CONNECTOR_IDS = 1
+
+# class Connector(models.Model):
+  # '''
+  # Instance of a connector pointing to an external service: connection
+  # '''
+#   type = models.CharField(max_length=32, db_index=True, help_text=_t('Type of connector, e.g. hive-tez, '))
 #   name = models.CharField(default='', max_length=255)
 #   description = models.TextField(default='')
-#   uuid = models.CharField(default=uuid_default, max_length=36, db_index=True)
-
-#   category = models.CharField(max_length=32, db_index=True, help_text=_t('Type of connector, e.g. query, browser, catalog...'))
-#   interface = models.CharField(max_length=32, db_index=True, help_text=_t('Type of connector, e.g. hiveserver2'))
+#   is_valid # Must be in lib
 
 #   settings = models.TextField(default='{}')
-
 #   last_modified = models.DateTimeField(auto_now=True, db_index=True, verbose_name=_t('Time last modified'))
+
+#   organization = models.ForeignKey(Organization, on_delete=models.CASCADE)
+
+  # class Meta:
+  #   verbose_name = _t('connector')
+  #   verbose_name_plural = _t('connectors')
+  #   unique_together = ('name', 'organization',)
+
+
+def _group_category_connectors(connectors):
+  return [{
+      'category': category['type'],
+      'category_name': category['name'],
+      'description': category['description'],
+      'values': [_connector for _connector in connectors if _connector['category'] == category['type']],
+    } for category in CATEGORIES
+  ]
+
+AVAILABLE_CONNECTORS = _group_category_connectors(CONNECTOR_TYPES)
+
+
+def _get_installed_connectors(category=None, categories=None, dialect=None, interface=None):
+  global CONNECTOR_INSTANCES
+  global CONNECTOR_IDS
+  config_connectors = CONNECTORS.get()
+
+  if CONNECTOR_INSTANCES is None:
+    CONNECTOR_INSTANCES = []
+
+    for i in config_connectors:
+      connector_types = []
+
+      for connector_type in CONNECTOR_TYPES:
+        if connector_type['dialect'] == config_connectors[i].DIALECT.get():
+          connector_types.insert(0, connector_type)
+        elif connector_type.get('interface') == config_connectors[i].INTERFACE.get():
+          connector_types.append(connector_type)
+
+      if not connector_types:
+        LOG.warn('Skipping connector %s as connector dialect %s or interface %s are not installed' % (
+            i, config_connectors[i].DIALECT.get(), config_connectors[i].INTERFACE.get()
+          )
+        )
+      else:
+        connector_type = connector_types[0]
+        connector = {
+          'nice_name': config_connectors[i].NICE_NAME.get() or i,
+          'name': i,
+          'dialect': config_connectors[i].DIALECT.get(),
+          'interface': config_connectors[i].INTERFACE.get(),
+          'settings': config_connectors[i].SETTINGS.get(),
+          'id': CONNECTOR_IDS,
+          'category': connector_type['category'],
+          'description': connector_type['description']
+        }
+        connector.update(connector_type['properties'])
+        CONNECTOR_INSTANCES.append(connector)
+        CONNECTOR_IDS += 1
+
+  connectors = CONNECTOR_INSTANCES
+
+  if categories is not None:
+    connectors = [connector for connector in connectors if connector['category'] in categories]
+  if category is not None:
+    connectors = [connector for connector in connectors if connector['category'] == category]
+  if dialect is not None:
+    connectors = [connector for connector in connectors if connector['dialect'] == dialect]
+  if interface is not None:
+    connectors = [connector for connector in connectors if connector['interface'] == interface]
+
+  return connectors
+
+
+def _get_connector_by_id(id):
+  global CONNECTOR_INSTANCES
+
+  instance = [connector for connector in CONNECTOR_INSTANCES if connector['id'] == id]
+
+  if instance:
+    return instance[0]
+  else:
+    raise PopupException(_('No connector with the id %s found.') % id)