link.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. import json
  18. import logging
  19. import socket
  20. from django.utils.encoding import smart_str
  21. from django.utils.translation import ugettext as _
  22. from sqoop import client, conf
  23. from sqoop.client.exception import SqoopException
  24. from decorators import get_link_or_exception
  25. from desktop.lib.django_util import JsonResponse
  26. from desktop.lib.exceptions import StructuredException
  27. from desktop.lib.rest.http_client import RestException
  28. from exception import handle_rest_exception
  29. from utils import list_to_dict
  30. from django.views.decorators.cache import never_cache
  31. __all__ = ['get_links', 'create_link', 'update_link', 'link', 'links', 'link_clone', 'link_delete']
  32. LOG = logging.getLogger(__name__)
  33. @never_cache
  34. def get_links(request):
  35. response = {
  36. 'status': 0,
  37. 'errors': None,
  38. 'links': []
  39. }
  40. try:
  41. c = client.SqoopClient(conf.SERVER_URL.get(), request.user.username, request.LANGUAGE_CODE, ssl_cert_ca_verify=conf.SSL_CERT_CA_VERIFY.get())
  42. response['links'] = list_to_dict(c.get_links())
  43. except RestException, e:
  44. response.update(handle_rest_exception(e, _('Could not get links.')))
  45. return JsonResponse(response)
  46. @never_cache
  47. def create_link(request):
  48. response = {
  49. 'status': 0,
  50. 'errors': None,
  51. 'link': None
  52. }
  53. if 'link' not in request.POST:
  54. raise StructuredException(code="INVALID_REQUEST_ERROR", message=_('Error saving link'), data={'errors': 'Link is missing.'}, error_code=400)
  55. d = json.loads(smart_str(request.POST.get('link')))
  56. link = client.Link.from_dict(d)
  57. try:
  58. c = client.SqoopClient(conf.SERVER_URL.get(), request.user.username, request.LANGUAGE_CODE, ssl_cert_ca_verify=conf.SSL_CERT_CA_VERIFY.get())
  59. response['link'] = c.create_link(link).to_dict()
  60. except RestException, e:
  61. response.update(handle_rest_exception(e, _('Could not create link.')))
  62. except SqoopException, e:
  63. response['status'] = 100
  64. response['errors'] = e.to_dict()
  65. return JsonResponse(response)
  66. @never_cache
  67. def update_link(request, link):
  68. response = {
  69. 'status': 0,
  70. 'errors': None,
  71. 'link': None
  72. }
  73. if 'link' not in request.POST:
  74. raise StructuredException(code="INVALID_REQUEST_ERROR", message=_('Error saving link'), data={'errors': 'Link is missing.'}, error_code=400)
  75. link.update_from_dict(json.loads(smart_str(request.POST.get('link'))))
  76. try:
  77. c = client.SqoopClient(conf.SERVER_URL.get(), request.user.username, request.LANGUAGE_CODE, ssl_cert_ca_verify=conf.SSL_CERT_CA_VERIFY.get())
  78. response['link'] = c.update_link(link).to_dict()
  79. except RestException, e:
  80. response.update(handle_rest_exception(e, _('Could not update link.')))
  81. except SqoopException, e:
  82. response['status'] = 100
  83. response['errors'] = e.to_dict()
  84. return JsonResponse(response)
  85. @never_cache
  86. def links(request):
  87. if request.method == 'GET':
  88. return get_links(request)
  89. elif request.method == 'POST':
  90. return create_link(request)
  91. else:
  92. raise StructuredException(code="INVALID_METHOD", message=_('GET or POST request required.'), error_code=405)
  93. @never_cache
  94. @get_link_or_exception()
  95. def link(request, link):
  96. response = {
  97. 'status': 0,
  98. 'errors': None,
  99. 'link': None
  100. }
  101. if request.method == 'GET':
  102. response['link'] = link.to_dict()
  103. return JsonResponse(response)
  104. elif request.method == 'POST':
  105. return update_link(request, link)
  106. else:
  107. raise StructuredException(code="INVALID_METHOD", message=_('GET or POST request required.'), error_code=405)
  108. @never_cache
  109. @get_link_or_exception()
  110. def link_clone(request, link):
  111. if request.method != 'POST':
  112. raise StructuredException(code="INVALID_METHOD", message=_('POST request required.'), error_code=405)
  113. response = {
  114. 'status': 0,
  115. 'errors': None,
  116. 'link': None
  117. }
  118. link.id = -1
  119. link.name = '%s-copy' % link.name
  120. try:
  121. c = client.SqoopClient(conf.SERVER_URL.get(), request.user.username, request.LANGUAGE_CODE, ssl_cert_ca_verify=conf.SSL_CERT_CA_VERIFY.get())
  122. response['link'] = c.create_link(link).to_dict()
  123. except RestException, e:
  124. response.update(handle_rest_exception(e, _('Could not clone link.')))
  125. except SqoopException, e:
  126. response['status'] = 100
  127. response['errors'] = e.to_dict()
  128. return JsonResponse(response)
  129. @never_cache
  130. @get_link_or_exception()
  131. def link_delete(request, link):
  132. if request.method != 'POST':
  133. raise StructuredException(code="INVALID_METHOD", message=_('POST request required.'), error_code=405)
  134. response = {
  135. 'status': 0,
  136. 'errors': None
  137. }
  138. try:
  139. c = client.SqoopClient(conf.SERVER_URL.get(), request.user.username, request.LANGUAGE_CODE, ssl_cert_ca_verify=conf.SSL_CERT_CA_VERIFY.get())
  140. c.delete_link(link)
  141. except RestException, e:
  142. response.update(handle_rest_exception(e, _('Could not delete link.')))
  143. except SqoopException, e:
  144. response['status'] = 100
  145. response['errors'] = e.to_dict()
  146. return JsonResponse(response)