Add ability to force immeadiate replication of a pgroup (#59785)

pull/59831/head
Simon Dodsley 5 years ago committed by ansibot
parent 24d8e82655
commit 27c60a7cec

@ -0,0 +1,3 @@
minor_changes:
- Add ability to force a protection group snapshot to immeadiatley replicate to a remote array (if configured)
- Add support for `check_mode`

@ -60,6 +60,21 @@ options:
- If not supplied this will default to the volume defined in I(restore) - If not supplied this will default to the volume defined in I(restore)
type: str type: str
version_added: 2.8 version_added: 2.8
now:
description: Whether to initiate a snapshot of the protection group immeadiately
type: bool
default: False
version_added: 2.9
apply_retention:
description: Apply retention schedule settings to the snapshot
type: bool
default: False
version_added: 2.9
remote:
description: Force immeadiate snapshot to remote targets
type: bool
default: False
version_added: 2.9
extends_documentation_fragment: extends_documentation_fragment:
- purestorage.fa - purestorage.fa
''' '''
@ -101,6 +116,17 @@ EXAMPLES = r'''
fa_url: 10.10.10.2 fa_url: 10.10.10.2
api_token: e31060a7-21fc-e277-6240-25983c6c4592 api_token: e31060a7-21fc-e277-6240-25983c6c4592
state: copy state: copy
- name: Create snapshot of existing pgroup foo with suffix and force immeadiate copy to remote targets
purefa_pgsnap:
name: pgname
suffix: force
now: True
apply_retention: True
remote: True
fa_url: 10.10.10.2
api_token: e31060a7-21fc-e277-6240-25983c6c4592
state: copy
''' '''
RETURN = r''' RETURN = r'''
@ -135,7 +161,7 @@ def get_rpgsnapshot(module, array):
"""Return iReplicated Snapshot or None""" """Return iReplicated Snapshot or None"""
try: try:
snapname = module.params['name'] + "." + module.params['suffix'] + "." + module.params['restore'] snapname = module.params['name'] + "." + module.params['suffix'] + "." + module.params['restore']
for snap in array.list_volumes(snap='true'): for snap in array.list_volumes(snap=True):
if snap['name'] == snapname: if snap['name'] == snapname:
return snapname return snapname
except Exception: except Exception:
@ -143,10 +169,10 @@ def get_rpgsnapshot(module, array):
def get_pgsnapshot(module, array): def get_pgsnapshot(module, array):
"""Return Snapshot or None""" """Return Snapshot (active or deleted) or None"""
try: try:
snapname = module.params['name'] + "." + module.params['suffix'] snapname = module.params['name'] + "." + module.params['suffix']
for snap in array.get_pgroup(module.params['name'], snap='true'): for snap in array.get_pgroup(module.params['name'], snap=True, pending=True):
if snap['name'] == snapname: if snap['name'] == snapname:
return snapname return snapname
except Exception: except Exception:
@ -155,48 +181,63 @@ def get_pgsnapshot(module, array):
def create_pgsnapshot(module, array): def create_pgsnapshot(module, array):
"""Create Protection Group Snapshot""" """Create Protection Group Snapshot"""
try: changed = True
array.create_pgroup_snapshot(source=module.params['name'], if not module.check_mode:
suffix=module.params['suffix'], try:
snap=True, if module.params['now'] and array.get_pgroup(module.params['name'])['targets'] is not None:
apply_retention=True) array.create_pgroup_snapshot(source=module.params['name'],
changed = True suffix=module.params['suffix'],
except Exception: snap=True,
changed = False apply_retention=module.params['apply_retention'],
replicate_now=module.params['remote'])
else:
array.create_pgroup_snapshot(source=module.params['name'],
suffix=module.params['suffix'],
snap=True,
apply_retention=module.params['apply_retention'])
except Exception:
module.fail_json(msg="Snapshot of pgroup {0} failed.".format(module.params['name']))
module.exit_json(changed=changed) module.exit_json(changed=changed)
def restore_pgsnapvolume(module, array): def restore_pgsnapvolume(module, array):
"""Restore a Protection Group Snapshot Volume""" """Restore a Protection Group Snapshot Volume"""
volume = module.params['name'] + "." + module.params['suffix'] + "." + module.params['restore'] changed = True
try: if not module.check_mode:
array.copy_volume(volume, module.params['target'], overwrite=module.params['overwrite']) if ":" in module.params['name']:
changed = True if get_rpgsnapshot(module, array)is None:
except Exception: module.fail_json(msg="Selected restore snapshot {0} does not exist in the Protection Group".format(module.params['restore']))
changed = False else:
if get_pgroupvolume(module, array) is None:
module.fail_json(msg="Selected restore volume {0} does not exist in the Protection Group".format(module.params['restore']))
volume = module.params['name'] + "." + module.params['suffix'] + "." + module.params['restore']
try:
array.copy_volume(volume, module.params['target'], overwrite=module.params['overwrite'])
except Exception:
module.fail_json(msg="Failed to restore {0} from pgroup {1}".format(volume, module.params['name']))
module.exit_json(changed=changed) module.exit_json(changed=changed)
def update_pgsnapshot(module, array): def update_pgsnapshot(module, array):
"""Update Protection Group Snapshot""" """Update Protection Group Snapshot"""
changed = False changed = True
module.exit_json(changed=changed) module.exit_json(changed=changed)
def delete_pgsnapshot(module, array): def delete_pgsnapshot(module, array):
""" Delete Protection Group Snapshot""" """ Delete Protection Group Snapshot"""
snapname = module.params['name'] + "." + module.params['suffix'] changed = True
try: if not module.check_mode:
array.destroy_pgroup(snapname) snapname = module.params['name'] + "." + module.params['suffix']
changed = True try:
if module.params['eradicate']: array.destroy_pgroup(snapname)
try: if module.params['eradicate']:
array.eradicate_pgroup(snapname) try:
changed = True array.eradicate_pgroup(snapname)
except Exception: except Exception:
changed = False module.fail_json(msg="Failed to eradicate pgroup {0}".format(snapname))
except Exception: except Exception:
changed = False module.fail_json(msg="Failed to delete pgroup {0}".format(snapname))
module.exit_json(changed=changed) module.exit_json(changed=changed)
@ -209,6 +250,9 @@ def main():
overwrite=dict(type='bool', default=False), overwrite=dict(type='bool', default=False),
target=dict(type='str'), target=dict(type='str'),
eradicate=dict(type='bool', default=False), eradicate=dict(type='bool', default=False),
now=dict(type='bool', default=False),
apply_retention=dict(type='bool', default=False),
remote=dict(type='bool', default=False),
state=dict(type='str', default='present', choices=['absent', 'present', 'copy']), state=dict(type='str', default='present', choices=['absent', 'present', 'copy']),
)) ))
@ -216,7 +260,7 @@ def main():
module = AnsibleModule(argument_spec, module = AnsibleModule(argument_spec,
required_if=required_if, required_if=required_if,
supports_check_mode=False) supports_check_mode=True)
if module.params['suffix'] is None: if module.params['suffix'] is None:
suffix = "snap-" + str((datetime.utcnow() - datetime(1970, 1, 1, 0, 0, 0, 0)).total_seconds()) suffix = "snap-" + str((datetime.utcnow() - datetime(1970, 1, 1, 0, 0, 0, 0)).total_seconds())
@ -229,26 +273,14 @@ def main():
array = get_system(module) array = get_system(module)
pgroup = get_pgroup(module, array) pgroup = get_pgroup(module, array)
if pgroup is None: if pgroup is None:
module.fail_json(msg="Protection Group {0} does not exist".format(module.params['name'])) module.fail_json(msg="Protection Group {0} does not exist.".format(module.params['name']))
pgsnap = get_pgsnapshot(module, array) pgsnap = get_pgsnapshot(module, array)
if pgsnap is None:
module.fail_json(msg="Selected volume {0} does not exist in the Protection Group".format(module.params['name'])) if state == 'copy':
if ":" in module.params['name']:
rvolume = get_rpgsnapshot(module, array)
if rvolume is None:
module.fail_json(msg="Selected restore snapshot {0} does not exist in the Protection Group".format(module.params['restore']))
else:
rvolume = get_pgroupvolume(module, array)
if rvolume is None:
module.fail_json(msg="Selected restore volume {0} does not exist in the Protection Group".format(module.params['restore']))
if state == 'copy' and rvolume:
restore_pgsnapvolume(module, array) restore_pgsnapvolume(module, array)
elif state == 'present' and pgroup and not pgsnap: elif state == 'present' and not pgsnap:
create_pgsnapshot(module, array) create_pgsnapshot(module, array)
elif state == 'present' and pgroup and pgsnap: elif state == 'present' and pgsnap:
update_pgsnapshot(module, array)
elif state == 'present' and not pgroup:
update_pgsnapshot(module, array) update_pgsnapshot(module, array)
elif state == 'absent' and pgsnap: elif state == 'absent' and pgsnap:
delete_pgsnapshot(module, array) delete_pgsnapshot(module, array)

Loading…
Cancel
Save