Browse Source

HUE-8758 [connector] Use proper query type when saving a query

Romain 5 years ago
parent
commit
d7209ad85e

+ 5 - 1
desktop/libs/notebook/src/notebook/api.py

@@ -444,7 +444,11 @@ def get_logs(request):
   return JsonResponse(response)
   return JsonResponse(response)
 
 
 def _save_notebook(notebook, user):
 def _save_notebook(notebook, user):
-  notebook_type = notebook.get('type', 'notebook')
+  if notebook['snippets'][0].get('connector'):
+    notebook_type = 'query-%(dialect)s' % notebook['snippets'][0]['connector']
+  else:
+    notebook_type = notebook.get('type', 'notebook')
+
   save_as = False
   save_as = False
 
 
   if notebook.get('parentSavedQueryUuid'): # We save into the original saved query, not into the query history
   if notebook.get('parentSavedQueryUuid'): # We save into the original saved query, not into the query history

+ 22 - 4
desktop/libs/notebook/src/notebook/tests.py

@@ -22,13 +22,14 @@ import sys
 
 
 from collections import OrderedDict
 from collections import OrderedDict
 from nose.plugins.attrib import attr
 from nose.plugins.attrib import attr
+from nose.plugins.skip import SkipTest
 from nose.tools import assert_equal, assert_true, assert_false
 from nose.tools import assert_equal, assert_true, assert_false
 
 
 from django.urls import reverse
 from django.urls import reverse
 from azure.conf import is_adls_enabled
 from azure.conf import is_adls_enabled
 
 
 from desktop import appmanager
 from desktop import appmanager
-from desktop.conf import APP_BLACKLIST
+from desktop.conf import APP_BLACKLIST, ENABLE_CONNECTORS
 from desktop.lib.django_test_util import make_logged_in_client
 from desktop.lib.django_test_util import make_logged_in_client
 from desktop.lib.test_utils import grant_access, add_permission
 from desktop.lib.test_utils import grant_access, add_permission
 from desktop.models import Directory, Document, Document2
 from desktop.models import Directory, Document, Document2
@@ -58,9 +59,6 @@ class TestNotebookApi(object):
     self.user = User.objects.get(username="test")
     self.user = User.objects.get(username="test")
     self.user_not_me = User.objects.get(username="not_perm_user")
     self.user_not_me = User.objects.get(username="not_perm_user")
 
 
-    grant_access("test", "default", "notebook")
-    grant_access("not_perm_user", "default", "notebook")
-
     self.notebook_json = """
     self.notebook_json = """
       {
       {
         "selectedSnippet": "hive",
         "selectedSnippet": "hive",
@@ -140,6 +138,26 @@ class TestNotebookApi(object):
     assert_equal(doc.search, "select * from default.web_logs where app = 'metastore';")
     assert_equal(doc.search, "select * from default.web_logs where app = 'metastore';")
 
 
 
 
+  def test_save_notebook_with_connector(self):
+    if not ENABLE_CONNECTORS.get():
+      raise SkipTest
+
+    notebook_cp = self.notebook.copy()
+    notebook_cp.pop('id')
+    notebook_cp['snippets'][0]['connector'] = {
+      "name": "MySql",
+      "dialect": "mysql"
+    }
+    notebook_json = json.dumps(notebook_cp)
+
+    response = self.client.post(reverse('notebook:save_notebook'), {'notebook': notebook_json})
+    data = json.loads(response.content)
+
+    assert_equal(0, data['status'], data)
+    doc = Document2.objects.get(pk=data['id'])
+    assert_equal('query-mysql', doc.type)
+
+
   def test_historify(self):
   def test_historify(self):
     # Starts with no history
     # Starts with no history
     assert_equal(0, Document2.objects.filter(name__contains=self.notebook['name'], is_history=True).count())
     assert_equal(0, Document2.objects.filter(name__contains=self.notebook['name'], is_history=True).count())