diff options
author | Yanis Guenane <yanis@guenane.org> | 2018-07-25 10:07:04 +0200 |
---|---|---|
committer | Yanis Guenane <yanis@guenane.org> | 2018-07-25 10:07:04 +0200 |
commit | 25994192b8ce36bd27bd749c435afcd4627b31eb (patch) | |
tree | e7bce5034dec245b32d053603eb082439207554b | |
parent | 58df96ff7ede61aa217acc98206235c36b23e9b8 (diff) |
vultr: Add support
Change-Id: I39d08d30eb42b6dc6ad1a153070f9ecbaa299de2
-rw-r--r-- | library/vr_block_storage.py | 205 | ||||
-rw-r--r-- | tasks/vultr/main.yml | 7 | ||||
-rw-r--r-- | tests/ansible.cfg | 1 | ||||
-rw-r--r-- | tests/inventory/host_vars/vultr | 1 |
4 files changed, 214 insertions, 0 deletions
diff --git a/library/vr_block_storage.py b/library/vr_block_storage.py new file mode 100644 index 0000000..e226b57 --- /dev/null +++ b/library/vr_block_storage.py @@ -0,0 +1,205 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +# +# (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) + +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +ANSIBLE_METADATA = {'metadata_version': '1.1', + 'status': ['preview'], + 'supported_by': 'community'} + +DOCUMENTATION = ''' +--- +module: vr_block_storage +short_description: Manages block storage volumes on Vultr. +description: + - Manage block storage volumes on Vultr. +version_added: "2.7" +author: "Yanis Guenane (@Spredzy)" +options: + name: + description: + - Name of the block storage volume. + required: true + aliases: [ description, label ] + size: + description: + - Size of the block storage volume in GB. + required: true + region: + description: + - Region the block storage volume is deployed into. + required: true + state: + description: + - State of the block storage volume. + default: present + choices: [ present, absent ] +extends_documentation_fragment: vultr +''' + +EXAMPLES = ''' +- name: Ensure a block storage volume is present + local_action: + module: vr_block_storage + name: myvolume + size: 10 + region: Amsterdam + +- name: Ensure a block storage volume is absent + local_action: + module: vr_block_storage + name: myvolume + state: absent +''' + +RETURN = ''' +--- +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: string + 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: string + sample: "https://api.vultr.com" +vultr_block_storage: + description: Response from Vultr API + returned: success + type: complex + contains: + id: + description: ID of the block storage volume + returned: success + type: string + sample: 1234abcd +''' + +from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils.vultr import ( + Vultr, + vultr_argument_spec, +) + + +class AnsibleVultrBlockStorage(Vultr): + + def __init__(self, module): + super(AnsibleVultrBlockStorage, self).__init__(module, "vultr_block_storage") + + self.returns = { + 'SUBID': dict(key='id'), + 'label': dict(key='name'), + 'DCID': dict(key='region', transform=self._get_region_name), + 'attached_to_SUBID': dict(), + 'cost_per_month': dict(convert_to='float'), + 'date_created': dict(), + 'id': dict(), + 'size_gb': dict(key='size', convert_to='int'), + 'status': dict() + } + + def _get_region_name(self, region_id=None): + return self.get_region().get('name') + + def get_block_storage_volumes(self): + volumes = self.api_query(path="/v1/block/list") + if volumes: + for volume in volumes: + if volume.get('label') == self.module.params.get('name'): + return volume + return {} + + def present_block_storage_volume(self): + volume = self.get_block_storage_volumes() + if not volume: + volume = self._create_block_storage_volume(volume) + return volume + + def _create_block_storage_volume(self, volume): + self.result['changed'] = True + data = { + 'label': self.module.params.get('name'), + 'DCID': self.get_region()['DCID'], + 'size_gb': self.module.params.get('size') + } + self.result['diff']['before'] = {} + self.result['diff']['after'] = data + + if not self.module.check_mode: + self.api_query( + path="/v1/block/create", + method="POST", + data=data + ) + volume = self.get_block_storage_volumes() + return volume + + def absent_block_storage_volume(self): + volume = self.get_block_storage_volumes() + if volume: + self.result['changed'] = True + + data = { + 'SUBID': volume['SUBID'], + } + + self.result['diff']['before'] = volume + self.result['diff']['after'] = {} + + if not self.module.check_mode: + self.api_query( + path="/v1/block/delete", + method="POST", + data=data + ) + return volume + + +def main(): + argument_spec = vultr_argument_spec() + argument_spec.update(dict( + name=dict(required=True, aliases=['description', 'label']), + size=dict(type='int'), + region=dict(), + state=dict(choices=['present', 'absent'], default='present'), + )) + + module = AnsibleModule( + argument_spec=argument_spec, + supports_check_mode=True, + required_if=[['state', 'present', ['size', 'region']]] + ) + + vr_block_storage = AnsibleVultrBlockStorage(module) + if module.params.get('state') == "absent": + volume = vr_block_storage.absent_block_storage_volume() + else: + volume = vr_block_storage.present_block_storage_volume() + + result = vr_block_storage.get_result(volume) + module.exit_json(**result) + + +if __name__ == '__main__': + main() diff --git a/tasks/vultr/main.yml b/tasks/vultr/main.yml new file mode 100644 index 0000000..1f340df --- /dev/null +++ b/tasks/vultr/main.yml @@ -0,0 +1,7 @@ +--- +- name: Manage volume + vr_block_storage: + name: '{{ cloud_volume_name }}' + size: '{{ cloud_volume_size }}' + region: '{{ cloud_volume_region }}' + state: '{{ cloud_volume_state|default("present") }}' diff --git a/tests/ansible.cfg b/tests/ansible.cfg index d06b57b..7d4a2ce 100644 --- a/tests/ansible.cfg +++ b/tests/ansible.cfg @@ -1,3 +1,4 @@ [defaults] roles_path = ../../ retry_files_enabled = False +library = ../library diff --git a/tests/inventory/host_vars/vultr b/tests/inventory/host_vars/vultr new file mode 100644 index 0000000..17b5202 --- /dev/null +++ b/tests/inventory/host_vars/vultr @@ -0,0 +1 @@ +cloud_volume_region: New Jersey |