Sfoglia il codice sorgente

[indexer] Move the alias deletion to the checkbox list

Romain Rigaux 10 anni fa
parent
commit
605e022

+ 17 - 12
desktop/libs/indexer/src/indexer/api.py

@@ -29,6 +29,7 @@ from search.conf import SOLR_URL, SECURITY_ENABLED
 from search.models import Collection
 
 from indexer.controller import CollectionManagerController
+from indexer.controller2 import CollectionController
 from indexer.utils import fields_from_log, field_values_from_separated_file, get_type_from_morphline_type, get_field_types
 
 
@@ -186,7 +187,7 @@ def collections_import(request):
 
   return JsonResponse(response)
 
-
+# Deprecated
 def collections_remove(request):
   if request.method != 'POST':
     raise PopupException(_('POST request required.'))
@@ -286,9 +287,6 @@ def create_or_edit_alias(request):
   alias = request.POST.get('alias', '')
   collections = json.loads(request.POST.get('collections', '[]'))
 
-  if collections:
-    collections = [collection['name'] for collection in collections]
-
   api = SolrApi(SOLR_URL.get(), request.user, SECURITY_ENABLED.get())
 
   try:
@@ -301,21 +299,28 @@ def create_or_edit_alias(request):
   return JsonResponse(response)
 
 
-def delete_alias(request):
+def delete_indexes(request):
   if request.method != 'POST':
     raise PopupException(_('POST request required.'))
 
   response = {'status': -1}
 
-  alias = request.POST.get('alias', '')
+  indexes = json.loads(request.POST.get('indexes', '[]'))
 
-  api = SolrApi(SOLR_URL.get(), request.user, SECURITY_ENABLED.get())
+  if not indexes:
+    response['message'] = _('No indexes to remove.')
+  else:
+    searcher = CollectionController(request.user)
+
+    for index in indexes:
+      if index['type'] == 'collection':
+        searcher.delete_collection(index['name'])
+      elif index['type'] == 'alias':
+        searcher.delete_alias(index['name'])
+      else:
+        LOG.warn('We could not delete: %s' % index)
 
-  try:
-    api.delete_alias(alias)
     response['status'] = 0
-    response['message'] = _('Alias deleted!')
-  except Exception, e:
-    response['message'] = _('Alias could not be deleted: %s') % e
+    response['message'] = _('Indexes removed!')
 
   return JsonResponse(response)

+ 18 - 17
desktop/libs/indexer/src/indexer/controller2.py

@@ -24,6 +24,7 @@ from django.utils.translation import ugettext as _
 from desktop.lib.exceptions_renderable import PopupException
 from libsolr.api import SolrApi
 from libzookeeper.conf import ENSEMBLE
+from libzookeeper.models import ZookeeperClient
 from search.conf import SOLR_URL, SECURITY_ENABLED
 
 from desktop.lib.i18n import smart_str
@@ -152,20 +153,20 @@ class CollectionController(object):
 #        # Delete instance directory if we couldn't create a collection.
 #        shutil.rmtree(instancedir)
 #        raise PopupException(_('Could not create collection. Check error logs for more info.'))
-#
-#  def delete_collection(self, name, core):
-#    if core:
-#      raise PopupException(_('Cannot remove Solr cores.'))
-#
-#    if self.api.remove_collection(name):
-#      # Delete instance directory.
-#      try:
-#        root_node = '%s/%s' % (ZK_SOLR_CONFIG_NAMESPACE, name)
-#        zc = ZookeeperClient(hosts=get_solr_ensemble(), read_only=False)
-#        zc.delete_path(root_node)
-#      except Exception, e:
-#        # Re-create collection so that we don't have an orphan config
-#        self.api.add_collection(name)
-#        raise PopupException(_('Error in deleting Solr configurations.'), detail=e)
-#    else:
-#      raise PopupException(_('Could not remove collection. Check error logs for more info.'))
+
+  def delete_collection(self, name):
+    if self.api.remove_collection(name):
+      # Delete instance directory.
+      try:
+        root_node = '%s/%s' % (ZK_SOLR_CONFIG_NAMESPACE, name)
+        zc = ZookeeperClient(hosts=get_solr_ensemble(), read_only=False)
+        zc.delete_path(root_node)
+      except Exception, e:
+        # Re-create collection so that we don't have an orphan config
+        self.api.add_collection(name)
+        raise PopupException(_('Error in deleting Solr configurations.'), detail=e)
+    else:
+      raise PopupException(_('Could not remove collection. Check error logs for more info.'))
+
+  def delete_alias(self, name):
+    return self.api.delete_alias(name)

+ 3 - 16
desktop/libs/indexer/src/indexer/templates/indexes.mako

@@ -73,9 +73,6 @@ ${ commonheader(_("Solr Indexes"), "spark", user, "60px") | n,unicode }
           <a data-bind="click: $root.alias.edit, visible: type() == 'alias'">
             <i class="fa fa-pencil"></i> ${ _('Edit') }
           </a>
-          <a data-bind="click: $root.alias.delete, visible: type() == 'alias'">
-            <i class="fa fa-times"></i> ${ _('Delete') }
-          </a>
         </td>
       </tr>
     </tbody>
@@ -137,7 +134,7 @@ ${ commonheader(_("Solr Indexes"), "spark", user, "60px") | n,unicode }
 
 
 <div id="deleteIndex" class="modal hide fade">
-  <form id="deleteIndexForm" method="POST" data-bind="submit: delete2">
+  <form id="deleteIndexForm" method="POST" data-bind="submit: deleteIndexes">
     ${ csrf_token(request) | n,unicode }
     <div class="modal-header">
       <a href="#" class="close" data-dismiss="modal">&times;</a>
@@ -185,16 +182,6 @@ ${ commonheader(_("Solr Indexes"), "spark", user, "60px") | n,unicode }
 
       self.showCreateModal(true);
     }
-    
-    self.delete = function(alias) {
-      $.post("${ url('indexer:delete_alias') }", {
-        "alias": alias.name()
-      }, function() {
-        window.location.reload();
-      }).fail(function (xhr, textStatus, errorThrown) {
-        $(document).trigger("error", xhr.responseText);
-      });
-    }
   };
 
   var Editor = function () {
@@ -230,8 +217,8 @@ ${ commonheader(_("Solr Indexes"), "spark", user, "60px") | n,unicode }
 
     self.datatable = null;
 
-    self.delete2 = function() {
-      $.post("${ url('spark:delete') }", {
+    self.deleteIndexes = function() {
+      $.post("${ url('indexer:delete_indexes') }", {
         "indexes": ko.mapping.toJSON(self.selectedJobs)
       }, function() {
         window.location.reload();

+ 1 - 1
desktop/libs/indexer/src/indexer/urls.py

@@ -37,5 +37,5 @@ urlpatterns += patterns('indexer.api',
 
   # V2
   url(r'^api/alias/create_or_edit/$', 'create_or_edit_alias', name='create_or_edit_alias'),
-  url(r'^api/alias/delete/$', 'delete_alias', name='delete_alias')
+  url(r'^api/indexes/delete/$', 'delete_indexes', name='delete_indexes')
 )