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

HUE-2869 [indexer] API calls for Managing collection aliases

Romain Rigaux 10 жил өмнө
parent
commit
1a4099b

+ 45 - 2
desktop/libs/indexer/src/indexer/api.py

@@ -24,10 +24,12 @@ from django.utils.translation import ugettext as _
 
 from desktop.lib.django_util import JsonResponse
 from desktop.lib.exceptions_renderable import PopupException
+from libsolr.api import SolrApi
+from search.conf import SOLR_URL, SECURITY_ENABLED
 from search.models import Collection
 
-from controller import CollectionManagerController
-from utils import fields_from_log, field_values_from_separated_file, get_type_from_morphline_type, get_field_types
+from indexer.controller import CollectionManagerController
+from indexer.utils import fields_from_log, field_values_from_separated_file, get_type_from_morphline_type, get_field_types
 
 
 LOG = logging.getLogger(__name__)
@@ -273,3 +275,44 @@ def collections_data(request, collection):
     response['message'] = _('Unsupported source %s') % source
 
   return JsonResponse(response)
+
+
+def create_or_edit_alias(request):
+  if request.method != 'POST':
+    raise PopupException(_('POST request required.'))
+
+  response = {'status': -1}
+
+  alias = json.loads(request.POST.get('alias', ''))
+  collections = json.loads(request.POST.get('collections', '[]'))
+
+  api = SolrApi(SOLR_URL.get(), request.user, SECURITY_ENABLED.get())
+
+  try:
+    api.create_or_modify_alias(alias, collections)
+    response['status'] = 0
+    response['message'] = _('Alias created or modified!')
+  except Exception, e:
+    response['message'] = _('Alias could not be created or modified: %s') % e
+
+  return JsonResponse(response)
+
+
+def delete_alias(request):
+  if request.method != 'POST':
+    raise PopupException(_('POST request required.'))
+
+  response = {'status': -1}
+
+  alias = json.loads(request.POST.get('alias', ''))
+
+  api = SolrApi(SOLR_URL.get(), request.user, SECURITY_ENABLED.get())
+
+  try:
+    api.delete_alias(alias)
+    response['status'] = 0
+    response['message'] = _('Alias deleted!')
+  except Exception, e:
+    response['message'] = _('Alias could not be deleted: %s') % e
+
+  return JsonResponse(response)

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

@@ -30,5 +30,8 @@ urlpatterns += patterns('indexer.api',
   url(r'^api/collections/remove/$', 'collections_remove', name='api_collections_remove'),
   url(r'^api/collections/(?P<collection>[^/]+)/fields/$', 'collections_fields', name='api_collections_fields'),
   url(r'^api/collections/(?P<collection>[^/]+)/update/$', 'collections_update', name='api_collections_update'),
-  url(r'^api/collections/(?P<collection>[^/]+)/data/$', 'collections_data', name='api_collections_data')
+  url(r'^api/collections/(?P<collection>[^/]+)/data/$', 'collections_data', name='api_collections_data'),
+
+  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')
 )

+ 0 - 1
desktop/libs/indexer/src/indexer/views.py

@@ -16,7 +16,6 @@
 # limitations under the License.
 
 import logging
-import json
 
 from django.utils.translation import ugettext as _
 

+ 34 - 1
desktop/libs/libsolr/src/libsolr/api.py

@@ -486,7 +486,7 @@ class SolrApi(object):
       )
 
       response = self._root.post('admin/cores', params=params, contenttype='application/json')
-      if response.get('responseHeader',{}).get('status',-1) == 0:
+      if response.get('responseHeader', {}).get('status', -1) == 0:
         return True
       else:
         LOG.error("Could not create core. Check response:\n%s" % json.dumps(response, indent=2))
@@ -498,6 +498,39 @@ class SolrApi(object):
       else:
         raise PopupException(e, title=_('Error while accessing Solr'))
 
+  def create_or_modify_alias(self, name, collections):
+    try:
+      params = self._get_params() + (
+        ('action', 'CREATEALIAS'),
+        ('name', name),
+        ('collections', ','.join(collections)),
+        ('wt', 'json'),
+      )
+
+      response = self._root.post('admin/collections', params=params, contenttype='application/json')
+      if response.get('responseHeader', {}).get('status', -1) != 0:
+        msg = _("Could not create or edit alias. Check response:\n%s") % json.dumps(response, indent=2)
+        LOG.error(msg)
+        raise PopupException(msg)
+    except RestException, e:
+        raise PopupException(e, title=_('Error while accessing Solr'))
+
+  def delete_alias(self, name):
+    try:
+      params = self._get_params() + (
+        ('action', 'DELETEALIAS'),
+        ('name', name),
+        ('wt', 'json'),
+      )
+
+      response = self._root.post('admin/collections', params=params, contenttype='application/json')
+      if response.get('responseHeader', {}).get('status', -1) != 0:
+        msg = _("Could not delete alias. Check response:\n%s") % json.dumps(response, indent=2)
+        LOG.error(msg)
+        raise PopupException(msg)
+    except RestException, e:
+        raise PopupException(e, title=_('Error while accessing Solr'))
+
   def remove_collection(self, name, replication=1):
     try:
       params = self._get_params() + (