api.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 logging
  18. from desktop.lib.django_util import JsonResponse
  19. from desktop.lib.fsmanager import FS_GETTERS
  20. from desktop.lib.i18n import smart_unicode
  21. from aws.conf import has_s3_access
  22. LOG = logging.getLogger(__name__)
  23. def error_handler(view_fn):
  24. def decorator(*args, **kwargs):
  25. response = {}
  26. try:
  27. return view_fn(*args, **kwargs)
  28. except Exception, e:
  29. LOG.exception('Error running %s' % view_fn)
  30. response['status'] = -1
  31. response['message'] = smart_unicode(e)
  32. return JsonResponse(response)
  33. return decorator
  34. @error_handler
  35. def get_filesystems(request):
  36. response = {}
  37. filesystems = {}
  38. for k, v in FS_GETTERS.items():
  39. if not k.startswith('s3') or has_s3_access(request.user):
  40. filesystems[k] = v is not None
  41. response['status'] = 0
  42. response['filesystems'] = filesystems
  43. return JsonResponse(response)