snowch / ibm-analytics-engine-python

Python library for IBM Analytics Engine - https://console.bluemix.net/docs/services/AnalyticsEngine/index.html
Apache License 2.0
11 stars 2 forks source link

Provide function to get namenode hostname #9

Closed snowch closed 6 years ago

snowch commented 6 years ago

Ambari Request example:

GET https://xxxx.bi.services.us-south.bluemix.net:9443/api/v1/clusters/AnalyticsEngine/services/HDFS/components/NAMENODE?fields=host_components

Ambari Response example:

{
  "href" : "https://xxxx.bi.services.us-south.bluemix.net:9443/api/v1/clusters/AnalyticsEngine/services/HDFS/components/NAMENODE?fields=host_components",
  "ServiceComponentInfo" : {
    "cluster_name" : "AnalyticsEngine",
    "component_name" : "NAMENODE",
    "service_name" : "HDFS"
  },
  "host_components" : [
    {
      "href" : "https://xxxx.bi.services.us-south.bluemix.net:9443/api/v1/clusters/AnalyticsEngine/hosts/xxxx.bi.services.us-south.bluemix.net/host_components/NAMENODE",
      "HostRoles" : {
        "cluster_name" : "AnalyticsEngine",
        "component_name" : "NAMENODE",
        "host_name" : "xxxx.bi.services.us-south.bluemix.net"
      }
    }
  ]
}

Python example

    # urllib example:
    try:
      url = '{0}://{1}:{2}{3}'.format(protocol, host, port, api_url)
      admin_auth = base64.encodestring('%s:%s' % (login, password)).replace('\n', '')
      request = urllib2.Request(url)
      request.add_header('Authorization', 'Basic %s' % admin_auth)
      request.add_header('X-Requested-By', 'ambari')
      request.add_data(request_body)
      request.get_method = lambda: request_type

      print(url, request_body, login, password)

      response = urllib2.urlopen(request)
      response_body = response.read()
      print(response_body)
    except Exception as exc:
      raise Exception('Problem with accessing api. Reason: {0}'.format(exc))

    name_node = response['host_components']['hostname']
snowch commented 6 years ago

See one approach, here: https://stackoverflow.com/questions/47844055/how-to-retrieve-the-name-node-host-name-using-ambari

snowch commented 6 years ago

Done: 4c9818d2fbe4aa8070eae5e632dca8ec58be1a22