Debian Codegraph @

ansible_collections/purestorage/flasharray/plugins/modules/purefa_snap.py

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # (c) 2017, Simon Dodsley ([email protected])
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8
9 __metaclass__ = type
10
11 ANSIBLE_METADATA = {
12 "metadata_version": "1.1",
13 "status": ["preview"],
14 "supported_by": "community",
15 }
16
17 DOCUMENTATION = r"""
18 ---
19 module: purefa_snap
20 version_added: '1.0.0'
21 short_description: Manage volume snapshots on Pure Storage FlashArrays
22 description:
23 - Create or delete volumes and volume snapshots on Pure Storage FlashArray.
24 author:
25 - Pure Storage Ansible Team (@sdodsley) <[email protected]>
26 options:
27 name:
28 description:
29 - The name of the source volume.
30 type: str
31 required: true
32 suffix:
33 description:
34 - Suffix of snapshot name.
35 type: str
36 target:
37 description:
38 - Name of target volume if creating from snapshot.
39 - Name of new snapshot suffix if renaming a snapshot
40 type: str
41 overwrite:
42 description:
43 - Define whether to overwrite existing volume when creating from snapshot.
44 type: bool
45 default: false
46 offload:
47 description:
48 - Only valid for Purity//FA 6.1 or higher
49 - Name of offload target for the snapshot.
50 - Target can be either another FlashArray or an Offload Target
51 - This is only applicable for creation, deletion and eradication of snapshots
52 - I(state) of I(copy) is not supported.
53 type: str
54 state:
55 description:
56 - Define whether the volume snapshot should exist or not.
57 choices: [ absent, copy, present, rename ]
58 type: str
59 default: present
60 eradicate:
61 description:
62 - Define whether to eradicate the snapshot on delete or leave in trash.
63 type: bool
64 default: false
65 ignore_repl:
66 description:
67 - Only valid for Purity//FA 6.1 or higher
68 - If set to true, allow destruction/eradication of snapshots in use by replication.
69 - If set to false, allow destruction/eradication of snapshots not in use by replication
70 type: bool
71 default: false
72 throttle:
73 description:
74 - If set to true, allows snapshot to fail if array health is not optimal.
75 type: bool
76 default: false
77 version_added: '1.21.0'
78 context:
79 description:
80 - Name of fleet member on which to perform the operation.
81 - This requires the array receiving the request is a member of a fleet
82 and the context name to be a member of the same fleet.
83 type: str
84 default: ""
85 version_added: '1.33.0'
86 extends_documentation_fragment:
87 - purestorage.flasharray.purestorage.fa
88 """
89
90 EXAMPLES = r"""
91 - name: Create snapshot foo.ansible
92 purestorage.flasharray.purefa_snap:
93 name: foo
94 suffix: ansible
95 fa_url: 10.10.10.2
96 api_token: e31060a7-21fc-e277-6240-25983c6c4592
97 state: present
98
99 - name: Create R/W clone foo_clone from snapshot foo.snap
100 purestorage.flasharray.purefa_snap:
101 name: foo
102 suffix: snap
103 target: foo_clone
104 fa_url: 10.10.10.2
105 api_token: e31060a7-21fc-e277-6240-25983c6c4592
106 state: copy
107
108 - name: Create R/W clone foo_clone from remote mnapshot arrayB:foo.snap
109 purestorage.flasharray.purefa_snap:
110 name: arrayB:foo
111 suffix: snap
112 target: foo_clone
113 fa_url: 10.10.10.2
114 api_token: e31060a7-21fc-e277-6240-25983c6c4592
115 state: copy
116
117 - name: Overwrite existing volume foo_clone with snapshot foo.snap
118 purestorage.flasharray.purefa_snap:
119 name: foo
120 suffix: snap
121 target: foo_clone
122 overwrite: true
123 fa_url: 10.10.10.2
124 api_token: e31060a7-21fc-e277-6240-25983c6c4592
125 state: copy
126
127 - name: Delete and eradicate snapshot named foo.snap
128 purestorage.flasharray.purefa_snap:
129 name: foo
130 suffix: snap
131 eradicate: true
132 fa_url: 10.10.10.2
133 api_token: e31060a7-21fc-e277-6240-25983c6c4592
134 state: absent
135
136 - name: Rename snapshot foo.fred to foo.dave
137 purestorage.flasharray.purefa_snap:
138 name: foo
139 suffix: fred
140 target: dave
141 fa_url: 10.10.10.2
142 api_token: e31060a7-21fc-e277-6240-25983c6c4592
143 state: rename
144
145 - name: Create a remote volume snapshot on offload device arrayB
146 purestorage.flasharray.purefa_snap:
147 name: foo
148 offload: arrayB
149 fa_url: 10.10.10.2
150 api_token: e31060a7-21fc-e277-6240-25983c6c4592
151
152 - name: Delete and eradicate a volume snapshot foo.1 on offload device arrayB
153 purestorage.flasharray.purefa_snap:
154 name: foo
155 suffix: 1
156 offload: arrayB
157 eradicate: true
158 state: absent
159 fa_url: 10.10.10.2
160 api_token: e31060a7-21fc-e277-6240-25983c6c4592
161 """
162
163 RETURN = r"""
164 suffix:
165 description: Data related to the created snapshot suffix
166 type: str
167 returned: success
168 """
169
170 HAS_PURESTORAGE = True
171 try:
172 from pypureclient.flasharray import (
173 RemoteVolumeSnapshotPost,
174 VolumeSnapshotPost,
175 DestroyedPatchPost,
176 VolumeSnapshotPatch,
177 VolumePost,
178 Reference,
179 )
180 except ImportError:
181 HAS_PURESTORAGE = False
182
183 import re
184 from ansible.module_utils.basic import AnsibleModule
185 from ansible_collections.purestorage.flasharray.plugins.module_utils.purefa import (
186 get_array,
187 purefa_argument_spec,
188 )
189 from ansible_collections.purestorage.flasharray.plugins.module_utils.version import (
190 LooseVersion,
191 )
192 from datetime import datetime
193
194 THROTTLE_API = "2.25"
195 SNAPSHOT_SUFFIX_API = "2.28"
196 CONTEXT_API_VERSION = "2.38"
197
198
199 def _check_offload(module, array):
200 api_version = array.get_rest_version()
201 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
202 res = array.get_offloads(
203 names=[module.params["offload"]], context_names=[module.params["context"]]
204 )
205 else:
206 res = array.get_offloads(names=[module.params["offload"]])
207 if res.status_code == 200:
208 if list(res.items)[0].status == "connected":
209 return True
210 return False
211
212
213 def _check_target(module, array):
214 api_version = array.get_rest_version()
215 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
216 res = array.get_array_connections(
217 names=[module.params["offload"]], context_names=[module.params["context"]]
218 )
219 else:
220 res = array.get_array_connections(names=[module.params["offload"]])
221 if res.status_code == 200:
222 if list(res.items)[0].status == "connected":
223 return True
224 return False
225
226
227 def _check_offload_snapshot(module, array):
228 """Return Remote Snapshot (active or deleted) or None"""
229 api_version = array.get_rest_version()
230 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
231 source_array = list(
232 array.get_arrays(context_names=[module.params["context"]]).items
233 )[0].name
234 else:
235 source_array = list(array.get_arrays().items)[0].name
236 snapname = (
237 source_array + ":" + module.params["name"] + "." + module.params["suffix"]
238 )
239 if _check_offload(module, array):
240 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
241 res = array.get_remote_volume_snapshots(
242 on=module.params["offload"],
243 names=[snapname],
244 destroyed=False,
245 context_names=[module.params["context"]],
246 )
247 else:
248 res = array.get_remote_volume_snapshots(
249 on=module.params["offload"], names=[snapname], destroyed=False
250 )
251 else:
252 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
253 res = array.get_volume_snapshots(
254 names=[snapname],
255 destroyed=False,
256 context_names=[module.params["context"]],
257 )
258 else:
259 res = array.get_volume_snapshots(names=[snapname], destroyed=False)
260 if res.status_code != 200:
261 return None
262 return list(res.items)[0]
263
264
265 def get_volume(module, array):
266 """Return Volume or None"""
267 api_version = array.get_rest_version()
268 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
269 res = array.get_volumes(
270 names=[module.params["name"]], context_names=[module.params["context"]]
271 )
272 else:
273 res = array.get_volumes(names=[module.params["name"]])
274 if res.status_code == 200:
275 return list(res.items)[0]
276 return None
277
278
279 def get_target(module, array):
280 """Return Volume or None"""
281 api_version = array.get_rest_version()
282 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
283 res = array.get_volumes(
284 names=[module.params["target"]], context_names=[module.params["context"]]
285 )
286 else:
287 res = array.get_volumes(names=[module.params["target"]])
288 if res.status_code == 200:
289 return list(res.items)[0]
290 return None
291
292
293 def get_deleted_snapshot(module, array):
294 """Return Deleted Snapshot"""
295 api_version = array.get_rest_version()
296 snapname = module.params["name"] + "." + module.params["suffix"]
297 if module.params["offload"]:
298 source_array = list(array.get_arrays().items)[0].name
299 snapname = module.params["name"] + "." + module.params["suffix"]
300 full_snapname = source_array + ":" + snapname
301 if _check_offload(module, array):
302 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
303 res = array.get_remote_volume_snapshots(
304 context_names=[module.params["context"]],
305 on=module.params["offload"],
306 names=[full_snapname],
307 destroyed=True,
308 )
309 else:
310 res = array.get_remote_volume_snapshots(
311 on=module.params["offload"], names=[full_snapname], destroyed=True
312 )
313 else:
314 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
315 res = array.get_volume_snapshots(
316 names=[snapname],
317 destroyed=True,
318 context_names=[module.params["context"]],
319 )
320 else:
321 res = array.get_volume_snapshots(names=[snapname], destroyed=True)
322 return bool(res.status_code == 200)
323 else:
324 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
325 return bool(
326 array.get_volume_snapshots(
327 names=[snapname],
328 context_names=[module.params["context"]],
329 destroyed=True,
330 ).status_code
331 == 200
332 )
333 return bool(
334 array.get_volume_snapshots(names=[snapname], destroyed=True).status_code
335 == 200
336 )
337
338
339 def get_snapshot(module, array):
340 """Return True if snapshot exists, False otherwise"""
341 api_version = array.get_rest_version()
342 snapname = module.params["name"] + "." + module.params["suffix"]
343 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
344 return bool(
345 array.get_volume_snapshots(
346 names=[snapname],
347 destroyed=False,
348 context_names=[module.params["context"]],
349 ).status_code
350 == 200
351 )
352 return bool(
353 array.get_volume_snapshots(names=[snapname], destroyed=False).status_code == 200
354 )
355
356
357 def create_snapshot(module, array):
358 """Create Snapshot"""
359 changed = False
360 api_version = array.get_rest_version()
361 if module.params["offload"]:
362 if LooseVersion(SNAPSHOT_SUFFIX_API) > LooseVersion(api_version):
363 module.params["suffix"] = None
364 changed = True
365 if not module.check_mode:
366 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
367 res = array.post_remote_volume_snapshots(
368 source_names=[module.params["name"]],
369 context_names=[module.params["context"]],
370 on=module.params["offload"],
371 remote_volume_snapshot=RemoteVolumeSnapshotPost(
372 suffix=module.params["suffix"]
373 ),
374 )
375 else:
376 if module.params["suffix"]:
377 res = array.post_remote_volume_snapshots(
378 source_names=[module.params["name"]],
379 on=module.params["offload"],
380 remote_volume_snapshot=RemoteVolumeSnapshotPost(
381 suffix=module.params["suffix"]
382 ),
383 )
384 else:
385 res = array.post_remote_volume_snapshots(
386 source_names=[module.params["name"]],
387 on=module.params["offload"],
388 )
389 if res.status_code != 200:
390 module.fail_json(
391 msg="Failed to create remote snapshot for volume {0}. Error: {1}".format(
392 module.params["name"], res.errors[0].message
393 )
394 )
395 else:
396 if LooseVersion(SNAPSHOT_SUFFIX_API) > LooseVersion(api_version):
397 remote_snap = list(res.items)[0].name
398 module.params["suffix"] = remote_snap.split(".")[1]
399 else:
400 changed = True
401 if not module.check_mode:
402 if LooseVersion(THROTTLE_API) <= LooseVersion(api_version):
403 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
404 res = array.post_volume_snapshots(
405 allow_throttle=module.params["throttle"],
406 context_names=[module.params["context"]],
407 volume_snapshot=VolumeSnapshotPost(
408 suffix=module.params["suffix"]
409 ),
410 source_names=[module.params["name"]],
411 )
412 else:
413 res = array.post_volume_snapshots(
414 allow_throttle=module.params["throttle"],
415 volume_snapshot=VolumeSnapshotPost(
416 suffix=module.params["suffix"]
417 ),
418 source_names=[module.params["name"]],
419 )
420 if res.status_code != 200:
421 module.fail_json(
422 msg="Failed to create snapshot for volume {0}. Error: {1}".format(
423 module.params["name"], res.errors[0].message
424 )
425 )
426 else:
427 res = array.post_volume_snapshots(
428 source_names=[module.params["name"]],
429 volume_snapshot=VolumeSnapshotPost(suffix=module.params["suffix"]),
430 )
431 if res.status_code != 200:
432 module.fail_json(
433 msg="Failed to create snapshot for volume {0}. Error: {1}".format(
434 module.params["name"], res.errors[0].message
435 )
436 )
437 module.exit_json(changed=changed, suffix=module.params["suffix"])
438
439
440 def create_from_snapshot(module, array):
441 """Create Volume from Snapshot"""
442 api_version = array.get_rest_version()
443 source = module.params["name"] + "." + module.params["suffix"]
444 tgt = get_target(module, array)
445 if tgt is None:
446 changed = True
447 if not module.check_mode:
448 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
449 res = array.post_volumes(
450 context_names=[module.params["context"]],
451 volume=VolumePost(source=Reference(name=source)),
452 names=[module.params["target"]],
453 )
454 else:
455 res = array.post_volumes(
456 volume=VolumePost(source=Reference(name=source)),
457 names=[module.params["target"]],
458 )
459 if res.status_code != 200:
460 module.fail_json(
461 msg="Failed to create volume {0} from snapshot {1}. Error: {2}".format(
462 module.params["target"], source, res.errors[9].message
463 )
464 )
465 elif tgt is not None and module.params["overwrite"]:
466 changed = True
467 if not module.check_mode:
468 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
469 res = array.post_volumes(
470 overwrite=module.params["overwrite"],
471 context_names=[module.params["context"]],
472 volume=VolumePost(source=Reference(name=source)),
473 names=[module.params["target"]],
474 )
475 else:
476 res = array.post_volumes(
477 overwrite=module.params["overwrite"],
478 volume=VolumePost(source=Reference(name=source)),
479 names=[module.params["target"]],
480 )
481 elif tgt is not None and not module.params["overwrite"]:
482 changed = False
483 module.exit_json(changed=changed)
484
485
486 def recover_snapshot(module, array):
487 """Recover Snapshot"""
488 api_version = array.get_rest_version()
489 changed = False
490 snapname = module.params["name"] + "." + module.params["suffix"]
491 if module.params["offload"] and _check_offload(module, array):
492 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
493 source_array = list(
494 array.get_arrays(context_names=[module.params["context"]]).items
495 )[0].name
496 else:
497 source_array = list(array.get_arrays().items)[0].name
498 snapname = source_array + module.params["name"] + "." + module.params["suffix"]
499 changed = True
500 if not module.check_mode:
501 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
502 res = array.patch_remote_volume_snapshots(
503 names=[snapname],
504 context_names=[module.params["context"]],
505 on=module.params["offload"],
506 remote_volume_snapshot=DestroyedPatchPost(destroyed=False),
507 )
508 else:
509 res = array.patch_remote_volume_snapshots(
510 names=[snapname],
511 on=module.params["offload"],
512 remote_volume_snapshot=DestroyedPatchPost(destroyed=False),
513 )
514 if res.status_code != 200:
515 module.fail_json(
516 msg="Failed to recover remote snapshot {0}".format(snapname)
517 )
518 else:
519 changed = True
520 if not module.check_mode:
521 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
522 res = array.patch_volume_snapshot(
523 names=[snapname],
524 context_names=[module.params["context"]],
525 volume_snapshot=VolumeSnapshotPatch(destroyed=False),
526 )
527 else:
528 res = array.patch_volume_snapshot(
529 names=[snapname],
530 volume_snapshot=VolumeSnapshotPatch(destroyed=False),
531 )
532 if res.sttaus_code != 200:
533 module.fail_json(
534 msg="Recovery of snapshot {0} failed. Error: {1}".format(
535 snapname, res.errors[0].message
536 )
537 )
538 module.exit_json(changed=changed)
539
540
541 def update_snapshot(module, array):
542 """Update Snapshot - basically just rename..."""
543 api_version = array.get_rest_version()
544 changed = True
545 if not module.check_mode:
546 current_name = module.params["name"] + "." + module.params["suffix"]
547 new_name = module.params["name"] + "." + module.params["target"]
548 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
549 res = array.patch_volume_snapshots(
550 names=[current_name],
551 context_names=[module.params["context"]],
552 volume_snapshot=VolumeSnapshotPatch(name=new_name),
553 )
554 else:
555 res = array.patch_volume_snapshots(
556 names=[current_name],
557 volume_snapshot=VolumeSnapshotPatch(name=new_name),
558 )
559 if res.status_code != 200:
560 module.fail_json(
561 msg="Failed to rename {0} to {1}. Error: {2}".format(
562 current_name, new_name, res.errors[0].message
563 )
564 )
565 module.exit_json(changed=changed)
566
567
568 def delete_snapshot(module, array):
569 """Delete Snapshot"""
570 api_version = array.get_rest_version()
571 changed = False
572 snapname = module.params["name"] + "." + module.params["suffix"]
573 if module.params["offload"] and _check_offload(module, array):
574 source_array = list(array.get_arrays().items)[0].name
575 full_snapname = source_array + ":" + snapname
576 changed = True
577 if not module.check_mode:
578 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
579 res = array.patch_remote_volume_snapshots(
580 names=[full_snapname],
581 context_names=[module.params["context"]],
582 on=module.params["offload"],
583 volume_snapshot=VolumeSnapshotPatch(destroyed=True),
584 replication_snapshot=module.params["ignore_repl"],
585 )
586 else:
587 res = array.patch_remote_volume_snapshots(
588 names=[full_snapname],
589 on=module.params["offload"],
590 volume_snapshot=VolumeSnapshotPatch(destroyed=True),
591 replication_snapshot=module.params["ignore_repl"],
592 )
593 if res.status_code != 200:
594 module.fail_json(
595 msg="Failed to delete remote snapshot {0}. Error: {1}".format(
596 snapname, res.errors[0].message
597 )
598 )
599 if module.params["eradicate"]:
600 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
601 res = array.delete_remote_volume_snapshots(
602 names=[full_snapname],
603 context_names=[module.params["context"]],
604 on=module.params["offload"],
605 replication_snapshot=module.params["ignore_repl"],
606 )
607 else:
608 res = array.delete_remote_volume_snapshots(
609 names=[full_snapname],
610 on=module.params["offload"],
611 replication_snapshot=module.params["ignore_repl"],
612 )
613 if res.status_code != 200:
614 module.fail_json(
615 msg="Failed to eradicate remote snapshot {0}. Error: {1}".format(
616 snapname, res.errors[0].message
617 )
618 )
619 elif module.params["offload"] and _check_target(module, array):
620 changed = True
621 if not module.check_mode:
622 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
623 res = array.patch_volume_snapshots(
624 names=[snapname],
625 context_names=[module.params["context"]],
626 volume_snapshot=DestroyedPatchPost(destroyed=True),
627 replication_snapshot=module.params["ignore_repl"],
628 )
629 else:
630 res = array.patch_volume_snapshots(
631 names=[snapname],
632 volume_snapshot=DestroyedPatchPost(destroyed=True),
633 replication_snapshot=module.params["ignore_repl"],
634 )
635 if res.status_code != 200:
636 module.fail_json(
637 msg="Failed to delete remote snapshot {0}. Error: {1}".format(
638 snapname, res.errors[0].message
639 )
640 )
641 if module.params["eradicate"]:
642 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
643 res = array.delete_volume_snapshots(
644 names=[snapname],
645 replication_snapshot=module.params["ignore_repl"],
646 )
647 else:
648 res = array.delete_volume_snapshots(
649 names=[snapname],
650 replication_snapshot=module.params["ignore_repl"],
651 )
652 if res.status_code != 200:
653 module.fail_json(
654 msg="Failed to eradicate remote snapshot {0}. Error: {1}".format(
655 snapname, res.errors[0].message
656 )
657 )
658 else:
659 changed = True
660 if not module.check_mode:
661 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
662 res = array.patch_volume_snapshots(
663 names=[snapname],
664 context_names=[module.params["context"]],
665 volume_snapshot=DestroyedPatchPost(destroyed=True),
666 replication_snapshot=module.params["ignore_repl"],
667 )
668 else:
669 res = array.patch_volume_snapshots(
670 names=[snapname],
671 volume_snapshot=DestroyedPatchPost(destroyed=True),
672 replication_snapshot=module.params["ignore_repl"],
673 )
674 if res.status_code != 200:
675 module.fail_json(
676 msg="Failed to delete remote snapshot {0}. Error: {1}".format(
677 snapname, res.errors[0].message
678 )
679 )
680 if module.params["eradicate"]:
681 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
682 res = array.delete_volume_snapshots(
683 names=[snapname],
684 context_names=[module.params["context"]],
685 replication_snapshot=module.params["ignore_repl"],
686 )
687 else:
688 res = array.delete_volume_snapshots(
689 names=[snapname],
690 replication_snapshot=module.params["ignore_repl"],
691 )
692 if res.status_code != 200:
693 module.fail_json(
694 msg="Failed to eradicate remote snapshot {0}. Error: {1}".format(
695 snapname, res.errors[0].message
696 )
697 )
698 module.exit_json(changed=changed)
699
700
701 def eradicate_snapshot(module, array):
702 """Eradicate snapshot"""
703 api_version = array.get_rest_version()
704 changed = True
705 snapname = module.params["name"] + "." + module.params["suffix"]
706 if not module.check_mode:
707 if module.params["offload"] and _check_offload(module, array):
708 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
709 source_array = list(
710 array.get_arrays(context_names=[module.params["context"]]).items
711 )[0].name
712 else:
713 source_array = list(array.get_arrays().items)[0].name
714 full_snapname = source_array + ":" + snapname
715 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
716 res = array.delete_remote_volume_snapshots(
717 names=[full_snapname],
718 context_names=[module.params["context"]],
719 on=module.params["offload"],
720 replication_snapshot=module.params["ignore_repl"],
721 )
722 else:
723 res = array.delete_remote_volume_snapshots(
724 names=[full_snapname],
725 on=module.params["offload"],
726 replication_snapshot=module.params["ignore_repl"],
727 )
728 if res.status_code != 200:
729 module.fail_json(
730 msg="Failed to eradicate remote snapshot {0}. Error: {1}".format(
731 snapname, res.errors[0].message
732 )
733 )
734 elif module.params["offload"] and _check_target(module, array):
735 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
736 res = array.delete_volume_snapshots(
737 context_names=[module.params["context"]],
738 names=[snapname],
739 replication_snapshot=module.params["ignore_repl"],
740 )
741 else:
742 res = array.delete_volume_snapshots(
743 names=[snapname], replication_snapshot=module.params["ignore_repl"]
744 )
745 if res.status_code != 200:
746 module.fail_json(
747 msg="Failed to eradicate remote snapshot {0}. Error: {1}".format(
748 snapname, res.errors[0].message
749 )
750 )
751 else:
752 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
753 res = array.delete_volume_snapshots(
754 context_names=[module.params["context"]], names=[snapname]
755 )
756 else:
757 res = array.delete_volume_snapshots(names=[snapname])
758 if res.status_code != 200:
759 module.fail_json(
760 msg="Failed to eradicate remote snapshot {0}. Error: {1}".format(
761 snapname, res.errors[0].message
762 )
763 )
764 module.exit_json(changed=changed)
765
766
767 def main():
768 argument_spec = purefa_argument_spec()
769 argument_spec.update(
770 dict(
771 name=dict(type="str", required=True),
772 suffix=dict(type="str"),
773 target=dict(type="str"),
774 offload=dict(type="str"),
775 throttle=dict(type="bool", default=False),
776 ignore_repl=dict(type="bool", default=False),
777 overwrite=dict(type="bool", default=False),
778 eradicate=dict(type="bool", default=False),
779 state=dict(
780 type="str",
781 default="present",
782 choices=["absent", "copy", "present", "rename"],
783 ),
784 context=dict(type="str", default=""),
785 )
786 )
787
788 required_if = [("state", "copy", ["target", "suffix"])]
789
790 module = AnsibleModule(
791 argument_spec, required_if=required_if, supports_check_mode=True
792 )
793 pattern1 = re.compile(
794 "^(?=.*[a-zA-Z-])[a-zA-Z0-9]([a-zA-Z0-9-]{0,63}[a-zA-Z0-9])?$"
795 )
796 pattern2 = re.compile("^([1-9])([0-9]{0,63}[0-9])?$")
797
798 state = module.params["state"]
799 if module.params["suffix"] is None:
800 suffix = "snap-" + str(
801 (datetime.utcnow() - datetime(1970, 1, 1, 0, 0, 0, 0)).total_seconds()
802 )
803 module.params["suffix"] = suffix.replace(".", "")
804 else:
805 if not module.params["offload"]:
806 if not (
807 pattern1.match(module.params["suffix"])
808 or pattern2.match(module.params["suffix"])
809 ) and state not in [
810 "absent",
811 "rename",
812 ]:
813 module.fail_json(
814 msg="Suffix name {0} does not conform to suffix name rules".format(
815 module.params["suffix"]
816 )
817 )
818 if state == "rename" and module.params["target"] is not None:
819 if not pattern1.match(module.params["target"]):
820 module.fail_json(
821 msg="Suffix target {0} does not conform to suffix name rules".format(
822 module.params["target"]
823 )
824 )
825
826 array = get_array(module)
827 if not HAS_PURESTORAGE:
828 module.fail_json(msg="py-pure-client sdk is required for this module")
829 if module.params["offload"]:
830 if not _check_offload(module, array) and not _check_target(module, array):
831 module.fail_json(
832 msg="Selected offload {0} not connected.".format(
833 module.params["offload"]
834 )
835 )
836 if (
837 state == "copy"
838 and module.params["offload"]
839 and not _check_target(module, array)
840 ):
841 module.fail_json(
842 msg="Snapshot copy is not supported when an offload target is defined"
843 )
844 destroyed = False
845 array_snap = False
846 offload_snap = False
847 volume = get_volume(module, array)
848 if module.params["offload"] and not _check_target(module, array):
849 offload_snap = _check_offload_snapshot(module, array)
850 if offload_snap is None:
851 offload_snap = False
852 else:
853 offload_snap = not offload_snap.destroyed
854 else:
855 array_snap = get_snapshot(module, array)
856 snap = array_snap or offload_snap
857
858 if not snap:
859 destroyed = get_deleted_snapshot(module, array)
860 if state == "present" and volume and not destroyed:
861 create_snapshot(module, array)
862 elif state == "present" and destroyed:
863 recover_snapshot(module, array)
864 elif state == "rename" and volume and snap:
865 update_snapshot(module, array)
866 elif state == "copy" and snap:
867 create_from_snapshot(module, array)
868 elif state == "absent" and snap and not destroyed:
869 delete_snapshot(module, array)
870 elif state == "absent" and destroyed and module.params["eradicate"]:
871 eradicate_snapshot(module, array)
872 elif state == "absent" and not snap:
873 module.exit_json(changed=False)
874
875 module.exit_json(changed=False)
876
877
878 if __name__ == "__main__":
879 main()