vultr: vultr_block_storage_facts to info (#60230)

This commit is contained in:
René Moser 2019-08-12 10:36:21 +02:00 committed by GitHub
parent 0a90ec90c0
commit 602aded6ec
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 196 additions and 34 deletions

View file

@ -7,7 +7,7 @@ from __future__ import (absolute_import, division, print_function)
__metaclass__ = type __metaclass__ = type
ANSIBLE_METADATA = {'metadata_version': '1.1', ANSIBLE_METADATA = {'metadata_version': '1.1',
'status': ['preview'], 'status': ['deprecated'],
'supported_by': 'community'} 'supported_by': 'community'}
DOCUMENTATION = r''' DOCUMENTATION = r'''
@ -18,6 +18,10 @@ description:
- Gather facts about block storage volumes available in Vultr. - Gather facts about block storage volumes available in Vultr.
version_added: "2.7" version_added: "2.7"
author: "Yanis Guenane (@Spredzy)" author: "Yanis Guenane (@Spredzy)"
deprecated:
removed_in: "2.12"
why: Transformed into an info module.
alternative: Use M(vultr_block_storage_info) instead.
extends_documentation_fragment: vultr extends_documentation_fragment: vultr
''' '''

View file

@ -0,0 +1,156 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# 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)
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_block_storage_info
short_description: Get infos about the Vultr block storage volumes available.
description:
- Get infos about block storage volumes available in Vultr.
version_added: "2.9"
author:
- "Yanis Guenane (@Spredzy)"
- "René Moser (@resmo)"
extends_documentation_fragment: vultr
'''
EXAMPLES = r'''
- name: Get Vultr block storage infos
vultr_block_storage_info:
register: result
- name: Print the infos
debug:
var: result.vultr_block_storage_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_block_storage_info:
description: Response from Vultr API as list
returned: success
type: complex
contains:
id:
description: ID of the block storage.
returned: success
type: int
sample: 17332323
size:
description: Size in GB of the block storage.
returned: success
type: int
sample: 10
region:
description: Region the block storage is located in.
returned: success
type: str
sample: New Jersey
name:
description: Name of the block storage.
returned: success
type: str
sample: my volume
cost_per_month:
description: Cost per month of the block storage.
returned: success
type: float
sample: 1.0
date_created:
description: Date created of the block storage.
returned: success
type: str
sample: 2018-07-24 12:59:59
status:
description: Status of the block storage.
returned: success
type: str
sample: active
attached_to_id:
description: Block storage is attached to this server ID.
returned: success
type: str
sample: null
'''
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.vultr import (
Vultr,
vultr_argument_spec,
)
class AnsibleVultrBlockStorageFacts(Vultr):
def __init__(self, module):
super(AnsibleVultrBlockStorageFacts, self).__init__(module, "vultr_block_storage_info")
self.returns = {
'attached_to_SUBID': dict(key='attached_to_id'),
'cost_per_month': dict(convert_to='float'),
'date_created': dict(),
'SUBID': dict(key='id'),
'label': dict(key='name'),
'DCID': dict(key='region', transform=self._get_region_name),
'size_gb': dict(key='size', convert_to='int'),
'status': dict()
}
def _get_region_name(self, region):
return self.get_region(region, 'DCID').get('name')
def get_block_storage_volumes(self):
return self.api_query(path="/v1/block/list")
def main():
argument_spec = vultr_argument_spec()
module = AnsibleModule(
argument_spec=argument_spec,
supports_check_mode=True,
)
volume_info = AnsibleVultrBlockStorageFacts(module)
result = volume_info.get_result(volume_info.get_block_storage_volumes())
module.exit_json(**result)
if __name__ == '__main__':
main()

View file

@ -1,33 +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 block storage volume facts - empty resource
vultr_block_storage_facts:
- name: Create the block storage volume
vultr_block_storage:
name: '{{ vultr_block_storage_name }}'
size: '{{ vultr_block_storage_size }}'
region: '{{ vultr_block_storage_region }}'
- name: test gather vultr block storage volume facts in check mode
vultr_block_storage_facts:
check_mode: yes
- name: verify test gather vultr block storage volume facts in check mode
assert:
that:
- ansible_facts.vultr_block_storage_facts|selectattr('name','equalto','{{ vultr_block_storage_name }}') | list | count == 1
- name: test gather vultr block storage volume facts
vultr_block_storage_facts:
- name: verify test gather vultr block storage volume facts
assert:
that:
- ansible_facts.vultr_block_storage_facts|selectattr('name','equalto','{{ vultr_block_storage_name }}') | list | count == 1
- name: Delete the block storage volume
vultr_block_storage:
name: '{{ vultr_block_storage_name }}'
state: absent

View file

@ -0,0 +1,35 @@
# 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 block storage volume info - empty resource
vultr_block_storage_info:
- name: Create the block storage volume
vultr_block_storage:
name: '{{ vultr_block_storage_name }}'
size: '{{ vultr_block_storage_size }}'
region: '{{ vultr_block_storage_region }}'
- name: test gather vultr block storage volume info in check mode
vultr_block_storage_info:
check_mode: yes
register: result
- name: verify test gather vultr block storage volume info in check mode
assert:
that:
- result.vultr_block_storage_info|selectattr('name','equalto','{{ vultr_block_storage_name }}') | list | count == 1
- name: test gather vultr block storage volume info
vultr_block_storage_info:
register: result
- name: verify test gather vultr block storage volume info
assert:
that:
- result.vultr_block_storage_info|selectattr('name','equalto','{{ vultr_block_storage_name }}') | list | count == 1
- name: Delete the block storage volume
vultr_block_storage:
name: '{{ vultr_block_storage_name }}'
state: absent