Эх сурвалжийг харах

[sentry] Fetch Solr collections

Romain Rigaux 9 жил өмнө
parent
commit
872726e

+ 38 - 1
apps/security/src/security/api/sentry.py

@@ -22,8 +22,10 @@ import time
 from django.utils.translation import ugettext as _
 
 from desktop.lib.django_util import JsonResponse
+from desktop.lib.exceptions_renderable import PopupException
 from beeswax.api import autocomplete
 from hadoop.cluster import get_defaultfs
+from libsolr.api import SolrApi
 
 from libsentry.api2 import get_api
 from libsentry.sentry_site import get_sentry_server_admin_groups
@@ -32,7 +34,16 @@ from libsentry.sentry_site import get_sentry_server_admin_groups
 LOG = logging.getLogger(__name__)
 
 
-def fetch_hive_path(request):
+def fetch_authorizables(request):
+  if request.GET['component'] == 'solr':
+    resp = _fetch_collections(request)
+  elif request.GET['component'] == 'hive':
+    resp = _fetch_hive_path(request)
+
+  return JsonResponse(resp)
+
+
+def _fetch_hive_path(request):
   path = request.GET['path']
 
   database = None
@@ -53,6 +64,32 @@ def fetch_hive_path(request):
   return resp
 
 
+def _fetch_collections(request):
+  from search.conf import SOLR_URL
+
+  path = request.GET['path']
+  item = None
+  name = None
+
+  if path:
+    item = path
+  if '/' in path:
+    item, name = path.split('/')
+
+  api = SolrApi(SOLR_URL.get(), request.user)
+
+  if not item:
+    return {"databases": ["collections", "configs"]}
+  elif item and name:
+    return {"hdfs_link": "/indexer/#edit/%s" % name, "extended_columns": [], "columns": [], "partition_keys": []}
+  elif item == 'collections':
+    return {"tables_meta": [{"comment": None, "type": "Table", "name": col} for col in api.collections2()]}
+  elif item == 'configs':
+    return {"tables_meta": [{"comment": None, "type": "Table", "name": "log_analytics_demo"}, {"comment": None, "type": "Table", "name": "schemalessTemplateSecure"}]}
+  else:
+    raise PopupException(_('Authorizable %s could not be retrieved') % path)
+
+
 def list_sentry_roles_by_group(request):
   result = {'status': -1, 'message': 'Error'}
   component = request.POST['component']

+ 13 - 12
apps/security/src/security/static/security/js/sentry.ko.js

