Browse Source

HUE-8298 [metadata] Skeleton of manager API

     $.get("/metadata/api/manager/hello/", function(data) {
        console.log(ko.mapping.toJSON(data));
      });
Romain Rigaux 7 years ago
parent
commit
30057f7629

+ 92 - 0
desktop/libs/metadata/src/metadata/conf.py

@@ -301,6 +301,98 @@ NAVIGATOR = ConfigSection(
 )
 
 
+MANAGER = ConfigSection(
+  key='manager',
+  help=_t("""Configuration options for Manager API"""),
+  members=dict(
+    API_URL=Config(
+      key='api_url',
+      help=_t('Base URL to API.'),
+      default=None),
+ 
+    AUTH_CM_USERNAME=Config(
+      key="navmetadataserver_cmdb_user",
+      help=_t("Username of the CM user used for authentication."),
+      dynamic_default=get_auth_username),
+    AUTH_CM_PASSWORD=Config(
+      key="navmetadataserver_cmdb_password",
+      help=_t("CM password of the user used for authentication."),
+      private=True,
+      dynamic_default=get_navigator_cm_password),
+    AUTH_CM_PASSWORD_SCRIPT=Config(
+      key="navmetadataserver_cmdb_password_script",
+      help=_t("Execute this script to produce the CM password. This will be used when the plain password is not set."),
+      private=True,
+      type=coerce_password_from_script,
+      default=None),
+
+    AUTH_LDAP_USERNAME=Config(
+      key="navmetadataserver_ldap_user",
+      help=_t("Username of the LDAP user used for authentication."),
+      dynamic_default=get_auth_username),
+    AUTH_LDAP_PASSWORD=Config(
+      key="navmetadataserver_ldap_password",
+      help=_t("LDAP password of the user used for authentication."),
+      private=True,
+      dynamic_default=get_navigator_ldap_password),
+    AUTH_LDAP_PASSWORD_SCRIPT=Config(
+      key="navmetadataserver_ldap_password_script",
+      help=_t("Execute this script to produce the LDAP password. This will be used when the plain password is not set."),
+      private=True,
+      type=coerce_password_from_script,
+      default=None),
+
+    AUTH_SAML_USERNAME=Config(
+      key="navmetadataserver_saml_user",
+      help=_t("Username of the SAML user used for authentication."),
+      dynamic_default=get_auth_username),
+    AUTH_SAML_PASSWORD=Config(
+      key="navmetadataserver_saml_password",
+      help=_t("SAML password of the user used for authentication."),
+      private=True,
+      dynamic_default=get_navigator_saml_password),
+    AUTH_SAML_PASSWORD_SCRIPT=Config(
+      key="navmetadataserver_saml_password_script",
+      help=_t("Execute this script to produce the SAML password. This will be used when the plain password  is not set."),
+      private=True,
+      type=coerce_password_from_script,
+      default=None),
+
+    CONF_DIR = Config(
+      key='conf_dir',
+      help=_t('Navigator configuration directory, where navigator.client.properties is located.'),
+      dynamic_default=default_navigator_config_dir
+    ),
+    APPLY_SENTRY_PERMISSIONS = Config(
+      key="apply_sentry_permissions",
+      help=_t("Perform Sentry privilege filtering. Default to true automatically if the cluster is secure."),
+      dynamic_default=get_security_default,
+      type=coerce_bool
+    ),
+    FETCH_SIZE_SEARCH = Config(
+      key="fetch_size_search",
+      help=_t("Max number of items to fetch in one call in object search."),
+      default=450,
+      type=int
+    ),
+    FETCH_SIZE_SEARCH_INTERACTIVE = Config(
+      key="fetch_size_search_interactive",
+      help=_t("Max number of items to fetch in one call in object search autocomplete."),
+      default=450,
+      type=int
+    ),
+
+    ENABLE_FILE_SEARCH = Config(
+      key="enable_file_search",
+      help=_t("Enable to search HDFS, S3 files."),
+      type=coerce_bool,
+      default=False
+    )
+  )
+)
+
+
+
 def test_metadata_configurations(user):
   from libsentry.conf import is_enabled
 

+ 87 - 0
desktop/libs/metadata/src/metadata/manager_api.py

