Updated Branches: refs/heads/trunk e55a1ecf2 -> d98cfd9ca
AMBARI-3244. Ambari Client improve errors handling. (Andrew vi 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/d98cfd9c Tree: http://git-wip-us.apache.org/repos/asf/incubator-ambari/tree/d98cfd9c Diff: http://git-wip-us.apache.org/repos/asf/incubator-ambari/diff/d98cfd9c Branch: refs/heads/trunk Commit: d98cfd9ca0ddd24f1e293bf0d3e877118a171765 Parents: e55a1ec Author: Mahadev Konar <[email protected]> Authored: Mon Sep 16 08:37:58 2013 -0700 Committer: Mahadev Konar <[email protected]> Committed: Mon Sep 16 08:37:58 2013 -0700 ---------------------------------------------------------------------- .../python/ambari_client/core/rest_resource.py | 26 +++++------- .../main/python/ambari_client/model/cluster.py | 16 +++---- .../python/ambari_client/model/component.py | 16 +++---- .../python/ambari_client/model/configuration.py | 16 +++---- .../src/main/python/ambari_client/model/host.py | 33 ++++++++------- .../main/python/ambari_client/model/service.py | 18 ++++---- .../main/python/ambari_client/model/stack.py | 8 ++-- .../main/python/ambari_client/model/status.py | 3 ++ .../main/python/ambari_client/model/utils.py | 44 +++++++++++++++++++- .../src/test/python/TestAmbariClient.py | 30 +++++++------ 10 files changed, 129 insertions(+), 81 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/d98cfd9c/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 f7e2e23..506308c 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 @@ -63,23 +63,19 @@ class RestResource(object): LOG.debug ("RESPONSE from the REST request >>>>>>> \n" + str(resp)) LOG.debug ("\n===========================================================") - #take care of REST calls with no response - if not resp and (code != 200 and code != 201): - 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 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)) + try: - if (code == 200 or code == 201) and not resp: - return {"status":code} - json_dict = json.loads(resp) - return json_dict + isOK = (code == 200 or code == 201) + + if isOK and not resp: + json_dict = {"status":code} + else: + json_dict = json.loads(resp) + + return json_dict, isOK except Exception, ex: - LOG.error('JSON decode error: %s' % (resp,)) - raise ex + LOG.error("Command '%s %s' failed with error %s\n%s" % (http_method, path, code ,resp)) + return {"status":code , "message":"Command '%s %s' failed with error %s" % (http_method, path, code)}, False def get(self, path=None): http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/d98cfd9c/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 75ac0c9..d3ee609 100755 --- a/ambari-client/src/main/python/ambari_client/model/cluster.py +++ b/ambari-client/src/main/python/ambari_client/model/cluster.py @@ -31,8 +31,8 @@ def _get_cluster(resource_root, cluster_name): @param cluster_name: cluster_name @return: A ClusterModel object """ - dic = resource_root.get("%s/%s" % (paths.CLUSTERS_PATH, cluster_name)) - return utils.ModelUtils.create_model(ClusterModel , dic, resource_root, "Clusters") + dic, is_success = resource_root.get("%s/%s" % (paths.CLUSTERS_PATH, cluster_name)) + return utils.ModelUtils.create_model_or_error(ClusterModel , dic, resource_root, "Clusters", is_success) def _get_all_clusters(root_resource): @@ -41,8 +41,8 @@ def _get_all_clusters(root_resource): @param root_resource: The root Resource . @return: A list of ClusterModel objects. """ - dic = root_resource.get(paths.CLUSTERS_PATH) - return utils.ModelUtils.get_model_list(ModelList, ClusterModel, dic, root_resource , "Clusters") + dic, is_success = root_resource.get(paths.CLUSTERS_PATH) + return utils.ModelUtils.get_model_list_or_error(ModelList, ClusterModel, dic, root_resource , "Clusters", is_success) def _create_cluster(root_resource, cluster_name, version): @@ -66,7 +66,7 @@ def _delete_cluster(root_resource, cluster_name): @param root_resource: The root Resource . @param name: Cluster name """ - resp = root_resource.delete("%s/%s" % (paths.CLUSTERS_PATH, cluster_name)) + resp, is_success = root_resource.delete("%s/%s" % (paths.CLUSTERS_PATH, cluster_name)) return utils.ModelUtils.create_model(status.StatusModel, resp, root_resource, "NO_KEY") @@ -78,7 +78,7 @@ def _install_all_services(root_resource, cluster_name): """ cpath = paths.CLUSTER_START_ALL_SERVICES % cluster_name data = {"RequestInfo": {"context" :"Install Services"}, "Body": {"ServiceInfo": {"state": "INSTALLED"}}} - resp = root_resource.put(path=cpath , payload=data) + resp, is_success = root_resource.put(path=cpath , payload=data) return utils.ModelUtils.create_model(status.StatusModel, resp, root_resource, "NO_KEY") @@ -90,7 +90,7 @@ def _stop_all_services(root_resource, cluster_name): """ cpath = paths.CLUSTER_STOP_ALL_SERVICES % cluster_name data = {"RequestInfo": {"context" :"Stop All Services"}, "Body": {"ServiceInfo": {"state": "INSTALLED"}}} - resp = root_resource.put(path=cpath , payload=data) + resp, is_success = root_resource.put(path=cpath , payload=data) return utils.ModelUtils.create_model(status.StatusModel, resp, root_resource, "NO_KEY") @@ -104,7 +104,7 @@ def _start_all_services(root_resource, cluster_name , run_smoke_test=False): if run_smoke_test: cpath = "%s&%s" % (cpath, "params/run_smoke_test=true¶ms/reconfigure_client=false") data = {"RequestInfo": {"context" :"Start All Services"}, "Body": {"ServiceInfo": {"state": "STARTED"}}} - resp = root_resource.put(path=cpath , payload=data) + resp, is_success = root_resource.put(path=cpath , payload=data) if isinstance(resp, dict) and resp.has_key("Requests"): resp = resp["Requests"] return utils.ModelUtils.create_model(status.StatusModel, resp, root_resource, "NO_KEY") http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/d98cfd9c/ambari-client/src/main/python/ambari_client/model/component.py ---------------------------------------------------------------------- diff --git a/ambari-client/src/main/python/ambari_client/model/component.py b/ambari-client/src/main/python/ambari_client/model/component.py index 586f21a..b9d879a 100755 --- a/ambari-client/src/main/python/ambari_client/model/component.py +++ b/ambari-client/src/main/python/ambari_client/model/component.py @@ -26,28 +26,28 @@ LOG = logging.getLogger(__name__) def get_host_components(resource_root, cluster_name , host_name): path = paths.HOSTS_COMPONENTS_PATH % (cluster_name, host_name) - dic = resource_root.get(path) - return utils.ModelUtils.get_model_list(ModelList, ComponentModel, dic, resource_root , "HostRoles") + dic, is_success = resource_root.get(path) + return utils.ModelUtils.get_model_list_or_error(ModelList, ComponentModel, dic, resource_root , "HostRoles", is_success) def get_host_component(resource_root, cluster_name , host_name , component_name): path = paths.HOSTS_COMPONENT_PATH % (cluster_name, host_name , component_name) - dic = resource_root.get(path) - comp_model = utils.ModelUtils.create_model(ComponentModel, dic, resource_root , "HostRoles" , status.StatusModel) + dic, is_success = resource_root.get(path) + comp_model = utils.ModelUtils.create_model_or_error(ComponentModel, dic, resource_root , "HostRoles" , is_success) #comp_model._setattr('host_name', dic["items"][0]['HostRoles']['host_name']) return comp_model def _get_service_components(resource_root, cluster_name , service_name): path = paths.SERVICE_COMPONENTS_PATH % (cluster_name, service_name) - dic = resource_root.get(path) - return utils.ModelUtils.get_model_list(ModelList, ComponentModel, dic, resource_root , "ServiceComponentInfo") + dic, is_success = resource_root.get(path) + return utils.ModelUtils.get_model_list_or_error(ModelList, ComponentModel, dic, resource_root , "ServiceComponentInfo", is_success) def _get_service_component(resource_root, cluster_name , service_name , component_name): path = paths.SERVICE_COMPONENT_PATH % (cluster_name, service_name , component_name) - dic = resource_root.get(path) - return utils.ModelUtils.create_model(ComponentModel, dic, resource_root , "ServiceComponentInfo") + dic, is_success = resource_root.get(path) + return utils.ModelUtils.create_model_or_error(ComponentModel, dic, resource_root , "ServiceComponentInfo", is_success) http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/d98cfd9c/ambari-client/src/main/python/ambari_client/model/configuration.py ---------------------------------------------------------------------- diff --git a/ambari-client/src/main/python/ambari_client/model/configuration.py b/ambari-client/src/main/python/ambari_client/model/configuration.py index 222ce5c..32bcd7a 100755 --- a/ambari-client/src/main/python/ambari_client/model/configuration.py +++ b/ambari-client/src/main/python/ambari_client/model/configuration.py @@ -30,11 +30,13 @@ def _get_configuration(resource_root, cluster_name , type , tag="version1"): @param type: type of config @return: A ConfigModel object """ - dic = resource_root.get(paths.CONFIGURATION_PATH % (cluster_name, type, tag)) - config_model = utils.ModelUtils.create_model(ConfigModel , dic["items"][0], resource_root, "NO_KEY") - ref_clss = utils.getREF_class_name("cluster_name") - config_model._setattr(ref_clss, dic["items"][0]['Config']['cluster_name']) - return config_model + dic, is_success = resource_root.get(paths.CONFIGURATION_PATH % (cluster_name, type, tag)) + config_model, err_model = utils.ModelUtils.create_model_or_error(ConfigModel , dic["items"][0], resource_root, "NO_KEY", is_success) + if not err_model: + ref_clss = utils.getREF_class_name("cluster_name") + config_model._setattr(ref_clss, dic["items"][0]['Config']['cluster_name']) + + return config_model, err_model def _update_configuration(resource_root, cluster_name , type , tag , config_model): @@ -61,7 +63,7 @@ def _add_config(root_resource, cluster_name, type, tag , properties): """ cpath = paths.CLUSTERS_CONFIG_PATH % cluster_name data = {"Clusters":{"desired_configs":{"type":type, "tag":tag, "properties":properties}}} - resp = root_resource.put(path=cpath , payload=data) + resp, is_success = root_resource.put(path=cpath , payload=data) return utils.ModelUtils.create_model(status.StatusModel, resp, root_resource, "NO_KEY") @@ -75,7 +77,7 @@ def _create_config(root_resource, cluster_name, type, tag , properties): """ cpath = paths.CLUSTERS_CONFIG_PATH % cluster_name data = {"type":type, "tag":tag, "properties":properties} - resp = root_resource.put(path=cpath , payload=data) + resp, is_success = root_resource.put(path=cpath , payload=data) return utils.ModelUtils.create_model(status.StatusModel, resp, root_resource, "NO_KEY") http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/d98cfd9c/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 2a0e101..5c6ce8d 100755 --- a/ambari-client/src/main/python/ambari_client/model/host.py +++ b/ambari-client/src/main/python/ambari_client/model/host.py @@ -34,12 +34,13 @@ def _get_host(root_resource, host_name): @param root_resource: The root Resource object. @param cluster_name: Cluster name @param host_name: Host name - @return: A HostModel object + @return model: A HostModel of the host + @return err_resp: StatusModel object of the error response """ path = paths.HOST_PATH % (host_name) - dic = root_resource.get(path) - - return utils.ModelUtils.create_model(HostModel , dic, root_resource, "Hosts") + dic, is_success = root_resource.get(path) + + return utils.ModelUtils.create_model_or_error(HostModel , dic, root_resource, "Hosts", is_success) def _get_cluster_host(root_resource, cluster_name , host_name): """ @@ -50,8 +51,8 @@ def _get_cluster_host(root_resource, cluster_name , host_name): @return: A HostModel object """ path = paths.CLUSTER_HOST_PATH % (cluster_name, host_name) - dic = root_resource.get(path) - return utils.ModelUtils.create_model(HostModel , dic, root_resource, "Hosts") + dic, is_success = root_resource.get(path) + return utils.ModelUtils.create_model_or_error(HostModel , dic, root_resource, "Hosts", is_success) @@ -67,7 +68,7 @@ def _create_hosts(root_resource, 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(paths.HOSTS_PATH, payload=data) + resp, is_success = 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): @@ -93,7 +94,7 @@ def _add_hosts(root_resource, cluster_name , host_list): cpath = paths.HOSTS_CREATE_PATH % (cluster_name) 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) + resp, is_success = root_resource.post(path=cpath, payload=data) return utils.ModelUtils.create_model(status.StatusModel, resp, root_resource, "NO_KEY") @@ -120,7 +121,7 @@ def _assign_role(root_resource, cluster_name , host_name , component_name): """ data = {"host_components":[{"HostRoles":{"component_name":component_name}}]} cpath = paths.HOSTS_ASSIGN_ROLE % (cluster_name, host_name) - resp = root_resource.post(path=cpath, payload=data) + resp, is_success = root_resource.post(path=cpath, payload=data) return utils.ModelUtils.create_model(status.StatusModel, resp, root_resource, "NO_KEY") @@ -130,7 +131,7 @@ def _get_all_hosts(root_resource): @param root_resource: The root Resource. @return: A list of HostModel objects. """ - dic = root_resource.get(paths.HOSTS_PATH) + dic, is_success = root_resource.get(paths.HOSTS_PATH) return utils.ModelUtils.get_model_list(ModelList, HostModel, dic, root_resource , "Hosts") @@ -143,8 +144,8 @@ def _get_all_cluster_hosts(root_resource, cluster_name): """ path = paths.CLUSTER_HOSTS_PATH % (cluster_name) path = path + '?fields=*' - dic = root_resource.get(path) - return utils.ModelUtils.get_model_list(ModelList, HostModel, dic, root_resource , "Hosts") + dic, is_success = root_resource.get(path) + return utils.ModelUtils.get_model_list_or_error(ModelList, HostModel, dic, root_resource , "Hosts", is_success) def _delete_host(root_resource, host_name): @@ -154,8 +155,8 @@ def _delete_host(root_resource, host_name): @param host_name: Host name @return: StatusModel object """ - resp = root_resource.delete(paths.HOST_PATH % (host_name)) - return utils.ModelUtils.create_model(status.StatusModel, resp, root_resource, "NO_KEY") + resp, is_success = root_resource.delete(paths.HOST_PATH % (host_name)) + return utils.ModelUtils.create_model(status.StatusModel, resp, root_resource, "NO_KEY", is_success) def _delete_cluster_host(root_resource, cluster_name , host_name): @@ -167,7 +168,7 @@ def _delete_cluster_host(root_resource, cluster_name , host_name): @return: StatusModel object """ path = paths.CLUSTER_HOST_PATH % (cluster_name, host_name) - resp = root_resource.delete(path) + resp, is_success = root_resource.delete(path) return utils.ModelUtils.create_model(status.StatusModel, resp, root_resource, "NO_KEY") @@ -179,7 +180,7 @@ def _bootstrap_hosts(root_resource , hosts_list, ssh_key): """ #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") + resp, is_success = 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/d98cfd9c/ambari-client/src/main/python/ambari_client/model/service.py ---------------------------------------------------------------------- diff --git a/ambari-client/src/main/python/ambari_client/model/service.py b/ambari-client/src/main/python/ambari_client/model/service.py index c384473..cafbc29 100755 --- a/ambari-client/src/main/python/ambari_client/model/service.py +++ b/ambari-client/src/main/python/ambari_client/model/service.py @@ -32,8 +32,8 @@ def _get_all_services(resource_root, cluster_name): """ path = paths.SERVICES_PATH % (cluster_name,) path = path + '?fields=*' - dic = resource_root.get(path) - return utils.ModelUtils.get_model_list(ModelList, ServiceModel, dic, resource_root , "ServiceInfo") + dic, is_success = resource_root.get(path) + return utils.ModelUtils.get_model_list_or_error(ModelList, ServiceModel, dic, resource_root , "ServiceInfo", is_success) def _get_service(resource_root, service_name, cluster_name): @@ -44,8 +44,8 @@ def _get_service(resource_root, service_name, cluster_name): @return: A ServiceModel object. """ path = "%s/%s" % (paths.SERVICES_PATH % (cluster_name,), service_name) - dic = resource_root.get(path) - return utils.ModelUtils.create_model(ServiceModel , dic, resource_root, "ServiceInfo") + dic, is_success = resource_root.get(path) + return utils.ModelUtils.create_model_or_error(ServiceModel , dic, resource_root, "ServiceInfo", is_success) def _create_services(root_resource, cluster_name , service_names): @@ -58,7 +58,7 @@ def _create_services(root_resource, cluster_name , service_names): """ data = [{"ServiceInfo":{"service_name":x}} for x in service_names] cpath = paths.SERVICES_PATH % cluster_name - resp = root_resource.post(path=cpath, payload=data) + resp, is_success = root_resource.post(path=cpath, payload=data) return utils.ModelUtils.create_model(status.StatusModel, resp, root_resource, "NO_KEY") @@ -72,7 +72,7 @@ def _create_service(root_resource, cluster_name , service_name): """ data = {"ServiceInfo":{"service_name":service_name}} cpath = paths.SERVICES_PATH % cluster_name - resp = root_resource.post(path=cpath, payload=data) + resp, is_success = root_resource.post(path=cpath, payload=data) return utils.ModelUtils.create_model(status.StatusModel, resp, root_resource, "NO_KEY") @@ -88,7 +88,7 @@ def _create_service_components(root_resource, cluster_name , version , service_n list_componnetinfo = [{"ServiceComponentInfo":{"component_name":x.component_name }} for x in components] data = {"components":list_componnetinfo} cpath = paths.SERVICE_CREATE_PATH % (cluster_name, service_name) - resp = root_resource.post(path=cpath, payload=data) + resp, is_success = root_resource.post(path=cpath, payload=data) return utils.ModelUtils.create_model(status.StatusModel, resp, root_resource, "NO_KEY") @@ -102,7 +102,7 @@ def _create_service_component(root_resource, cluster_name , version , service_na @return: An ServiceModel object """ cpath = paths.SERVICE_COMPONENT_PATH % (cluster_name, service_name, component_name) - resp = root_resource.post(path=cpath, payload=None) + resp, is_success = root_resource.post(path=cpath, payload=None) return utils.ModelUtils.create_model(status.StatusModel, resp, root_resource, "NO_KEY") @@ -114,7 +114,7 @@ def _delete_service(root_resource, service_name, cluster_name): @param cluster_name: Cluster service_name @return: The StatusModel object """ - resp = root_resource.delete("%s/%s" % (paths.SERVICES_PATH % (cluster_name,), service_name)) + resp, is_success = root_resource.delete("%s/%s" % (paths.SERVICES_PATH % (cluster_name,), service_name)) time.sleep(3) return utils.ModelUtils.create_model(status.StatusModel, resp, root_resource, "NO_KEY") http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/d98cfd9c/ambari-client/src/main/python/ambari_client/model/stack.py ---------------------------------------------------------------------- diff --git a/ambari-client/src/main/python/ambari_client/model/stack.py b/ambari-client/src/main/python/ambari_client/model/stack.py index 40088f7..1b6b4be 100755 --- a/ambari-client/src/main/python/ambari_client/model/stack.py +++ b/ambari-client/src/main/python/ambari_client/model/stack.py @@ -28,8 +28,8 @@ def _get_configuration_from_stack(resource_root, version , service_name , tag="v @param type: type of config @return: A ModelList of ConfigModel object """ - dic = resource_root.get(paths.STACK_SERVICES_CONFIG_PATH % (version, service_name)) - return utils.ModelUtils.get_model_list(ModelList, StackConfigModel, dic, resource_root , "StackConfigurations") + dic, is_success = resource_root.get(paths.STACK_SERVICES_CONFIG_PATH % (version, service_name)) + return utils.ModelUtils.get_model_list_or_error(ModelList, StackConfigModel, dic, resource_root , "StackConfigurations", is_success) def _get_components_from_stack(resource_root, version , service_name , tag="version1"): @@ -41,8 +41,8 @@ def _get_components_from_stack(resource_root, version , service_name , tag="vers @return: A ModelList of ConfigModel object """ path = paths.STACK_SERVICES_COMPONENTS_PATH % (version, service_name) - dic = resource_root.get(path) - return utils.ModelUtils.get_model_list(ModelList, StackComponentModel, dic, resource_root , "StackServiceComponents") + dic, is_success = resource_root.get(path) + return utils.ModelUtils.get_model_list_or_error(ModelList, StackComponentModel, dic, resource_root , "StackServiceComponents", is_success) class StackConfigModel(BaseModel): http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/d98cfd9c/ambari-client/src/main/python/ambari_client/model/status.py ---------------------------------------------------------------------- diff --git a/ambari-client/src/main/python/ambari_client/model/status.py b/ambari-client/src/main/python/ambari_client/model/status.py index 1eba35d..d81177b 100755 --- a/ambari-client/src/main/python/ambari_client/model/status.py +++ b/ambari-client/src/main/python/ambari_client/model/status.py @@ -42,6 +42,9 @@ class StatusModel(BaseModel): def get_request_path(self): return paths.REQUEST_PATH % (self._get_id()) + + def is_error(self): + return (self.status != 200 and self.status != 201) def _get_message(self): if hasattr(self, 'message'): http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/d98cfd9c/ambari-client/src/main/python/ambari_client/model/utils.py ---------------------------------------------------------------------- diff --git a/ambari-client/src/main/python/ambari_client/model/utils.py b/ambari-client/src/main/python/ambari_client/model/utils.py index 4a7fa5e..1dbb366 100755 --- a/ambari-client/src/main/python/ambari_client/model/utils.py +++ b/ambari-client/src/main/python/ambari_client/model/utils.py @@ -28,6 +28,28 @@ ref_pkg_dic = {"ClusterModelRef":"ambari_client.model.cluster"} LIST_KEY = "items" class ModelUtils(object): + + @staticmethod + def get_model_list_or_error(member_list_clss, member_cls, collection_dict, resource_root , RESOURCE_KEY_WORD, is_success): + """ + create a model list or error response. + @param member_list_clss : model_list class. + @param model_cls : model class. + @param collection_dict : collection dict used for creating the list of objects. + @param resource_root : resource object. + @param RESOURCE_KEY_WORD : tsake subset of model_dict based on this key. + @return model_list: A ModelList object. + @return err_resp: StatusModel object of the error response + """ + model_list = err_resp = None + + if is_success: + model_list = ModelUtils.get_model_list(member_list_clss, member_cls, collection_dict, resource_root , RESOURCE_KEY_WORD) + else: + from ambari_client.model.status import StatusModel + err_resp = ModelUtils.create_model(StatusModel, collection_dict, resource_root, "NO_KEY") + + return model_list, err_resp @staticmethod def get_model_list(member_list_clss, member_cls, collection_dict, resource_root , RESOURCE_KEY_WORD): @@ -64,7 +86,27 @@ class ModelUtils(object): objects = [ ModelUtils.create_model(member_cls, x, resource_root , RESOURCE_KEY_WORD) for x in json_list_new ] LOG.debug (objects) return member_list_clss(objects) - + + @staticmethod + def create_model_or_error(model_cls, model_dict, resource_root, RESOURCE_KEY_WORD, is_success, exception_cls=None): + """ + create a model or error response. + @param model_cls : model class. + @param model_dict : model dict used for creating the object. + @param resource_root : resource object. + @param RESOURCE_KEY_WORD : tsake subset of model_dict based on this key. + @return model: A model cls object + @return err_resp: StatusModel object of the error response + """ + model = err_resp = None + + if is_success: + model = ModelUtils.create_model(model_cls , model_dict, resource_root, RESOURCE_KEY_WORD, exception_cls) + else: + from ambari_client.model.status import StatusModel + err_resp = ModelUtils.create_model(StatusModel, model_dict, resource_root, "NO_KEY") + + return model, err_resp @staticmethod def create_model(model_cls, model_dict, resource_root, RESOURCE_KEY_WORD, exception_cls=None): http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/d98cfd9c/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 0fba169..e30991f 100755 --- a/ambari-client/src/test/python/TestAmbariClient.py +++ b/ambari-client/src/test/python/TestAmbariClient.py @@ -58,7 +58,7 @@ class TestAmbariClient(unittest.TestCase): http_client_mock = MagicMock() http_client.return_value = http_client_mock - mocked_code = "200" + mocked_code = 200 mocked_content = "text/plain" expected_output = {'items': [{'cluster_name': u'test1', 'version': u'HDP-1.2.1'}]} @@ -68,9 +68,10 @@ class TestAmbariClient(unittest.TestCase): client = AmbariClient("localhost", 8080, "admin", "admin", version=1 , client=http_client_mock) - all_clusters = client.get_all_clusters() + all_clusters, err = client.get_all_clusters() self.assertEqual(len(all_clusters), 1, "There should be a cluster from the response") + self.assertEqual(err, None, "No error should be present") self.assertEqual(all_clusters.to_json_dict(), expected_output, "to_json_dict should convert ModelList") @patch("ambari_client.core.http_client.HttpClient") @@ -82,7 +83,7 @@ class TestAmbariClient(unittest.TestCase): http_client_mock = MagicMock() http_client.return_value = http_client_mock - mocked_code = "200" + mocked_code = 200 mocked_content = "text/plain" linestring = open('json/get_all_hosts.json', 'r').read() @@ -104,7 +105,7 @@ class TestAmbariClient(unittest.TestCase): """ http_client_mock = MagicMock() http_client.returned_obj = http_client_mock - mocked_code = "200" + mocked_code = 200 mocked_content = "text/plain" linestring = open('json/get_cluster.json', 'r').read() @@ -113,9 +114,10 @@ class TestAmbariClient(unittest.TestCase): http_client_mock.invoke.return_value = mocked_response , mocked_code , mocked_content client = AmbariClient("localhost", 8080, "admin", "admin", version=1, client=http_client_mock) - cluster = client.get_cluster('test1') + cluster, err = client.get_cluster('test1') self.assertEqual(cluster.cluster_name, "test1", "cluster_name should be test1 ") + self.assertEqual(err, None, "No error should be present") self.assertEqual(cluster.to_json_dict(), expected_dict_output, "to_json_dict should convert ClusterModel") @@ -128,19 +130,20 @@ class TestAmbariClient(unittest.TestCase): """ http_client_mock = MagicMock() http_client.returned_obj = http_client_mock - mocked_code = "200" + mocked_code = 200 mocked_content = "text/plain" expected_dict_output = {'cluster_name': u'test1', 'version': u'HDP-1.2.1'} http_client_mock.invoke.side_effect = http_client_invoke_side_effects client = AmbariClient("localhost", 8080, "admin", "admin", version=1, client=http_client_mock) - cluster = client.get_cluster('test1') - serviceList = cluster.get_all_services() + cluster, err = client.get_cluster('test1') + serviceList, err = cluster.get_all_services() self.assertEqual(cluster.cluster_name, "test1", "cluster_name should be test1 ") self.assertEqual(cluster.to_json_dict(), expected_dict_output, "to_json_dict should convert ClusterModel") self.assertEqual(len(serviceList), 3, "There should be a 3 services from the response") + self.assertEqual(err, None, "No error should be present") @patch("ambari_client.core.http_client.HttpClient") def test_get_cluster_service_valid(self , http_client): @@ -150,22 +153,23 @@ class TestAmbariClient(unittest.TestCase): """ http_client_mock = MagicMock() http_client.returned_obj = http_client_mock - mocked_code = "200" + mocked_code = 200 mocked_content = "text/plain" expected_dict_output = {'cluster_name': u'test1', 'version': u'HDP-1.2.1'} http_client_mock.invoke.side_effect = http_client_invoke_side_effects client = AmbariClient("localhost", 8080, "admin", "admin", version=1, client=http_client_mock) - cluster = client.get_cluster('test1') - serviceList = cluster.get_all_services() - ganglia = cluster.get_service("GANGLIA") + cluster, err = client.get_cluster('test1') + serviceList, err = cluster.get_all_services() + ganglia, err = cluster.get_service("GANGLIA") self.assertEqual(cluster.cluster_name, "test1", "cluster_name should be test1 ") self.assertEqual(cluster.to_json_dict(), expected_dict_output, "to_json_dict should convert ClusterModel") self.assertEqual(len(serviceList), 3, "There should be a 3 services from the response") self.assertEqual(str(ganglia.state), "STARTED", "The ganglia service state should be fetched as STARTED") self.assertEqual(ganglia.clusterRef.cluster_name, cluster.cluster_name, "The clusterRef value for service should be fetched ") + self.assertEqual(err, None, "No error should be present") @@ -173,7 +177,7 @@ class TestAmbariClient(unittest.TestCase): def http_client_invoke_side_effects(*args, **kwargs): print locals() - mocked_code = "200" + mocked_code = 200 mocked_content = "text/plain" if args[1] == "//clusters/test1": mocked_response = open('json/get_cluster.json', 'r').read()
