api2.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. filters = [(key, value) for _filter in json.loads(request.POST.get('filters', '[]')) for key, value in _filter.items() if value]
  42. response['apps'] = get_api(request.user, interface).apps(filters)
  43. response['status'] = 0
  44. return JsonResponse(response)
  45. @api_error_handler
  46. def job(request):
  47. response = {'status': -1}
  48. interface = json.loads(request.POST.get('interface'))
  49. app_id = json.loads(request.POST.get('app_id'))
  50. response['app'] = get_api(request.user, interface).app(app_id)
  51. response['status'] = 0
  52. return JsonResponse(response)
  53. @api_error_handler
  54. def action(request):
  55. response = {'status': -1}
  56. interface = json.loads(request.POST.get('interface'))
  57. app_id = json.loads(request.POST.get('app_id'))
  58. operation = json.loads(request.POST.get('operation'))
  59. response['operation'] = operation
  60. response.update(get_api(request.user, interface).action(app_id, operation))
  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. log_name = json.loads(request.POST.get('name'))
  69. response['logs'] = get_api(request.user, interface).logs(app_id, app_type, log_name)
  70. response['status'] = 0
  71. return JsonResponse(response)
  72. @api_error_handler
  73. def profile(request):
  74. response = {'status': -1}
  75. interface = json.loads(request.POST.get('interface'))
  76. app_id = json.loads(request.POST.get('app_id'))
  77. app_type = json.loads(request.POST.get('app_type'))
  78. app_property = json.loads(request.POST.get('app_property'))
  79. api = get_api(request.user, interface)
  80. api._set_request(request) # For YARN
  81. response[app_property] = api.profile(app_id, app_type, app_property)
  82. response['status'] = 0
  83. return JsonResponse(response)