From 5143f52def0beb4d19d573e6d8d249be2022c5ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Moser?= Date: Thu, 13 Jun 2019 07:05:00 +0200 Subject: [PATCH] vultr: vultr_ssh_key_facts -> info (#57571) * vultr: deprecate vultr_ssh_key_facts * vultr: rename tests * vultr: adjust tests * vultr: new vultr_ssh_key_info module * vultr: test: vultr_ssh_key_info: improve tests * update docs --- ...h_key_facts.py => _vultr_ssh_key_facts.py} | 6 +- .../modules/cloud/vultr/vultr_ssh_key_info.py | 137 ++++++++++++++++++ .../vultr_ssh_key_facts/tasks/main.yml | 34 ----- .../aliases | 0 .../defaults/main.yml | 2 +- .../targets/vultr_ssh_key_info/tasks/main.yml | 44 ++++++ 6 files changed, 187 insertions(+), 36 deletions(-) rename lib/ansible/modules/cloud/vultr/{vultr_ssh_key_facts.py => _vultr_ssh_key_facts.py} (94%) create mode 100644 lib/ansible/modules/cloud/vultr/vultr_ssh_key_info.py delete mode 100644 test/integration/targets/vultr_ssh_key_facts/tasks/main.yml rename test/integration/targets/{vultr_ssh_key_facts => vultr_ssh_key_info}/aliases (100%) rename test/integration/targets/{vultr_ssh_key_facts => vultr_ssh_key_info}/defaults/main.yml (88%) create mode 100644 test/integration/targets/vultr_ssh_key_info/tasks/main.yml diff --git a/lib/ansible/modules/cloud/vultr/vultr_ssh_key_facts.py b/lib/ansible/modules/cloud/vultr/_vultr_ssh_key_facts.py similarity index 94% rename from lib/ansible/modules/cloud/vultr/vultr_ssh_key_facts.py rename to lib/ansible/modules/cloud/vultr/_vultr_ssh_key_facts.py index 18682683572..f325b03654d 100644 --- a/lib/ansible/modules/cloud/vultr/vultr_ssh_key_facts.py +++ b/lib/ansible/modules/cloud/vultr/_vultr_ssh_key_facts.py @@ -8,7 +8,7 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type ANSIBLE_METADATA = {'metadata_version': '1.1', - 'status': ['preview'], + 'status': ['deprecated'], 'supported_by': 'community'} DOCUMENTATION = r''' @@ -19,6 +19,10 @@ description: - Gather facts about SSH keys available. version_added: "2.7" author: "Yanis Guenane (@Spredzy)" +deprecated: + removed_in: "2.12" + why: Transformed into an info module. + alternative: Use M(vultr_ssh_key_info) instead. extends_documentation_fragment: vultr ''' diff --git a/lib/ansible/modules/cloud/vultr/vultr_ssh_key_info.py b/lib/ansible/modules/cloud/vultr/vultr_ssh_key_info.py new file mode 100644 index 00000000000..6abda518d57 --- /dev/null +++ b/lib/ansible/modules/cloud/vultr/vultr_ssh_key_info.py @@ -0,0 +1,137 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +# +# (c) 2018, Yanis Guenane +# (c) 2019, René Moser + +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +ANSIBLE_METADATA = {'metadata_version': '1.1', + 'status': ['preview'], + 'supported_by': 'community'} + +DOCUMENTATION = r''' +--- +module: vultr_ssh_key_info +short_description: Get infos about the Vultr SSH keys available. +description: + - Get infos about SSH keys available. +version_added: "2.9" +author: + - "Yanis Guenane (@Spredzy)" + - "René Moser (@resmo)" +extends_documentation_fragment: vultr +''' + +EXAMPLES = r''' +- name: Get Vultr SSH keys infos + vultr_ssh_key_info: + register: result + +- name: Print the infos + debug: + var: result.vultr_ssh_key_info +''' + +RETURN = r''' +--- +vultr_api: + description: Response from Vultr API with a few additions/modification + returned: success + type: complex + contains: + api_account: + description: Account used in the ini file to select the key + returned: success + type: str + sample: default + api_timeout: + description: Timeout used for the API requests + returned: success + type: int + sample: 60 + api_retries: + description: Amount of max retries for the API requests + returned: success + type: int + sample: 5 + api_endpoint: + description: Endpoint used for the API requests + returned: success + type: str + sample: "https://api.vultr.com" +vultr_ssh_key_info: + description: Response from Vultr API as list + returned: success + type: complex + contains: + id: + description: ID of the ssh key + returned: success + type: str + sample: 5904bc6ed9234 + name: + description: Name of the ssh key + returned: success + type: str + sample: my ssh key + date_created: + description: Date the ssh key was created + returned: success + type: str + sample: "2017-08-26 12:47:48" + ssh_key: + description: SSH public key + returned: success + type: str + sample: "ssh-rsa AA... someother@example.com" +''' + +from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils.vultr import ( + Vultr, + vultr_argument_spec, +) + + +class AnsibleVultrSSHKeyInfo(Vultr): + + def __init__(self, module): + super(AnsibleVultrSSHKeyInfo, self).__init__(module, "vultr_ssh_key_info") + + self.returns = { + 'SSHKEYID': dict(key='id'), + 'name': dict(), + 'ssh_key': dict(), + 'date_created': dict(), + } + + def get_sshkeys(self): + return self.api_query(path="/v1/sshkey/list") + + +def parse_keys_list(keys_list): + if not keys_list: + return [] + + return [key for id, key in keys_list.items()] + + +def main(): + argument_spec = vultr_argument_spec() + + module = AnsibleModule( + argument_spec=argument_spec, + supports_check_mode=True, + ) + + sshkey_info = AnsibleVultrSSHKeyInfo(module) + result = sshkey_info.get_result(parse_keys_list(sshkey_info.get_sshkeys())) + module.exit_json(**result) + + +if __name__ == '__main__': + main() diff --git a/test/integration/targets/vultr_ssh_key_facts/tasks/main.yml b/test/integration/targets/vultr_ssh_key_facts/tasks/main.yml deleted file mode 100644 index 426228d0ea8..00000000000 --- a/test/integration/targets/vultr_ssh_key_facts/tasks/main.yml +++ /dev/null @@ -1,34 +0,0 @@ -# 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 - vultr_ssh_key_facts: - -- name: Upload an ssh key - vultr_ssh_key: - name: '{{ ssh_key_name }}' - ssh_key: '{{ ssh_key_content }}' - -- name: test gather vultr ssh key facts in check mode - vultr_ssh_key_facts: - check_mode: yes - -- name: verify test gather vultr ssh key facts in check mode - assert: - that: - - ansible_facts.vultr_ssh_key_facts|selectattr('name','equalto','{{ ssh_key_name }}') | list | count == 1 - - ansible_facts.vultr_ssh_key_facts|selectattr('ssh_key','equalto','{{ ssh_key_content }}') | list | count == 1 - -- name: test gather vultr ssh key fact - vultr_ssh_key_facts: - -- name: verify test gather vultr ssh key facts - assert: - that: - - ansible_facts.vultr_ssh_key_facts|selectattr('name','equalto','{{ ssh_key_name }}') | list | count == 1 - - ansible_facts.vultr_ssh_key_facts|selectattr('ssh_key','equalto','{{ ssh_key_content }}') | list | count == 1 - -- name: Destroy the ssh key - vultr_ssh_key: - name: ansibletest-sshkey - state: absent diff --git a/test/integration/targets/vultr_ssh_key_facts/aliases b/test/integration/targets/vultr_ssh_key_info/aliases similarity index 100% rename from test/integration/targets/vultr_ssh_key_facts/aliases rename to test/integration/targets/vultr_ssh_key_info/aliases diff --git a/test/integration/targets/vultr_ssh_key_facts/defaults/main.yml b/test/integration/targets/vultr_ssh_key_info/defaults/main.yml similarity index 88% rename from test/integration/targets/vultr_ssh_key_facts/defaults/main.yml rename to test/integration/targets/vultr_ssh_key_info/defaults/main.yml index 00db51ad70a..63bda99f2ee 100644 --- a/test/integration/targets/vultr_ssh_key_facts/defaults/main.yml +++ b/test/integration/targets/vultr_ssh_key_info/defaults/main.yml @@ -1,4 +1,4 @@ --- vultr_resource_prefix: "vultr_test_prefix" ssh_key_name: "{{ vultr_resource_prefix }}-sshkey" -ssh_key_content: ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC+ZFQv3MyjtL1BMpSA0o0gIkzLVVC711rthT29hBNeORdNowQ7FSvVWUdAbTq00U7Xzak1ANIYLJyn+0r7olsdG4XEiUR0dqgC99kbT/QhY5mLe5lpl7JUjW9ctn00hNmt+TswpatCKWPNwdeAJT2ERynZaqPobENgvIq7jfOFWQIVew7qrewtqwerqwrewUr2Cdq7Nb7U0XFXh3x1p0v0+MbL4tiJwPlMAGvFTKIMt+EaA+AsRIxiOo9CMk5ZuOl9pT8h5vNuEOcvS0qx4v44EAD2VOsCVCcrPNMcpuSzZP8dRTGU9wRREAWXngD0Zq9YJMH38VTxHiskoBw1NnPz ansibletest@sshkey +ssh_key_content: ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC+ZFQv3MyjtL1BMpSA0o0gIkzLVVC711rthT29hBNeORdNowQ7FSvVWUdAbTq00U7Xzak1ANIYLJyn+0r7olsdG4XEiUR0dqgC99kbT/QhY5mLe5lpl7JUjW9ctn00hNmt+TswpatCKWPNwdeAJT2ERynZaqPobENgvIq7jfOFWQIVew7qrewtqwerqwrewUr2Cdq7Nb7U0XFXh3x1p0v0+MbL4tiJwPlMAGvFTKIMt+EaA+AsRIxiOo9CMk5ZuOl9pT8h5vNuEOcvS0qx4v44EAD2VOsCVCcrPNMcpuSzZP8dRTGU9wRREAWXngD0Zq9YJMH38VTxHiskoBw1NnPz ansibletest-{{ vultr_resource_prefix }}@sshkey diff --git a/test/integration/targets/vultr_ssh_key_info/tasks/main.yml b/test/integration/targets/vultr_ssh_key_info/tasks/main.yml new file mode 100644 index 00000000000..6a44144b1b3 --- /dev/null +++ b/test/integration/targets/vultr_ssh_key_info/tasks/main.yml @@ -0,0 +1,44 @@ +# Copyright (c) 2018, Yanis Guenane +# Copyright (c) 2019, René Moser +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) +--- +- name: test get vultr ssh key info - empty resources + vultr_ssh_key_info: + register: result + +- name: verify test get vultr ssh key infos in check mode + assert: + that: + - result.vultr_ssh_key_info|selectattr('name','equalto','{{ ssh_key_name }}') | list | count == 0 + - result.vultr_ssh_key_info|selectattr('ssh_key','equalto','{{ ssh_key_content }}') | list | count == 0 + +- name: Upload an ssh key + vultr_ssh_key: + name: '{{ ssh_key_name }}' + ssh_key: '{{ ssh_key_content }}' + +- name: test get vultr ssh key infos in check mode + vultr_ssh_key_info: + check_mode: yes + register: result + +- name: verify test get vultr ssh key infos in check mode + assert: + that: + - result.vultr_ssh_key_info|selectattr('name','equalto','{{ ssh_key_name }}') | list | count == 1 + - result.vultr_ssh_key_info|selectattr('ssh_key','equalto','{{ ssh_key_content }}') | list | count == 1 + +- name: test get vultr ssh key info + vultr_ssh_key_info: + register: result + +- name: verify test get vultr ssh key infos + assert: + that: + - result.vultr_ssh_key_info|selectattr('name','equalto','{{ ssh_key_name }}') | list | count == 1 + - result.vultr_ssh_key_info|selectattr('ssh_key','equalto','{{ ssh_key_content }}') | list | count == 1 + +- name: Destroy the ssh key + vultr_ssh_key: + name: '{{ ssh_key_name }}' + state: absent