Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (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_pg
20 version_added: '1.0.0'
21 short_description: Manage protection groups on Pure Storage FlashArrays
22 description:
23 - Create, delete or modify protection groups on Pure Storage FlashArrays.
24 - If a protection group exists and you try to add non-valid types, eg. a host
25 to a volume protection group the module will ignore the invalid types.
26 - Protection Groups on Offload targets are supported.
27 author:
28 - Pure Storage Ansible Team (@sdodsley) <[email protected]>
29 options:
30 name:
31 description:
32 - The name of the protection group.
33 type: str
34 aliases: [ pgroup ]
35 required: true
36 state:
37 description:
38 - Define whether the protection group should exist or not.
39 - If specified with I(volume) or I(host) or I(hostgroup) will
40 act on those items in the protection group only.
41 type: str
42 default: present
43 choices: [ absent, present ]
44 volume:
45 description:
46 - List of existing volumes to add to protection group.
47 - Note that volume are case-sensitive however FlashArray volume names are unique
48 and ignore case - you cannot have I(volumea) and I(volumeA)
49 type: list
50 elements: str
51 host:
52 description:
53 - List of existing hosts to add to protection group.
54 - Note that hostnames are case-sensitive however FlashArray hostnames are unique
55 and ignore case - you cannot have I(hosta) and I(hostA)
56 type: list
57 elements: str
58 hostgroup:
59 description:
60 - List of existing hostgroups to add to protection group.
61 - Note that hostgroups are case-sensitive however FlashArray hostgroup names are unique
62 and ignore case - you cannot have I(groupa) and I(groupA)
63 type: list
64 elements: str
65 eradicate:
66 description:
67 - Define whether to eradicate the protection group on delete and leave in trash.
68 type : bool
69 default: false
70 enabled:
71 description:
72 - Define whether to enabled snapshots for the protection group.
73 type : bool
74 default: true
75 target:
76 description:
77 - List of remote arrays or offload target for replication protection group
78 to connect to.
79 - Note that all replicated protection groups are asynchronous.
80 - Target arrays or offload targets must already be connected to the source array.
81 - Maximum number of targets per Portection Group is 4, assuming your
82 configuration supports this.
83 type: list
84 elements: str
85 rename:
86 description:
87 - Rename a protection group
88 - If the source protection group is in a Pod or Volume Group 'container'
89 you only need to provide the new protection group name in the same 'container'
90 type: str
91 safe_mode:
92 description:
93 - Enables SafeMode restrictions on the protection group
94 - B(Once set disabling this can only be performed by Pure Technical Support)
95 type: bool
96 default: false
97 version_added: '1.13.0'
98 context:
99 description:
100 - Name of fleet member on which to perform the operation.
101 - This requires the array receiving the request is a member of a fleet
102 and the context name to be a member of the same fleet.
103 type: str
104 default: ""
105 version_added: '1.33.0'
106 extends_documentation_fragment:
107 - purestorage.flasharray.purestorage.fa
108 """
109
110 EXAMPLES = r"""
111 - name: Create new local protection group
112 purestorage.flasharray.purefa_pg:
113 name: foo
114 fa_url: 10.10.10.2
115 api_token: e31060a7-21fc-e277-6240-25983c6c4592
116
117 - name: Create new protection group called bar in pod called foo
118 purestorage.flasharray.purefa_pg:
119 name: "foo::bar"
120 fa_url: 10.10.10.2
121 api_token: e31060a7-21fc-e277-6240-25983c6c4592
122
123 - name: Create new replicated protection group
124 purestorage.flasharray.purefa_pg:
125 name: foo
126 target:
127 - arrayb
128 - arrayc
129 fa_url: 10.10.10.2
130 api_token: e31060a7-21fc-e277-6240-25983c6c4592
131
132 - name: Create new replicated protection group to offload target and remote array
133 purestorage.flasharray.purefa_pg:
134 name: foo
135 target:
136 - offload
137 - arrayc
138 fa_url: 10.10.10.2
139 api_token: e31060a7-21fc-e277-6240-25983c6c4592
140
141 - name: Create new protection group with snapshots disabled
142 purestorage.flasharray.purefa_pg:
143 name: foo
144 enabled: false
145 fa_url: 10.10.10.2
146 api_token: e31060a7-21fc-e277-6240-25983c6c4592
147
148 - name: Delete protection group
149 purestorage.flasharray.purefa_pg:
150 name: foo
151 eradicate: true
152 fa_url: 10.10.10.2
153 api_token: e31060a7-21fc-e277-6240-25983c6c4592
154 state: absent
155
156 - name: Eradicate protection group foo on offload target where source array is arrayA
157 purestorage.flasharray.purefa_pg:
158 name: "arrayA:foo"
159 target: offload
160 eradicate: true
161 fa_url: 10.10.10.2
162 api_token: e31060a7-21fc-e277-6240-25983c6c4592
163 state: absent
164
165 - name: Rename protection group foo in pod arrayA to bar
166 purestorage.flasharray.purefa_pg:
167 name: "arrayA::foo"
168 rename: bar
169 fa_url: 10.10.10.2
170 api_token: e31060a7-21fc-e277-6240-25983c6c4592
171
172 - name: Create protection group for hostgroups
173 purestorage.flasharray.purefa_pg:
174 name: bar
175 hostgroup:
176 - hg1
177 - hg2
178 fa_url: 10.10.10.2
179 api_token: e31060a7-21fc-e277-6240-25983c6c4592
180
181 - name: Create protection group for hosts
182 purestorage.flasharray.purefa_pg:
183 name: bar
184 host:
185 - host1
186 - host2
187 fa_url: 10.10.10.2
188 api_token: e31060a7-21fc-e277-6240-25983c6c4592
189
190 - name: Create replicated protection group for volumes
191 purestorage.flasharray.purefa_pg:
192 name: bar
193 volume:
194 - vol1
195 - vol2
196 target: arrayb
197 fa_url: 10.10.10.2
198 api_token: e31060a7-21fc-e277-6240-25983c6c4592
199
200 - name: Remove a volume from protection group
201 purestorage.flasharray.purefa_pg:
202 name: bar
203 volume:
204 - vol1
205 state: absent
206 fa_url: 10.10.10.2
207 api_token: e31060a7-21fc-e277-6240-25983c6c4592
208 """
209
210 RETURN = r"""
211 """
212
213 import re
214 from ansible.module_utils.basic import AnsibleModule
215 from ansible_collections.purestorage.flasharray.plugins.module_utils.purefa import (
216 purefa_argument_spec,
217 get_array,
218 )
219 from ansible_collections.purestorage.flasharray.plugins.module_utils.version import (
220 LooseVersion,
221 )
222
223 HAS_PURESTORAGE = True
224 try:
225 from pypureclient.flasharray import (
226 ProtectionGroup,
227 ReplicationSchedule,
228 SnapshotSchedule,
229 )
230 except ImportError:
231 HAS_PURESTORAGE = False
232
233
234 RETENTION_LOCK_VERSION = "2.13"
235 CONTEXT_API_VERSION = "2.38"
236
237
238 def get_pod(module, array):
239 """Get ActiveCluster Pod"""
240 api_version = array.get_rest_version()
241 pod_name = module.params["name"].split("::")[0]
242 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
243 res = array.get_pods(names=[pod_name], context_names=[module.params["context"]])
244 else:
245 res = array.get_pods(names=[pod_name])
246 if res.status_code == 200:
247 return list(res.items)[0]
248 return None
249
250
251 def get_targets(module, array):
252 """Get Offload Targets"""
253 api_version = array.get_rest_version()
254 targets = []
255 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
256 target_details = list(
257 array.get_offloads(context_names=[module.params["context"]]).items
258 )
259 else:
260 target_details = list(array.get_offloads().items)
261
262 for target in target_details:
263 if target.status in ["connected", "partially_connected"]:
264 targets.append(target.name)
265 return targets
266
267
268 def get_arrays(module, array):
269 """Get Connected Arrays"""
270 api_version = array.get_rest_version()
271 arrays = []
272 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
273 array_details = list(
274 array.get_array_connections(context_names=[module.params["context"]]).items
275 )
276 else:
277 array_details = list(array.get_array_connections().items)
278 for array_detail in array_details:
279 if array_detail.status in [
280 "connected",
281 "partially_connected",
282 ]:
283 arrays.append(array_detail.name)
284 return arrays
285
286
287 def get_pending_pgroup(module, array):
288 """Get Protection Group"""
289 api_version = array.get_rest_version()
290 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
291 res = array.get_protection_groups(
292 names=[module.params["name"]],
293 destroyed=True,
294 context_names=[module.params["context"]],
295 )
296 else:
297 res = array.get_protection_groups(names=[module.params["name"]], destroyed=True)
298 if res.status_code == 200:
299 return list(res.items)[0]
300 return None
301
302
303 def get_pgroup(module, array):
304 """Get Protection Group"""
305 api_version = array.get_rest_version()
306 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
307 res = array.get_protection_groups(
308 names=[module.params["name"]],
309 destroyed=False,
310 context_names=[module.params["context"]],
311 )
312 else:
313 res = array.get_protection_groups(
314 names=[module.params["name"]], destroyed=False
315 )
316 if res.status_code == 200:
317 return list(res.items)[0]
318 return None
319
320
321 def get_pgroup_sched(module, array):
322 """Get Protection Group Schedule"""
323 api_version = array.get_rest_version()
324 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
325 res = array.get_protection_groups(
326 names=[module.params["name"]],
327 destroyed=False,
328 context_names=[module.params["context"]],
329 )
330 else:
331 res = array.get_protection_groups(
332 names=[module.params["name"]], destroyed=False
333 )
334 if res.status_code == 200:
335 return list(res.items)[0]
336 return None
337
338
339 def check_pg_on_offload(module, array):
340 """Check if PG already exists on offload target"""
341 api_version = array.get_rest_version()
342 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
343 array_name = list(
344 array.get_arrays(context_names=[module.params["context"]]).items
345 )[0].name
346 else:
347 array_name = list(array.get_arrays().items)[0].name
348 remote_pg = array_name + ":" + module.params["name"]
349 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
350 res = array.get_remote_protection_groups(
351 names=[remote_pg], context_names=[module.params["context"]]
352 )
353 else:
354 res = array.get_remote_protection_groups(names=[remote_pg])
355 if res.status_code == 200:
356 return list(res.items)[0].remote.name
357 return None
358
359
360 def make_pgroup(module, array):
361 """Create Protection Group"""
362 api_version = array.get_rest_version()
363 changed = True
364 if module.params["target"]:
365 connected_targets = []
366 connected_arrays = get_arrays(module, array)
367 connected_targets = get_targets(module, array)
368 offload_name = check_pg_on_offload(module, array)
369 if offload_name and offload_name in module.params["target"][0:4]:
370 module.fail_json(
371 msg="Protection Group {0} already exists on offload target {1}.".format(
372 module.params["name"], offload_name
373 )
374 )
375
376 connected_arrays = connected_arrays + connected_targets
377 if not connected_arrays:
378 module.fail_json(msg="No connected targets on source array.")
379 if set(module.params["target"][0:4]).issubset(connected_arrays):
380 if not module.check_mode:
381 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
382 res = array.post_protection_groups(
383 names=[module.params["name"]],
384 context_names=[module.params["context"]],
385 )
386 else:
387 res = array.post_protection_groups(names=[module.params["name"]])
388 if res.status_code != 200:
389 module.fail_json(
390 msg="Failed to create protection group {0}. Error: {1}".format(
391 module.params["name"], res.errors[0].message
392 )
393 )
394 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
395 res = array.post_protection_groups_targets(
396 group_names=[module.params["name"]],
397 member_names=module.params["target"][0:4],
398 context_names=[module.params["context"]],
399 )
400 else:
401 res = array.post_protection_groups_targets(
402 group_names=[module.params["name"]],
403 member_names=module.params["target"][0:4],
404 )
405 if res.status_code != 200:
406 module.fail_json(
407 msg="Failed to add targets to protection group {0}. Error: {1}".format(
408 module.params["name"], res.errors[0].message
409 )
410 )
411 else:
412 module.fail_json(
413 msg="Check all selected targets are connected to the source array."
414 )
415 else:
416 if not module.check_mode:
417 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
418 res = array.post_protection_groups(
419 names=[module.params["name"]],
420 context_names=[module.params["context"]],
421 )
422 else:
423 res = array.post_protection_groups(names=[module.params["name"]])
424 if res.status_code != 200:
425 module.fail_json(
426 msg="Failed to create protection group {0}. Error: {1}".format(
427 module.params["name"], res.errors[0].message
428 )
429 )
430 if not module.check_mode:
431 if module.params["target"]:
432 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
433 res = array.patch_protection_groups(
434 names=[module.params["name"]],
435 context_names=[module.params["context"]],
436 protection_group=ProtectionGroup(
437 replication_schedule=ReplicationSchedule(
438 enabled=module.params["enabled"]
439 )
440 ),
441 )
442 else:
443 res = array.patch_protection_groups(
444 names=[module.params["name"]],
445 protection_group=ProtectionGroup(
446 replication_schedule=ReplicationSchedule(
447 enabled=module.params["enabled"]
448 )
449 ),
450 )
451 else:
452 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
453 res = array.patch_protection_groups(
454 names=[module.params["name"]],
455 context_names=[module.params["context"]],
456 protection_group=ProtectionGroup(
457 snapshot_schedule=SnapshotSchedule(
458 enabled=module.params["enabled"]
459 )
460 ),
461 )
462 else:
463 res = array.patch_protection_groups(
464 names=[module.params["name"]],
465 protection_group=ProtectionGroup(
466 snapshot_schedule=SnapshotSchedule(
467 enabled=module.params["enabled"]
468 )
469 ),
470 )
471 if res.status_code != 200:
472 module.fail_json(
473 msg="Enabling pgroup {0} failed. Error: {1}".format(
474 module.params["name"], res.errors[0].message
475 )
476 )
477 if module.params["volume"]:
478 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
479 res = array.post_protection_groups_volumes(
480 group_names=[module.params["name"]],
481 member_names=module.params["volume"],
482 context_names=[module.params["context"]],
483 )
484 else:
485 res = array.post_protection_groups_volumes(
486 group_names=[module.params["name"]],
487 member_names=module.params["volume"],
488 )
489 if res.status_code != 200:
490 module.fail_json(
491 msg="Adding volumes to pgroup {0} failed. Error: {1}".format(
492 module.params["name"], res.errors[0].message
493 )
494 )
495 if module.params["host"]:
496 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
497 res = array.post_protection_groups_hosts(
498 group_names=[module.params["name"]],
499 member_names=module.params["host"],
500 context_names=[module.params["context"]],
501 )
502 else:
503 res = array.post_protection_groups_hosts(
504 group_names=[module.params["name"]],
505 member_names=module.params["host"],
506 )
507 if res.status_code != 200:
508 module.fail_json(
509 msg="Adding hosts to pgroup {0} failed. Error: {1}".format(
510 module.params["name"], res.errors[0].message
511 )
512 )
513 if module.params["hostgroup"]:
514 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
515 res = array.post_protection_groups_host_groups(
516 group_names=[module.params["name"]],
517 member_names=module.params["hostgroup"],
518 context_names=[module.params["context"]],
519 )
520 else:
521 res = array.post_protection_groups_host_groups(
522 group_names=[module.params["name"]],
523 member_names=module.params["hostgroup"],
524 )
525 if res.status_code != 200:
526 module.fail_json(
527 msg="Adding hostgroups to pgroup {0} failed. Error: {1}".format(
528 module.params["name"], res.errors[0].message
529 )
530 )
531 if module.params["safe_mode"]:
532 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
533 res = array.patch_protection_groups(
534 context_names=[module.params["context"]],
535 names=[module.params["name"]],
536 protection_group=ProtectionGroup(retention_lock="ratcheted"),
537 )
538 else:
539 res = array.patch_protection_groups(
540 names=[module.params["name"]],
541 protection_group=ProtectionGroup(retention_lock="ratcheted"),
542 )
543 if res.status_code != 200:
544 module.fail_json(
545 msg="Failed to set SafeMode on pgroup {0}. Error: {1}".format(
546 module.params["name"], res.errors[0].message
547 )
548 )
549 module.exit_json(changed=changed)
550
551
552 def rename_exists(module, array):
553 """Determine if rename target already exists"""
554 api_version = array.get_rest_version()
555 new_name = module.params["rename"]
556 if ":" in module.params["name"]:
557 container = module.params["name"].split(":")[0]
558 new_name = container + ":" + module.params["rename"]
559 if "::" in module.params["name"]:
560 new_name = container + "::" + module.params["rename"]
561 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
562 res = array.get_protection_groups(
563 names=[new_name], context_names=[module.params["context"]]
564 )
565 else:
566 res = array.get_protection_groups(names=[new_name])
567 if res.status_code == 200:
568 return True
569 return False
570
571
572 def update_pgroup(module, array):
573 """Update Protection Group"""
574 api_version = array.get_rest_version()
575 changed = renamed = False
576 state = module.params["state"]
577 if module.params["target"]:
578 connected_targets = []
579 connected_arrays = get_arrays(module, array)
580 connected_targets = get_targets(module, array)
581 connected_arrays = connected_arrays + connected_targets
582 if not connected_arrays:
583 module.fail_json(msg="No targets connected to source array.")
584 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
585 current_connects = list(
586 array.get_protection_groups_targets(
587 group_names=[module.params["name"]],
588 context_names=[module.params["context"]],
589 ).items
590 )
591 else:
592 current_connects = list(
593 array.get_protection_groups_targets(
594 group_names=[module.params["name"]]
595 ).items
596 )
597 current_targets = []
598
599 if current_connects:
600 for connect in current_connects:
601 current_targets.append(connect.member.name)
602
603 if set(module.params["target"][0:4]) != set(current_targets):
604 if not set(module.params["target"][0:4]).issubset(connected_arrays):
605 module.fail_json(
606 msg="Check all selected targets are connected to the source array."
607 )
608 changed = True
609 if not module.check_mode:
610 for target in module.params["target"][0:4]:
611 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
612 res = array.post_protection_groups_targets(
613 group_names=[module.params["name"]],
614 context_names=[module.params["context"]],
615 member_names=[target],
616 )
617 else:
618 res = array.post_protection_groups_targets(
619 group_names=[module.params["name"]],
620 member_names=[target],
621 )
622 if res.status_code != 200:
623 module.fail_json(
624 msg="Changing targets for pgroup {0} failed. Error: {1}".format(
625 module.params["name"], res.errors[0].message
626 )
627 )
628
629 if (
630 module.params["target"]
631 and module.params["enabled"]
632 != get_pgroup_sched(module, array).replication_schedule.enabled
633 ):
634 changed = True
635 if not module.check_mode:
636 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
637 res = array.patch_protection_groups(
638 names=[module.params["name"]],
639 context_names=[module.params["context"]],
640 protection_group=ProtectionGroup(
641 replication_schedule=ReplicationSchedule(
642 enabled=module.params["enabled"]
643 ),
644 ),
645 )
646 else:
647 res = array.patch_protection_groups(
648 names=[module.params["name"]],
649 protection_group=ProtectionGroup(
650 replication_schedule=ReplicationSchedule(
651 enabled=module.params["enabled"]
652 )
653 ),
654 )
655 if res.status_code != 200:
656 module.fail_json(
657 msg="Changing replication enabled state of pgroup {0} failed. Error: {1}".format(
658 module.params["name"], res.errors[0].message
659 )
660 )
661 elif (
662 not module.params["target"]
663 and module.params["enabled"]
664 != get_pgroup_sched(module, array).snapshot_schedule.enabled
665 ):
666 changed = True
667 if not module.check_mode:
668 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
669 res = array.patch_protection_groups(
670 names=[module.params["name"]],
671 protection_group=ProtectionGroup(
672 snapshot_schedule=SnapshotSchedule(
673 enabled=module.params["enabled"]
674 ),
675 ),
676 context_names=[module.params["context"]],
677 )
678 else:
679 res = array.patch_protection_groups(
680 names=[module.params["name"]],
681 protection_group=ProtectionGroup(
682 snapshot_schedule=SnapshotSchedule(
683 enabled=module.params["enabled"]
684 )
685 ),
686 )
687 if res.status_code != 200:
688 module.fail_json(
689 msg="Changing snapshot enabled state of pgroup {0} failed. Error: {1}".format(
690 module.params["name"], res.errors[0].message
691 )
692 )
693 if (
694 module.params["volume"]
695 and get_pgroup(module, array).host_count == 0
696 and get_pgroup(module, array).host_group_count == 0
697 ):
698 if get_pgroup(module, array).volume_count == 0:
699 if not module.check_mode:
700 changed = True
701 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
702 res = array.post_protection_groups_volumes(
703 group_names=[module.params["name"]],
704 member_names=module.params["volume"],
705 context_names=[module.params["context"]],
706 )
707 else:
708 res = array.post_protection_groups_volumes(
709 group_names=[module.params["name"]],
710 member_names=module.params["volume"],
711 )
712 if res.status_code != 200:
713 module.fail_json(
714 msg="Adding volumes to pgroup {0} failed. Error: {1}".format(
715 module.params["name"], res.errors[0].message
716 )
717 )
718 else:
719 pgvols = []
720 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
721 vols = list(
722 array.get_protection_groups_volumes(
723 group_names=[module.params["name"]],
724 context_names=[module.params["context"]],
725 ).items
726 )
727 else:
728 vols = list(
729 array.get_protection_groups_volumes(
730 group_names=[module.params["name"]]
731 ).items
732 )
733 for vol in vols:
734 pgvols.append(vol.member["name"])
735 if state == "present":
736 if not all(x in pgvols for x in module.params["volume"]):
737 if not module.check_mode:
738 changed = True
739 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(
740 api_version
741 ):
742 res = array.post_protection_groups_volumes(
743 group_names=[module.params["name"]],
744 context_names=[module.params["context"]],
745 member_names=module.params["volume"],
746 )
747 else:
748 res = array.post_protection_groups_volumes(
749 group_names=[module.params["name"]],
750 member_names=module.params["volume"],
751 )
752 if res.status_code != 200:
753 module.fail_json(
754 msg="Adding volumes in pgroup {0} failed. Error: {1}".format(
755 module.params["name"], res.errors[0].message
756 )
757 )
758 else:
759 if all(x in pgvols for x in module.params["volume"]):
760 if not module.check_mode:
761 changed = True
762 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(
763 api_version
764 ):
765 res = array.delete_protection_groups_volumes(
766 group_names=[module.params["name"]],
767 context_names=[module.params["context"]],
768 member_names=module.params["volume"],
769 )
770 else:
771 res = array.delete_protection_groups_volumes(
772 group_names=[module.params["name"]],
773 member_names=module.params["volume"],
774 )
775 if res.status_code != 200:
776 module.fail_json(
777 msg="Removing volumes from pgroup {0} failed. Error: {1}".format(
778 module.params["name"], res.errors[0].message
779 )
780 )
781
782 if (
783 module.params["host"]
784 and get_pgroup(module, array).volume_count == 0
785 and get_pgroup(module, array).host_group_count == 0
786 ):
787 if get_pgroup(module, array).host_count == 0:
788 if not module.check_mode:
789 changed = True
790 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
791 res = array.post_protection_groups_hosts(
792 group_names=[module.params["name"]],
793 member_names=module.params["host"],
794 context_names=[module.params["context"]],
795 )
796 else:
797 res = array.post_protection_groups_hosts(
798 group_names=[module.params["name"]],
799 member_names=module.params["host"],
800 )
801 if res.status_code != 200:
802 module.fail_json(
803 msg="Adding hosts to pgroup {0} failed. Error: {1}".format(
804 module.params["name"], res.errors[0].message
805 )
806 )
807 else:
808 pghosts = []
809 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
810 hosts = list(
811 array.get_protection_groups_hosts(
812 group_names=[module.params["name"]],
813 context_names=[module.params["context"]],
814 ).items
815 )
816 else:
817 hosts = list(
818 array.get_protection_groups_hosts(
819 group_names=[module.params["name"]]
820 ).items
821 )
822 for host in hosts:
823 pghosts.append(host.member["name"])
824 if state == "present":
825 if not all(x in pghosts for x in module.params["host"]):
826 if not module.check_mode:
827 changed = True
828 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(
829 api_version
830 ):
831 res = array.post_protection_groups_hosts(
832 group_names=[module.params["name"]],
833 context_names=[module.params["context"]],
834 member_names=module.params["host"],
835 )
836 else:
837 res = array.post_protection_groups_hosts(
838 group_names=[module.params["name"]],
839 member_names=module.params["host"],
840 )
841 if res.status_code != 200:
842 module.fail_json(
843 msg="Adding hosts in pgroup {0} failed. Error: {1}".format(
844 module.params["name"], res.errors[0].message
845 )
846 )
847 else:
848 if all(x in pghosts for x in module.params["host"]):
849 if not module.check_mode:
850 changed = True
851 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(
852 api_version
853 ):
854 res = array.delete_protection_groups_hosts(
855 group_names=[module.params["name"]],
856 context_names=[module.params["context"]],
857 member_names=module.params["host"],
858 )
859 else:
860 res = array.delete_protection_groups_hosts(
861 group_names=[module.params["name"]],
862 member_names=module.params["host"],
863 )
864 if res.status_code != 200:
865 module.fail_json(
866 msg="Removing hosts from pgroup {0} failed. Error: {1}".format(
867 module.params["name"], res.errors[0].message
868 )
869 )
870
871 if (
872 module.params["hostgroup"]
873 and get_pgroup(module, array).host_count == 0
874 and get_pgroup(module, array).volume_count == 0
875 ):
876 if get_pgroup(module, array).host_group_count == 0:
877 if not module.check_mode:
878 changed = True
879 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
880 res = array.post_protection_groups_host_groups(
881 group_names=[module.params["name"]],
882 context_names=[module.params["context"]],
883 member_names=module.params["host"],
884 )
885 else:
886 res = array.post_protection_groups_host_groups(
887 group_names=[module.params["name"]],
888 member_names=module.params["host"],
889 )
890 if res.status_code != 200:
891 module.fail_json(
892 msg="Adding hostgroups in pgroup {0} failed. Error: {1}".format(
893 module.params["name"], res.errors[0].message
894 )
895 )
896 else:
897 pghostgs = []
898 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
899 hostgs = list(
900 array.get_protection_groups_host_groups(
901 group_names=[module.params["name"]],
902 context_names=[module.params["context"]],
903 ).items
904 )
905 else:
906 hostgs = list(
907 array.get_protection_groups_host_groups(
908 group_names=[module.params["name"]]
909 ).items
910 )
911 for hostg in hostgs:
912 pghostgs.append(hostg.member["name"])
913 if state == "present":
914 if not all(x in pghostgs for x in module.params["hostgroup"]):
915 if not module.check_mode:
916 changed = True
917 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(
918 api_version
919 ):
920 res = array.post_protection_groups_host_groups(
921 group_names=[module.params["name"]],
922 context_names=[module.params["context"]],
923 member_names=module.params["hostgroup"],
924 )
925 else:
926 res = array.post_protection_groups_host_groups(
927 group_names=[module.params["name"]],
928 member_names=module.params["hostgroup"],
929 )
930 if res.status_code != 200:
931 module.fail_json(
932 msg="Adding hostgroups in pgroup {0} failed. Error: {1}".format(
933 module.params["name"], res.errors[0].message
934 )
935 )
936 else:
937 if all(x in pghostgs for x in module.params["hostgroup"]):
938 if not module.check_mode:
939 changed = True
940 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(
941 api_version
942 ):
943 res = array.delete_protection_groups_host_groups(
944 group_names=[module.params["name"]],
945 context_names=[module.params["context"]],
946 member_names=module.params["hostgroup"],
947 )
948 else:
949 res = array.delete_protection_groups_host_groups(
950 group_names=[module.params["name"]],
951 member_names=module.params["hostgroup"],
952 )
953 if res.status_code != 200:
954 module.fail_json(
955 msg="Removing hostgroups from pgroup {0} failed. Error: {1}".format(
956 module.params["name"], res.errors[0].message
957 )
958 )
959 if module.params["rename"]:
960 if not rename_exists(module, array):
961 if ":" in module.params["name"]:
962 container = module.params["name"].split(":")[0]
963 if "::" in module.params["name"]:
964 rename = container + "::" + module.params["rename"]
965 else:
966 rename = container + ":" + module.params["rename"]
967 else:
968 rename = module.params["rename"]
969 renamed = True
970 if not module.check_mode:
971 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
972 res = array.patch_protection_groups(
973 names=[module.params["name"]],
974 protection_group=ProtectionGroup(name=rename),
975 context_names=[module.params["context"]],
976 )
977 else:
978 res = array.patch_protection_groups(
979 names=[module.params["name"]],
980 protection_group=ProtectionGroup(name=rename),
981 )
982 module.params["name"] = rename
983 if res.status_code != 200:
984 module.fail_json(
985 msg="Rename to {0} failed. Error: {1}".format(
986 rename, res.errors[0].message
987 )
988 )
989 else:
990 module.warn(
991 "Rename failed. Protection group {0} already exists in container. Continuing with other changes...".format(
992 module.params["rename"]
993 )
994 )
995 if LooseVersion(RETENTION_LOCK_VERSION) <= LooseVersion(api_version):
996 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
997 current_pg = list(
998 array.get_protection_groups(
999 names=[module.params["name"]],
1000 context_names=[module.params["context"]],
1001 ).items
1002 )[0]
1003 else:
1004 current_pg = list(
1005 array.get_protection_groups(names=[module.params["name"]]).items
1006 )[0]
1007 if current_pg.retention_lock == "unlocked" and module.params["safe_mode"]:
1008 changed = True
1009 if not module.check_mode:
1010 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
1011 res = array.patch_protection_groups(
1012 names=[module.params["name"]],
1013 context_names=[module.params["context"]],
1014 protection_group=ProtectionGroup(retention_lock="ratcheted"),
1015 )
1016 else:
1017 res = array.patch_protection_groups(
1018 names=[module.params["name"]],
1019 protection_group=ProtectionGroup(retention_lock="ratcheted"),
1020 )
1021 if res.status_code != 200:
1022 module.fail_json(
1023 msg="Failed to set SafeMode on protection group {0}. Error: {1}".format(
1024 module.params["name"],
1025 res.errors[0].message,
1026 )
1027 )
1028 if current_pg.retention_lock == "ratcheted" and not module.params["safe_mode"]:
1029 module.warn(
1030 "Disabling SafeMode on protection group {0} can only be performed by Pure Technical Support".format(
1031 module.params["name"]
1032 )
1033 )
1034 changed = changed or renamed
1035 module.exit_json(changed=changed)
1036
1037
1038 def eradicate_pgroup(module, array):
1039 """Eradicate Protection Group"""
1040 api_version = array.get_rest_version()
1041 changed = True
1042 if not module.check_mode:
1043 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
1044 res = array.delete_protection_groups(
1045 names=[module.params["name"]], context_names=[module.params["context"]]
1046 )
1047 else:
1048 res = array.delete_protection_groups(names=[module.params["name"]])
1049 if res.status_code != 200:
1050 module.fail_json(
1051 msg="Eradicating pgroup {0} failed. Error: {1}".format(
1052 module.params["name"], res.errors[0].message
1053 )
1054 )
1055 module.exit_json(changed=changed)
1056
1057
1058 def delete_pgroup(module, array):
1059 """Delete Protection Group"""
1060 api_version = array.get_rest_version()
1061 changed = True
1062 if not module.check_mode:
1063 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
1064 res = array.patch_protection_groups(
1065 names=[module.params["name"]],
1066 context_names=[module.params["context"]],
1067 protection_group=ProtectionGroup(destroyed=True),
1068 )
1069 else:
1070 res = array.patch_protection_groups(
1071 names=[module.params["name"]],
1072 protection_group=ProtectionGroup(destroyed=True),
1073 )
1074 if res.status_code != 200:
1075 module.fail_json(
1076 msg="Deleting pgroup {0} failed. Error: {1}".format(
1077 module.params["name"], res.errors[0].message
1078 )
1079 )
1080 if module.params["eradicate"]:
1081 eradicate_pgroup(module, array)
1082
1083 module.exit_json(changed=changed)
1084
1085
1086 def recover_pgroup(module, array):
1087 """Recover deleted protection group"""
1088 api_version = array.get_rest_version()
1089 changed = True
1090 if not module.check_mode:
1091 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
1092 res = array.patch_protection_groups(
1093 names=[module.params["name"]],
1094 context_names=[module.params["context"]],
1095 protection_group=ProtectionGroup(destroyed=False),
1096 )
1097 else:
1098 res = array.patch_protection_groups(
1099 names=[module.params["name"]],
1100 protection_group=ProtectionGroup(destroyed=False),
1101 )
1102 if res.status_code != 200:
1103 module.fail_json(
1104 msg="Recover pgroup {0} failed. Error: {1}".format(
1105 module.params["name"], res.errors[0].message
1106 )
1107 )
1108 module.exit_json(changed=changed)
1109
1110
1111 def main():
1112 argument_spec = purefa_argument_spec()
1113 argument_spec.update(
1114 dict(
1115 name=dict(type="str", required=True, aliases=["pgroup"]),
1116 state=dict(type="str", default="present", choices=["absent", "present"]),
1117 volume=dict(type="list", elements="str"),
1118 host=dict(type="list", elements="str"),
1119 hostgroup=dict(type="list", elements="str"),
1120 target=dict(type="list", elements="str"),
1121 safe_mode=dict(type="bool", default=False),
1122 eradicate=dict(type="bool", default=False),
1123 enabled=dict(type="bool", default=True),
1124 rename=dict(type="str"),
1125 context=dict(type="str", default=""),
1126 )
1127 )
1128
1129 mutually_exclusive = [["volume", "host", "hostgroup"]]
1130 module = AnsibleModule(
1131 argument_spec, mutually_exclusive=mutually_exclusive, supports_check_mode=True
1132 )
1133 if not HAS_PURESTORAGE:
1134 module.fail_json(msg="py-pure-client sdk is required.")
1135
1136 state = module.params["state"]
1137 array = get_array(module)
1138 pattern = re.compile("^[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?$")
1139 if module.params["rename"]:
1140 if not pattern.match(module.params["rename"]):
1141 module.fail_json(
1142 msg="Rename value {0} does not conform to naming convention".format(
1143 module.params["rename"]
1144 )
1145 )
1146 if not pattern.match(module.params["name"].split(":")[-1]):
1147 module.fail_json(
1148 msg="Protection Group name {0} does not conform to naming convention".format(
1149 module.params["name"]
1150 )
1151 )
1152 api_version = array.get_rest_version()
1153 if module.params["safe_mode"] and LooseVersion(
1154 RETENTION_LOCK_VERSION
1155 ) > LooseVersion(api_version):
1156 module.fail_json(
1157 msg="API version does not support setting SafeMode on a protection group."
1158 )
1159 if ":" in module.params["name"]:
1160 if "::" in module.params["name"]:
1161 pgname = module.params["name"].split("::")[1]
1162 else:
1163 pgname = module.params["name"].split(":")[1]
1164 if not pattern.match(pgname):
1165 module.fail_json(
1166 msg="Protection Group name {0} does not conform to naming convention".format(
1167 pgname
1168 )
1169 )
1170 else:
1171 if not pattern.match(module.params["name"]):
1172 module.fail_json(
1173 msg="Protection Group name {0} does not conform to naming convention".format(
1174 module.params["name"]
1175 )
1176 )
1177
1178 pgroup = get_pgroup(module, array)
1179 xpgroup = get_pending_pgroup(module, array)
1180 if "::" in module.params["name"]:
1181 if not get_pod(module, array):
1182 module.fail_json(
1183 msg="Pod {0} does not exist.".format(
1184 module.params["name"].split("::")[0]
1185 )
1186 )
1187
1188 if module.params["host"]:
1189 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
1190 host_exists = array.get_hosts(
1191 names=module.params["host"], context_names=[module.params["context"]]
1192 )
1193 else:
1194 host_exists = array.get_hosts(names=module.params["host"])
1195 if host_exists.status_code != 200:
1196 module.fail_json(
1197 msg="Host {0} not found. Error: {1}".format(
1198 host_exists.errors[0].context, host_exists.errors[0].message
1199 )
1200 )
1201
1202 if module.params["hostgroup"]:
1203 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
1204 hg_exists = array.get_host_groups(
1205 names=module.params["hostgroup"],
1206 context_names=[module.params["context"]],
1207 )
1208 else:
1209 hg_exists = array.get_host_groups(names=module.params["hostgroup"])
1210 if hg_exists.status_code != 200:
1211 module.fail_json(
1212 msg="Host Group {0} not found. Error: {1}".format(
1213 hg_exists.errors[0].context, hg_exists.errors[0].message
1214 )
1215 )
1216
1217 if pgroup and state == "present":
1218 update_pgroup(module, array)
1219 elif (
1220 pgroup
1221 and state == "absent"
1222 and (
1223 module.params["volume"]
1224 or module.params["host"]
1225 or module.params["hostgroup"]
1226 )
1227 ):
1228 update_pgroup(module, array)
1229 elif pgroup and state == "absent":
1230 delete_pgroup(module, array)
1231 elif xpgroup and state == "absent" and module.params["eradicate"]:
1232 eradicate_pgroup(module, array)
1233 elif (
1234 not pgroup
1235 and not xpgroup
1236 and state == "present"
1237 and not module.params["rename"]
1238 ):
1239 make_pgroup(module, array)
1240 elif not pgroup and state == "present" and module.params["rename"]:
1241 module.exit_json(changed=False)
1242 elif xpgroup and state == "present":
1243 recover_pgroup(module, array)
1244 elif pgroup is None and state == "absent":
1245 module.exit_json(changed=False)
1246
1247 module.exit_json(changed=False)
1248
1249
1250 if __name__ == "__main__":
1251 main()