connector.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/usr/bin/env python
  2. # Licensed to Cloudera, Inc. under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. Cloudera, Inc. licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. try:
  18. import json
  19. except ImportError:
  20. import simplejson as json
  21. import logging
  22. import socket
  23. from django.utils.translation import ugettext as _
  24. from sqoop import client, conf
  25. from decorators import get_connector_or_exception
  26. from desktop.lib.django_util import JsonResponse
  27. from desktop.lib.exceptions import StructuredException
  28. from desktop.lib.rest.http_client import RestException
  29. from exception import handle_rest_exception
  30. from utils import list_to_dict
  31. from django.views.decorators.cache import never_cache
  32. __all__ = ['get_connectors', 'connectors', 'connector']
  33. LOG = logging.getLogger(__name__)
  34. @never_cache
  35. def get_connectors(request):
  36. response = {
  37. 'status': 0,
  38. 'errors': None,
  39. 'connectors': []
  40. }
  41. try:
  42. c = client.SqoopClient(conf.SERVER_URL.get(), request.user.username, request.LANGUAGE_CODE)
  43. response['connectors'] = list_to_dict(c.get_connectors())
  44. except RestException, e:
  45. response.update(handle_rest_exception(e, _('Could not get connectors.')))
  46. return JsonResponse(response)
  47. def connectors(request):
  48. if request.method == 'GET':
  49. return get_connectors(request)
  50. else:
  51. raise StructuredException(code="INVALID_METHOD", message=_('GET request required.'), error_code=405)
  52. @never_cache
  53. @get_connector_or_exception()
  54. def connector(request, connector):
  55. response = {
  56. 'status': 0,
  57. 'errors': None,
  58. 'connector': None
  59. }
  60. if request.method == 'GET':
  61. response['connector'] = connector.to_dict()
  62. return JsonResponse(response)
  63. else:
  64. raise StructuredException(code="INVALID_METHOD", message=_('GET request required.'), error_code=405)