Browse Source

AMBARI-5663: Python Client should support HTTP Headers.(subin)

Subin 11 years ago
parent
commit
f8dcdfcfbd

+ 4 - 4
ambari-client/src/main/python/ambari_client/ambari_api.py

@@ -36,7 +36,7 @@ class AmbariClient(RestResource):
   """
 
   def __init__(self, host_name, port=None, user_name="admin", password="admin", use_https = False,
-               version=API_VERSION , client=None):
+               version=API_VERSION , client=None ,http_header=None):
     """
     Creates a RestResource object.
 
@@ -59,11 +59,11 @@ class AmbariClient(RestResource):
       if port is None: 
         port = 8080
       
-
-      
     host_url = "%s://%s:%s/api/v%s" % (protocol, host_name, port, version)
     if client is None:
-        client = HttpClient(host_url, user_name , password)
+        client = HttpClient(host_url, user_name , password )
+        if http_header:
+            client.set_headers(http_header)
     RestResource.__init__(self, client)
 
 

+ 12 - 6
ambari-client/src/main/python/ambari_client/core/http_client.py

@@ -115,13 +115,16 @@ class HttpClient(object):
         self.c.setopt(pycurl.UPLOAD, 1)
     else:
         self.c.setopt(pycurl.CUSTOMREQUEST, http_method)
-        
+
+    data = None    
     if http_method in ('POST','PUT'):
       LOG.debug( "data..........."+str(payload))
       data = json.dumps(payload)
-      data= data.decode('unicode-escape')
-      LOG.debug( data)
+      #data= data.decode('unicode-escape')
+      #LOG.debug( "after unicode decode")
+      #LOG.debug( data)
       data = self._to_bytestring(data)
+      LOG.debug( "after _to_bytestring")
       LOG.debug( data)
       content = StringIO.StringIO(data)
       LOG.debug( content)
@@ -132,13 +135,16 @@ class HttpClient(object):
         self.c.setopt(pycurl.POSTFIELDSIZE, content_length)
       else:
         self.c.setopt(pycurl.INFILESIZE, content_length)
+        
       self.c.setopt(pycurl.READFUNCTION, content.read)
       
-      
+
     self.c.setopt(self.c.URL, url)
     headers = self._get_headers(headers)
-    self.c.setopt(pycurl.HTTPHEADER,
-                        ["%s: %s" % pair for pair in sorted(headers.iteritems())])
+    headers_l = ["%s: %s" % pair for pair in sorted(headers.iteritems())]
+    LOG.debug( headers_l)
+    self.c.setopt(pycurl.HTTPHEADER,headers_l)
+
 
     LOG.debug ("invoke : pycurl.EFFECTIVE_URL = "+self.c.getinfo(pycurl.EFFECTIVE_URL))
     try:

+ 4 - 1
ambari-client/src/main/python/ambari_client/model/base_model.py

@@ -19,7 +19,7 @@ import sys
 import logging
 import time
 from  ambari_client.model.utils  import get_REF_object, get_unicode, getREF_var_name , LIST_KEY
-
+from operator import itemgetter, attrgetter
 
 __docformat__ = "epytext"
 
@@ -101,6 +101,9 @@ class ModelList(object):
   def __iter__(self):
     return self.objects.__iter__()
 
+  def sort(self, sortkey):
+    self.objects = sorted(self.objects, key=sortkey ,reverse=True) 
+      
   def __getitem__(self, i):
     return self.objects.__getitem__(i)
 

+ 14 - 11
ambari-client/src/main/python/ambari_client/model/cluster.py

@@ -162,12 +162,15 @@ class ClusterModel(BaseModel):
     """
     return configuration._get_configuration(self._get_resource_root(), self.cluster_name, "global")
 
-  def get_core_site_config(self, detail=None):
+  def get_core_site_config(self, tag="version1" ,detail=None):
     """
     Get core-site configuration of  cluster.
-    @return: A ConfigModel object.
+    @return: A ConfigModel object or ModelList<ConfiObject>
     """
-    return configuration._get_configuration(self._get_resource_root(), self.cluster_name, "core-site")
+    if(detail == utils.ALL):
+        return configuration._get_all_configuration(self._get_resource_root(), self.cluster_name, "core-site")
+    else:
+        return configuration._get_configuration(self._get_resource_root(), self.cluster_name,  "core-site" ,tag)
 
   def get_hdfs_site_config(self, detail=None):
     """
