From 68e65877482188ac43949ede7b7456033cd3c96b Mon Sep 17 00:00:00 2001 From: Yanis Guenane Date: Tue, 7 Aug 2018 20:12:36 +0200 Subject: [PATCH] Vultr: Ensure facts works when no resource exists (#43603) Vultr API is being inconsisten in what it returns. An empty list when no resources exists, but a dict of dict when they do. The case needs to be handled so the module do not fail. An extra test has been added. --- lib/ansible/modules/cloud/vultr/vr_firewall_group_facts.py | 3 +++ lib/ansible/modules/cloud/vultr/vr_ssh_key_facts.py | 3 +++ lib/ansible/modules/cloud/vultr/vr_startup_script_facts.py | 3 +++ test/legacy/roles/vr_firewall_group_facts/tasks/main.yml | 3 +++ test/legacy/roles/vr_ssh_key_facts/tasks/main.yml | 3 +++ test/legacy/roles/vr_startup_script_facts/tasks/main.yml | 3 +++ 6 files changed, 18 insertions(+) diff --git a/lib/ansible/modules/cloud/vultr/vr_firewall_group_facts.py b/lib/ansible/modules/cloud/vultr/vr_firewall_group_facts.py index 29326a87329..46161ca6095 100644 --- a/lib/ansible/modules/cloud/vultr/vr_firewall_group_facts.py +++ b/lib/ansible/modules/cloud/vultr/vr_firewall_group_facts.py @@ -103,6 +103,9 @@ class AnsibleVultrFirewallGroupFacts(Vultr): def parse_fw_group_list(fwgroups_list): + if not fwgroups_list: + return [] + return [group for id, group in fwgroups_list.items()] diff --git a/lib/ansible/modules/cloud/vultr/vr_ssh_key_facts.py b/lib/ansible/modules/cloud/vultr/vr_ssh_key_facts.py index b733dcaf6e2..fe0feb0711f 100644 --- a/lib/ansible/modules/cloud/vultr/vr_ssh_key_facts.py +++ b/lib/ansible/modules/cloud/vultr/vr_ssh_key_facts.py @@ -98,6 +98,9 @@ class AnsibleVultrSSHKeyFacts(Vultr): def parse_keys_list(keys_list): + if not keys_list: + return [] + return [key for id, key in keys_list.items()] diff --git a/lib/ansible/modules/cloud/vultr/vr_startup_script_facts.py b/lib/ansible/modules/cloud/vultr/vr_startup_script_facts.py index 2388a586563..7918b28badd 100644 --- a/lib/ansible/modules/cloud/vultr/vr_startup_script_facts.py +++ b/lib/ansible/modules/cloud/vultr/vr_startup_script_facts.py @@ -102,6 +102,9 @@ class AnsibleVultrStartupScriptFacts(Vultr): def parse_startupscript_list(startupscipts_list): + if not startupscipts_list: + return [] + return [startupscript for id, startupscript in startupscipts_list.items()] diff --git a/test/legacy/roles/vr_firewall_group_facts/tasks/main.yml b/test/legacy/roles/vr_firewall_group_facts/tasks/main.yml index 80639ab89ca..d4cb3695b45 100644 --- a/test/legacy/roles/vr_firewall_group_facts/tasks/main.yml +++ b/test/legacy/roles/vr_firewall_group_facts/tasks/main.yml @@ -1,6 +1,9 @@ # Copyright (c) 2018, Yanis Guenane # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) --- +- name: test gather vultr firewall group facts - empty resources + vr_firewall_group_facts: + - name: Create the firewall group vr_firewall_group: name: '{{ firewall_group_name }}' diff --git a/test/legacy/roles/vr_ssh_key_facts/tasks/main.yml b/test/legacy/roles/vr_ssh_key_facts/tasks/main.yml index af48277e734..d7b82cb6e74 100644 --- a/test/legacy/roles/vr_ssh_key_facts/tasks/main.yml +++ b/test/legacy/roles/vr_ssh_key_facts/tasks/main.yml @@ -1,6 +1,9 @@ # Copyright (c) 2018, Yanis Guenane # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) --- +- name: test gather vultr ssh key fact - empty resources + vr_ssh_key_facts: + - name: Upload an ssh key vr_ssh_key: name: '{{ ssh_key_name }}' diff --git a/test/legacy/roles/vr_startup_script_facts/tasks/main.yml b/test/legacy/roles/vr_startup_script_facts/tasks/main.yml index 4808aaa3062..28325a50e93 100644 --- a/test/legacy/roles/vr_startup_script_facts/tasks/main.yml +++ b/test/legacy/roles/vr_startup_script_facts/tasks/main.yml @@ -1,6 +1,9 @@ # Copyright (c) 2018, Yanis Guenane # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) --- +- name: test gather vultr startup script facts - empty resources + vr_startup_script_facts: + - name: Create the script vr_startup_script: name: '{{ startup_script_name }}'