Updated Branches:
  refs/heads/trunk 81b64b48b -> e55a1ecf2

AMBARI-3201. Ambari-Client code updates. (Andrew via mahadev)


Project: http://git-wip-us.apache.org/repos/asf/incubator-ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ambari/commit/e55a1ecf
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ambari/tree/e55a1ecf
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ambari/diff/e55a1ecf

Branch: refs/heads/trunk
Commit: e55a1ecf27307b107d903481fe3d510f8788569d
Parents: 81b64b4
Author: Mahadev Konar <[email protected]>
Authored: Mon Sep 16 08:33:21 2013 -0700
Committer: Mahadev Konar <[email protected]>
Committed: Mon Sep 16 08:33:21 2013 -0700

----------------------------------------------------------------------
 .../src/main/python/ambari_client/ambari_api.py | 32 +++++++-
 .../python/ambari_client/core/rest_resource.py  |  2 +-
 .../main/python/ambari_client/model/cluster.py  |  4 +-
 .../src/main/python/ambari_client/model/host.py | 81 +++++++++++++-------
 .../main/python/ambari_client/model/paths.py    |  3 +-
 .../python/ambari_client/resources/hosts.py     | 57 +++++++++++++-
 .../src/test/python/TestAmbariClient.py         |  2 +-
 7 files changed, 145 insertions(+), 36 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/e55a1ecf/ambari-client/src/main/python/ambari_client/ambari_api.py
----------------------------------------------------------------------
diff --git a/ambari-client/src/main/python/ambari_client/ambari_api.py 
b/ambari-client/src/main/python/ambari_client/ambari_api.py
index 14e8734..d9bb4b7 100755
--- a/ambari-client/src/main/python/ambari_client/ambari_api.py
+++ b/ambari-client/src/main/python/ambari_client/ambari_api.py
@@ -83,6 +83,15 @@ class AmbariClient(RestResource):
     @return : An ClusterModel.
     """
     return clusters._get_cluster(self, cluster_name)
+  
+  def get_host(self, host_name):
+    """
+    Lookup a host by name
+    @param root_resource: The root Resource.
+    @param host_name: Host name
+    @return: A HostModel object
+    """
+    return hosts._get_host(self, host_name)
 
 
   def get_all_hosts(self):
@@ -119,7 +128,28 @@ class AmbariClient(RestResource):
     @param version : HDP version.
     @return  ClusterModel object.
     """
-    return clusters._create_cluster(self, cluster_name, version)  
+    return clusters._create_cluster(self, cluster_name, version)
+  
+  def create_host(self, host_name, ip, rack_info='/default-rack'):
+    """
+    Create a host
+    @param root_resource: The root Resource.
+    @param host_name: Host name
+    @param ip: IP address
+    @param rack_info: Rack id. Default None
+    @return: A HostModel object
+    """
+    return hosts._create_host(self, host_name, ip, rack_info)
+  
+  
+  def create_hosts(self, host_list):
+    """
+    Create a host
+    @param root_resource: The root Resource.
+    @param host_list: ModelList list of hosts
+    @return: A HostModel object
+    """
+    return hosts._create_hosts(self, host_list)
 
 
   def delete_cluster(self , cluster_name):

http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/e55a1ecf/ambari-client/src/main/python/ambari_client/core/rest_resource.py
----------------------------------------------------------------------
diff --git a/ambari-client/src/main/python/ambari_client/core/rest_resource.py 
b/ambari-client/src/main/python/ambari_client/core/rest_resource.py
index 2a79ee7..f7e2e23 100755
--- a/ambari-client/src/main/python/ambari_client/core/rest_resource.py
+++ b/ambari-client/src/main/python/ambari_client/core/rest_resource.py
@@ -68,7 +68,7 @@ class RestResource(object):
         LOG.error("Command '%s %s' failed with error %s" % (http_method, path, 
code))
         return {"status":code , "message":"Command '%s %s' failed with error 
%s" % (http_method, path, code)}
         #raise Exception("Command '%s %s' failed with error %s" % 
(http_method, path, code))
-    if resp and (code == 404 or code == 405):
+    if resp and (code == 404 or code == 405 or code == 500):
         LOG.error("Command '%s %s' failed with error %s" % (http_method, path, 
code))
         return {"status":code , "message":"Command '%s %s' failed with error 
%s" % (http_method, path, code)}
         #raise Exception("Command '%s %s' failed with error %s" % 
(http_method, path, code))

