From 1d5b9c33b533ff6ff04d407cf7e8dd14e93d233a Mon Sep 17 00:00:00 2001 From: "Joshua M. Boniface" Date: Wed, 2 Dec 2020 19:15:33 -0500 Subject: [PATCH] Unify handling of API list returns Ensure that every API return is handled appropriately as it is a list now. --- client-cli/cli_lib/ceph.py | 44 ++++++++++++++++++++++--- client-cli/cli_lib/network.py | 33 +++++++++++++++++-- client-cli/cli_lib/node.py | 11 ++++++- client-cli/cli_lib/provisioner.py | 55 ++++++++++++++++++++++++++++--- client-cli/cli_lib/vm.py | 10 +++--- 5 files changed, 135 insertions(+), 18 deletions(-) diff --git a/client-cli/cli_lib/ceph.py b/client-cli/cli_lib/ceph.py index 34acc3b1..5915ec00 100644 --- a/client-cli/cli_lib/ceph.py +++ b/client-cli/cli_lib/ceph.py @@ -164,7 +164,16 @@ def ceph_osd_info(config, osd): response = call_api(config, 'get', '/storage/ceph/osd/{osd}'.format(osd=osd)) if response.status_code == 200: - return True, response.json() + if isinstance(response.json(), list) and len(response.json()) != 1: + # No exact match; return not found + return False, "OSD not found." + else: + # Return a single instance if the response is a list + if isinstance(response.json(), list): + return True, response.json()[0] + # This shouldn't happen, but is here just in case + else: + return True, response.json() else: return False, response.json().get('message', '') @@ -552,7 +561,16 @@ def ceph_pool_info(config, pool): response = call_api(config, 'get', '/storage/ceph/pool/{pool}'.format(pool=pool)) if response.status_code == 200: - return True, response.json() + if isinstance(response.json(), list) and len(response.json()) != 1: + # No exact match; return not found + return False, "Pool not found." + else: + # Return a single instance if the response is a list + if isinstance(response.json(), list): + return True, response.json()[0] + # This shouldn't happen, but is here just in case + else: + return True, response.json() else: return False, response.json().get('message', '') @@ -829,7 +847,16 @@ def ceph_volume_info(config, pool, volume): response = call_api(config, 'get', '/storage/ceph/volume/{pool}/{volume}'.format(volume=volume, pool=pool)) if response.status_code == 200: - return True, response.json() + if isinstance(response.json(), list) and len(response.json()) != 1: + # No exact match; return not found + return False, "Volume not found." + else: + # Return a single instance if the response is a list + if isinstance(response.json(), list): + return True, response.json()[0] + # This shouldn't happen, but is here just in case + else: + return True, response.json() else: return False, response.json().get('message', '') @@ -1103,7 +1130,16 @@ def ceph_snapshot_info(config, pool, volume, snapshot): response = call_api(config, 'get', '/storage/ceph/snapshot/{pool}/{volume}/{snapshot}'.format(snapshot=snapshot, volume=volume, pool=pool)) if response.status_code == 200: - return True, response.json() + if isinstance(response.json(), list) and len(response.json()) != 1: + # No exact match; return not found + return False, "Snapshot not found." + else: + # Return a single instance if the response is a list + if isinstance(response.json(), list): + return True, response.json()[0] + # This shouldn't happen, but is here just in case + else: + return True, response.json() else: return False, response.json().get('message', '') diff --git a/client-cli/cli_lib/network.py b/client-cli/cli_lib/network.py index efa159ff..22dd9d11 100644 --- a/client-cli/cli_lib/network.py +++ b/client-cli/cli_lib/network.py @@ -67,7 +67,16 @@ def net_info(config, net): response = call_api(config, 'get', '/network/{net}'.format(net=net)) if response.status_code == 200: - return True, response.json() + if isinstance(response.json(), list) and len(response.json()) != 1: + # No exact match; return not found + return False, "Network not found." + else: + # Return a single instance if the response is a list + if isinstance(response.json(), list): + return True, response.json()[0] + # This shouldn't happen, but is here just in case + else: + return True, response.json() else: return False, response.json().get('message', '') @@ -196,7 +205,16 @@ def net_dhcp_info(config, net, mac): response = call_api(config, 'get', '/network/{net}/lease/{mac}'.format(net=net, mac=mac)) if response.status_code == 200: - return True, response.json() + if isinstance(response.json(), list) and len(response.json()) != 1: + # No exact match; return not found + return False, "Lease not found." + else: + # Return a single instance if the response is a list + if isinstance(response.json(), list): + return True, response.json()[0] + # This shouldn't happen, but is here just in case + else: + return True, response.json() else: return False, response.json().get('message', '') @@ -281,7 +299,16 @@ def net_acl_info(config, net, description): response = call_api(config, 'get', '/network/{net}/acl/{description}'.format(net=net, description=description)) if response.status_code == 200: - return True, response.json() + if isinstance(response.json(), list) and len(response.json()) != 1: + # No exact match; return not found + return False, "ACL not found." + else: + # Return a single instance if the response is a list + if isinstance(response.json(), list): + return True, response.json()[0] + # This shouldn't happen, but is here just in case + else: + return True, response.json() else: return False, response.json().get('message', '') diff --git a/client-cli/cli_lib/node.py b/client-cli/cli_lib/node.py index 37804caf..039e93c4 100644 --- a/client-cli/cli_lib/node.py +++ b/client-cli/cli_lib/node.py @@ -81,7 +81,16 @@ def node_info(config, node): response = call_api(config, 'get', '/node/{node}'.format(node=node)) if response.status_code == 200: - return True, response.json() + if isinstance(response.json(), list) and len(response.json()) != 1: + # No exact match, return not found + return False, "Node not found." + else: + # Return a single instance if the response is a list + if isinstance(response.json(), list): + return True, response.json()[0] + # This shouldn't happen, but is here just in case + else: + return True, response.json() else: return False, response.json().get('message', '') diff --git a/client-cli/cli_lib/provisioner.py b/client-cli/cli_lib/provisioner.py index 024c7ebd..560cac72 100644 --- a/client-cli/cli_lib/provisioner.py +++ b/client-cli/cli_lib/provisioner.py @@ -42,7 +42,16 @@ def template_info(config, template, template_type): response = call_api(config, 'get', '/provisioner/template/{template_type}/{template}'.format(template_type=template_type, template=template)) if response.status_code == 200: - return True, response.json() + if isinstance(response.json(), list) and len(response.json()) != 1: + # No exact match; return not found + return False, "Template not found." + else: + # Return a single instance if the response is a list + if isinstance(response.json(), list): + return True, response.json()[0] + # This shouldn't happen, but is here just in case + else: + return True, response.json() else: return False, response.json().get('message', '') @@ -171,7 +180,16 @@ def userdata_info(config, userdata): response = call_api(config, 'get', '/provisioner/userdata/{userdata}'.format(userdata=userdata)) if response.status_code == 200: - return True, response.json()[0] + if isinstance(response.json(), list) and len(response.json()) != 1: + # No exact match; return not found + return False, "Userdata not found." + else: + # Return a single instance if the response is a list + if isinstance(response.json(), list): + return True, response.json()[0] + # This shouldn't happen, but is here just in case + else: + return True, response.json() else: return False, response.json().get('message', '') @@ -294,7 +312,16 @@ def script_info(config, script): response = call_api(config, 'get', '/provisioner/script/{script}'.format(script=script)) if response.status_code == 200: - return True, response.json()[0] + if isinstance(response.json(), list) and len(response.json()) != 1: + # No exact match; return not found + return False, "Script not found." + else: + # Return a single instance if the response is a list + if isinstance(response.json(), list): + return True, response.json()[0] + # This shouldn't happen, but is here just in case + else: + return True, response.json() else: return False, response.json().get('message', '') @@ -417,7 +444,16 @@ def ova_info(config, name): response = call_api(config, 'get', '/provisioner/ova/{name}'.format(name=name)) if response.status_code == 200: - return True, response.json()[0] + if isinstance(response.json(), list) and len(response.json()) != 1: + # No exact match; return not found + return False, "OVA not found." + else: + # Return a single instance if the response is a list + if isinstance(response.json(), list): + return True, response.json()[0] + # This shouldn't happen, but is here just in case + else: + return True, response.json() else: return False, response.json().get('message', '') @@ -504,7 +540,16 @@ def profile_info(config, profile): response = call_api(config, 'get', '/provisioner/profile/{profile}'.format(profile=profile)) if response.status_code == 200: - return True, response.json()[0] + if isinstance(response.json(), list) and len(response.json()) != 1: + # No exact match; return not found + return False, "Profile not found." + else: + # Return a single instance if the response is a list + if isinstance(response.json(), list): + return True, response.json()[0] + # This shouldn't happen, but is here just in case + else: + return True, response.json() else: return False, response.json().get('message', '') diff --git a/client-cli/cli_lib/vm.py b/client-cli/cli_lib/vm.py index e00a521e..7b4d729e 100644 --- a/client-cli/cli_lib/vm.py +++ b/client-cli/cli_lib/vm.py @@ -41,16 +41,16 @@ def vm_info(config, vm): response = call_api(config, 'get', '/vm/{vm}'.format(vm=vm)) if response.status_code == 200: - if isinstance(response.json(), list) and len(response.json()) > 1: + if isinstance(response.json(), list) and len(response.json()) != 1: # No exact match; return not found return False, "VM not found." else: + # Return a single instance if the response is a list if isinstance(response.json(), list): - response = response.json()[0] + return True, response.json()[0] + # This shouldn't happen, but is here just in case else: - response = response.json() - return True, response - return True, response.json() + return True, response.json() else: return False, response.json().get('message', '')