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.
This commit is contained in:
Yanis Guenane 2018-08-13 16:59:27 +02:00 committed by René Moser
parent ed29cd7f44
commit fd554e9d75

View file

@ -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))