@@ -0,0 +1,87 @@
+#!/usr/bin/env python
+# -- coding: utf-8 --
+# Licensed to Cloudera, Inc. under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  Cloudera, Inc. licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import json
+import logging
+
+from metadata.manager_client import ManagerApi
+
+try:
+  from collections import OrderedDict
+except ImportError:
+  from ordereddict import OrderedDict # Python 2.6
+
+from django.http import Http404
+from django.utils.html import escape
+from django.utils.translation import ugettext as _
+from django.views.decorators.http import require_POST
+
+from desktop.lib.django_util import JsonResponse
+from desktop.lib.i18n import force_unicode, smart_unicode
+
+from metadata.conf import has_navigator
+from metadata.navigator_client import NavigatorApiException
+
+
+LOG = logging.getLogger(__name__)
+
+
+class ManagerApiException(Exception):
+  def __init__(self, message=None):
+    self.message = message or _('No error message, please check the logs.')
+
+  def __str__(self):
+    return str(self.message)
+
+  def __unicode__(self):
+    return smart_unicode(self.message)
+
+
+def error_handler(view_fn):
+  def decorator(*args, **kwargs):
+    status = 500
+    response = {
+      'message': ''
+    }
+
+    try:
+      if has_navigator(args[0].user): # TODO
+        return view_fn(*args, **kwargs)
+      else:
+        raise NavigatorApiException('Navigator API is not configured.')
+    except NavigatorApiException, e:
+      try:
+        response['message'] = json.loads(e.message)
+      except Exception:
+        response['message'] = force_unicode(e.message)
+    except Exception, e:
+      message = force_unicode(e)
+      response['message'] = message
+      LOG.exception(message)
+
+    return JsonResponse(response, status=status)
+  return decorator
+
+
+@error_handler
+def hello(request):
+  api = ManagerApi(request.user)
+
+  response = api.tools_echo()
+
+  return JsonResponse(response)

+ 67 - 0
desktop/libs/metadata/src/metadata/manager_client.py

@@ -0,0 +1,67 @@
+#!/usr/bin/env python
+# -- coding: utf-8 --
+# Licensed to Cloudera, Inc. under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  Cloudera, Inc. licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import logging
+
+from django.core.cache import cache
+from django.utils.translation import ugettext as _
+
+from desktop.lib.rest.http_client import RestException, HttpClient
+
+from metadata.conf import MANAGER
+from desktop.lib.rest.resource import Resource
+from metadata.manager_api import ManagerApiException
+
+
+LOG = logging.getLogger(__name__)
+VERSION = 'v19'
+
+
+class ManagerApi(object):
+  """
+  https://cloudera.github.io/cm_api/
+  """
+
+  def __init__(self, user=None, security_enabled=False, ssl_cert_ca_verify=False):
+    self._api_url = '%s/%s' % (MANAGER.API_URL.get().strip('/'), VERSION)
+    self._username = 'hue' #get_navigator_auth_username()
+    self._password = 'hue' #get_navigator_auth_password()
+
+    self.user = user
+    self._client = HttpClient(self._api_url, logger=LOG)
+
+    if security_enabled:
+      self._client.set_kerberos_auth()
+    else:
+      self._client.set_basic_auth(self._username, self._password)
+
+    self._client.set_verify(ssl_cert_ca_verify)
+    self._root = Resource(self._client)
+
+
+  def tools_echo(self):
+    try:
+      params = (
+        ('message', 'hello'),
+      )
+
+      LOG.info(params)
+      return self._root.get('tools/echo', params=params)
+    except RestException, e:
+      LOG.error('Failed to search for entities with search query')
+      raise ManagerApiException(e)

+ 7 - 0
desktop/libs/metadata/src/metadata/urls.py

@@ -64,6 +64,13 @@ urlpatterns += patterns('metadata.optimizer_api',
 )
 
 
+# Manager API
+urlpatterns += patterns('metadata.manager_api',
+  url(r'^api/manager/hello/?$', 'hello', name='hello'),
+)
+
+
+
 # Workload Analytics API
 urlpatterns += patterns('metadata.workload_analytics_api',
   url(r'^api/workload_analytics/get_operation_execution_details/?$', 'get_operation_execution_details', name='get_operation_execution_details'),