api2.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. from django.utils.translation import ugettext as _
  20. from desktop.lib.i18n import smart_unicode
  21. from desktop.lib.django_util import JsonResponse
  22. from jobbrowser.apis.base_api import get_api
  23. LOG = logging.getLogger(__name__)
  24. def api_error_handler(func):
  25. def decorator(*args, **kwargs):
  26. response = {}
  27. try:
  28. return func(*args, **kwargs)
  29. except Exception, e:
  30. LOG.exception('Error running %s' % func)
  31. response['status'] = -1
  32. response['message'] = smart_unicode(e)
  33. finally:
  34. if response:
  35. return JsonResponse(response)
  36. return decorator
  37. @api_error_handler
  38. def jobs(request):
  39. response = {'status': -1}
  40. interface = json.loads(request.POST.get('interface'))
  41. response['apps'] = get_api(request.user, interface).apps()
  42. response['status'] = 0
  43. return JsonResponse(response)
  44. @api_error_handler
  45. def job(request):
  46. response = {'status': -1}
  47. interface = json.loads(request.POST.get('interface'))
  48. app_id = json.loads(request.POST.get('app_id'))
  49. response['app'] = get_api(request.user, interface).app(app_id)
  50. response['status'] = 0
  51. return JsonResponse(response)
  52. @api_error_handler
  53. def action(request):
  54. response = {'status': -1}
  55. interface = json.loads(request.POST.get('interface'))
  56. app_id = json.loads(request.POST.get('app_id'))
  57. action = json.loads(request.POST.get('action'))
  58. response['action'] = action
  59. response['action_status'] = get_api(request.user, interface).action(app_id, action)
  60. response['status'] = 0
  61. return JsonResponse(response)
  62. @api_error_handler
  63. def logs(request):
  64. response = {'status': -1}
  65. interface = json.loads(request.POST.get('interface'))
  66. app_id = json.loads(request.POST.get('app_id'))
  67. app_type = json.loads(request.POST.get('type'))
  68. response['logs'] = get_api(request.user, interface).logs(app_id, app_type)
  69. response['status'] = 0
  70. return JsonResponse(response)
  71. @api_error_handler
  72. def profile(request):
  73. response = {'status': -1}
  74. interface = json.loads(request.POST.get('interface'))
  75. app_id = json.loads(request.POST.get('app_id'))
  76. app_type = json.loads(request.POST.get('app_type'))
  77. app_property = json.loads(request.POST.get('app_property'))
  78. api = get_api(request.user, interface)
  79. api._set_request(request) # For YARN
  80. response[app_property] = api.profile(app_id, app_type, app_property)
  81. response['status'] = 0
  82. return JsonResponse(response)