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
This commit is contained in:
parent
8e9f29c40c
commit
5143f52def
6 changed files with 187 additions and 36 deletions
|
@ -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
|
||||
'''
|
||||
|
137
lib/ansible/modules/cloud/vultr/vultr_ssh_key_info.py
Normal file
137
lib/ansible/modules/cloud/vultr/vultr_ssh_key_info.py
Normal file
|
@ -0,0 +1,137 @@
|
|||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# (c) 2018, Yanis Guenane <yanis+ansible@guenane.org>
|
||||
# (c) 2019, René Moser <mail@renemoser.net>
|
||||
|
||||
# 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()
|
|
@ -1,34 +0,0 @@
|
|||
# Copyright (c) 2018, Yanis Guenane <yanis+ansible@guenane.org>
|
||||
# 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
|
|
@ -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
|
44
test/integration/targets/vultr_ssh_key_info/tasks/main.yml
Normal file
44
test/integration/targets/vultr_ssh_key_info/tasks/main.yml
Normal file
|
@ -0,0 +1,44 @@
|
|||
# Copyright (c) 2018, Yanis Guenane <yanis+ansible@guenane.org>
|
||||
# Copyright (c) 2019, René Moser <mail@renemoser.net>
|
||||
# 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
|
Loading…
Reference in a new issue