@@ -631,20 +631,20 @@ var Assist = function (vm, initial) {
   self.refreshTree = function (force) {
     self.growingTree(jQuery.extend(true, {}, self.initialGrowingTree));
     // load root first
-    self.fetchHivePath("", function () {
+    self.fetchAuthorizablesPath("", function () {
       Object.keys(self.treeAdditionalData).forEach(function (path) {
         if (path.indexOf(".") == -1 && path != "") {
           if (typeof force == "boolean" && force) {
-            self.fetchHivePath(path);
+            self.fetchAuthorizablesPath(path);
           }
           else {
             if (self.treeAdditionalData[path].loaded) {
-              self.fetchHivePath(path, function () {
+              self.fetchAuthorizablesPath(path, function () {
                 self.updatePathProperty(self.growingTree(), path, "isExpanded", self.treeAdditionalData[path].expanded);
                 var _withTable = false;
                 Object.keys(self.treeAdditionalData).forEach(function (ipath) {
                   if (ipath.split(".").length == 2 && ipath.split(".")[0] == path) {
-                    self.fetchHivePath(ipath, function () {
+                    self.fetchAuthorizablesPath(ipath, function () {
                       _withTable = true;
                       self.updatePathProperty(self.growingTree(), ipath, "isExpanded", self.treeAdditionalData[ipath].expanded);
                       self.loadData(self.growingTree());
@@ -691,7 +691,7 @@ var Assist = function (vm, initial) {
       }
     }
     else {
-      self.fetchHivePath();
+      self.fetchAuthorizablesPath();
     }
   }
 
@@ -703,7 +703,7 @@ var Assist = function (vm, initial) {
     e.preventDefault();
     e.stopPropagation();
     e.stopImmediatePropagation();
-    self.fetchHivePath(obj.path(), function(data){
+    self.fetchAuthorizablesPath(obj.path(), function(data){
       location.href = "/security/hdfs#" + data.hdfs_link.substring("/filebrowser/view=".length);
     });
   }
@@ -767,13 +767,13 @@ var Assist = function (vm, initial) {
   }
 
   self.loadParents = function (callback) {
-    self.fetchHivePath("", function () {
+    self.fetchAuthorizablesPath("", function () {
       self.updatePathProperty(self.growingTree(), "", "isExpanded", true);
       var _crumbs = self.path().split(".");
-      self.fetchHivePath(_crumbs[0], function () {
+      self.fetchAuthorizablesPath(_crumbs[0], function () {
         self.updatePathProperty(self.growingTree(), _crumbs[0], "isExpanded", true);
         if (_crumbs.length > 1) {
-          self.fetchHivePath(_crumbs[0] + "." + _crumbs[1], function () {
+          self.fetchAuthorizablesPath(_crumbs[0] + "." + _crumbs[1], function () {
             self.updatePathProperty(self.growingTree(), _crumbs[0] + "." + _crumbs[1], "isExpanded", true);
             self.loadData(self.growingTree());
             if (typeof callback != "undefined") {
@@ -799,7 +799,7 @@ var Assist = function (vm, initial) {
     });
   }
 
-  self.fetchHivePath = function (optionalPath, loadCallback) {
+  self.fetchAuthorizablesPath = function (optionalPath, loadCallback) {
     var _originalPath = typeof optionalPath != "undefined" ? optionalPath : self.path();
 
     if (_originalPath.split(".").length < 4) {
@@ -807,9 +807,10 @@ var Assist = function (vm, initial) {
 
       var _path = _originalPath.replace('.', '/');
       var request = {
-        url: '/security/api/sentry/fetch_hive_path',
+        url: '/security/api/sentry/fetch_authorizables',
         data: {
           'path': _path,
+          'component': vm.component(),
           'doas': vm.doAs(),
           'isDiffMode': self.isDiffMode()
         },
@@ -1000,7 +1001,7 @@ var HiveViewModel = function (initial) {
       if (path != "") {
         self.assist.loadParents();
       } else {
-        self.assist.fetchHivePath();
+        self.assist.fetchAuthorizablesPath();
       }
     }, 100);
   };

+ 6 - 1
apps/security/src/security/templates/sentry.mako

@@ -147,7 +147,12 @@ ${ layout.menubar(section=component) }
 
       <div id="edit" class="mainSection card card-small">
         <h1 class="card-heading simple">
+          <!-- ko if: component() == 'hive' -->
           ${ _('Database and Table privileges') }
+          <!-- /ko -->
+          <!-- ko if: component() == 'solr' -->
+          ${ _('Collections privileges') }
+          <!-- /ko -->
           <div id="help-content" class="hide">
             ${ _('Check the') } <a href="http://gethue.com/apache-sentry-made-easy-with-the-new-hue-security-app/#howto" target="_blank">${ _('documentation!') }</a>
           </div>
@@ -598,7 +603,7 @@ ${ tree.import_templates(itemClick='$root.assist.setPath', iconClick='$root.assi
         }
         viewModel.assist.path(path);
         viewModel.assist.updatePathProperty(viewModel.assist.growingTree(), path, "isExpanded", true);
-        viewModel.assist.fetchHivePath();
+        viewModel.assist.fetchAuthorizablesPath();
       }
 
       $("#path").jHueHiveAutocomplete({

+ 1 - 1
apps/security/src/security/urls.py

@@ -60,7 +60,7 @@ urlpatterns += patterns('security.api.hive',
 
 # Generic API V2
 urlpatterns += patterns('security.api.sentry',
-  url(r'^api/sentry/fetch_hive_path', 'fetch_hive_path', name='fetch_hive_path'),
+  url(r'^api/sentry/fetch_authorizables', 'fetch_authorizables', name='fetch_authorizables'),
 
   url(r'^api/sentry/list_sentry_roles_by_group', 'list_sentry_roles_by_group', name='list_sentry_roles_by_group'),
   url(r'^api/sentry/list_sentry_privileges_by_role', 'list_sentry_privileges_by_role', name='list_sentry_privileges_by_role'),