http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/e55a1ecf/ambari-client/src/main/python/ambari_client/model/cluster.py
----------------------------------------------------------------------
diff --git a/ambari-client/src/main/python/ambari_client/model/cluster.py 
b/ambari-client/src/main/python/ambari_client/model/cluster.py
index f4cd6f5..75ac0c9 100755
--- a/ambari-client/src/main/python/ambari_client/model/cluster.py
+++ b/ambari-client/src/main/python/ambari_client/model/cluster.py
@@ -154,7 +154,7 @@ class ClusterModel(BaseModel):
     Get a specific hosts in this cluster.
     @return: A HostModel object.
     """
-    return host._get_host(self._get_resource_root(), self.cluster_name, 
hostname)
+    return host._get_cluster_host(self._get_resource_root(), 
self.cluster_name, hostname)
 
   def get_global_config(self, detail=None):
     """
@@ -253,7 +253,7 @@ class ClusterModel(BaseModel):
   def create_hosts(self, host_list , detail=None):
     """
     Creates hosts.
-    @param host_list: list of Host name
+    @param host_list: list of HostModel
     @return: StatusModel.
     """
     return host._add_hosts(self._get_resource_root(), self.cluster_name , 
host_list)

http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/e55a1ecf/ambari-client/src/main/python/ambari_client/model/host.py
----------------------------------------------------------------------
diff --git a/ambari-client/src/main/python/ambari_client/model/host.py 
b/ambari-client/src/main/python/ambari_client/model/host.py
index d88ba8d..2a0e101 100755
--- a/ambari-client/src/main/python/ambari_client/model/host.py
+++ b/ambari-client/src/main/python/ambari_client/model/host.py
@@ -28,26 +28,22 @@ from ambari_client.model import status , component , paths 
, utils
 LOG = logging.getLogger(__name__)
 
 
-def _create_host(root_resource, host_name, ip, rack_info=None):
+def _get_host(root_resource, host_name):
   """
-  Create a host
-  @param root_resource: The root Resource.
+  Lookup up by host_name
+  @param root_resource: The root Resource object.
+  @param cluster_name: Cluster name
   @param host_name: Host name
-  @param ip: IP address
-  @param rack_info: Rack id. Default None
-  @return: An HostModel object
+  @return: A HostModel object
   """
-  host = HostModel(root_resource, host_name, ip, rack_info)
-  host_list = ModelList([host])
-  body = json.dumps(host_list.to_json_dict())
-  resp = root_resource.post(paths.HOSTS_PATH, data=body)
-  # The server returns a created hosts 
-  return _get_host(root_resource, host_name)
-
+  path = paths.HOST_PATH % (host_name)
+  dic = root_resource.get(path)
+  
+  return utils.ModelUtils.create_model(HostModel , dic, root_resource, "Hosts")
 
-def _get_host(root_resource, cluster_name , host_name):
+def _get_cluster_host(root_resource, cluster_name , host_name):
   """
-  Lookup up by host_name
+  Lookup cluster host up by host_name
   @param root_resource: The root Resource object.
   @param cluster_name: Cluster name
   @param host_name: Host name
@@ -55,40 +51,67 @@ def _get_host(root_resource, cluster_name , host_name):
   """
   path = paths.CLUSTER_HOST_PATH % (cluster_name, host_name)
   dic = root_resource.get(path)
-  return utils.ModelUtils.create_model(HostModel , dic, root_resource, 
"Hosts") 
+  return utils.ModelUtils.create_model(HostModel , dic, root_resource, "Hosts")
+
 
 
+def _create_hosts(root_resource, host_list):
+  """
+  Create hosts from list
+  @param root_resource: The root Resource.
+  @param host_name: Host name
+  @param ip: IP address
+  @param rack_info: Rack id. Default None
+  @return: An HostList object
+  """
+  
+  data = 
[{"Hosts":{"host_name":x.host_name,"ip":x.ip,"rack_info":x.rack_info}} 
+          for x in host_list]
+  resp = root_resource.post(paths.HOSTS_PATH, payload=data)
+  return utils.ModelUtils.create_model(status.StatusModel, resp, 
root_resource, "NO_KEY")
+
+def _create_host(root_resource, host_name, ip, rack_info=None):
+  """
+  Create a host
+  @param root_resource: The root Resource.
+  @param host_name: Host name
+  @param ip: IP address
+  @param rack_info: Rack id. Default None
+  @return: An HostModel object
+  """
+  host_list = ModelList([HostModel(host_name, ip, rack_info)])
+  return _create_hosts(root_resource, host_list)
+
 def _add_hosts(root_resource, cluster_name , host_list):
   """
-  Lookup up by host_name
+  Adds a hosts to a cluster.
   @param root_resource: The root Resource object.
   @param cluster_name: Cluster name
-  @param host_list: list of hostnames
+  @param host_list: list of hosts
   @return: A StatusModel object
   """
   cpath = paths.HOSTS_CREATE_PATH % (cluster_name)
