2020-03-09 10:11:07 +01:00
|
|
|
#!/usr/bin/python
|
2021-08-08 10:40:22 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2020-03-09 10:11:07 +01:00
|
|
|
#
|
|
|
|
# Scaleway volumes management module
|
|
|
|
#
|
|
|
|
# Copyright (C) 2018 Henryk Konsek Consulting (hekonsek@gmail.com).
|
|
|
|
#
|
2022-08-05 13:17:19 +02:00
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: scaleway_volume
|
|
|
|
short_description: Scaleway volumes management module
|
|
|
|
author: Henryk Konsek (@hekonsek)
|
|
|
|
description:
|
2023-01-12 21:56:21 +01:00
|
|
|
- "This module manages volumes on Scaleway account U(https://developer.scaleway.com)."
|
2020-03-09 10:11:07 +01:00
|
|
|
extends_documentation_fragment:
|
|
|
|
- community.general.scaleway
|
|
|
|
|
|
|
|
|
|
|
|
options:
|
|
|
|
state:
|
2020-11-12 08:26:54 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
2023-01-12 21:56:21 +01:00
|
|
|
- Indicate desired state of the volume.
|
2020-03-09 10:11:07 +01:00
|
|
|
default: present
|
|
|
|
choices:
|
|
|
|
- present
|
|
|
|
- absent
|
|
|
|
region:
|
2020-11-12 08:26:54 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
2023-01-12 21:56:21 +01:00
|
|
|
- Scaleway region to use (for example par1).
|
2020-03-09 10:11:07 +01:00
|
|
|
required: true
|
|
|
|
choices:
|
|
|
|
- ams1
|
|
|
|
- EMEA-NL-EVS
|
|
|
|
- par1
|
|
|
|
- EMEA-FR-PAR1
|
2021-01-28 12:51:07 +01:00
|
|
|
- par2
|
|
|
|
- EMEA-FR-PAR2
|
|
|
|
- waw1
|
|
|
|
- EMEA-PL-WAW1
|
2020-03-09 10:11:07 +01:00
|
|
|
name:
|
2020-11-12 08:26:54 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
2023-01-12 21:56:21 +01:00
|
|
|
- Name used to identify the volume.
|
2020-03-09 10:11:07 +01:00
|
|
|
required: true
|
2022-01-05 17:53:00 +01:00
|
|
|
project:
|
|
|
|
type: str
|
|
|
|
description:
|
2023-01-12 21:56:21 +01:00
|
|
|
- Scaleway project ID to which volume belongs.
|
2022-01-05 17:53:00 +01:00
|
|
|
version_added: 4.3.0
|
2020-03-09 10:11:07 +01:00
|
|
|
organization:
|
2020-11-12 08:26:54 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
2023-01-12 21:56:21 +01:00
|
|
|
- ScaleWay organization ID to which volume belongs.
|
2020-03-09 10:11:07 +01:00
|
|
|
size:
|
2020-11-12 08:26:54 +01:00
|
|
|
type: int
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
2023-01-12 21:56:21 +01:00
|
|
|
- Size of the volume in bytes.
|
2020-03-09 10:11:07 +01:00
|
|
|
volume_type:
|
2020-11-12 08:26:54 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
2023-01-12 21:56:21 +01:00
|
|
|
- Type of the volume (for example 'l_ssd').
|
2020-03-09 10:11:07 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
2020-05-15 12:12:41 +02:00
|
|
|
- name: Create 10GB volume
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.scaleway_volume:
|
2020-05-15 12:12:41 +02:00
|
|
|
name: my-volume
|
|
|
|
state: present
|
|
|
|
region: par1
|
2022-01-05 17:53:00 +01:00
|
|
|
project: "{{ scw_org }}"
|
2020-05-15 12:12:41 +02:00
|
|
|
"size": 10000000000
|
|
|
|
volume_type: l_ssd
|
|
|
|
register: server_creation_check_task
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2020-05-15 12:12:41 +02:00
|
|
|
- name: Make sure volume deleted
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.scaleway_volume:
|
2020-05-15 12:12:41 +02:00
|
|
|
name: my-volume
|
|
|
|
state: absent
|
|
|
|
region: par1
|
2020-03-09 10:11:07 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
RETURN = '''
|
|
|
|
data:
|
2023-01-12 21:56:21 +01:00
|
|
|
description: This is only present when I(state=present).
|
|
|
|
returned: when I(state=present)
|
2020-03-09 10:11:07 +01:00
|
|
|
type: dict
|
|
|
|
sample: {
|
|
|
|
"volume": {
|
|
|
|
"export_uri": null,
|
|
|
|
"id": "c675f420-cfeb-48ff-ba2a-9d2a4dbe3fcd",
|
|
|
|
"name": "volume-0-3",
|
2022-01-05 17:53:00 +01:00
|
|
|
"project": "000a115d-2852-4b0a-9ce8-47f1134ba95a",
|
2023-01-12 21:56:21 +01:00
|
|
|
"server": null,
|
|
|
|
"size": 10000000000,
|
|
|
|
"volume_type": "l_ssd"
|
2020-03-09 10:11:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
'''
|
|
|
|
|
|
|
|
from ansible_collections.community.general.plugins.module_utils.scaleway import SCALEWAY_LOCATION, scaleway_argument_spec, Scaleway
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
|
|
|
|
|
|
|
|
def core(module):
|
2022-01-05 17:53:00 +01:00
|
|
|
region = module.params["region"]
|
2020-03-09 10:11:07 +01:00
|
|
|
state = module.params['state']
|
|
|
|
name = module.params['name']
|
|
|
|
organization = module.params['organization']
|
2022-01-05 17:53:00 +01:00
|
|
|
project = module.params['project']
|
2020-03-09 10:11:07 +01:00
|
|
|
size = module.params['size']
|
|
|
|
volume_type = module.params['volume_type']
|
2022-01-05 17:53:00 +01:00
|
|
|
module.params['api_url'] = SCALEWAY_LOCATION[region]["api_endpoint"]
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
account_api = Scaleway(module)
|
|
|
|
response = account_api.get('volumes')
|
|
|
|
status_code = response.status_code
|
|
|
|
volumes_json = response.json
|
|
|
|
|
2022-01-05 17:53:00 +01:00
|
|
|
if project is None:
|
|
|
|
project = organization
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
if not response.ok:
|
|
|
|
module.fail_json(msg='Error getting volume [{0}: {1}]'.format(
|
|
|
|
status_code, response.json['message']))
|
|
|
|
|
|
|
|
volumeByName = None
|
|
|
|
for volume in volumes_json['volumes']:
|
2022-01-05 17:53:00 +01:00
|
|
|
if volume['project'] == project and volume['name'] == name:
|
2020-03-09 10:11:07 +01:00
|
|
|
volumeByName = volume
|
|
|
|
|
|
|
|
if state in ('present',):
|
|
|
|
if volumeByName is not None:
|
|
|
|
module.exit_json(changed=False)
|
|
|
|
|
2022-01-05 17:53:00 +01:00
|
|
|
payload = {'name': name, 'project': project, 'size': size, 'volume_type': volume_type}
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
response = account_api.post('/volumes', payload)
|
|
|
|
|
|
|
|
if response.ok:
|
|
|
|
module.exit_json(changed=True, data=response.json)
|
|
|
|
|
|
|
|
module.fail_json(msg='Error creating volume [{0}: {1}]'.format(
|
|
|
|
response.status_code, response.json))
|
|
|
|
|
|
|
|
elif state in ('absent',):
|
|
|
|
if volumeByName is None:
|
|
|
|
module.exit_json(changed=False)
|
|
|
|
|
|
|
|
if module.check_mode:
|
|
|
|
module.exit_json(changed=True)
|
|
|
|
|
|
|
|
response = account_api.delete('/volumes/' + volumeByName['id'])
|
|
|
|
if response.status_code == 204:
|
|
|
|
module.exit_json(changed=True, data=response.json)
|
|
|
|
|
|
|
|
module.fail_json(msg='Error deleting volume [{0}: {1}]'.format(
|
|
|
|
response.status_code, response.json))
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
argument_spec = scaleway_argument_spec()
|
|
|
|
argument_spec.update(dict(
|
|
|
|
state=dict(default='present', choices=['absent', 'present']),
|
|
|
|
name=dict(required=True),
|
|
|
|
size=dict(type='int'),
|
2022-01-05 17:53:00 +01:00
|
|
|
project=dict(),
|
2020-03-09 10:11:07 +01:00
|
|
|
organization=dict(),
|
|
|
|
volume_type=dict(),
|
2020-11-12 08:26:54 +01:00
|
|
|
region=dict(required=True, choices=list(SCALEWAY_LOCATION.keys())),
|
2020-03-09 10:11:07 +01:00
|
|
|
))
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=argument_spec,
|
|
|
|
supports_check_mode=True,
|
2022-01-05 17:53:00 +01:00
|
|
|
mutually_exclusive=[
|
|
|
|
('organization', 'project'),
|
|
|
|
],
|
|
|
|
required_one_of=[
|
|
|
|
('organization', 'project'),
|
|
|
|
],
|
2020-03-09 10:11:07 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
core(module)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|