2017-07-28 10:24:31 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2018-06-13 16:52:51 +00:00
|
|
|
# (c) 2018, Simon Dodsley (simon@purestorage.com)
|
Remove wildcard imports
Made the following changes:
* Removed wildcard imports
* Replaced long form of GPL header with short form
* Removed get_exception usage
* Added from __future__ boilerplate
* Adjust division operator to // where necessary
For the following files:
* web_infrastructure modules
* system modules
* linode, lxc, lxd, atomic, cloudscale, dimensiondata, ovh, packet,
profitbricks, pubnub, smartos, softlayer, univention modules
* compat dirs (disabled as its used intentionally)
2017-07-28 05:55:24 +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
|
|
|
|
|
2017-08-16 03:16:38 +00:00
|
|
|
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
2017-07-28 10:24:31 +00:00
|
|
|
'status': ['preview'],
|
|
|
|
'supported_by': 'community'}
|
|
|
|
|
2017-08-22 00:25:13 +00:00
|
|
|
DOCUMENTATION = r'''
|
2017-07-28 10:24:31 +00:00
|
|
|
---
|
|
|
|
module: purefa_volume
|
2017-08-22 00:25:13 +00:00
|
|
|
version_added: '2.4'
|
|
|
|
short_description: Manage volumes on Pure Storage FlashArrays
|
2017-07-28 10:24:31 +00:00
|
|
|
description:
|
2017-08-22 00:25:13 +00:00
|
|
|
- Create, delete or extend the capacity of a volume on Pure Storage FlashArray.
|
|
|
|
author:
|
|
|
|
- Simon Dodsley (@sdodsley)
|
2017-07-28 10:24:31 +00:00
|
|
|
options:
|
|
|
|
name:
|
|
|
|
description:
|
2017-08-22 00:25:13 +00:00
|
|
|
- The name of the volume.
|
2017-07-28 10:24:31 +00:00
|
|
|
required: true
|
2017-08-21 22:15:51 +00:00
|
|
|
target:
|
|
|
|
description:
|
2017-08-22 00:25:13 +00:00
|
|
|
- The name of the target volume, if copying.
|
2017-07-28 10:24:31 +00:00
|
|
|
state:
|
|
|
|
description:
|
2017-08-22 00:25:13 +00:00
|
|
|
- Define whether the volume should exist or not.
|
2017-07-28 10:24:31 +00:00
|
|
|
default: present
|
2017-08-22 00:25:13 +00:00
|
|
|
choices: [ absent, present ]
|
2017-07-28 10:24:31 +00:00
|
|
|
eradicate:
|
|
|
|
description:
|
2017-08-22 00:25:13 +00:00
|
|
|
- Define whether to eradicate the volume on delete or leave in trash.
|
2017-07-28 10:24:31 +00:00
|
|
|
type: bool
|
2017-08-22 00:25:13 +00:00
|
|
|
default: 'no'
|
2017-08-21 22:15:51 +00:00
|
|
|
overwrite:
|
|
|
|
description:
|
2017-08-22 00:25:13 +00:00
|
|
|
- Define whether to overwrite a target volume if it already exisits.
|
2017-08-21 22:15:51 +00:00
|
|
|
type: bool
|
2017-08-22 00:25:13 +00:00
|
|
|
default: 'no'
|
2017-07-28 10:24:31 +00:00
|
|
|
size:
|
|
|
|
description:
|
2017-08-22 00:25:13 +00:00
|
|
|
- Volume size in M, G, T or P units.
|
2017-07-28 10:24:31 +00:00
|
|
|
extends_documentation_fragment:
|
2018-02-22 20:33:48 +00:00
|
|
|
- purestorage.fa
|
2017-07-28 10:24:31 +00:00
|
|
|
'''
|
|
|
|
|
2017-08-22 00:25:13 +00:00
|
|
|
EXAMPLES = r'''
|
2017-07-28 10:24:31 +00:00
|
|
|
- name: Create new volume named foo
|
|
|
|
purefa_volume:
|
|
|
|
name: foo
|
|
|
|
size: 1T
|
|
|
|
fa_url: 10.10.10.2
|
|
|
|
api_token: e31060a7-21fc-e277-6240-25983c6c4592
|
2017-08-22 00:25:13 +00:00
|
|
|
state: present
|
2017-07-28 10:24:31 +00:00
|
|
|
|
|
|
|
- name: Extend the size of an existing volume named foo
|
|
|
|
purefa_volume:
|
|
|
|
name: foo
|
|
|
|
size: 2T
|
|
|
|
fa_url: 10.10.10.2
|
|
|
|
api_token: e31060a7-21fc-e277-6240-25983c6c4592
|
2017-08-22 00:25:13 +00:00
|
|
|
state: present
|
2017-07-28 10:24:31 +00:00
|
|
|
|
|
|
|
- name: Delete and eradicate volume named foo
|
|
|
|
purefa_volume:
|
|
|
|
name: foo
|
2017-08-22 00:25:13 +00:00
|
|
|
eradicate: yes
|
2017-07-28 10:24:31 +00:00
|
|
|
fa_url: 10.10.10.2
|
2017-08-21 22:15:51 +00:00
|
|
|
api_token: e31060a7-21fc-e277-6240-25983c6c4592
|
2017-08-22 00:25:13 +00:00
|
|
|
state: absent
|
2017-08-21 22:15:51 +00:00
|
|
|
|
|
|
|
- name: Create clone of volume bar named foo
|
|
|
|
purefa_volume:
|
|
|
|
name: foo
|
|
|
|
target: bar
|
|
|
|
fa_url: 10.10.10.2
|
|
|
|
api_token: e31060a7-21fc-e277-6240-25983c6c4592
|
2017-08-22 00:25:13 +00:00
|
|
|
state: present
|
2017-08-21 22:15:51 +00:00
|
|
|
|
|
|
|
- name: Overwrite volume bar with volume foo
|
|
|
|
purefa_volume:
|
|
|
|
name: foo
|
|
|
|
target: bar
|
2017-08-22 00:25:13 +00:00
|
|
|
overwrite: yes
|
2017-08-21 22:15:51 +00:00
|
|
|
fa_url: 10.10.10.2
|
2017-08-22 00:25:13 +00:00
|
|
|
api_token: e31060a7-21fc-e277-6240-25983c6c4592
|
|
|
|
state: present
|
|
|
|
'''
|
2017-07-28 10:24:31 +00:00
|
|
|
|
2017-08-22 00:25:13 +00:00
|
|
|
RETURN = r'''
|
2017-07-28 10:24:31 +00:00
|
|
|
'''
|
|
|
|
|
|
|
|
try:
|
|
|
|
from purestorage import purestorage
|
2017-08-22 00:25:13 +00:00
|
|
|
HAS_PURESTORAGE = True
|
2017-07-28 10:24:31 +00:00
|
|
|
except ImportError:
|
|
|
|
HAS_PURESTORAGE = False
|
|
|
|
|
Remove wildcard imports
Made the following changes:
* Removed wildcard imports
* Replaced long form of GPL header with short form
* Removed get_exception usage
* Added from __future__ boilerplate
* Adjust division operator to // where necessary
For the following files:
* web_infrastructure modules
* system modules
* linode, lxc, lxd, atomic, cloudscale, dimensiondata, ovh, packet,
profitbricks, pubnub, smartos, softlayer, univention modules
* compat dirs (disabled as its used intentionally)
2017-07-28 05:55:24 +00:00
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
from ansible.module_utils.pure import get_system, purefa_argument_spec
|
|
|
|
|
2017-07-28 10:24:31 +00:00
|
|
|
|
|
|
|
def human_to_bytes(size):
|
|
|
|
"""Given a human-readable byte string (e.g. 2G, 30M),
|
|
|
|
return the number of bytes. Will return 0 if the argument has
|
|
|
|
unexpected form.
|
|
|
|
"""
|
|
|
|
bytes = size[:-1]
|
|
|
|
unit = size[-1]
|
2017-08-21 22:15:51 +00:00
|
|
|
if bytes.isdigit():
|
2017-07-28 10:24:31 +00:00
|
|
|
bytes = int(bytes)
|
2017-08-21 22:15:51 +00:00
|
|
|
if unit == 'P':
|
2017-07-28 10:24:31 +00:00
|
|
|
bytes *= 1125899906842624
|
2017-08-21 22:15:51 +00:00
|
|
|
elif unit == 'T':
|
2017-07-28 10:24:31 +00:00
|
|
|
bytes *= 1099511627776
|
2017-08-21 22:15:51 +00:00
|
|
|
elif unit == 'G':
|
2017-07-28 10:24:31 +00:00
|
|
|
bytes *= 1073741824
|
2017-08-21 22:15:51 +00:00
|
|
|
elif unit == 'M':
|
2017-07-28 10:24:31 +00:00
|
|
|
bytes *= 1048576
|
|
|
|
else:
|
|
|
|
bytes = 0
|
|
|
|
else:
|
|
|
|
bytes = 0
|
|
|
|
return bytes
|
|
|
|
|
|
|
|
|
|
|
|
def get_volume(module, array):
|
|
|
|
"""Return Volume or None"""
|
|
|
|
try:
|
|
|
|
return array.get_volume(module.params['name'])
|
|
|
|
except:
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2017-08-21 22:15:51 +00:00
|
|
|
def get_target(module, array):
|
|
|
|
"""Return Volume or None"""
|
|
|
|
try:
|
|
|
|
return array.get_volume(module.params['target'])
|
|
|
|
except:
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2017-07-28 10:24:31 +00:00
|
|
|
def create_volume(module, array):
|
|
|
|
"""Create Volume"""
|
|
|
|
size = module.params['size']
|
2018-06-13 16:52:51 +00:00
|
|
|
changed = True
|
2017-07-28 10:24:31 +00:00
|
|
|
if not module.check_mode:
|
2018-06-13 16:52:51 +00:00
|
|
|
try:
|
|
|
|
array.create_volume(module.params['name'], size)
|
|
|
|
except:
|
|
|
|
changed = False
|
|
|
|
module.exit_json(changed=changed)
|
2017-07-28 10:24:31 +00:00
|
|
|
|
|
|
|
|
2017-08-21 22:15:51 +00:00
|
|
|
def copy_from_volume(module, array):
|
|
|
|
"""Create Volume Clone"""
|
|
|
|
changed = False
|
|
|
|
|
|
|
|
tgt = get_target(module, array)
|
|
|
|
|
|
|
|
if tgt is None:
|
|
|
|
changed = True
|
|
|
|
if not module.check_mode:
|
|
|
|
array.copy_volume(module.params['name'],
|
|
|
|
module.params['target'])
|
|
|
|
elif tgt is not None and module.params['overwrite']:
|
|
|
|
changed = True
|
|
|
|
if not module.check_mode:
|
|
|
|
array.copy_volume(module.params['name'],
|
|
|
|
module.params['target'],
|
|
|
|
overwrite=module.params['overwrite'])
|
|
|
|
|
|
|
|
module.exit_json(changed=changed)
|
|
|
|
|
|
|
|
|
2017-07-28 10:24:31 +00:00
|
|
|
def update_volume(module, array):
|
|
|
|
"""Update Volume"""
|
|
|
|
changed = True
|
2017-08-22 00:25:13 +00:00
|
|
|
vol = array.get_volume(module.params['name'])
|
|
|
|
if human_to_bytes(module.params['size']) > vol['size']:
|
|
|
|
if not module.check_mode:
|
2017-07-28 10:24:31 +00:00
|
|
|
array.extend_volume(module.params['name'], module.params['size'])
|
2017-08-22 00:25:13 +00:00
|
|
|
else:
|
|
|
|
changed = False
|
2017-07-28 10:24:31 +00:00
|
|
|
module.exit_json(changed=changed)
|
|
|
|
|
|
|
|
|
|
|
|
def delete_volume(module, array):
|
|
|
|
""" Delete Volume"""
|
2018-06-13 16:52:51 +00:00
|
|
|
changed = True
|
2017-07-28 10:24:31 +00:00
|
|
|
if not module.check_mode:
|
2018-06-13 16:52:51 +00:00
|
|
|
try:
|
|
|
|
array.destroy_volume(module.params['name'])
|
|
|
|
if module.params['eradicate']:
|
|
|
|
try:
|
|
|
|
array.eradicate_volume(module.params['name'])
|
|
|
|
except:
|
|
|
|
changed = False
|
|
|
|
except:
|
|
|
|
changed = False
|
2017-07-28 10:24:31 +00:00
|
|
|
module.exit_json(changed=True)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
argument_spec = purefa_argument_spec()
|
2017-08-22 00:25:13 +00:00
|
|
|
argument_spec.update(dict(
|
|
|
|
name=dict(type='str', required=True),
|
|
|
|
target=dict(type='str'),
|
|
|
|
overwrite=dict(type='bool', default=False),
|
|
|
|
eradicate=dict(type='bool', default=False),
|
|
|
|
state=dict(type='str', default='present', choices=['absent', 'present']),
|
|
|
|
size=dict(type='str'),
|
|
|
|
))
|
2017-07-28 10:24:31 +00:00
|
|
|
|
2017-08-21 22:15:51 +00:00
|
|
|
mutually_exclusive = [['size', 'target']]
|
2017-07-28 10:24:31 +00:00
|
|
|
|
|
|
|
module = AnsibleModule(argument_spec,
|
2017-08-21 22:15:51 +00:00
|
|
|
mutually_exclusive=mutually_exclusive,
|
2017-07-28 10:24:31 +00:00
|
|
|
supports_check_mode=True)
|
|
|
|
|
|
|
|
if not HAS_PURESTORAGE:
|
|
|
|
module.fail_json(msg='purestorage sdk is required for this module in volume')
|
|
|
|
|
2017-08-21 22:15:51 +00:00
|
|
|
size = module.params['size']
|
2017-07-28 10:24:31 +00:00
|
|
|
state = module.params['state']
|
|
|
|
array = get_system(module)
|
|
|
|
volume = get_volume(module, array)
|
2017-08-21 22:15:51 +00:00
|
|
|
target = get_target(module, array)
|
2017-07-28 10:24:31 +00:00
|
|
|
|
2017-08-21 22:15:51 +00:00
|
|
|
if state == 'present' and not volume and size:
|
2017-07-28 10:24:31 +00:00
|
|
|
create_volume(module, array)
|
2017-08-21 22:15:51 +00:00
|
|
|
elif state == 'present' and volume and size:
|
2017-07-28 10:24:31 +00:00
|
|
|
update_volume(module, array)
|
2017-08-21 22:15:51 +00:00
|
|
|
elif state == 'present' and volume and target:
|
|
|
|
copy_from_volume(module, array)
|
|
|
|
elif state == 'present' and volume and not target:
|
|
|
|
copy_from_volume(module, array)
|
2017-07-28 10:24:31 +00:00
|
|
|
elif state == 'absent' and volume:
|
|
|
|
delete_volume(module, array)
|
2017-08-21 22:15:51 +00:00
|
|
|
elif state == 'present' and not volume or not size:
|
|
|
|
module.exit_json(changed=False)
|
2017-07-28 10:24:31 +00:00
|
|
|
elif state == 'absent' and not volume:
|
|
|
|
module.exit_json(changed=False)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|