-  data = [{"Hosts":{"host_name":x}} for x in host_list]
+  data = 
[{"Hosts":{"host_name":x.host_name,"ip":x.ip,"rack_info":x.rack_info}} 
+          for x in host_list]
   resp = root_resource.post(path=cpath, payload=data)
   return utils.ModelUtils.create_model(status.StatusModel, resp, 
root_resource, "NO_KEY")
 
 
-def _add_host(root_resource, cluster_name , host_name , ip, rack_info):
+def _add_host(root_resource, cluster_name , host_name , ip, rack_info=None):
   """
-  Creates host.
+  Adds a host to a cluster.
   @param host_name: Host name
   @param ip: ip of Host 
   @param rack_info: rack information
   @return: StatusModel.
   """
-  cpath = paths.HOSTS_CREATE_PATH % (cluster_name)
-  data = [{"Hosts":{"host_name":host_name , "ip":ip, "rack_info":rack_info}}]
-  resp = root_resource.post(path=cpath, payload=data)
-  return utils.ModelUtils.create_model(status.StatusModel, resp, 
root_resource, "NO_KEY")
+  host_list = ModelList([HostModel(root_resource, host_name, ip, rack_info)])
+  return _add_hosts(root_resource, cluster_name, host_list)
 
 
 def _assign_role(root_resource, cluster_name , host_name , component_name):
   """
-  Lookup up by host_name
+  Add a new component to a node
   @param root_resource: The root Resource object.
   @param cluster_name: Cluster name
   @param component_name : name of component.
@@ -131,7 +154,7 @@ def _delete_host(root_resource, host_name):
   @param host_name: Host name
   @return: StatusModel object
   """
-  resp = root_resource.delete("%s/%s" % (paths.HOSTS_PATH, host_name))
+  resp = root_resource.delete(paths.HOST_PATH % (host_name))
   return utils.ModelUtils.create_model(status.StatusModel, resp, 
root_resource, "NO_KEY")
   
 
@@ -154,8 +177,8 @@ def _bootstrap_hosts(root_resource , hosts_list, ssh_key):
   @param hosts_list list of host_names.
   @return: A  StatusModel object.
   """
-  #payload_dic = {'sshKey':ssh_key.encode('string_escape') , 
'hosts':hosts_list}
-  payload_dic = {'sshKey':ssh_key , 'hosts':hosts_list}
+  #payload_dic = {'sshKey':ssh_key , 'hosts':hosts_list}
+  payload_dic = {'sshKey':ssh_key.encode('string_escape') , 'hosts':hosts_list}
   resp = root_resource.post(paths.BOOTSTRAP_PATH, payload_dic , 
content_type="application/json")
   LOG.debug(resp)
   return utils.ModelUtils.create_model(status.StatusModel, resp, 
root_resource, "NO_KEY")

http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/e55a1ecf/ambari-client/src/main/python/ambari_client/model/paths.py
----------------------------------------------------------------------
diff --git a/ambari-client/src/main/python/ambari_client/model/paths.py 
b/ambari-client/src/main/python/ambari_client/model/paths.py
index 9221fa0..034f91b 100755
--- a/ambari-client/src/main/python/ambari_client/model/paths.py
+++ b/ambari-client/src/main/python/ambari_client/model/paths.py
@@ -30,7 +30,8 @@ SERVICE_COMPONENTS_PATH = 
"/clusters/%s/services/%s/components?fields=*"
 SERVICE_COMPONENT_PATH = "/clusters/%s/services/%s/components/%s"
 
 
-HOSTS_PATH = "/hosts?fields=*"
+HOST_PATH = "/hosts/%s"
+HOSTS_PATH = "/hosts"
 HOSTS_CREATE_PATH = "/clusters/%s/hosts"
 HOSTS_COMPONENTS_PATH = 
"/clusters/%s/hosts/%s/host_components?ServiceComponentInfo"
 HOSTS_COMPONENT_PATH = "/clusters/%s/hosts/%s/host_components/%s" 

http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/e55a1ecf/ambari-client/src/main/python/ambari_client/resources/hosts.py
----------------------------------------------------------------------
diff --git a/ambari-client/src/main/python/ambari_client/resources/hosts.py 
b/ambari-client/src/main/python/ambari_client/resources/hosts.py
index c1a262c..41071c1 100755
--- a/ambari-client/src/main/python/ambari_client/resources/hosts.py
+++ b/ambari-client/src/main/python/ambari_client/resources/hosts.py
@@ -18,6 +18,35 @@
 from ambari_client.model import  host
 __docformat__ = "epytext"
 
+def _add_hosts(root_resource, cluster_name , host_list):
+  """
+  Adds a hosts to a cluster.
+  @param root_resource: The root Resource object.
+  @param cluster_name: Cluster name
+  @param host_list: list of hosts
+  @return: A StatusModel object
+  """
+  return _add_hosts(root_resource, cluster_name , host_list)
+
+def _add_host(root_resource, cluster_name , host_name , ip, rack_info=None):
+  """
+  Adds a host to a cluster.
+  @param host_name: Host name
+  @param ip: ip of Host 
+  @param rack_info: rack information
+  @return: StatusModel.
+  """
+  return _add_host(root_resource, cluster_name , host_name , ip, rack_info)
+
+
+def _create_hosts(root_resource, host_list):
+  """
+  Create a host
+  @param root_resource: The root Resource.
+  @param host_list: ModelList list of hosts
+  @return: A HostModel object
+  """
+  return host._create_hosts(root_resource, host_list)
 
 def _create_host(root_resource, host_name, ip, rack_info=None):
   """