@@ -183,37 +186,37 @@ class ClusterModel(BaseModel):
     """
     return configuration._get_configuration(self._get_resource_root(), self.cluster_name, "mapred-site")
 
-  def update_global_config(self, config_model , detail=None):
+  def update_global_config(self, config_model , tag="version1" ,detail=None):
     """
     Updates the  global configuration of  cluster.
     @param config_model: The configModel object
     @return: A ConfigModel object.
     """
-    return configuration._update_configuration(self._get_resource_root(), self.cluster_name, "global" , "version1", config_model)
+    return configuration._update_configuration(self._get_resource_root(), self.cluster_name, "global" , tag, config_model)
 
-  def update_core_site_config(self, config_model , detail=None):
+  def update_core_site_config(self, config_model , tag="version1" ,detail=None):
     """
     Updates the  core-site configuration of  cluster.
     @param config_model: The configModel object
     @return: A ConfigModel object.
     """
-    return configuration._update_configuration(self._get_resource_root(), self.cluster_name, "core-site", "version1", config_model)
+    return configuration._update_configuration(self._get_resource_root(), self.cluster_name, "core-site", tag, config_model)
 
-  def update_hdfs_site_config(self, config_model , detail=None):
+  def update_hdfs_site_config(self, config_model , tag="version1" , detail=None):
     """
     Updates the  hdfs-site configuration of  cluster.
     @param config_model: The configModel object
     @return: A ConfigModel object.
     """
-    return configuration._update_configuration(self._get_resource_root(), self.cluster_name, "hdfs-site", "version1", config_model)
+    return configuration._update_configuration(self._get_resource_root(), self.cluster_name, "hdfs-site", tag, config_model)
 
-  def update_mapred_site_config(self, config_model , detail=None):
+  def update_mapred_site_config(self, config_model ,tag="version1" , detail=None):
     """
     Updates the  mapred-site configuration of  cluster.
     @param config_model: The configModel object
     @return: A ConfigModel object.
     """
-    return configuration._update_configuration(self._get_resource_root(), self.cluster_name, "mapred-site", "version1", config_model)
+    return configuration._update_configuration(self._get_resource_root(), self.cluster_name, "mapred-site", tag, config_model)
 
   def create_services(self, services_list , detail=None):
     """

+ 27 - 2
ambari-client/src/main/python/ambari_client/model/configuration.py

@@ -16,7 +16,7 @@
 #  limitations under the License.
 
 
-from ambari_client.model.base_model import  BaseModel 
+from ambari_client.model.base_model import  BaseModel , ModelList
 from ambari_client.model import paths , status , utils
 
 
@@ -41,6 +41,28 @@ def _get_configuration(resource_root, cluster_name , type , tag="version1"):
   return config_model
 
 
+def _get_all_configuration(resource_root, cluster_name , type ):
+  """
+  Gets ALL configuration of a cluster of a given type
+  @param resource_root: The root Resource .
+  @param cluster_name: cluster_name
+  @param type: type of config
+  @return: A ConfigModel object
+  """
+  dic = resource_root.get(paths.CONFIGURATION_ALL_PATH % (cluster_name, type))
+  
+  if len(dic["items"]) == 0:
+    return None
+
+  objects = []
+  for cfgm in dic["items"]:
+      config_model = utils.ModelUtils.create_model(ConfigModel , cfgm, resource_root, "NO_KEY")
+      ref_clss = utils.getREF_class_name("cluster_name")
+      config_model._setattr(ref_clss, cfgm['Config']['cluster_name'])
+      objects.append(config_model)
+  return ModelList(objects)
+  
+  
 def _update_configuration(resource_root, cluster_name , type , tag , config_model):
   """
   Update configuration of a cluster
@@ -51,7 +73,7 @@ def _update_configuration(resource_root, cluster_name , type , tag , config_mode
   @return: A ConfigModel object
   """
   data = {"Clusters":{"desired_configs":{ "type":type, "tag":tag, "properties":config_model.properties}}}
