From fd554e9d75d8489c94370c16403a260222128139 Mon Sep 17 00:00:00 2001 From: Yanis Guenane Date: Mon, 13 Aug 2018 16:59:27 +0200 Subject: [PATCH] vultr.py: query_resource_by_key Handle list and dict (#43301) The Vultr API is inconsistent in the type of the value it returns based on the resources. While most of the time it will be a dict, for some resources it will be a list (/v1/user/list, /v1/block/list). query_resource_by_key() fails if the return value isn't a dict (.items() does not exist on list). This patch aims to support both list and dict. --- lib/ansible/module_utils/vultr.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/lib/ansible/module_utils/vultr.py b/lib/ansible/module_utils/vultr.py index 641e4e656fd..e26ae15208b 100644 --- a/lib/ansible/module_utils/vultr.py +++ b/lib/ansible/module_utils/vultr.py @@ -219,13 +219,20 @@ class Vultr: if not r_list: return {} - - for r_id, r_data in r_list.items(): - if str(r_data[key]) == str(value): - self.api_cache.update({ - resource: r_data - }) - return r_data + elif isinstance(r_list, list): + for r_data in r_list: + if str(r_data[key]) == str(value): + self.api_cache.update({ + resource: r_data + }) + return r_data + elif isinstance(r_list, dict): + for r_id, r_data in r_list.items(): + if str(r_data[key]) == str(value): + self.api_cache.update({ + resource: r_data + }) + return r_data self.module.fail_json(msg="Could not find %s with %s: %s" % (resource, key, value))