@@ -28,7 +57,7 @@ def _create_host(root_resource, host_name, ip, 
rack_info=None):
   @param rack_info: Rack id. Default None
   @return: A HostModel object
   """
-  return host._create_host(root_resource, host_name, ip, rack_info=None)
+  return host._create_host(root_resource, host_name, ip, rack_info)
 
 
 def _get_host(root_resource, host_name):
@@ -40,6 +69,15 @@ def _get_host(root_resource, host_name):
   """
   return host._get_host(root_resource, host_name)
 
+def _get_cluster_host(root_resource, cluster_name, host_name):
+  """
+  Lookup a host by name
+  @param root_resource: The root Resource.
+  @param host_name: Host name
+  @return: A HostModel object
+  """
+  return host.get_cluster_host(root_resource, cluster_name, host_name)
+
 
 
 
@@ -51,6 +89,14 @@ def _get_all_hosts(root_resource):
   """
   return host._get_all_hosts(root_resource)
 
+def _get_all_cluster_hosts(root_resource, cluster_name):
+  """
+  Get all cluster hosts
+  @param root_resource: The root Resource.
+  @return: A list of HostModel objects.
+  """
+  return host._get_all_cluster_hosts(root_resource, cluster_name)
+
 
 def _delete_host(root_resource, host_name):
   """
@@ -61,6 +107,15 @@ def _delete_host(root_resource, host_name):
   """
   return host._delete_host(root_resource, host_name)
 
+def _delete_cluster_host(root_resource, cluster_name , host_name):
+  """
+  Delete a cluster host by id
+  @param root_resource: The root Resource.
+  @param host_name: Host name
+  @return: The deleted HostModel object
+  """
+  return host._delete_cluster_host(root_resource, cluster_name , host_name)
+
 
 def _bootstrap_hosts(root_resource , hosts_list , ssh_key):
   """

http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/e55a1ecf/ambari-client/src/test/python/TestAmbariClient.py
----------------------------------------------------------------------
diff --git a/ambari-client/src/test/python/TestAmbariClient.py 
b/ambari-client/src/test/python/TestAmbariClient.py
index 3f97009..0fba169 100755
--- a/ambari-client/src/test/python/TestAmbariClient.py
+++ b/ambari-client/src/test/python/TestAmbariClient.py
@@ -19,7 +19,7 @@ limitations under the License.
 '''
 
 
-from mock import MagicMock, patch
+from mock.mock import MagicMock, patch
 from ambari_client.ambari_api import  AmbariClient 
 
 import unittest

Reply via email to