Skip to content

Commit

Permalink
vultr: vultr_block_storage_facts to info (ansible#60230)
Browse files Browse the repository at this point in the history
  • Loading branch information
resmo authored Aug 12, 2019
1 parent 0a90ec9 commit 602aded
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
__metaclass__ = type

ANSIBLE_METADATA = {'metadata_version': '1.1',
'status': ['preview'],
'status': ['deprecated'],
'supported_by': 'community'}

DOCUMENTATION = r'''
Expand All @@ -18,6 +18,10 @@
- Gather facts about block storage volumes available in Vultr.
version_added: "2.7"
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
'''

Expand Down
156 changes: 156 additions & 0 deletions lib/ansible/modules/cloud/vultr/vultr_block_storage_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2018, Yanis Guenane <[email protected]>
# Copyright (c) 2019, René Moser <[email protected]>
# 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()
33 changes: 0 additions & 33 deletions test/integration/targets/vultr_block_storage_facts/tasks/main.yml

This file was deleted.

35 changes: 35 additions & 0 deletions test/integration/targets/vultr_block_storage_info/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Copyright (c) 2018, Yanis Guenane <[email protected]>
# 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

0 comments on commit 602aded

Please sign in to comment.