2015-03-31 20:37:07 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
# Copyright (c) 2014 Hewlett-Packard Development Company, L.P.
|
2017-10-09 16:56:26 +00:00
|
|
|
# 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
|
|
|
|
|
2015-03-31 20:37:07 +00:00
|
|
|
|
2017-08-16 03:16:38 +00:00
|
|
|
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
2017-03-14 16:07:22 +00:00
|
|
|
'status': ['preview'],
|
|
|
|
'supported_by': 'community'}
|
|
|
|
|
2016-12-06 10:35:05 +00:00
|
|
|
|
2015-03-31 20:37:07 +00:00
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: os_volume
|
|
|
|
short_description: Create/Delete Cinder Volumes
|
|
|
|
extends_documentation_fragment: openstack
|
2015-06-15 18:41:22 +00:00
|
|
|
version_added: "2.0"
|
|
|
|
author: "Monty Taylor (@emonty)"
|
2015-03-31 20:37:07 +00:00
|
|
|
description:
|
|
|
|
- Create or Remove cinder block storage volumes
|
|
|
|
options:
|
|
|
|
size:
|
|
|
|
description:
|
2016-03-21 20:13:29 +00:00
|
|
|
- Size of volume in GB. This parameter is required when the
|
|
|
|
I(state) parameter is 'present'.
|
2015-03-31 20:37:07 +00:00
|
|
|
display_name:
|
|
|
|
description:
|
|
|
|
- Name of volume
|
|
|
|
required: true
|
|
|
|
display_description:
|
|
|
|
description:
|
|
|
|
- String describing the volume
|
|
|
|
volume_type:
|
|
|
|
description:
|
|
|
|
- Volume type for volume
|
|
|
|
image:
|
2016-12-08 02:33:38 +00:00
|
|
|
description:
|
2015-03-31 20:37:07 +00:00
|
|
|
- Image name or id for boot from volume
|
|
|
|
snapshot_id:
|
|
|
|
description:
|
|
|
|
- Volume snapshot id to create from
|
2016-10-07 14:11:08 +00:00
|
|
|
volume:
|
2016-10-06 12:43:03 +00:00
|
|
|
description:
|
2016-10-07 14:11:08 +00:00
|
|
|
- Volume name or id to create from
|
2016-10-06 13:52:45 +00:00
|
|
|
version_added: "2.3"
|
2015-03-31 20:37:07 +00:00
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- Should the resource be present or absent.
|
|
|
|
choices: [present, absent]
|
|
|
|
default: present
|
2017-02-17 20:49:03 +00:00
|
|
|
availability_zone:
|
|
|
|
description:
|
2017-06-12 06:55:19 +00:00
|
|
|
- Ignored. Present for backwards compatibility
|
2017-07-04 09:51:07 +00:00
|
|
|
scheduler_hints:
|
|
|
|
description:
|
|
|
|
- Scheduler hints passed to volume API in form of dict
|
|
|
|
version_added: "2.4"
|
2016-12-08 02:33:38 +00:00
|
|
|
requirements:
|
2018-05-29 12:51:58 +00:00
|
|
|
- "python >= 2.7"
|
2018-05-26 01:40:39 +00:00
|
|
|
- "openstacksdk"
|
2015-03-31 20:37:07 +00:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
# Creates a new volume
|
|
|
|
- name: create a volume
|
|
|
|
hosts: localhost
|
|
|
|
tasks:
|
|
|
|
- name: create 40g test volume
|
|
|
|
os_volume:
|
|
|
|
state: present
|
|
|
|
cloud: mordred
|
|
|
|
availability_zone: az2
|
|
|
|
size: 40
|
|
|
|
display_name: test_volume
|
2017-07-04 09:51:07 +00:00
|
|
|
scheduler_hints:
|
|
|
|
same_host: 243e8d3c-8f47-4a61-93d6-7215c344b0c0
|
2015-03-31 20:37:07 +00:00
|
|
|
'''
|
2018-12-10 17:29:34 +00:00
|
|
|
|
|
|
|
RETURNS = '''
|
|
|
|
id:
|
|
|
|
description: Cinder's unique ID for this volume
|
|
|
|
returned: always
|
|
|
|
type: str
|
|
|
|
sample: fcc4ac1c-e249-4fe7-b458-2138bfb44c06
|
|
|
|
|
|
|
|
volume:
|
|
|
|
description: Cinder's representation of the volume object
|
|
|
|
returned: always
|
|
|
|
type: dict
|
|
|
|
sample: {'...'}
|
|
|
|
'''
|
2017-10-09 16:56:26 +00:00
|
|
|
from distutils.version import StrictVersion
|
2015-03-31 20:37:07 +00:00
|
|
|
|
2017-02-02 19:45:22 +00:00
|
|
|
|
2017-10-09 16:56:26 +00:00
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
2018-02-15 14:20:49 +00:00
|
|
|
from ansible.module_utils.openstack import openstack_full_argument_spec, openstack_module_kwargs, openstack_cloud_from_module
|
2017-07-04 09:51:07 +00:00
|
|
|
|
2015-03-31 20:37:07 +00:00
|
|
|
|
|
|
|
def _present_volume(module, cloud):
|
|
|
|
if cloud.volume_exists(module.params['display_name']):
|
|
|
|
v = cloud.get_volume(module.params['display_name'])
|
2015-06-17 09:24:08 +00:00
|
|
|
module.exit_json(changed=False, id=v['id'], volume=v)
|
2015-03-31 20:37:07 +00:00
|
|
|
|
|
|
|
volume_args = dict(
|
|
|
|
size=module.params['size'],
|
|
|
|
volume_type=module.params['volume_type'],
|
|
|
|
display_name=module.params['display_name'],
|
|
|
|
display_description=module.params['display_description'],
|
|
|
|
snapshot_id=module.params['snapshot_id'],
|
|
|
|
availability_zone=module.params['availability_zone'],
|
|
|
|
)
|
|
|
|
if module.params['image']:
|
|
|
|
image_id = cloud.get_image_id(module.params['image'])
|
|
|
|
volume_args['imageRef'] = image_id
|
|
|
|
|
2016-10-07 14:11:08 +00:00
|
|
|
if module.params['volume']:
|
|
|
|
volume_id = cloud.get_volume_id(module.params['volume'])
|
2016-10-06 13:00:38 +00:00
|
|
|
if not volume_id:
|
2016-10-07 14:11:08 +00:00
|
|
|
module.fail_json(msg="Failed to find volume '%s'" % module.params['volume'])
|
2016-10-06 12:43:03 +00:00
|
|
|
volume_args['source_volid'] = volume_id
|
|
|
|
|
2017-07-04 09:51:07 +00:00
|
|
|
if module.params['scheduler_hints']:
|
|
|
|
volume_args['scheduler_hints'] = module.params['scheduler_hints']
|
|
|
|
|
2015-03-31 20:37:07 +00:00
|
|
|
volume = cloud.create_volume(
|
|
|
|
wait=module.params['wait'], timeout=module.params['timeout'],
|
|
|
|
**volume_args)
|
2015-06-17 09:24:08 +00:00
|
|
|
module.exit_json(changed=True, id=volume['id'], volume=volume)
|
2015-03-31 20:37:07 +00:00
|
|
|
|
|
|
|
|
2018-05-26 01:40:39 +00:00
|
|
|
def _absent_volume(module, cloud, sdk):
|
2017-05-24 16:16:23 +00:00
|
|
|
changed = False
|
|
|
|
if cloud.volume_exists(module.params['display_name']):
|
|
|
|
try:
|
|
|
|
changed = cloud.delete_volume(name_or_id=module.params['display_name'],
|
|
|
|
wait=module.params['wait'],
|
|
|
|
timeout=module.params['timeout'])
|
2018-10-30 11:50:46 +00:00
|
|
|
except sdk.exceptions.ResourceTimeout:
|
2017-05-24 16:16:23 +00:00
|
|
|
module.exit_json(changed=changed)
|
|
|
|
|
|
|
|
module.exit_json(changed=changed)
|
2015-03-31 20:37:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
argument_spec = openstack_full_argument_spec(
|
|
|
|
size=dict(default=None),
|
|
|
|
volume_type=dict(default=None),
|
|
|
|
display_name=dict(required=True, aliases=['name']),
|
|
|
|
display_description=dict(default=None, aliases=['description']),
|
|
|
|
image=dict(default=None),
|
|
|
|
snapshot_id=dict(default=None),
|
2016-10-07 14:11:08 +00:00
|
|
|
volume=dict(default=None),
|
2015-03-31 20:37:07 +00:00
|
|
|
state=dict(default='present', choices=['absent', 'present']),
|
2017-07-04 09:51:07 +00:00
|
|
|
scheduler_hints=dict(default=None, type='dict')
|
2015-03-31 20:37:07 +00:00
|
|
|
)
|
|
|
|
module_kwargs = openstack_module_kwargs(
|
|
|
|
mutually_exclusive=[
|
2016-10-07 14:11:08 +00:00
|
|
|
['image', 'snapshot_id', 'volume'],
|
2015-03-31 20:37:07 +00:00
|
|
|
],
|
|
|
|
)
|
|
|
|
module = AnsibleModule(argument_spec=argument_spec, **module_kwargs)
|
|
|
|
|
|
|
|
state = module.params['state']
|
|
|
|
|
|
|
|
if state == 'present' and not module.params['size']:
|
|
|
|
module.fail_json(msg="Size is required when state is 'present'")
|
|
|
|
|
2018-05-26 01:40:39 +00:00
|
|
|
sdk, cloud = openstack_cloud_from_module(module)
|
2015-03-31 20:37:07 +00:00
|
|
|
try:
|
|
|
|
if state == 'present':
|
|
|
|
_present_volume(module, cloud)
|
|
|
|
if state == 'absent':
|
2018-05-26 01:40:39 +00:00
|
|
|
_absent_volume(module, cloud, sdk)
|
|
|
|
except sdk.exceptions.OpenStackCloudException as e:
|
2016-01-13 16:00:16 +00:00
|
|
|
module.fail_json(msg=str(e))
|
2015-03-31 20:37:07 +00:00
|
|
|
|
2017-10-09 16:56:26 +00:00
|
|
|
|
2016-12-08 02:33:38 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|