Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # (c) 2019, 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_pod
20 short_description: Manage AC pods in Pure Storage FlashArrays
21 version_added: '1.0.0'
22 description:
23 - Manage AC pods in a Pure Storage FlashArray.
24 author:
25 - Pure Storage Ansible Team (@sdodsley) <[email protected]>
26 options:
27 name:
28 description:
29 - The name of the pod.
30 type: str
31 required: true
32 stretch:
33 description:
34 - The name of the array to stretch to/unstretch from. Must be synchromously replicated.
35 - To unstretch an array use state I(absent)
36 - You can only specify a remote array, ie you cannot unstretch a pod from the
37 current array and then restretch back to the current array.
38 - To restretch a pod you must perform this from the remaining array the pod
39 resides on.
40 type: str
41 failover:
42 description:
43 - The name of the array given priority to stay online if arrays loose
44 contact with each other.
45 - Options are either array in the cluster, or I(auto)
46 type: list
47 elements: str
48 state:
49 description:
50 - Define whether the pod should exist or not.
51 default: present
52 choices: [ absent, present ]
53 type: str
54 eradicate:
55 description:
56 - Define whether to eradicate the pod on delete or leave in trash.
57 type: bool
58 default: false
59 target:
60 description:
61 - Name of clone target pod.
62 type: str
63 mediator:
64 description:
65 - Name of the mediator to use for a pod
66 type: str
67 default: purestorage
68 promote:
69 description:
70 - Promote/demote any pod not in a stretched relationship. .
71 - Demoting a pod will render it read-only.
72 required: false
73 type: bool
74 quiesce:
75 description:
76 - Quiesce/Skip quiesce when I(promote) is false and demoting an ActiveDR pod.
77 - Quiesce will ensure all local data has been replicated before demotion.
78 - Skipping quiesce looses all pending data to be replicated to the remote pod.
79 - Can only demote the pod if it is in a Acrive DR replica link relationship.
80 - This will default to True
81 required: false
82 type: bool
83 undo:
84 description:
85 - Use the I(undo-remote) pod when I(promote) is true and promoting an ActiveDR pod.
86 - This will default to True
87 required: false
88 type: bool
89 quota:
90 description:
91 - Logical quota limit of the pod in K, M, G, T or P units, or bytes.
92 type: str
93 version_added: '1.18.0'
94 ignore_usage:
95 description:
96 - Flag used to override checks for quota management
97 operations.
98 - If set to true, pod usage is not checked against the
99 quota_limits that are set.
100 - If set to false, the actual logical bytes in use are prevented
101 from exceeding the limits set on the pod.
102 - Client operations might be impacted.
103 - If the limit exceeds the quota, the operation is not allowed.
104 default: false
105 type: bool
106 version_added: '1.18.0'
107 throttle:
108 description:
109 - Allows pod creation to fail if array health is not optimal
110 type: bool
111 default: false
112 version_added: '1.29.0'
113 delete_contents:
114 description:
115 - This enables you to eradicate pods with contents.
116 type: bool
117 default: False
118 version_added: '1.29.0'
119 context:
120 description:
121 - Name of fleet member on which to perform the operation.
122 - This requires the array receiving the request is a member of a fleet
123 and the context name to be a member of the same fleet.
124 type: str
125 default: ""
126 version_added: '1.33.0'
127 with_default_protection:
128 description:
129 - Whether to keep the default container protection for the pod
130 - Only applicable for first creation of a pod
131 type: bool
132 default: true
133 version_added: '1.37.0'
134 default_protection_pg:
135 description:
136 - Name of the default protection default for the pod
137 - Only applicable for existing pods
138 - Name must include the pod name
139 - Will create the PG in the pod if it doesn't already exist
140 - To remove an existing default protection group provide I([])
141 type: str
142 version_added: '1.37.0'
143 retention_lock:
144 description:
145 - Define if I(default_protection_pg) has retention lock enabled
146 type: bool
147 default: True
148 version_added: '1.37.0'
149 extends_documentation_fragment:
150 - purestorage.flasharray.purestorage.fa
151 """
152
153 EXAMPLES = r"""
154 - name: Create new pod named foo without SafeMode default protection
155 purestorage.flasharray.purefa_pod:
156 name: foo
157 with_default_protection: false
158 fa_url: 10.10.10.2
159 api_token: e31060a7-21fc-e277-6240-25983c6c4592
160 state: present
161
162 - name: Create new pod named foo with default protection PG safe, and with PG retention lock disabled
163 purestorage.flasharray.purefa_pod:
164 name: foo
165 default_protection_pg: safe
166 retention_lock: false
167 fa_url: 10.10.10.2
168 api_token: e31060a7-21fc-e277-6240-25983c6c4592
169 state: present
170
171 - name: Delete and eradicate pod named foo
172 purestorage.flasharray.purefa_pod:
173 name: foo
174 eradicate: true
175 fa_url: 10.10.10.2
176 api_token: e31060a7-21fc-e277-6240-25983c6c4592
177 state: absent
178
179 - name: Set failover array for pod named foo
180 purestorage.flasharray.purefa_pod:
181 name: foo
182 failover:
183 - array1
184 fa_url: 10.10.10.2
185 api_token: e31060a7-21fc-e277-6240-25983c6c4592
186
187 - name: Set mediator for pod named foo
188 purestorage.flasharray.purefa_pod:
189 name: foo
190 mediator: bar
191 fa_url: 10.10.10.2
192 api_token: e31060a7-21fc-e277-6240-25983c6c4592
193
194 - name: Stretch a pod named foo to array2
195 purestorage.flasharray.purefa_pod:
196 name: foo
197 stretch: array2
198 fa_url: 10.10.10.2
199 api_token: e31060a7-21fc-e277-6240-25983c6c4592
200
201 - name: Unstretch a pod named foo from array2
202 purestorage.flasharray.purefa_pod:
203 name: foo
204 stretch: array2
205 state: absent
206 fa_url: 10.10.10.2
207 api_token: e31060a7-21fc-e277-6240-25983c6c4592
208
209 - name: Create clone of pod foo named bar
210 purestorage.flasharray.purefa_pod:
211 name: foo
212 target: bar
213 fa_url: 10.10.10.2
214 api_token: e31060a7-21fc-e277-6240-25983c6c4592
215 state: present
216 """
217
218 RETURN = r"""
219 """
220
221 HAS_PURESTORAGE = True
222 try:
223 from pypureclient.flasharray import (
224 PodPost,
225 PodPatch,
226 Reference,
227 ContainerDefaultProtection,
228 DefaultProtectionReference,
229 ProtectionGroup,
230 )
231 except ImportError:
232 HAS_PURESTORAGE = False
233
234 from ansible.module_utils.basic import AnsibleModule
235 from ansible_collections.purestorage.flasharray.plugins.module_utils.purefa import (
236 get_array,
237 purefa_argument_spec,
238 )
239 from ansible_collections.purestorage.flasharray.plugins.module_utils.common import (
240 human_to_bytes,
241 )
242 from ansible_collections.purestorage.flasharray.plugins.module_utils.version import (
243 LooseVersion,
244 )
245
246 DEFAULT_API_VERSION = "2.16"
247 POD_QUOTA_VERSION = "2.23"
248 THROTTLE_VERSION = "2.31"
249 MEMBERS_VERSION = "2.36"
250 CONTEXT_VERSION = "2.38"
251
252
253 def get_pod(module, array):
254 """Return Pod or None"""
255 api_version = array.get_rest_version()
256 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
257 return bool(
258 array.get_pods(
259 names=[module.params["name"]],
260 context_names=[module.params["context"]],
261 destroyed=False,
262 ).status_code
263 == 200
264 )
265 return bool(
266 array.get_pods(names=[module.params["name"]], destroyed=False).status_code
267 == 200
268 )
269
270
271 def get_undo_pod(module, array):
272 """Return Undo Pod or None"""
273 api_version = array.get_rest_version()
274 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
275 res = array.get_pods(
276 names=[module.params["name"] + ".undo-demote.*"],
277 context_names=[module.params["context"]],
278 )
279 else:
280 res = array.get_pods(
281 names=[module.params["name"] + ".undo-demote.*"],
282 )
283 if res.status_code == 200:
284 return list(res.items)
285 return None
286
287
288 def get_target(module, array):
289 api_version = array.get_rest_version()
290 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
291 return bool(
292 array.get_pods(
293 names=[module.params["target"]],
294 context_names=[module.params["context"]],
295 destroyed=False,
296 ).status_code
297 == 200
298 )
299 return bool(
300 array.get_pods(names=[module.params["target"]], destroyed=False).status_code
301 == 200
302 )
303
304
305 def get_destroyed_pod(module, array):
306 """Return Destroyed Volume or None"""
307 api_version = array.get_rest_version()
308 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
309 return bool(
310 array.get_pods(
311 names=[module.params["name"]],
312 context_names=[module.params["context"]],
313 destroyed=True,
314 ).status_code
315 == 200
316 )
317 return bool(
318 array.get_pods(names=[module.params["name"]], destroyed=True).status_code == 200
319 )
320
321
322 def get_destroyed_target(module, array):
323 """Return Destroyed Volume or None"""
324 api_version = array.get_rest_version()
325 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
326 return bool(
327 array.get_pods(
328 names=[module.params["target"]],
329 context_names=[module.params["context"]],
330 destroyed=True,
331 ).status_code
332 == 200
333 )
334 return bool(
335 array.get_pods(names=[module.params["target"]], destroyed=True).status_code
336 == 200
337 )
338
339
340 def check_arrays(module, array):
341 """Check if array name provided are sync-replicated"""
342 api_version = array.get_rest_version()
343 good_arrays = []
344 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
345 good_arrays.append(
346 list(array.get_arrays(context_names=[module.params["context"]]).items)[
347 0
348 ].name
349 )
350 connected_arrays = list(
351 array.get_array_connections(context_names=[module.params["context"]]).items
352 )
353 else:
354 good_arrays.append(list(array.get_arrays().items)[0].name)
355 connected_arrays = list(array.get_array_connections().items)
356 for arr in connected_arrays:
357 if arr.type == "sync-replication":
358 good_arrays.append(arr.name)
359 if module.params["failover"] is not None:
360 if module.params["failover"] == ["auto"]:
361 failover_array = []
362 else:
363 failover_array = module.params["failover"]
364 if failover_array != []:
365 for arr in failover_array:
366 if arr not in good_arrays:
367 module.fail_json(msg="Failover array {0} is not valid.".format(arr))
368 if module.params["stretch"] is not None:
369 if module.params["stretch"] not in good_arrays:
370 module.fail_json(
371 msg="Stretch: Array {0} is not connected.".format(
372 module.params["stretch"]
373 )
374 )
375 return None
376
377
378 def create_pod(module, array):
379 """Create Pod"""
380 api_version = array.get_rest_version()
381 changed = True
382 if module.params["target"]:
383 module.fail_json(msg="Cannot clone non-existent pod.")
384 if not module.check_mode:
385 if module.params["failover"]:
386 failovers = []
387 for fo_array in module.params["failover"]:
388 failovers.append(Reference(name=fo_array))
389 if LooseVersion(THROTTLE_VERSION) > LooseVersion(api_version):
390 res = array.post_pods(
391 names=[module.params["name"]],
392 pod=PodPost(failover_preferences=failovers),
393 )
394 else:
395 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
396 res = array.post_pods(
397 names=[module.params["name"]],
398 context_names=[module.params["context"]],
399 pod=PodPost(failover_preferences=failovers),
400 allow_throttle=module.params["throttle"],
401 )
402 else:
403 res = array.post_pods(
404 names=[module.params["name"]],
405 pod=PodPost(failover_preferences=failovers),
406 allow_throttle=module.params["throttle"],
407 )
408 else:
409 if LooseVersion(THROTTLE_VERSION) > LooseVersion(api_version):
410 res = array.post_pods(
411 names=[module.params["name"]],
412 pod=PodPost(),
413 )
414 else:
415 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
416 res = array.post_pods(
417 names=[module.params["name"]],
418 pod=PodPost(),
419 context_names=[module.params["context"]],
420 allow_throttle=module.params["throttle"],
421 )
422 else:
423 res = array.post_pods(
424 names=[module.params["name"]],
425 pod=PodPost(),
426 allow_throttle=module.params["throttle"],
427 )
428 if res.status_code != 200:
429 module.fail_json(
430 msg="Pod {0} creation failed. Error: {1}".format(
431 module.params["name"], res.errors[0].message
432 )
433 )
434 if module.params["mediator"] != "purestorage":
435 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
436 res = array.patch_pods(
437 names=[module.params["name"]],
438 context_names=[module.params["context"]],
439 pod=PodPatch(mediator=module.params["mediator"]),
440 )
441 else:
442 res = array.patch_pods(
443 names=[module.params["name"]],
444 pod=PodPatch(mediator=module.params["mediator"]),
445 )
446 if res.status_code != 200:
447 module.warn(
448 "Failed to communicate with mediator {0}, using default value.".format(
449 module.params["mediator"]
450 )
451 )
452 if module.params["stretch"]:
453 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
454 current_array = list(
455 array.get_arrays(context_names=[module.params["context"]]).items
456 )[0].name
457 else:
458 current_array = list(array.get_arrays().items)[0].name
459 if module.params["stretch"] != current_array:
460 if LooseVersion(MEMBERS_VERSION) <= LooseVersion(api_version):
461 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
462 res = array.post_pods_members(
463 pod_names=[module.params["name"]],
464 context_names=[module.params["context"]],
465 member_names=[module.params["stretch"]],
466 )
467 else:
468 res = array.post_pods_members(
469 pod_names=[module.params["name"]],
470 member_names=[module.params["stretch"]],
471 )
472 else:
473 res = array.post_pods_arrays(
474 group_names=[module.params["name"]],
475 member_names=[module.params["stretch"]],
476 )
477 if res.status_code != 200:
478 module.fail_json(
479 msg="Failed to stretch pod {0} to array {1}. Error: {2}".format(
480 module.params["name"],
481 module.params["stretch"],
482 res.errors[0].message,
483 )
484 )
485 if module.params["quota"] and LooseVersion(POD_QUOTA_VERSION) <= LooseVersion(
486 api_version
487 ):
488 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
489 res = array.patch_pods(
490 names=[module.params["name"]],
491 context_names=[module.params["context"]],
492 pod=PodPatch(quota_limit=human_to_bytes(module.params["quota"])),
493 )
494 else:
495 res = array.patch_pods(
496 names=[module.params["name"]],
497 pod=PodPatch(quota_limit=human_to_bytes(module.params["quota"])),
498 )
499 if res.status_code != 200:
500 module.fail_json(
501 msg="Failed to apply quota to pod {0}. Error: {1}".format(
502 module.params["name"], res.errors[0].message
503 )
504 )
505 if LooseVersion(DEFAULT_API_VERSION) <= LooseVersion(api_version):
506 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
507 res = array.get_container_default_protections(
508 names=[module.params["name"]],
509 context_names=[module.params["context"]],
510 )
511 else:
512 res = array.get_container_default_protections(
513 names=[module.params["name"]]
514 )
515 if res.status_code != 200:
516 module.fail_json(
517 msg="Failed to get container default protection for pod {0}. Error: {1}".format(
518 module.params["name"], res.errors[0].message
519 )
520 )
521 safemode_pg = list(res.items)[0].default_protections
522 if safemode_pg:
523 pgname = safemode_pg[0].name
524 else:
525 pgname = None
526 if pgname and not module.params["with_default_protection"]:
527 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
528 res = array.patch_container_default_protections(
529 names=[module.params["name"]],
530 context_names=[module.params["context"]],
531 container_default_protection=(
532 ContainerDefaultProtection(default_protections=[])
533 ),
534 )
535 else:
536 res = array.patch_container_default_protections(
537 names=[module.params["name"]],
538 container_default_protection=(
539 ContainerDefaultProtection(default_protections=[])
540 ),
541 )
542 if res.status_code != 200:
543 module.fail_json(
544 msg="Failed to remove default protection for pod {0}. Error: {1}".format(
545 module.params["name"], res.errors[0].message
546 )
547 )
548 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
549 res = array.patch_protection_groups(
550 names=[pgname],
551 context_names=[module.params["context"]],
552 protection_group=ProtectionGroup(destroyed=True),
553 )
554 else:
555 res = array.patch_protection_groups(
556 names=[pgname],
557 protection_group=ProtectionGroup(destroyed=True),
558 )
559 if res.status_code != 200:
560 module.fail_json(
561 msg="Deleting safemode default pgroup {0} failed. Error: {1}".format(
562 module.params["name"], res.errors[0].message
563 )
564 )
565 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
566 res = array.delete_protection_groups(
567 names=[pgname], context_names=[module.params["context"]]
568 )
569 else:
570 res = array.delete_protection_groups(names=[pgname])
571 if res.status_code != 200:
572 module.fail_json(
573 msg="Eradicating safemode default pgroup {0} failed. Error: {1}".format(
574 module.params["name"], res.errors[0].message
575 )
576 )
577 if (
578 safemode_pg
579 and not module.params["with_default_protection"]
580 and module.params["default_protection_pg"]
581 ):
582 if module.params["default_protection_pg"] == "[]":
583 module.fail_json(
584 msg="use with_default_protection: false to set no default protection"
585 )
586 if pgname != module.params["default_protection_pg"]:
587 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
588 res = array.get_protection_groups(
589 context_names=[module.params["context"]],
590 names=[module.params["default_protection_pg"]],
591 )
592 else:
593 res = array.get_protection_groups(
594 names=[module.params["default_protection_pg"]]
595 )
596 if res.status_code != 200:
597 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
598 pg_res = array.post_protection_groups(
599 context_names=[module.params["context"]],
600 names=[module.params["default_protection_pg"]],
601 )
602 else:
603 pg_res = array.post_protection_groups(
604 names=[module.params["default_protection_pg"]]
605 )
606 if pg_res.status_code != 200:
607 module.fail_json(
608 msg="Failed to create default protection group {0}. Error: {1}".format(
609 module.params["name"], res.errors[0].message
610 )
611 )
612 if (
613 module.params["retention_lock"]
614 and module.params["default_protection_pg"] != []
615 ):
616 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
617 res = array.patch_protection_groups(
618 context_names=[module.params["context"]],
619 names=[module.params["default_protection_pg"]],
620 protection_group=ProtectionGroup(
621 retention_lock="ratcheted"
622 ),
623 )
624 else:
625 res = array.patch_protection_groups(
626 names=[module.params["default_protection_pg"]],
627 protection_group=ProtectionGroup(
628 retention_lock="ratcheted"
629 ),
630 )
631 if res.status_code != 200:
632 module.fail_json(
633 msg="Failed to set retention lock for protection group {0}. Error: {1}".format(
634 module.params["default_protection_pg"],
635 res.errors[0].message,
636 )
637 )
638 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
639 array.patch_container_default_protections(
640 context_names=[module.params["context"]],
641 names=[module.params["name"]],
642 container_default_protection=(
643 ContainerDefaultProtection(default_protections=[])
644 ),
645 )
646 else:
647 array.patch_container_default_protections(
648 names=[module.params["name"]],
649 container_default_protection=(
650 ContainerDefaultProtection(default_protections=[])
651 ),
652 )
653 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
654 res = array.patch_container_default_protections(
655 context_names=[module.params["context"]],
656 names=[module.params["name"]],
657 container_default_protection=(
658 ContainerDefaultProtection(
659 default_protections=[
660 DefaultProtectionReference(
661 name=module.params["default_protection_pg"],
662 type="protection_group",
663 )
664 ]
665 )
666 ),
667 )
668 else:
669 res = array.patch_container_default_protections(
670 names=[module.params["name"]],
671 container_default_protection=(
672 ContainerDefaultProtection(
673 default_protections=[
674 DefaultProtectionReference(
675 name=module.params["default_protection_pg"],
676 type="protection_group",
677 )
678 ]
679 )
680 ),
681 )
682 if res.status_code != 200:
683 module.fail_json(
684 msg="Failed to set default protection for pod {0}. Error: {1}".format(
685 module.params["name"], res.errors[0].message
686 )
687 )
688 module.exit_json(changed=changed)
689
690
691 def clone_pod(module, array):
692 """Create Pod Clone"""
693 api_version = array.get_rest_version()
694 changed = False
695 if not get_target(module, array):
696 if not get_destroyed_target(module, array):
697 changed = True
698 if not module.check_mode:
699 if LooseVersion(THROTTLE_VERSION) > LooseVersion(api_version):
700 res = array.post_pods(
701 pod=PodPost(source=Reference(name=module.params["name"])),
702 names=[module.params["target"]],
703 )
704 else:
705 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
706 res = array.post_pods(
707 pod=PodPost(source=Reference(name=module.params["name"])),
708 names=[module.params["target"]],
709 context_names=[module.params["context"]],
710 allow_throttle=module.params["throttle"],
711 )
712 else:
713 res = array.post_pods(
714 pod=PodPost(source=Reference(name=module.params["name"])),
715 names=[module.params["target"]],
716 allow_throttle=module.params["throttle"],
717 )
718 if res.status_code != 200:
719 module.fail_json(
720 msg="Clone pod {0} to pod {1} failed. Error: {2}".format(
721 module.params["name"],
722 module.params["target"],
723 res.errors[0].message,
724 )
725 )
726 else:
727 module.fail_json(
728 msg="Target pod {0} already exists but deleted.".format(
729 module.params["target"]
730 )
731 )
732
733 module.exit_json(changed=changed)
734
735
736 def update_pod(module, array):
737 """Update Pod configuration"""
738 api_version = array.get_rest_version()
739 changed = False
740 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
741 current_config = list(
742 array.get_pods(
743 names=[module.params["name"]], context_names=[module.params["context"]]
744 ).items
745 )[0]
746 else:
747 current_config = list(array.get_pods(names=[module.params["name"]]).items)[0]
748 if module.params["failover"]:
749 current_failover = current_config.failover_preferences
750 if current_failover == [] or sorted(module.params["failover"]) != sorted(
751 current_failover
752 ):
753 changed = True
754 if not module.check_mode:
755 if module.params["failover"] == ["auto"]:
756 if current_failover != []:
757 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
758 res = array.patch_pods(
759 names=[module.params["name"]],
760 context_names=[module.params["context"]],
761 pod=PodPatch(failover_preferences=[]),
762 )
763 else:
764 res = array.patch_pods(
765 names=[module.params["name"]],
766 pod=PodPatch(failover_preferences=[]),
767 )
768 if res.status_code != 200:
769 module.fail_json(
770 msg="Failed to clear failover preference for pod {0}. Error: {1}".format(
771 module.params["name"],
772 res.errors[0].message,
773 )
774 )
775 else:
776 failovers = []
777 for fo_array in module.params["failover"]:
778 failovers.append(Reference(name=fo_array))
779 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
780 res = array.patch_pods(
781 names=[module.params["name"]],
782 context_names=[module.params["context"]],
783 pod=PodPatch(failover_preferences=failovers),
784 )
785 else:
786 res = array.patch_pods(
787 names=[module.params["name"]],
788 pod=PodPatch(failover_preferences=failovers),
789 )
790 if res.status_code != 200:
791 module.fail_json(
792 msg="Failed to set failover preference for pod {0}. Error: {1}".format(
793 module.params["name"],
794 res.errors[0].message,
795 )
796 )
797 if current_config.mediator != module.params["mediator"]:
798 changed = True
799 if not module.check_mode:
800 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
801 res = array.patch_pods(
802 names=[module.params["name"]],
803 context_names=[module.params["context"]],
804 pod=PodPatch(mediator=module.params["mediator"]),
805 )
806 else:
807 res = array.patch_pods(
808 names=[module.params["name"]],
809 pod=PodPatch(mediator=module.params["mediator"]),
810 )
811 if res.status_code != 200:
812 module.warn(
813 "Failed to communicate with mediator {0}. Setting unchanged.i Error: {1}".format(
814 module.params["mediator"],
815 res.errors[0].message,
816 )
817 )
818 if module.params["promote"] is not None:
819 if current_config.array_count > 1:
820 module.fail_json(
821 msg="Promotion/Demotion not permitted. Pod {0} is stretched".format(
822 module.params["name"]
823 )
824 )
825 else:
826 changed = True
827 if not module.check_mode:
828 if (
829 current_config.promotion_status == "demoted"
830 and module.params["promote"]
831 ):
832 if module.params["undo"] is None:
833 module.params["undo"] = True
834 if current_config.promotion_status == "quiescing":
835 module.fail_json(
836 msg="Cannot promote pod {0} as it is still quiesing".format(
837 module.params["name"]
838 )
839 )
840 elif module.params["undo"]:
841 undo_pod = get_undo_pod(module, array)
842 if undo_pod:
843 if len(undo_pod) == 1:
844 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(
845 api_version
846 ):
847 res = array.patch_pods(
848 pod=PodPatch(
849 requested_promotion_state="promoted"
850 ),
851 names=[module.params["name"]],
852 context_names=[module.params["context"]],
853 promote_from=undo_pod[0].name,
854 )
855 else:
856 res = array.patch_pods(
857 pod=PodPatch(
858 requested_promotion_state="promoted"
859 ),
860 names=[module.params["name"]],
861 promote_from=undo_pod[0].name,
862 )
863 else:
864 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(
865 api_version
866 ):
867 res = array.patch_pods(
868 pod=PodPatch(
869 requested_promotion_state="promoted"
870 ),
871 names=[module.params["name"]],
872 context_names=[module.params["context"]],
873 promote_from=undo_pod[-1].name,
874 )
875 else:
876 res = array.patch_pods(
877 pod=PodPatch(
878 requested_promotion_state="promoted"
879 ),
880 names=[module.params["name"]],
881 promote_from=undo_pod[-1].name,
882 )
883 module.warn(
884 "undo-demote pod(s) remaining for {0}. Consider eradicating.".format(
885 module.params["name"]
886 )
887 )
888 else:
889 changed = False
890 module.warn(
891 "undo-demote pod(s) missing for {0}. Check use of `undo` parameter.".format(
892 module.params["name"]
893 )
894 )
895 else:
896 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
897 res = array.patch_pods(
898 names=[module.params["name"]],
899 context_names=[module.params["context"]],
900 pod=PodPatch(requested_promotion_state="promoted"),
901 )
902 else:
903 res = array.patch_pods(
904 names=[module.params["name"]],
905 pod=PodPatch(requested_promotion_state="promoted"),
906 )
907 if res.status_code != 200:
908 module.fail_json(
909 msg="Failed to promote pod {0}. Error: {1}".format(
910 module.params["name"], res.erroros[0].message
911 )
912 )
913 elif (
914 current_config.promotion_status != "demoted"
915 and not module.params["promote"]
916 ):
917 if get_undo_pod(module, array):
918 module.fail_json(
919 msg="Cannot demote pod {0} due to associated undo-demote "
920 "pod not being eradicated".format(module.params["name"])
921 )
922 if module.params["quiesce"] is None:
923 module.params["quiesce"] = True
924 if current_config["link_target_count"] == 0:
925 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
926 res = array.patch_pods(
927 names=[module.params["name"]],
928 context_names=[module.params["context"]],
929 pod=PodPatch(requested_promotion_state="demoted"),
930 )
931 else:
932 res = array.patch_pods(
933 names=[module.params["name"]],
934 pod=PodPatch(requested_promotion_state="demoted"),
935 )
936 elif not module.params["quiesce"]:
937 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
938 res = array.patch_pods(
939 names=[module.params["name"]],
940 context_names=[module.params["context"]],
941 pod=PodPatch(requested_promotion_state="demoted"),
942 skip_quiesce=True,
943 )
944 else:
945 res = array.patch_pods(
946 names=[module.params["name"]],
947 pod=PodPatch(requested_promotion_state="demoted"),
948 skip_quiesce=True,
949 )
950 else:
951 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
952 res = array.patch_pods(
953 names=[module.params["name"]],
954 context_names=[module.params["context"]],
955 pod=PodPatch(requested_promotion_state="demoted"),
956 quiesce=True,
957 )
958 else:
959 res = array.patch_pods(
960 names=[module.params["name"]],
961 pod=PodPatch(requested_promotion_state="demoted"),
962 quiesce=True,
963 )
964 if res.status_code != 200:
965 module.fail_json(
966 msg="Failed to demote pod {0}. Error: {1}".format(
967 module.params["name"], res.errors[0].message
968 )
969 )
970 if module.params["quota"] and LooseVersion(POD_QUOTA_VERSION) <= LooseVersion(
971 api_version
972 ):
973 quota = human_to_bytes(module.params["quota"])
974 if current_config.quota_limit != quota:
975 changed = True
976 if not module.check_mode:
977 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
978 res = array.patch_pods(
979 names=[module.params["name"]],
980 context_names=[module.params["context"]],
981 pod=PodPatch(
982 quota_limit=quota,
983 ignore_usage=module.params["ignore_usage"],
984 ),
985 )
986 else:
987 res = array.patch_pods(
988 names=[module.params["name"]],
989 pod=PodPatch(
990 quota_limit=quota,
991 ignore_usage=module.params["ignore_usage"],
992 ),
993 )
994 if res.status_code != 200:
995 module.fail_json(
996 msg="Failed to update quota on pod {0}. Error: {1}".format(
997 module.params["name"], res.errors[0].message
998 )
999 )
1000 if module.params["default_protection_pg"] and LooseVersion(
1001 DEFAULT_API_VERSION
1002 ) <= LooseVersion(api_version):
1003 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
1004 safemode_pg = list(
1005 array.get_container_default_protections(
1006 names=[module.params["name"]],
1007 context_names=[module.params["context"]],
1008 ).items
1009 )[0].default_protections
1010 else:
1011 safemode_pg = list(
1012 array.get_container_default_protections(
1013 names=[module.params["name"]]
1014 ).items
1015 )[0].default_protections
1016 if safemode_pg:
1017 pgname = safemode_pg[0].name
1018 else:
1019 pgname = []
1020 if pgname != module.params["default_protection_pg"]:
1021 changed = True
1022 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
1023 res = array.get_protection_groups(
1024 names=[module.params["default_protection_pg"]],
1025 context_names=[module.params["context"]],
1026 )
1027 else:
1028 res = array.get_protection_groups(
1029 names=[module.params["default_protection_pg"]]
1030 )
1031 if res.status_code != 200:
1032 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
1033 pg_res = array.post_protection_groups(
1034 context_names=[module.params["context"]],
1035 names=[module.params["default_protection_pg"]],
1036 )
1037 else:
1038 pg_res = array.post_protection_groups(
1039 names=[module.params["default_protection_pg"]]
1040 )
1041 if pg_res.status_code != 200:
1042 module.fail_json(
1043 msg="Failed to create default protection group {0}. Error: {1}".format(
1044 module.params["name"], res.errors[0].message
1045 )
1046 )
1047 if (
1048 module.params["retention_lock"]
1049 and module.params["default_protection_pg"] != []
1050 ):
1051 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
1052 res = array.patch_protection_groups(
1053 context_names=[module.params["context"]],
1054 names=[module.params["default_protection_pg"]],
1055 protection_group=ProtectionGroup(retention_lock="ratcheted"),
1056 )
1057 else:
1058 res = array.patch_protection_groups(
1059 names=[module.params["default_protection_pg"]],
1060 protection_group=ProtectionGroup(retention_lock="ratcheted"),
1061 )
1062 if res.status_code != 200:
1063 module.fail_json(
1064 msg="Failed to set retention lock for protection group {0}. Error: {1}".format(
1065 module.params["default_protection_pg"],
1066 res.errors[0].message,
1067 )
1068 )
1069 if safemode_pg:
1070 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
1071 array.patch_container_default_protections(
1072 context_names=[module.params["context"]],
1073 names=[module.params["name"]],
1074 container_default_protection=(
1075 ContainerDefaultProtection(default_protections=[])
1076 ),
1077 )
1078 else:
1079 array.patch_container_default_protections(
1080 names=[module.params["name"]],
1081 container_default_protection=(
1082 ContainerDefaultProtection(default_protections=[])
1083 ),
1084 )
1085 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
1086 res = array.patch_container_default_protections(
1087 context_names=[module.params["context"]],
1088 names=[module.params["name"]],
1089 container_default_protection=(
1090 ContainerDefaultProtection(
1091 default_protections=[
1092 DefaultProtectionReference(
1093 name=module.params["default_protection_pg"],
1094 type="protection_group",
1095 )
1096 ]
1097 )
1098 ),
1099 )
1100 else:
1101 res = array.patch_container_default_protections(
1102 names=[module.params["name"]],
1103 container_default_protection=(
1104 ContainerDefaultProtection(
1105 default_protections=[
1106 DefaultProtectionReference(
1107 name=module.params["default_protection_pg"],
1108 type="protection_group",
1109 )
1110 ]
1111 )
1112 ),
1113 )
1114 if res.status_code != 200:
1115 module.fail_json(
1116 msg="Failed to update default protection for pod {0}. Error: {1}".format(
1117 module.params["name"], res.errors[0].message
1118 )
1119 )
1120 module.exit_json(changed=changed)
1121
1122
1123 def stretch_pod(module, array):
1124 """Stretch/unstretch Pod configuration"""
1125 api_version = array.get_rest_version()
1126 changed = False
1127 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
1128 current_config = list(
1129 array.get_pods(
1130 names=[module.params["name"]], context_names=[module.params["context"]]
1131 ).items
1132 )[0]
1133 else:
1134 current_config = list(array.get_pods(names=[module.params["name"]]).items)[0]
1135 if module.params["stretch"]:
1136 current_arrays = []
1137 for arr in current_config.arrays:
1138 current_arrays.append(arr["name"])
1139 if (
1140 module.params["stretch"] not in current_arrays
1141 and module.params["state"] == "present"
1142 ):
1143 changed = True
1144 if not module.check_mode:
1145 if LooseVersion(MEMBERS_VERSION) <= LooseVersion(api_version):
1146 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
1147 res = array.post_pods_members(
1148 pod_names=[module.params["name"]],
1149 context_names=[module.params["context"]],
1150 member_names=[module.params["stretch"]],
1151 )
1152 else:
1153 res = array.post_pods_members(
1154 pod_names=[module.params["name"]],
1155 member_names=[module.params["stretch"]],
1156 )
1157 else:
1158 res = array.post_pods_arrays(
1159 group_names=[module.params["name"]],
1160 member_names=[module.params["stretch"]],
1161 )
1162 if res.status_code != 200:
1163 module.fail_json(
1164 msg="Failed to stretch pod {0} to array {1}. Error: {2}".format(
1165 module.params["name"],
1166 module.params["stretch"],
1167 res.errors[0].message,
1168 )
1169 )
1170
1171 if (
1172 module.params["stretch"] in current_arrays
1173 and module.params["state"] == "absent"
1174 ):
1175 changed = True
1176 if not module.check_mode:
1177 if LooseVersion(MEMBERS_VERSION) <= LooseVersion(api_version):
1178 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
1179 res = array.delete_pods_members(
1180 pod_names=[module.params["name"]],
1181 context_names=[module.params["context"]],
1182 member_names=[module.params["stretch"]],
1183 )
1184 else:
1185 res = array.delete_pods_members(
1186 pod_names=[module.params["name"]],
1187 member_names=[module.params["stretch"]],
1188 )
1189 else:
1190 res = array.delete_pods_arrays(
1191 group_names=[module.params["name"]],
1192 member_names=[module.params["stretch"]],
1193 )
1194 if res.status_code != 200:
1195 module.fail_json(
1196 msg="Failed to unstretch pod {0} from array {1}. Error: {2}".format(
1197 module.params["name"],
1198 module.params["stretch"],
1199 res.errors[0].message,
1200 )
1201 )
1202
1203 module.exit_json(changed=changed)
1204
1205
1206 def delete_pod(module, array):
1207 """Delete Pod"""
1208 api_version = array.get_rest_version()
1209 changed = True
1210 if not module.check_mode:
1211 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
1212 res = array.patch_pods(
1213 names=[module.params["name"]],
1214 context_names=[module.params["context"]],
1215 pod=PodPatch(destroyed=True),
1216 destroy_contents=module.params["delete_contents"],
1217 )
1218 else:
1219 res = array.patch_pods(
1220 names=[module.params["name"]],
1221 pod=PodPatch(destroyed=True),
1222 destroy_contents=module.params["delete_contents"],
1223 )
1224 if res.status_code != 200:
1225 module.fail_json(
1226 msg="Delete pod {0} failed. Error: {1}".format(
1227 module.params["name"], res.errors[0].message
1228 )
1229 )
1230 if module.params["eradicate"]:
1231 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
1232 res = array.delete_pods(
1233 names=[module.params["name"]],
1234 context_names=[module.params["context"]],
1235 eradicate_contents=module.params["delete_contents"],
1236 )
1237 else:
1238 res = array.delete_pods(
1239 names=[module.params["name"]],
1240 eradicate_contents=module.params["delete_contents"],
1241 )
1242 if res.status_code != 200:
1243 module.fail_json(
1244 msg="Eradicate pod {0} failed. Error: {1}".format(
1245 module.params["name"], res.errors[0].message
1246 )
1247 )
1248 module.exit_json(changed=changed)
1249
1250
1251 def eradicate_pod(module, array):
1252 """Eradicate Deleted Pod"""
1253 api_version = array.get_rest_version()
1254 if module.params["eradicate"]:
1255 changed = True
1256 if not module.check_mode:
1257 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
1258 res = array.delete_pods(
1259 names=[module.params["name"]],
1260 context_names=[module.params["context"]],
1261 eradicate_contents=module.params["delete_contents"],
1262 )
1263 else:
1264 res = array.delete_pods(
1265 names=[module.params["name"]],
1266 eradicate_contents=module.params["delete_contents"],
1267 )
1268 if res.status_code != 200:
1269 module.fail_json(
1270 msg="Eradication of pod {0} failed. Error: {1}".format(
1271 module.params["name"], res.errors[0].message
1272 )
1273 )
1274 module.exit_json(changed=changed)
1275
1276
1277 def recover_pod(module, array):
1278 """Recover Deleted Pod"""
1279 api_version = array.get_rest_version()
1280 changed = True
1281 if not module.check_mode:
1282 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
1283 res = array.patch_pods(
1284 names=[module.params["name"]],
1285 pod=PodPatch(destroyed=False),
1286 context_names=[module.params["context"]],
1287 )
1288 else:
1289 res = array.patch_pods(
1290 names=[module.params["name"]], pod=PodPatch(destroyed=False)
1291 )
1292 if res.status_code != 200:
1293 module.fail_json(
1294 msg="Recovery of pod {0} failed. Error: {1}".format(
1295 module.params["name"], res.errors[0].message
1296 )
1297 )
1298 module.exit_json(changed=changed)
1299
1300
1301 def main():
1302 argument_spec = purefa_argument_spec()
1303 argument_spec.update(
1304 dict(
1305 name=dict(type="str", required=True),
1306 stretch=dict(type="str"),
1307 target=dict(type="str"),
1308 mediator=dict(type="str", default="purestorage"),
1309 failover=dict(type="list", elements="str"),
1310 promote=dict(type="bool"),
1311 undo=dict(type="bool"),
1312 quiesce=dict(type="bool"),
1313 eradicate=dict(type="bool", default=False),
1314 state=dict(type="str", default="present", choices=["absent", "present"]),
1315 quota=dict(type="str"),
1316 ignore_usage=dict(type="bool", default=False),
1317 throttle=dict(type="bool", default=False),
1318 delete_contents=dict(type="bool", default=False),
1319 context=dict(type="str", default=""),
1320 with_default_protection=dict(type="bool", default=True),
1321 default_protection_pg=dict(type="str"),
1322 retention_lock=dict(type="bool", default=True),
1323 )
1324 )
1325
1326 mutually_exclusive = [
1327 ["stretch", "failover"],
1328 ["stretch", "eradicate"],
1329 ["stretch", "mediator"],
1330 ["target", "mediator"],
1331 ["target", "stretch"],
1332 ["target", "failover"],
1333 ["target", "eradicate"],
1334 ]
1335
1336 module = AnsibleModule(
1337 argument_spec, mutually_exclusive=mutually_exclusive, supports_check_mode=True
1338 )
1339
1340 state = module.params["state"]
1341 array = get_array(module)
1342
1343 pod = get_pod(module, array)
1344 destroyed = ""
1345 if not pod:
1346 destroyed = get_destroyed_pod(module, array)
1347 if module.params["failover"] or module.params["failover"] != "auto":
1348 check_arrays(module, array)
1349
1350 if state == "present" and not pod:
1351 create_pod(module, array)
1352 elif pod and module.params["stretch"]:
1353 stretch_pod(module, array)
1354 elif state == "present" and pod and module.params["target"]:
1355 clone_pod(module, array)
1356 elif state == "present" and pod and module.params["target"]:
1357 clone_pod(module, array)
1358 elif state == "present" and pod:
1359 update_pod(module, array)
1360 elif state == "absent" and pod and not module.params["stretch"]:
1361 delete_pod(module, array)
1362 elif state == "present" and destroyed:
1363 recover_pod(module, array)
1364 elif state == "absent" and destroyed:
1365 eradicate_pod(module, array)
1366 elif state == "absent" and not pod:
1367 module.exit_json(changed=False)
1368
1369
1370 if __name__ == "__main__":
1371 main()