-  resp = resource_root.post(path=paths.CREATE_CONFIGURATION_PATH % cluster_name , payload=data)
+  resp = resource_root.put(path=paths.UPDATE_CONFIGURATION_PATH % cluster_name , payload=data )
   return utils.ModelUtils.create_model(status.StatusModel, resp, resource_root, "NO_KEY")
 
 
@@ -103,6 +125,9 @@ class ConfigModel(BaseModel):
       return self.clusterRef.cluster_name
     return None
 
+  def __lt__(self, other):
+    return self.tag < other.tag
+
   def _path(self):
     """
     Return the API path for this service.

+ 2 - 0
ambari-client/src/main/python/ambari_client/model/paths.py

@@ -42,7 +42,9 @@ REQUEST_STATUS_PATH = "/clusters/%s/requests/%s?fields=tasks/Tasks/status"
 REQUEST_PATH = "clusters/%s/requests/%s"
 
 CONFIGURATION_PATH = "/clusters/%s/configurations?type=%s&tag=%s"
+CONFIGURATION_ALL_PATH = "/clusters/%s/configurations?type=%s"
 CREATE_CONFIGURATION_PATH = "/clusters/%s/configurations"
+UPDATE_CONFIGURATION_PATH="/clusters/%s"
 
 STACK_SERVICES_COMPONENTS_PATH = "/stacks2/HDP/versions/%s/stackServices/%s/serviceComponents?fields=*"
 STACK_SERVICES_CONFIG_PATH = "/stacks2/HDP/versions/%s/stackServices/%s/configurations?fields=*"

+ 12 - 4
ambari-client/src/main/python/ambari_client/model/service.py

@@ -159,18 +159,26 @@ class ServiceModel(BaseModel):
       status_model.request_path = None
     return status_model
 
-  def start(self):
+  def start(self ,message = None):
     """
     Start a service.
     """
-    data = {"ServiceInfo": {"state": "STARTED"}}
+    data = None
+    if message:
+        data = {"RequestInfo":{"context":message},"Body":{"ServiceInfo":{"state":"STARTED"}}}
+    else:
+        data = {"ServiceInfo": {"state": "STARTED"}}
     return self._action(data)
 
-  def stop(self):
+  def stop(self ,message = None):
     """
     Stop a service.
     """
-    data = {"ServiceInfo": {"state": "INSTALLED"}}
+    data = None
+    if message:
+        data = {"RequestInfo":{"context":message},"Body":{"ServiceInfo":{"state":"INSTALLED"}}}
+    else:
+        data = {"ServiceInfo": {"state": "INSTALLED"}}
     return self._action(data)
 
   def install(self):

+ 11 - 1
ambari-client/src/main/python/ambari_client/model/status.py

@@ -35,7 +35,7 @@ class StatusModel(BaseModel):
     utils.retain_self_helper(BaseModel, **locals())
 
   def __str__(self):
-    return "<<StatusModel>> status = %s ; requestId = %s ;message = %s" % (self.status, self._get_id() , self.get_message())
+    return "<<StatusModel>> status = %s ; requestId = %s ;message = %s" % (self._get_status(), self._get_id() , self.get_message())
 
   def get_bootstrap_path(self):
     return paths.BOOTSTRAP_PATH + '/' + str(self.requestId)
@@ -59,3 +59,13 @@ class StatusModel(BaseModel):
         return self.id
     else:
         None
+
+  def _get_status(self):
+    if hasattr(self, 'status') and isinstance(self.status, basestring):
+        self.message = self.status
+        self.status = 200
+        return self.status
+    elif hasattr(self, 'status') and isinstance(self.status, int):
+        return self.status
+    else:
+        None

+ 1 - 0
ambari-client/src/main/python/ambari_client/model/utils.py

@@ -27,6 +27,7 @@ ref_dic = {"cluster_name":"clusterRef"}
 ref_class_dic = {"ClusterModelRef":"cluster_name"}
 ref_pkg_dic = {"ClusterModelRef":"ambari_client.model.cluster"}
 LIST_KEY = "items"
+ALL="ALL" 
   
 class ModelUtils(object):
   

File diff suppressed because it is too large
+ 6 - 6
ambari-client/src/test/python/TestClusterModel.py


Some files were not shown because too many files changed in this diff