From 577427b1c20dd27bdcd42e417d9ffccfb669b374 Mon Sep 17 00:00:00 2001 From: Yanis Guenane Date: Tue, 17 Jul 2018 16:24:21 +0200 Subject: [PATCH] vultr: Make get_result handle list (#42890) Currently get_result() can only handle a dict. When requiring facts, and when there is more than one fact, get_result() should be able to handle a list of dicts, so the normalization process happens to all the items. --- lib/ansible/module_utils/vultr.py | 35 ++++++++++++++++++------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/lib/ansible/module_utils/vultr.py b/lib/ansible/module_utils/vultr.py index ecb85f8cd91..0dd185c73cc 100644 --- a/lib/ansible/module_utils/vultr.py +++ b/lib/ansible/module_utils/vultr.py @@ -225,20 +225,27 @@ class Vultr: self.module.fail_json(msg="Could not find %s with %s: %s" % (resource, key, value)) + def normalize_result(self, resource): + for search_key, config in self.returns.items(): + if search_key in resource: + if 'convert_to' in config: + if config['convert_to'] == 'int': + resource[search_key] = int(resource[search_key]) + elif config['convert_to'] == 'float': + resource[search_key] = float(resource[search_key]) + elif config['convert_to'] == 'bool': + resource[search_key] = True if resource[search_key] == 'yes' else False + + if 'key' in config: + resource[config['key']] = resource[search_key] + + return resource + def get_result(self, resource): if resource: - for search_key, config in self.returns.items(): - if search_key in resource: - if 'convert_to' in config: - if config['convert_to'] == 'int': - resource[search_key] = int(resource[search_key]) - elif config['convert_to'] == 'float': - resource[search_key] = float(resource[search_key]) - elif config['convert_to'] == 'bool': - resource[search_key] = True if resource[search_key] == 'yes' else False - - if 'key' in config: - self.result[self.namespace][config['key']] = resource[search_key] - else: - self.result[self.namespace][search_key] = resource[search_key] + if isinstance(resource, list): + self.result[self.namespace] = [self.normalize_result(item) for item in resource] + else: + self.result[self.namespace] = self.normalize_result(resource) + return self.result