Update purefa_pg module to support Offload protection groups (#53895)

pull/53889/head
Simon Dodsley 5 years ago committed by ansibot
parent 47f2101e8d
commit 2a38df8260

@ -20,6 +20,7 @@ description:
- Create, delete or modify protection groups on Pure Storage FlashArrays.
- If a protection group exists and you try to add non-valid types, eg. a host
to a volume protection group the module will ignore the invalid types.
- Protection Groups on Offload targets are supported.
author:
- Simon Dodsley (@sdodsley)
options:
@ -53,9 +54,10 @@ options:
default: 'yes'
target:
description:
- List of remote arrays for replication protection group to connect to.
- List of remote arrays or offload target for replication protection group
to connect to.
- Note that all replicated protection groups are asynchronous.
- Target arrays must already be connected to the source array.
- Target arrays or offload targets must already be connected to the source array.
- Maximum number of targets per Portection Group is 4, assuming your
configuration suppors this.
version_added: '2.8'
@ -79,6 +81,15 @@ EXAMPLES = r'''
fa_url: 10.10.10.2
api_token: e31060a7-21fc-e277-6240-25983c6c4592
- name: Create new replicated protection group to offload target and remote array
purefa_pg:
pgroup: foo
target:
- offload
- arrayc
fa_url: 10.10.10.2
api_token: e31060a7-21fc-e277-6240-25983c6c4592
- name: Create new protection group with snapshots disabled
purefa_pg:
pgroup: foo
@ -94,6 +105,15 @@ EXAMPLES = r'''
api_token: e31060a7-21fc-e277-6240-25983c6c4592
state: absent
- name: Eradicate protection group foo on offload target where source array is arrayA
purefa_pg:
pgroup: "arrayA:foo"
target: offload
eradicate: true
fa_url: 10.10.10.2
api_token: e31060a7-21fc-e277-6240-25983c6c4592
state: absent
- name: Create protection group for hostgroups
purefa_pg:
pgroup: bar
@ -130,24 +150,64 @@ from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.pure import get_system, purefa_argument_spec
OFFLOAD_API_VERSION = '1.16'
def get_targets(array):
"""Get Offload Targets"""
targets = []
try:
target_details = array.list_offload()
except Exception:
return None
for targetcnt in range(0, len(target_details)):
if target_details[targetcnt]['status'] == "connected":
targets.append(target_details[targetcnt]['name'])
return targets
def get_arrays(array):
""" Get Connected Arrays"""
arrays = []
array_details = array.list_array_connections()
for arraycnt in range(0, len(array_details)):
arrays.append(array_details[arraycnt]['array_name'])
if array_details[arraycnt]['connected']:
arrays.append(array_details[arraycnt]['array_name'])
return arrays
def get_pgroup(module, array):
def get_pending_pgroup(module, array):
""" Get Protection Group"""
pgroup = None
if ":" in module.params['pgroup']:
for pgrp in array.list_pgroups(pending=True, on="*"):
if pgrp["name"] == module.params['pgroup'] and pgrp['time_remaining']:
pgroup = pgrp
break
else:
for pgrp in array.list_pgroups(pending=True):
if pgrp["name"] == module.params['pgroup'] and pgrp['time_remaining']:
pgroup = pgrp
break
for pgrp in array.list_pgroups():
if pgrp["name"] == module.params['pgroup']:
pgroup = pgrp
break
return pgroup
def get_pgroup(module, array):
""" Get Protection Group"""
pgroup = None
if ":" in module.params['pgroup']:
for pgrp in array.list_pgroups(on="*"):
if pgrp["name"] == module.params['pgroup']:
pgroup = pgrp
break
else:
for pgrp in array.list_pgroups():
if pgrp["name"] == module.params['pgroup']:
pgroup = pgrp
break
return pgroup
@ -164,20 +224,42 @@ def get_pgroup_sched(module, array):
return pgroup
def check_pg_on_offload(module, array):
""" Check if PG already exists on offload target """
array_name = array.get()['array_name']
remote_pg = array_name + ":" + module.params['pgroup']
targets = get_targets(array)
for target in targets:
remote_pgs = array.list_pgroups(pending=True, on=target)
for rpg in range(0, len(remote_pgs)):
if remote_pg == remote_pgs[rpg]['name']:
return target
return None
def make_pgroup(module, array):
""" Create Protection Group"""
changed = False
if module.params['target']:
api_version = array._list_available_rest_versions()
connected_targets = []
connected_arrays = get_arrays(array)
if OFFLOAD_API_VERSION in api_version:
connected_targets = get_targets(array)
offload_name = check_pg_on_offload(module, array)
if offload_name and offload_name in module.params['target'][0:4]:
module.fail_json(msg='Protection Group {0} already exists on offload target {1}.'.format(module.params['pgroup'], offload_name))
connected_arrays = connected_arrays + connected_targets
if connected_arrays == []:
module.fail_json(msg='No target arrays not connected to source array.')
module.fail_json(msg='No connected targets on source array.')
if set(module.params['target'][0:4]).issubset(connected_arrays):
try:
array.create_pgroup(module.params['pgroup'], targetlist=[module.params['target'][0:4]])
array.create_pgroup(module.params['pgroup'], targetlist=module.params['target'][0:4])
except Exception:
module.fail_json(msg='Creation of replicated pgroup {0} failed.'.format(module.params['pgroup']))
module.fail_json(msg='Creation of replicated pgroup {0} failed. {1}'.format(module.params['pgroup'], module.params['target'][0:4]))
else:
module.fail_json(msg='Target arrays {0} not connected to source array.'.format(module.params['target']))
module.fail_json(msg='Check all selected targets are connected to the source array.')
else:
try:
array.create_pgroup(module.params['pgroup'])
@ -213,12 +295,31 @@ def update_pgroup(module, array):
""" Update Protection Group"""
changed = False
if module.params['target']:
api_version = array._list_available_rest_versions()
connected_targets = []
connected_arrays = get_arrays(array)
if OFFLOAD_API_VERSION in api_version:
connected_targets = get_targets(array)
offload_name = check_pg_on_offload(module, array)
if offload_name and offload_name in module.params['target'][0:4]:
module.fail_json(msg='Protection Group {0} already exists on offload target {1}.'.format(module.params['pgroup'], offload_name))
connected_arrays = connected_arrays + connected_targets
if connected_arrays == []:
module.fail_json(msg='No target arrays not connected to source array.')
if not all(x in connected_arrays for x in module.params['target'][0:4]):
module.fail_json(msg='No targets connected to source array.')
current_connects = array.get_pgroup(module.params['pgroup'])['targets']
current_targets = []
if current_connects:
for targetcnt in range(0, len(current_connects)):
current_targets.append(current_connects[targetcnt]['name'])
if set(module.params['target'][0:4]) != set(current_targets):
if not set(module.params['target'][0:4]).issubset(connected_arrays):
module.fail_json(msg='Check all selected targets are connected to the source array.')
try:
array.set_pgroup(module.params['pgroup'], targetlist=[module.params['target'][0:4]])
array.set_pgroup(module.params['pgroup'], targetlist=module.params['target'][0:4])
changed = True
except Exception:
module.fail_json(msg='Changing targets for pgroup {0} failed.'.format(module.params['pgroup']))
@ -284,12 +385,44 @@ def update_pgroup(module, array):
module.exit_json(changed=changed)
def eradicate_pgroup(module, array):
""" Eradicate Protection Group"""
changed = False
if ":" in module.params['pgroup']:
try:
target = ''.join(module.params['target'])
array.destroy_pgroup(module.params['pgroup'], on=target, eradicate=True)
changed = True
except Exception:
module.fail_json(msg='Eradicating pgroup {0} failed.'.format(module.params['pgroup']))
else:
try:
array.destroy_pgroup(module.params['pgroup'], eradicate=True)
changed = True
except Exception:
module.fail_json(msg='Eradicating pgroup {0} failed.'.format(module.params['pgroup']))
module.exit_json(changed=changed)
def delete_pgroup(module, array):
""" Delete Protection Group"""
changed = True
array.destroy_pgroup(module.params['pgroup'])
changed = False
if ":" in module.params['pgroup']:
try:
target = ''.join(module.params['target'])
array.destroy_pgroup(module.params['pgroup'], on=target)
changed = True
except Exception:
module.fail_json(msg='Deleting pgroup {0} failed.'.format(module.params['pgroup']))
else:
try:
array.destroy_pgroup(module.params['pgroup'])
changed = True
except Exception:
module.fail_json(msg='Deleting pgroup {0} failed.'.format(module.params['pgroup']))
if module.params['eradicate']:
array.eradicate_pgroup(module.params['pgroup'])
eradicate_pgroup(module, array)
module.exit_json(changed=changed)
@ -313,7 +446,12 @@ def main():
state = module.params['state']
array = get_system(module)
api_version = array._list_available_rest_versions()
if ":" in module.params['pgroup'] and OFFLOAD_API_VERSION not in api_version:
module.fail_json(msg='API version does not support offload protection groups.')
pgroup = get_pgroup(module, array)
xpgroup = get_pending_pgroup(module, array)
if module.params['host']:
try:
@ -333,10 +471,12 @@ def main():
update_pgroup(module, array)
elif pgroup and state == 'absent':
delete_pgroup(module, array)
elif xpgroup and state == 'absent' and module.params['eradicate']:
eradicate_pgroup(module, array)
elif not pgroup and not xpgroup and state == 'present':
make_pgroup(module, array)
elif pgroup is None and state == 'absent':
module.exit_json(changed=False)
else:
make_pgroup(module, array)
if __name__ == '__main__':

Loading…
Cancel
Save