Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # (c) 2018, 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_volume
20 version_added: '1.0.0'
21 short_description: Manage volumes on Pure Storage FlashArrays
22 description:
23 - Create, delete or extend the capacity of a volume 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 volume.
30 - Volume could be created in a POD with this syntax POD_NAME::VOLUME_NAME.
31 - Volume could be created in a volume group with this syntax VG_NAME/VOLUME_NAME.
32 - Multi-volume support available from Purity//FA 6.0.0
33 B(***NOTE***) Manual deletion or eradication of individual volumes created
34 using multi-volume will cause idempotency to fail
35 - Multi-volume support only exists for volume creation
36 type: str
37 required: true
38 target:
39 description:
40 - The name of the target volume, if copying.
41 type: str
42 state:
43 description:
44 - Define whether the volume should exist or not.
45 default: present
46 choices: [ absent, present ]
47 type: str
48 eradicate:
49 description:
50 - Define whether to eradicate the volume on delete or leave in trash.
51 type: bool
52 default: false
53 overwrite:
54 description:
55 - Define whether to overwrite a target volume if it already exists.
56 type: bool
57 default: false
58 size:
59 description:
60 - Volume size in M, G, T or P units.
61 type: str
62 count:
63 description:
64 - Number of volumes to be created in a multiple volume creation
65 - Only supported from Purity//FA v6.0.0 and higher
66 type: int
67 start:
68 description:
69 - Number at which to start the multiple volume creation index
70 - Only supported from Purity//FA v6.0.0 and higher
71 type: int
72 default: 0
73 digits:
74 description:
75 - Number of digits to use for multiple volume count. This
76 will pad the index number with zeros where necessary
77 - Only supported from Purity//FA v6.0.0 and higher
78 - Range is between 1 and 10
79 type: int
80 default: 1
81 suffix:
82 description:
83 - Suffix string, if required, for multiple volume create
84 - Volume names will be formed as I(<name>#I<suffix>), where
85 I(#) is a placeholder for the volume index
86 See associated descriptions
87 - Only supported from Purity//FA v6.0.0 and higher
88 type: str
89 default: ""
90 bw_qos:
91 description:
92 - Bandwidth limit for volume in M or G units.
93 M will set MB/s
94 G will set GB/s
95 To clear an existing QoS setting use 0 (zero)
96 type: str
97 aliases: [ qos ]
98 iops_qos:
99 description:
100 - IOPs limit for volume - use value or K or M
101 K will mean 1000
102 M will mean 1000000
103 To clear an existing IOPs setting use 0 (zero)
104 type: str
105 move:
106 description:
107 - Move a volume in and out of a pod or vgroup
108 - Provide the name of pod or vgroup to move the volume to
109 - Pod and Vgroup names must be unique in the array
110 - To move to the local array, specify C(local)
111 - This is not idempotent - use C(ignore_errors) in the play
112 type: str
113 rename:
114 description:
115 - Value to rename the specified volume to.
116 - Rename only applies to the container the current volumes is in.
117 - There is no requirement to specify the pod or vgroup name as this is implied.
118 type: str
119 pgroup:
120 description:
121 - Name of existing, not deleted, protection group to add volume to
122 - Only application for volume(s) creation
123 - Superseded from Purity//FA 6.3.4 by I(add_to_pgs)
124 type: str
125 version_added: 1.8.0
126 priority_operator:
127 description:
128 - DMM Priority Adjustment operator
129 type: str
130 choices: [ '=', '+', '-' ]
131 version_added: '1.13.0'
132 priority_value:
133 description:
134 - DMM Priority Adjustment value
135 type: int
136 choices: [ -10, 0, 10 ]
137 version_added: '1.13.0'
138 with_default_protection:
139 description:
140 - Whether to add the default container protection groups to
141 those specified in I(add_to_pgs) as the initial protection
142 of a new volume.
143 type: bool
144 default: true
145 version_added: '1.14.0'
146 add_to_pgs:
147 description:
148 - A new volume will be added to the specified protection groups
149 on creation
150 type: list
151 elements: str
152 version_added: '1.14.0'
153 promotion_state:
154 description:
155 - Promote or demote the volume so that the volume starts or
156 stops accepting write requests.
157 type: str
158 choices: [ promoted, demoted ]
159 version_added: '1.16.0'
160 context:
161 description:
162 - Name of fleet member on which to perform the operation.
163 - This requires the array receiving the request is a member of a fleet
164 and the context name to be a member of the same fleet.
165 type: str
166 default: ""
167 version_added: '1.33.0'
168 extends_documentation_fragment:
169 - purestorage.flasharray.purestorage.fa
170 """
171
172 EXAMPLES = r"""
173 - name: Create new volume named foo with a QoS limit
174 purestorage.flasharray.purefa_volume:
175 name: foo
176 size: 1T
177 bw_qos: 58M
178 iops_qos: 23K
179 fa_url: 10.10.10.2
180 api_token: e31060a7-21fc-e277-6240-25983c6c4592
181 state: present
182
183 - name: Create new volume named foo with a DMM priority (Purity//FA 6.1.2+)
184 purestorage.flasharray.purefa_volume:
185 name: foo
186 size: 1T
187 priority_operator: +
188 priority_value: 10
189 fa_url: 10.10.10.2
190 api_token: e31060a7-21fc-e277-6240-25983c6c4592
191 state: present
192
193 - name: Create new volume named foo in protection group pg1 (this cannot be used with context)
194 purestorage.flasharray.purefa_volume:
195 name: foo
196 pgroup: pg1
197 size: 1T
198 fa_url: 10.10.10.2
199 api_token: e31060a7-21fc-e277-6240-25983c6c4592
200 state: present
201
202 - name: Create 10 volumes with index starting at 10 but padded with 3 digits
203 purestorage.flasharray.purefa_volume:
204 name: foo
205 size: 1T
206 suffix: bar
207 count: 10
208 start: 10
209 digits: 3
210 fa_url: 10.10.10.2
211 api_token: e31060a7-21fc-e277-6240-25983c6c4592
212 state: present
213
214 - name: Extend the size of an existing volume named foo
215 purestorage.flasharray.purefa_volume:
216 name: foo
217 size: 2T
218 fa_url: 10.10.10.2
219 api_token: e31060a7-21fc-e277-6240-25983c6c4592
220 state: present
221
222 - name: Delete and eradicate volume named foo
223 purestorage.flasharray.purefa_volume:
224 name: foo
225 eradicate: true
226 fa_url: 10.10.10.2
227 api_token: e31060a7-21fc-e277-6240-25983c6c4592
228 state: absent
229
230 - name: Create clone of volume bar named foo
231 purestorage.flasharray.purefa_volume:
232 name: foo
233 target: bar
234 fa_url: 10.10.10.2
235 api_token: e31060a7-21fc-e277-6240-25983c6c4592
236 state: present
237
238 - name: Overwrite volume bar with volume foo
239 purestorage.flasharray.purefa_volume:
240 name: foo
241 target: bar
242 overwrite: true
243 fa_url: 10.10.10.2
244 api_token: e31060a7-21fc-e277-6240-25983c6c4592
245 state: present
246
247 - name: Clear volume QoS from volume foo
248 purestorage.flasharray.purefa_volume:
249 name: foo
250 bw_qos: 0
251 iops_qos: 0
252 fa_url: 10.10.10.2
253 api_token: e31060a7-21fc-e277-6240-25983c6c4592
254 state: present
255
256 - name: Move local volume foo from local array to pod bar
257 purestorage.flasharray.purefa_volume:
258 name: foo
259 move: bar
260 fa_url: 10.10.10.2
261 api_token: e31060a7-21fc-e277-6240-25983c6c4592
262
263 - name: Move volume foo in pod bar to local array
264 purestorage.flasharray.purefa_volume:
265 name: bar::foo
266 move: local
267 fa_url: 10.10.10.2
268 api_token: e31060a7-21fc-e277-6240-25983c6c4592
269
270 - name: Move volume foo in pod bar to vgroup fin
271 purestorage.flasharray.purefa_volume:
272 name: bar::foo
273 move: fin
274 fa_url: 10.10.10.2
275 api_token: e31060a7-21fc-e277-6240-25983c6c4592
276 """
277
278 RETURN = r"""
279 volume:
280 description: A dictionary describing the changed volume. Only some
281 attributes below will be returned with various actions.
282 type: dict
283 returned: success
284 contains:
285 source:
286 description: Volume name of source volume used for volume copy
287 type: str
288 serial:
289 description: Volume serial number
290 type: str
291 sample: '361019ECACE43D83000120A4'
292 nvme_nguid:
293 description: Volume NVMe namespace globally unique identifier
294 type: str
295 sample: 'eui.00cd6b99ef25864724a937c5000be684'
296 page83_naa:
297 description: Volume NAA canonical name
298 type: str
299 sample: 'naa.624a9370361019ecace43db3000120a4'
300 created:
301 description: Volume creation time
302 type: str
303 sample: '2019-03-13T22:49:24Z'
304 name:
305 description: Volume name
306 type: str
307 size:
308 description: Volume size in bytes
309 type: int
310 bandwidth_limit:
311 description: Volume bandwidth limit in bytes/sec
312 type: int
313 iops_limit:
314 description: Volume IOPs limit
315 type: int
316 priority_operator:
317 description: DMM Priority Adjustment operator
318 type: str
319 priority_value:
320 description: DMM Priority Adjustment value
321 type: int
322 """
323
324 HAS_PURESTORAGE = True
325 try:
326 from pypureclient.flasharray import (
327 Qos,
328 VolumePost,
329 VolumePatch,
330 PriorityAdjustment,
331 Reference,
332 ReferenceType,
333 )
334 except ImportError:
335 HAS_PURESTORAGE = False
336
337 import re
338 import time
339 from ansible.module_utils.basic import AnsibleModule
340 from ansible_collections.purestorage.flasharray.plugins.module_utils.purefa import (
341 get_array,
342 purefa_argument_spec,
343 )
344 from ansible_collections.purestorage.flasharray.plugins.module_utils.common import (
345 human_to_bytes,
346 human_to_real,
347 )
348 from ansible_collections.purestorage.flasharray.plugins.module_utils.version import (
349 LooseVersion,
350 )
351
352 PURE_OUI = "naa.624a9370"
353 PRIORITY_API_VERSION = "2.11"
354 DEFAULT_API_VERSION = "2.16"
355 CONTEXT_API_VERSION = "2.38"
356
357
358 def _volfact(module, array, volume_name):
359 api_version = array.get_rest_version()
360 if not module.check_mode:
361 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
362 volume_data = list(
363 array.get_volumes(
364 names=[volume_name], context_names=[module.params["context"]]
365 ).items
366 )[0]
367 else:
368 volume_data = list(array.get_volumes(names=[volume_name]).items)[0]
369 volfact = {
370 volume_name: {
371 "size": volume_data.provisioned,
372 "serial": volume_data.serial,
373 "created": time.strftime(
374 "%Y-%m-%d %H:%M:%S", time.localtime(volume_data.created / 1000)
375 ),
376 "page83_naa": PURE_OUI + volume_data.serial.lower(),
377 "nvme_nguid": _create_nguid(volume_data.serial.lower()),
378 "iops_limit": getattr(volume_data.qos, "iops_limit", 0),
379 "bandwidth_limit": getattr(volume_data.qos, "bandwidth_limit", 0),
380 "requested_promotion_state": volume_data.requested_promotion_state,
381 "promotion_status": volume_data.promotion_status,
382 "priority": getattr(volume_data, "priority", 0),
383 "priority_operator": "",
384 "priority_value": "",
385 "destroyed": volume_data.destroyed,
386 }
387 }
388 if LooseVersion(PRIORITY_API_VERSION) <= LooseVersion(api_version):
389 volfact[volume_name][
390 "priority_operator"
391 ] = volume_data.priority_adjustment.priority_adjustment_operator
392 volfact[volume_name][
393 "priority_value"
394 ] = volume_data.priority_adjustment.priority_adjustment_value
395 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
396 volfact[volume_name]["context"] = volume_data.context.name
397 else:
398 volfact = {}
399 return volfact
400
401
402 def _create_nguid(serial):
403 nguid = "eui.00" + serial[0:14] + "24a937" + serial[-10:]
404 return nguid
405
406
407 def get_pod(module, array):
408 """Get ActiveCluster Pod"""
409 api_version = array.get_rest_version()
410 pod_name = "::".join(module.params["pgroup"].split("::")[:-1])
411 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
412 res = array.get_pods(names=[pod_name], context_names=[module.params["context"]])
413 else:
414 res = array.get_pods(names=[pod_name])
415 if res.status_code == 200:
416 return list(res.items)[0]
417 return None
418
419
420 def get_pending_pgroup(module, array):
421 """Get Protection Group"""
422 api_version = array.get_rest_version()
423 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
424 res = array.get_protection_groups(
425 names=[module.params["pgroup"]], context_names=[module.params["context"]]
426 )
427 else:
428 res = array.get_protection_groups(names=[module.params["pgroup"]])
429 if res.status_code == 200:
430 pgroup = list(res.items)[0]
431 if pgroup.destroyed:
432 return list(res.items)[0]
433 return None
434
435
436 def get_pgroup(module, array):
437 """Get Protection Group"""
438 api_version = array.get_rest_version()
439 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
440 res = array.get_protection_groups(
441 names=[module.params["pgroup"]], context_names=[module.params["context"]]
442 )
443 else:
444 res = array.get_protection_groups(names=[module.params["pgroup"]])
445 if res.status_code == 200:
446 return list(res.items)[0]
447 return None
448
449
450 def pg_exists(module, pgs, array):
451 """Get Protection Group"""
452 api_version = array.get_rest_version()
453 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
454 res = array.get_protection_groups(
455 names=[pgs], context_names=[module.params["context"]]
456 )
457 else:
458 res = array.get_protection_groups(names=[pgs])
459 return bool(res.status_code == 200)
460
461
462 def get_multi_volumes(module, array):
463 """Return True is all volumes exist or None"""
464 names = []
465 api_version = array.get_rest_version()
466 for vol_num in range(
467 module.params["start"], module.params["count"] + module.params["start"]
468 ):
469 names.append(
470 module.params["name"]
471 + str(vol_num).zfill(module.params["digits"])
472 + module.params["suffix"]
473 )
474 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
475 res = array.get_volumes(
476 names=names, destroyed=False, context_names=[module.params["context"]]
477 )
478 else:
479 res = array.get_volumes(names=names, destroyed=False)
480 if res.status_code == 200:
481 return list(res.items)[0]
482 return None
483
484
485 def get_volume(module, array):
486 """Return Volume or None"""
487 api_version = array.get_rest_version()
488 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
489 res = array.get_volumes(
490 names=[module.params["name"]],
491 destroyed=False,
492 context_names=[module.params["context"]],
493 )
494 else:
495 res = array.get_volumes(names=[module.params["name"]], destroyed=False)
496 if res.status_code == 200:
497 return list(res.items)[0]
498 return None
499
500
501 def get_endpoint(module, name, array):
502 """Return Endpoint or None"""
503 api_version = array.get_rest_version()
504 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
505 res = array.get_volumes(names=[name], context_names=[module.params["context"]])
506 else:
507 res = array.get_volumes(names=[name])
508 if res.status_code == 200:
509 volume = list(res.items)[0]
510 if volume.subtype == "protocol_endpoint":
511 return volume
512 return None
513
514
515 def get_destroyed_volume(module, array):
516 """Return Destroyed Volume or None"""
517 api_version = array.get_rest_version()
518 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
519 res = array.get_volumes(
520 names=[module.params["name"]],
521 destroyed=True,
522 context_names=[module.params["context"]],
523 )
524 else:
525 res = array.get_volumes(names=[module.params["name"]], destroyed=True)
526 if res.status_code == 200:
527 return list(res.items)[0]
528 return None
529
530
531 def get_target(module, array):
532 """Return Volume or None"""
533 api_version = array.get_rest_version()
534 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
535 res = array.get_volumes(
536 names=[module.params["target"]], context_names=[module.params["context"]]
537 )
538 else:
539 res = array.get_volumes(names=[module.params["target"]])
540 if res.status_code == 200:
541 return list(res.items)[0]
542 return None
543
544
545 def check_vgroup(module, array):
546 """Check is the requested VG to create volume in exists"""
547 api_version = array.get_rest_version()
548 vg_name = module.params["name"].split("/")[0]
549 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
550 res = array.get_volume_groups(
551 names=[vg_name], context_names=[module.params["context"]]
552 )
553 else:
554 res = array.get_volume_groups(names=[vg_name])
555 return bool(res.status_code == 200)
556
557
558 def check_pod(module, array):
559 """Check is the requested pod to create volume in exists"""
560 api_version = array.get_rest_version()
561 pod_name = "::".join(module.params["name"].split("::")[:-1])
562 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
563 res = array.get_pods(names=[pod_name], context_names=[module.params["context"]])
564 else:
565 res = array.get_pods(names=[pod_name])
566 return bool(res.status_code == 200)
567
568
569 def create_volume(module, array):
570 """Create Volume"""
571 changed = False
572 api_version = array.get_rest_version()
573 if module.params["add_to_pgs"]:
574 module.fail_json(msg="For Purity//FA 6.3.4 or lower, use pgroup parameter")
575 if "/" in module.params["name"] and not check_vgroup(module, array):
576 module.fail_json(
577 msg="Failed to create volume {0}. Volume Group does not exist.".format(
578 module.params["name"]
579 )
580 )
581 if "::" in module.params["name"]:
582 if not check_pod(module, array):
583 module.fail_json(
584 msg="Failed to create volume {0}. Pod does not exist".format(
585 module.params["name"]
586 )
587 )
588 pod_name = "::".join(module.params["name"].split("::")[:-1])
589 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
590 res = list(
591 array.get_pods(
592 names=[pod_name], context_names=[module.params["context"]]
593 ).items
594 )[0]
595 else:
596 res = list(array.get_pods(names=[pod_name]).items)[0]
597 if res.promotion_status == "demoted":
598 module.fail_json(msg="Volume cannot be created in a demoted pod")
599 if not module.params["size"]:
600 module.fail_json(msg="Size for a new volume must be specified")
601 if module.params["bw_qos"] or module.params["iops_qos"]:
602 if module.params["bw_qos"] and not module.params["iops_qos"]:
603 changed = True
604 if not module.check_mode:
605 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
606 res = array.post_volumes(
607 names=[module.params["name"]],
608 volume=VolumePost(
609 provisioned=int(human_to_bytes(module.params["size"])),
610 qos=Qos(
611 bandwidth_limit=int(
612 human_to_bytes(module.params["bw_qos"])
613 )
614 ),
615 ),
616 context_names=[module.params["context"]],
617 )
618 else:
619 res = array.post_volumes(
620 names=[module.params["name"]],
621 volume=VolumePost(
622 provisioned=int(human_to_bytes(module.params["size"])),
623 qos=Qos(
624 bandwidth_limit=int(
625 human_to_bytes(module.params["bw_qos"])
626 )
627 ),
628 ),
629 )
630 if res.status_code != 200:
631 module.fail_json(
632 msg="Volume {0} creation failed. Error: {1}".format(
633 module.params["name"],
634 res.errors[0].message,
635 )
636 )
637 elif module.params["iops_qos"] and not module.params["bw_qos"]:
638 changed = True
639 if not module.check_mode:
640 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
641 res = array.post_volumes(
642 names=[module.params["name"]],
643 volume=VolumePost(
644 provisioned=int(human_to_bytes(module.params["size"])),
645 qos=Qos(iops_limit=int(module.params["iops_qos"])),
646 ),
647 context_names=[module.params["context"]],
648 )
649 else:
650 res = array.post_volumes(
651 names=[module.params["name"]],
652 volume=VolumePost(
653 provisioned=int(human_to_bytes(module.params["size"])),
654 qos=Qos(iops_limit=int(module.params["iops_qos"])),
655 ),
656 )
657 if res.status_code != 200:
658 module.fail_json(
659 msg="Volume {0} creation failed. Error: {1}".format(
660 module.params["name"],
661 res.errors[0].message,
662 )
663 )
664 else:
665 changed = True
666 if not module.check_mode:
667 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
668 res = array.post_volumes(
669 names=[module.params["name"]],
670 volume=VolumePost(
671 provisioned=int(human_to_bytes(module.params["size"])),
672 qos=Qos(
673 iops_limit=int(
674 human_to_real(module.params["iops_qos"])
675 ),
676 bandwidth_limit=int(
677 human_to_bytes(module.params["bw_qos"])
678 ),
679 ),
680 ),
681 context_names=[module.params["context"]],
682 )
683 else:
684 res = array.post_volumes(
685 names=[module.params["name"]],
686 volume=VolumePost(
687 provisioned=int(human_to_bytes(module.params["size"])),
688 qos=Qos(
689 iops_limit=int(
690 human_to_real(module.params["iops_qos"])
691 ),
692 bandwidth_limit=int(
693 human_to_bytes(module.params["bw_qos"])
694 ),
695 ),
696 ),
697 )
698 if res.status_code != 200:
699 module.fail_json(
700 msg="Volume {0} creation failed. Error: {1}".format(
701 module.params["name"],
702 res.errors[0].message,
703 )
704 )
705 else:
706 changed = True
707 if not module.check_mode:
708 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
709 res = array.post_volumes(
710 names=[module.params["name"]],
711 volume=VolumePost(
712 provisioned=int(human_to_bytes(module.params["size"]))
713 ),
714 context_names=[module.params["context"]],
715 )
716 else:
717 res = array.post_volumes(
718 names=[module.params["name"]],
719 volume=VolumePost(
720 provisioned=int(human_to_bytes(module.params["size"]))
721 ),
722 )
723 if res.status_code != 200:
724 module.fail_json(
725 msg="Volume {0} creation failed. Error: {1}".format(
726 module.params["name"],
727 res.errors[0].message,
728 )
729 )
730 if module.params["promotion_state"]:
731 volume = VolumePatch(requested_promotion_state=module.params["promotion_state"])
732 changed = True
733 if not module.check_mode:
734 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
735 res = array.patch_volumes(
736 names=[module.params["name"]],
737 volume=volume,
738 context_names=[module.params["context"]],
739 )
740 else:
741 res = array.patch_volumes(names=[module.params["name"]], volume=volume)
742 if res.status_code != 200:
743 message = res.errors[0].message
744 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
745 array.patch_volumes(
746 names=[module.params["name"]],
747 volume=VolumePatch(destroyed=True),
748 context_names=[module.params["context"]],
749 )
750 array.delete_volumes(
751 names=[module.params["name"]],
752 context_names=[module.params["context"]],
753 )
754 else:
755 array.patch_volumes(
756 names=[module.params["name"]],
757 volume=VolumePatch(destroyed=True),
758 )
759 array.delete_volumes(names=[module.params["name"]])
760 module.fail_json(
761 msg="Failed to set Promotion State for volume {0}. Error: {1}".format(
762 module.params["name"],
763 message,
764 )
765 )
766 if module.params["priority_operator"]:
767 volume = VolumePatch(
768 priority_adjustment=PriorityAdjustment(
769 priority_adjustment_operator=module.params["priority_operator"],
770 priority_adjustment_value=module.params["priority_value"],
771 )
772 )
773 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
774 res = array.patch_volumes(
775 names=[module.params["name"]],
776 volume=volume,
777 context_names=[module.params["context"]],
778 )
779 else:
780 res = array.patch_volumes(names=[module.params["name"]], volume=volume)
781 if res.status_code != 200:
782 message = res.errors[0].message
783 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
784 array.patch_volumes(
785 names=[module.params["name"]],
786 volume=VolumePatch(destroyed=True),
787 context_names=[module.params["context"]],
788 )
789 array.delete_volumes(
790 names=[module.params["name"]],
791 context_names=[module.params["context"]],
792 )
793 else:
794 array.patch_volumes(
795 names=[module.params["name"]],
796 volume=VolumePatch(destroyed=True),
797 )
798 array.delete_volumes(names=[module.params["name"]])
799 module.fail_json(
800 msg="Failed to set DMM Priority Adjustment on volume {0}. Error: {1}".format(
801 module.params["name"], message
802 )
803 )
804 if module.params["pgroup"]:
805 changed = True
806 if not module.check_mode:
807 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
808 res = array.patch_volumes(
809 names=[module.params["name"]],
810 add_to_protection_groups=ReferenceType(
811 name=module.params["pgroup"]
812 ),
813 context_names=[module.params["context"]],
814 )
815 else:
816 res = array.patch_volumes(
817 names=[module.params["name"]],
818 add_to_protection_groups=ReferenceType(
819 name=module.params["pgroup"]
820 ),
821 )
822 if res.status_code != 200:
823 module.warn_json(
824 "Failed to add {0} to protection group {1}. Error: {2}".format(
825 module.params["name"],
826 module.params["pgroup"],
827 res.errors[0].message,
828 )
829 )
830
831 module.exit_json(
832 changed=changed, volume=_volfact(module, array, module.params["name"])
833 )
834
835
836 def create_multi_volume(module, array, single=False):
837 """Create Volume"""
838 volfact = {}
839 vols = VolumePost()
840 changed = True
841 api_version = array.get_rest_version()
842 if module.params["pgroup"]:
843 module.fail_json(msg="For Purity//FA 6.3.4 or higher, use add_to_pgs parameter")
844 names = []
845 if "/" in module.params["name"] and not check_vgroup(module, array):
846 module.fail_json(
847 msg="Multi-volume create failed. Volume Group {0} does not exist.".format(
848 module.params["name"].split("/")[0]
849 )
850 )
851 if "::" in module.params["name"]:
852 if not check_pod(module, array):
853 module.fail_json(
854 msg="Multi-volume create failed. Pod {0} does not exist".format(
855 "::".join(module.params["name"].split("::")[:-1])
856 )
857 )
858 pod_name = "::".join(module.params["name"].split("::")[:-1])
859 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
860 if (
861 list(
862 array.get_pods(
863 names=[pod_name], context_names=[module.params["context"]]
864 ).items
865 )[0].promotion_status
866 == "demoted"
867 ):
868 module.fail_json(msg="Volume cannot be created in a demoted pod")
869 else:
870 if (
871 list(array.get_pods(names=[pod_name]).items)[0].promotion_status
872 == "demoted"
873 ):
874 module.fail_json(msg="Volume cannot be created in a demoted pod")
875 if not single:
876 for vol_num in range(
877 module.params["start"], module.params["count"] + module.params["start"]
878 ):
879 names.append(
880 module.params["name"]
881 + str(vol_num).zfill(module.params["digits"])
882 + module.params["suffix"]
883 )
884 else:
885 names.append(module.params["name"])
886 if module.params["bw_qos"] and module.params["iops_qos"]:
887 vols = VolumePost(
888 provisioned=int(human_to_bytes(module.params["size"])),
889 qos=Qos(
890 bandwidth_limit=int(human_to_bytes(module.params["bw_qos"])),
891 iops_limit=int(human_to_real(module.params["iops_qos"])),
892 ),
893 subtype="regular",
894 )
895 elif not module.params["bw_qos"] and not module.params["iops_qos"]:
896 vols = VolumePost(
897 provisioned=int(human_to_bytes(module.params["size"])), subtype="regular"
898 )
899 elif not module.params["bw_qos"] and module.params["iops_qos"]:
900 vols = VolumePost(
901 provisioned=int(human_to_bytes(module.params["size"])),
902 qos=Qos(iops_limit=int(human_to_real(module.params["iops_qos"]))),
903 subtype="regular",
904 )
905 elif module.params["bw_qos"] and not module.params["iops_qos"]:
906 vols = VolumePost(
907 provisioned=int(human_to_bytes(module.params["size"])),
908 qos=Qos(bandwidth_limit=int(human_to_bytes(module.params["bw_qos"]))),
909 subtype="regular",
910 )
911 if not module.check_mode:
912 if module.params["add_to_pgs"]:
913 add_to_pgs = []
914 for add_pg in module.params["add_to_pgs"]:
915 add_to_pgs.append(ReferenceType(name=add_pg))
916 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(
917 api_version
918 ) and module.params["context"] not in [
919 "",
920 list(array.get_arrays().items)[0].name,
921 ]:
922 module.fail_json(
923 msg="Cannot specify a remote fleet member and a protection group"
924 )
925 else:
926 if "::" in module.params["name"]:
927 pod_name = "::".join(module.params["name"].split("::")[:-1])
928 for pgs in module.params["add_to_pgs"]:
929 if "::" not in module.params["add_to_pgs"][pgs]:
930 module.fail_json(msg="Specified PG is not a pod PG")
931 elif pg_exists(module, pgs, array) and pod_name != "::".join(
932 pgs.split("::")[:-1]
933 ):
934 module.fail_json(
935 msg="Protection Group {0} is not associated with pod {1}".format(
936 pgs,
937 pod_name,
938 )
939 )
940 elif not pg_exists(module, pgs, array):
941 module.fail_json(
942 msg="Protection Group {0} does not exist".format(pgs)
943 )
944 res = array.post_volumes(
945 names=names,
946 volume=vols,
947 with_default_protection=module.params[
948 "with_default_protection"
949 ],
950 )
951 else:
952 res = array.post_volumes(
953 names=names,
954 volume=vols,
955 with_default_protection=module.params[
956 "with_default_protection"
957 ],
958 add_to_protection_groups=add_to_pgs,
959 )
960 else:
961 # Initialize res
962 res = {}
963 if (
964 LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version)
965 and module.params["context"]
966 not in ["", list(array.get_arrays().items)[0].name]
967 and module.params["with_default_protection"]
968 ):
969 module.fail_json(
970 msg="Cannot specify a remote fleet member and default protection group"
971 )
972 else:
973 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
974 res = array.post_volumes(
975 names=names,
976 volume=vols,
977 context_names=[module.params["context"]],
978 with_default_protection=module.params[
979 "with_default_protection"
980 ],
981 )
982 else:
983 res = array.post_volumes(
984 names=names,
985 volume=vols,
986 with_default_protection=module.params[
987 "with_default_protection"
988 ],
989 )
990 if res.status_code != 200:
991 module.fail_json(
992 msg="Multi-Volume {0}#{1} creation failed. Error: {2}".format(
993 module.params["name"],
994 module.params["suffix"],
995 res.errors[0].message,
996 )
997 )
998 if module.params["promotion_state"]:
999 volume = VolumePatch(
1000 requested_promotion_state=module.params["promotion_state"]
1001 )
1002 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
1003 prom_res = array.patch_volumes(
1004 names=names,
1005 volume=volume,
1006 context_names=[module.params["context"]],
1007 )
1008 else:
1009 prom_res = array.patch_volumes(names=names, volume=volume)
1010 if prom_res.status_code != 200:
1011 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
1012 array.patch_volumes(
1013 names=names,
1014 context_names=[module.params["context"]],
1015 volume=VolumePatch(destroyed=True),
1016 )
1017 array.delete_volumes(names=names)
1018 else:
1019 array.patch_volumes(
1020 names=names,
1021 volume=VolumePatch(destroyed=True),
1022 )
1023 array.delete_volumes(names=names)
1024 module.warn(
1025 "Failed to set promotion status on volumes. Error: {0}".format(
1026 prom_res.errors[0].message
1027 )
1028 )
1029 if module.params["priority_operator"]:
1030 volume = VolumePatch(
1031 priority_adjustment=PriorityAdjustment(
1032 priority_adjustment_operator=module.params["priority_operator"],
1033 priority_adjustment_value=module.params["priority_value"],
1034 )
1035 )
1036 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
1037 prio_res = array.patch_volumes(
1038 names=names,
1039 volume=volume,
1040 context_names=[module.params["context"]],
1041 )
1042 else:
1043 prio_res = array.patch_volumes(names=names, volume=volume)
1044 if prio_res.status_code != 200:
1045 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
1046 array.patch_volumes(
1047 names=names,
1048 context_names=[module.params["context"]],
1049 volume=VolumePatch(destroyed=True),
1050 )
1051 array.delete_volumes(names=names)
1052 else:
1053 array.patch_volumes(
1054 names=names,
1055 volume=VolumePatch(destroyed=True),
1056 )
1057 array.delete_volumes(names=names)
1058 module.fail_json(
1059 msg="Failed to set DMM Priority Adjustment on volumes. Error: {0}".format(
1060 prio_res.errors[0].message
1061 )
1062 )
1063 prio_temp = list(prio_res.items)
1064 temp = list(res.items)
1065 for count, item in enumerate(temp):
1066 vol_name = item.name
1067
1068 # Base volume info
1069 volfact[vol_name] = {
1070 "size": item.provisioned,
1071 "serial": item.serial,
1072 "created": time.strftime(
1073 "%Y-%m-%d %H:%M:%S", time.localtime(item.created / 1000)
1074 ),
1075 "page83_naa": PURE_OUI + item.serial.lower(),
1076 "nvme_nguid": _create_nguid(item.serial.lower()),
1077 }
1078
1079 # Optional fields
1080 if module.params["bw_qos"]:
1081 volfact[vol_name]["bandwidth_limit"] = item.qos.bandwidth_limit
1082 if module.params["iops_qos"]:
1083 volfact[vol_name]["iops_limit"] = item.qos.iops_limit
1084 if module.params["promotion_state"]:
1085 volfact[vol_name]["promotion_status"] = prio_temp[
1086 count
1087 ].promotion_status
1088 if module.params["priority_operator"]:
1089 adj = prio_temp[count].priority_adjustment
1090 volfact[vol_name][
1091 "priority_operator"
1092 ] = adj.priority_adjustment_operator
1093 volfact[vol_name]["priority_value"] = adj.priority_adjustment_value
1094
1095 module.exit_json(changed=changed, volume=volfact)
1096
1097
1098 def copy_from_volume(module, array):
1099 """Create Volume Clone"""
1100 changed = False
1101 tgt = get_target(module, array)
1102 api_version = array.get_rest_version()
1103 if tgt is None:
1104 changed = True
1105 if not module.check_mode:
1106 if LooseVersion(DEFAULT_API_VERSION) <= LooseVersion(api_version):
1107 if module.params["add_to_pgs"]:
1108 add_to_pgs = []
1109 for add_pg in module.params["add_to_pgs"]:
1110 add_to_pgs.append(ReferenceType(name=add_pg))
1111 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
1112 res = array.post_volumes(
1113 with_default_protection=module.params[
1114 "with_default_protection"
1115 ],
1116 add_to_protection_groups=add_to_pgs,
1117 context_names=[module.params["context"]],
1118 names=[module.params["target"]],
1119 volume=VolumePost(
1120 source=Reference(name=module.params["name"])
1121 ),
1122 )
1123 else:
1124 res = array.post_volumes(
1125 with_default_protection=module.params[
1126 "with_default_protection"
1127 ],
1128 add_to_protection_groups=add_to_pgs,
1129 names=[module.params["target"]],
1130 volume=VolumePost(
1131 source=Reference(name=module.params["name"])
1132 ),
1133 )
1134 else:
1135 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
1136 res = array.post_volumes(
1137 with_default_protection=module.params[
1138 "with_default_protection"
1139 ],
1140 names=[module.params["target"]],
1141 context_names=[module.params["context"]],
1142 volume=VolumePost(
1143 source=Reference(name=module.params["name"])
1144 ),
1145 )
1146 else:
1147 res = array.post_volumes(
1148 with_default_protection=module.params[
1149 "with_default_protection"
1150 ],
1151 names=[module.params["target"]],
1152 volume=VolumePost(
1153 source=Reference(name=module.params["name"])
1154 ),
1155 )
1156
1157 if res.status_code != 200:
1158 module.fail_json(
1159 msg="Failed to copy volume {0} to {1}. Error: {2}".format(
1160 module.params["name"],
1161 module.params["target"],
1162 res.errors[0].message,
1163 )
1164 )
1165 else:
1166 res = array.post_volumes(
1167 names=[module.params["target"]],
1168 volume=VolumePost(source=Reference(name=module.params["name"])),
1169 )
1170 if res.status_code != 200:
1171 module.fail_json(
1172 msg="Copy volume {0} to volume {1} failed. Error: {2}".format(
1173 module.params["name"],
1174 module.params["target"],
1175 res.errors[0].message,
1176 )
1177 )
1178 elif tgt is not None and module.params["overwrite"]:
1179 changed = True
1180 if not module.check_mode:
1181 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
1182 res = array.post_volumes(
1183 names=[module.params["target"]],
1184 volume=VolumePost(source=Reference(name=module.params["name"])),
1185 context_names=[module.params["context"]],
1186 overwrite=module.params["overwrite"],
1187 )
1188 else:
1189 res = array.post_volumes(
1190 names=[module.params["target"]],
1191 volume=VolumePost(source=Reference(name=module.params["name"])),
1192 overwrite=module.params["overwrite"],
1193 )
1194 if res.status_code != 200:
1195 module.fail_json(
1196 msg="Copy volume {0} to volume {1} failed. Error: {2}".format(
1197 module.params["name"],
1198 module.params["target"],
1199 res.errors[0].message,
1200 )
1201 )
1202 module.exit_json(
1203 changed=changed, volume=_volfact(module, array, module.params["target"])
1204 )
1205
1206
1207 def update_volume(module, array):
1208 """Update Volume size and/or QoS"""
1209 changed = False
1210 api_version = array.get_rest_version()
1211 if (
1212 LooseVersion(api_version) >= LooseVersion(DEFAULT_API_VERSION)
1213 and module.params["pgroup"]
1214 ):
1215 module.fail_json(msg="For Purity//FA 6.3.4 or higher, use add_to_pgs parameter")
1216 elif (
1217 LooseVersion(api_version) <= LooseVersion(DEFAULT_API_VERSION)
1218 and module.params["add_to_pgs"]
1219 ):
1220 module.fail_json(msg="For Purity//FA 6.3.4 or lower, use pgroup parameter")
1221 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
1222 vol = list(
1223 array.get_volumes(
1224 names=[module.params["name"]], context_names=[module.params["context"]]
1225 ).items
1226 )[0]
1227 else:
1228 vol = list(array.get_volumes(names=[module.params["name"]]).items)[0]
1229 vol_qos = vol.qos
1230 if not hasattr(vol_qos, "bandwidth_limit"):
1231 vol.qos.bandwidth_limit = 549755813888
1232 if not hasattr(vol_qos, "iops_limit"):
1233 vol.qos.iops_limit = 100000000
1234 if module.params["size"]:
1235 if human_to_bytes(module.params["size"]) != vol.provisioned:
1236 if human_to_bytes(module.params["size"]) > vol.provisioned:
1237 changed = True
1238 if not module.check_mode:
1239 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
1240 res = array.patch_volumes(
1241 names=[module.params["name"]],
1242 context_names=[module.params["context"]],
1243 volume=VolumePatch(
1244 provisioned=int(human_to_bytes(module.params["size"]))
1245 ),
1246 )
1247 else:
1248 res = array.patch_volumes(
1249 names=[module.params["name"]],
1250 volume=VolumePatch(
1251 provisioned=int(human_to_bytes(module.params["size"]))
1252 ),
1253 )
1254 if res.status_code != 200:
1255 module.fail_json(
1256 msg="Volume {0} resize failed. Error: {1}".format(
1257 module.params["name"],
1258 res.errors[0].message,
1259 )
1260 )
1261 if module.params["bw_qos"] and int(human_to_bytes(module.params["bw_qos"])) != int(
1262 vol_qos.bandwidth_limit
1263 ):
1264 if module.params["bw_qos"] == "0":
1265 changed = True
1266 if not module.check_mode:
1267 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
1268 res = array.patch_volumes(
1269 context_names=[module.params["context"]],
1270 names=[module.params["name"]],
1271 volume=VolumePatch(qos=Qos(bandwidth_limit=549755813888)),
1272 )
1273 else:
1274 res = array.patch_volumes(
1275 names=[module.params["name"]],
1276 volume=VolumePatch(qos=Qos(bandwidth_limit=549755813888)),
1277 )
1278 if res.status_code != 200:
1279 module.fail_json(
1280 msg="Volume {0} Bandwidth QoS removal failed. Error: {1}".format(
1281 module.params["name"],
1282 res.errors[0].message,
1283 )
1284 )
1285 else:
1286 changed = True
1287 if not module.check_mode:
1288 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
1289 res = array.patch_volumes(
1290 names=[module.params["name"]],
1291 context_names=[module.params["context"]],
1292 volume=VolumePatch(
1293 qos=Qos(
1294 bandwidth_limit=int(
1295 human_to_bytes(module.params["bw_qos"])
1296 )
1297 )
1298 ),
1299 )
1300 else:
1301 res = array.patch_volumes(
1302 names=[module.params["name"]],
1303 volume=VolumePatch(
1304 qos=Qos(
1305 bandwidth_limit=int(
1306 human_to_bytes(module.params["bw_qos"])
1307 )
1308 )
1309 ),
1310 )
1311 if res.status_code != 200:
1312 module.fail_json(
1313 msg="Volume {0} Bandwidth QoS change failed. Error: {1}".format(
1314 module.params["name"],
1315 res.errors[0].message,
1316 )
1317 )
1318 if module.params["iops_qos"] and int(
1319 human_to_real(module.params["iops_qos"])
1320 ) != int(vol_qos["iops_limit"]):
1321 if module.params["iops_qos"] == "0":
1322 changed = True
1323 if not module.check_mode:
1324 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
1325 res = array.patch_volumes(
1326 context_names=[module.params["context"]],
1327 names=[module.params["name"]],
1328 volume=VolumePatch(qos=Qos(iops_limit=100000000)),
1329 )
1330 else:
1331 res = array.patch_volumes(
1332 names=[module.params["name"]],
1333 volume=VolumePatch(qos=Qos(iops_limit=100000000)),
1334 )
1335 if res.status_code != 200:
1336 module.fail_json(
1337 msg="Volume {0} IOPs QoS removal failed. Error: {1}".format(
1338 module.params["name"], res.errors[0].message
1339 )
1340 )
1341 else:
1342 changed = True
1343 if not module.check_mode:
1344 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
1345 res = array.patch_volumes(
1346 context_names=[module.params["context"]],
1347 names=[module.params["name"]],
1348 volume=VolumePatch(
1349 qos=Qos(
1350 iops_limit=int(human_to_real(module.params["iops_qos"]))
1351 )
1352 ),
1353 )
1354 else:
1355 res = array.patch_volumes(
1356 names=[module.params["name"]],
1357 volume=VolumePatch(
1358 qos=Qos(
1359 iops_limit=int(human_to_real(module.params["iops_qos"]))
1360 )
1361 ),
1362 )
1363 if res.status_code != 200:
1364 module.fail_json(
1365 msg="Volume {0} IOPs QoS change failed. Error: {1}".format(
1366 module.params["name"], res.errors[0].message
1367 )
1368 )
1369 if module.params["promotion_state"]:
1370 if module.params["promotion_state"] != vol.promotion_status:
1371 volume_patch = VolumePatch(
1372 requested_promotion_state=module.params["promotion_state"]
1373 )
1374 changed = True
1375 if not module.check_mode:
1376 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
1377 res = array.patch_volumes(
1378 context_names=[module.params["context"]],
1379 names=[module.params["name"]],
1380 volume=volume_patch,
1381 )
1382 else:
1383 res = array.patch_volumes(
1384 names=[module.params["name"]], volume=volume_patch
1385 )
1386 if res.status_code != 200:
1387 module.fail_json(
1388 msg="Failed to change promotion status for volume {0}. Error: {1}".format(
1389 module.params["name"],
1390 res.errors[0].message,
1391 )
1392 )
1393 if module.params["priority_operator"]:
1394 change_prio = False
1395 if (
1396 module.params["priority_operator"]
1397 != vol.priority_adjustment.priority_adjustment_operator
1398 ):
1399 change_prio = True
1400 newop = module.params["priority_operator"]
1401 else:
1402 newop = vol.priority_adjustment.priority_adjustment_operator
1403 if (
1404 module.params["priority_value"]
1405 and module.params["priority_value"]
1406 != vol.priority_adjustment.priority_adjustment_value
1407 ):
1408 change_prio = True
1409 newval = module.params["priority_value"]
1410 elif (
1411 not module.params["priority_value"]
1412 and vol.priority_adjustment.priority_adjustment_value != 0
1413 ):
1414 change_prio = True
1415 newval = 0
1416 else:
1417 newval = vol.priority_adjustment.priority_adjustment_value
1418 volumepatch = VolumePatch(
1419 priority_adjustment=PriorityAdjustment(
1420 priority_adjustment_operator=newop,
1421 priority_adjustment_value=newval,
1422 )
1423 )
1424 if change_prio:
1425 changed = True
1426 if not module.check_mode:
1427 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
1428 prio_res = array.patch_volumes(
1429 context_names=[module.params["context"]],
1430 names=[module.params["name"]],
1431 volume=volumepatch,
1432 )
1433 else:
1434 prio_res = array.patch_volumes(
1435 names=[module.params["name"]], volume=volumepatch
1436 )
1437 if prio_res.status_code != 200:
1438 module.fail_json(
1439 msg="Failed to change DMM Priority Adjustment for {0}. Error: {1}".format(
1440 module.params["name"], prio_res.errors[0].message
1441 )
1442 )
1443 if module.params["add_to_pgs"]:
1444 pgs_now = []
1445 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
1446 current_pgs = list(
1447 array.get_protection_groups_volumes(
1448 context_names=[module.params["context"]],
1449 member_names=[module.params["name"]],
1450 ).items
1451 )
1452 else:
1453 current_pgs = list(
1454 array.get_protection_groups_volumes(
1455 member_names=[module.params["name"]]
1456 ).items
1457 )
1458 for current_pg in current_pgs:
1459 pgs_now.append(current_pg.group.name)
1460 new_pgs = list(filter(lambda x: x not in pgs_now, module.params["add_to_pgs"]))
1461 if new_pgs:
1462 changed = True
1463 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
1464 res = array.post_volumes_protection_groups(
1465 member_names=[module.params["name"]],
1466 group_names=new_pgs,
1467 context_names=[module.params["context"]],
1468 )
1469 else:
1470 res = array.post_volumes_protection_groups(
1471 member_names=[module.params["name"]],
1472 group_names=new_pgs,
1473 )
1474 if res.status_code != 200:
1475 module.fail_json(
1476 msg="Failed to add volume {0} to new PGs {1}: Error: {2}".format(
1477 module.params["name"],
1478 new_pgs,
1479 res.errors[0].message,
1480 )
1481 )
1482 module.exit_json(
1483 changed=changed, volume=_volfact(module, array, module.params["name"])
1484 )
1485
1486
1487 def rename_volume(module, array):
1488 """Rename volume within a container, ie pod, vgroup or local array"""
1489 changed = False
1490 pod_name = ""
1491 vgroup_name = ""
1492 target_exists = False
1493 api_version = array.get_rest_version()
1494 if "::" in module.params["name"]:
1495 pod_name = "::".join(module.params["name"].split("::")[:-1])
1496 target_name = pod_name + "::" + module.params["rename"]
1497 elif "/" in module.params["name"]:
1498 vgroup_name = module.params["name"].split("/")[0]
1499 target_name = vgroup_name + "/" + module.params["rename"]
1500 else:
1501 target_name = module.params["rename"]
1502 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
1503 target_exists = bool(
1504 array.get_volumes(
1505 names=[target_name], context_names=[module.params["context"]]
1506 ).status_code
1507 == 200
1508 )
1509 else:
1510 target_exists = bool(array.get_volumes(names=[target_name]).status_code == 200)
1511 if target_exists:
1512 module.fail_json(msg="Target volume {0} already exists.".format(target_name))
1513 else:
1514 changed = True
1515 if not module.check_mode:
1516 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
1517 res = array.patch_volumes(
1518 names=[module.params["name"]],
1519 context_names=[module.params["context"]],
1520 volume=VolumePatch(name=module.params["rename"]),
1521 )
1522 else:
1523 res = array.patch_volumes(
1524 names=[module.params["name"]],
1525 volume=VolumePatch(name=module.params["rename"]),
1526 )
1527 if res.status_code != 200:
1528 module.fail_json(
1529 msg="Rename volume {0} to {1} failed. Error: {2}".format(
1530 module.params["name"],
1531 module.params["rename"],
1532 res.errors[0].message,
1533 )
1534 )
1535
1536 module.exit_json(
1537 changed=changed, volume=_volfact(module, array, module.params["rename"])
1538 )
1539
1540
1541 def move_volume(module, array):
1542 """Move volume between pods, vgroups or local array"""
1543 api_version = array.get_rest_version()
1544 changed = vgroup_exists = target_exists = pod_exists = False
1545 pod_name = ""
1546 vgroup_name = ""
1547 volume_name = module.params["name"]
1548 if "::" in module.params["name"]:
1549 volume_name = module.params["name"].rsplit("::", 1)[1]
1550 pod_name = "::".join(module.params["name"].split("::")[:-1])
1551 if "/" in module.params["name"]:
1552 volume_name = module.params["name"].split("/")[1]
1553 vgroup_name = module.params["name"].split("/")[0]
1554 if module.params["move"] == "local":
1555 if "::" not in module.params["name"]:
1556 if "/" not in module.params["name"]:
1557 module.fail_json(
1558 msg="Source and destination [local] cannot be the same."
1559 )
1560 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
1561 target_exists = bool(
1562 array.get_volumes(
1563 names=[volume_name], context_names=[module.params["context"]]
1564 ).status_code
1565 == 200
1566 )
1567 else:
1568 target_exists = bool(
1569 array.get_volumes(names=[volume_name]).status_code == 200
1570 )
1571 if target_exists:
1572 module.fail_json(msg="Target volume {0} already exists".format(volume_name))
1573 else:
1574 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
1575 pod_exists = bool(
1576 array.get_pods(
1577 names=[module.params["move"]],
1578 context_names=[module.params["context"]],
1579 ).status_code
1580 == 200
1581 )
1582 else:
1583 pod_exists = bool(
1584 array.get_pods(names=[module.params["move"]]).status_code == 200
1585 )
1586 if pod_exists:
1587 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
1588 pod = list(
1589 array.get_pods(
1590 names=[module.params["move"]],
1591 context_names=[module.params["context"]],
1592 ).items
1593 )[0]
1594 else:
1595 pod = list(array.get_pods(names=[module.params["move"]]).items)[0]
1596 if pod.array_count > 1:
1597 module.fail_json(msg="Volume cannot be moved into a stretched pod")
1598 if pod.link_target_count != 0:
1599 module.fail_json(msg="Volume cannot be moved into a linked source pod")
1600 if pod.promotion_status == "demoted":
1601 module.fail_json(msg="Volume cannot be moved into a demoted pod")
1602 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
1603 target_exists = bool(
1604 array.get_volumes(
1605 names=[module.params["move"] + "::" + volume_name],
1606 context_names=[module.params["context"]],
1607 ).status_code
1608 == 200
1609 )
1610 else:
1611 target_exists = bool(
1612 array.get_volumes(
1613 names=[module.params["move"] + "::" + volume_name]
1614 ).status_code
1615 == 200
1616 )
1617 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
1618 vgroup_exists = bool(
1619 array.get_volume_groups(
1620 names=[module.params["move"]],
1621 context_names=[module.params["context"]],
1622 ).status_code
1623 == 200
1624 )
1625 else:
1626 vgroup_exists = bool(
1627 array.get_volume_groups(names=[module.params["move"]]).status_code
1628 == 200
1629 )
1630 if vgroup_exists:
1631 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
1632 target_exists = bool(
1633 array.get_volumes(
1634 names=[module.params["move"] + "/" + volume_name],
1635 context_names=[module.params["context"]],
1636 ).status_code
1637 == 200
1638 )
1639 else:
1640 target_exists = bool(
1641 array.get_volumes(
1642 names=[module.params["move"] + "/" + volume_name]
1643 ).status_code
1644 == 200
1645 )
1646 if target_exists:
1647 module.fail_json(msg="Volume of same name already exists in move location")
1648 if pod_exists and vgroup_exists:
1649 module.fail_json(
1650 msg="Move location {0} matches both a pod and a vgroup. Please rename one of these.".format(
1651 module.params["move"]
1652 )
1653 )
1654 if not pod_exists and not vgroup_exists:
1655 module.fail_json(
1656 msg="Move location {0} does not exist.".format(module.params["move"])
1657 )
1658 if "::" in module.params["name"] and not vgroup_exists:
1659 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
1660 pod = list(array.get_pods(names=[pod_name]).items)[0]
1661 else:
1662 pod = list(
1663 array.get_pods(
1664 names=[pod_name], context_names=[module.params["context"]]
1665 ).items
1666 )[0]
1667 if pod.array_count > 1:
1668 module.fail_json(msg="Volume cannot be moved out of a stretched pod")
1669 if pod.linked_target_count != 0:
1670 module.fail_json(
1671 msg="Volume cannot be moved out of a linked source pod"
1672 )
1673 if pod.promotion_status == "demoted":
1674 module.fail_json(msg="Volume cannot be moved out of a demoted pod")
1675 if "/" in module.params["name"]:
1676 if (
1677 vgroup_name == module.params["move"]
1678 or pod_name == module.params["move"]
1679 ):
1680 module.fail_json(msg="Source and destination cannot be the same")
1681 if get_endpoint(module, module.params["move"], array):
1682 module.fail_json(
1683 msg="Target volume {0} is a protocol-endpoinnt".format(
1684 module.params["move"]
1685 )
1686 )
1687 changed = True
1688 if not module.check_mode:
1689 if pod_exists:
1690 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
1691 res = array.patch_volumes(
1692 context_names=[module.params["context"]],
1693 names=[module.params["name"]],
1694 volume=VolumePatch(pod=Reference(name=module.params["move"])),
1695 )
1696 else:
1697 res = array.patch_volumes(
1698 names=[module.params["name"]],
1699 volume=VolumePatch(pod=Reference(name=module.params["move"])),
1700 )
1701 elif vgroup_exists:
1702 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
1703 res = array.patch_volumes(
1704 context_names=[module.params["context"]],
1705 names=[module.params["name"]],
1706 volume=VolumePatch(
1707 volume_group=Reference(name=module.params["move"])
1708 ),
1709 )
1710 else:
1711 res = array.patch_volumes(
1712 names=[module.params["name"]],
1713 volume=VolumePatch(
1714 volume_group=Reference(name=module.params["move"])
1715 ),
1716 )
1717 elif module.params["move"] == "local":
1718 if "/" in module.params["name"]:
1719 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
1720 res = array.patch_volumes(
1721 context_names=[module.params["context"]],
1722 names=[module.params["name"]],
1723 volume=VolumePatch(volume_group=Reference(name="")),
1724 )
1725 else:
1726 res = array.patch_volumes(
1727 names=[module.params["name"]],
1728 volume=VolumePatch(volume_group=Reference(name="")),
1729 )
1730 else:
1731 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
1732 res = array.patch_volumes(
1733 context_names=[module.params["context"]],
1734 names=[module.params["name"]],
1735 volume=VolumePatch(pod=Reference(name="")),
1736 )
1737 else:
1738 res = array.patch_volumes(
1739 names=[module.params["name"]],
1740 volume=VolumePatch(pod=Reference(name="")),
1741 )
1742 if res.status_code != 200:
1743 module.fail_json(
1744 msg="Move of volume {0} to {1} failed. Error: {2}".format(
1745 module.params["name"],
1746 module.params["move"],
1747 res.errors[0].message,
1748 )
1749 )
1750 else:
1751 volume_name = list(res.items)[0].name
1752 else:
1753 volume_name = ""
1754 module.exit_json(changed=changed, volume=_volfact(module, array, volume_name))
1755
1756
1757 def delete_volume(module, array):
1758 """Delete Volume"""
1759 api_version = array.get_rest_version()
1760 changed = False
1761 if module.params["add_to_pgs"]:
1762 pgs_now = []
1763 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
1764 current_pgs = list(
1765 array.get_protection_groups_volumes(
1766 context_names=[module.params["context"]],
1767 member_names=[module.params["name"]],
1768 ).items
1769 )
1770 else:
1771 current_pgs = list(
1772 array.get_protection_groups_volumes(
1773 member_names=[module.params["name"]]
1774 ).items
1775 )
1776 for current_pg in current_pgs:
1777 pgs_now.append(current_pg.group.name)
1778 old_pgs = list(filter(lambda x: x in module.params["add_to_pgs"], pgs_now))
1779 if old_pgs:
1780 changed = True
1781 if not module.check_mode:
1782 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
1783 res = array.delete_volumes_protection_groups(
1784 member_names=[module.params["name"]],
1785 group_names=old_pgs,
1786 context_names=[module.params["context"]],
1787 )
1788 else:
1789 res = array.delete_volumes_protection_groups(
1790 member_names=[module.params["name"]],
1791 group_names=old_pgs,
1792 )
1793 if res.status_code != 200:
1794 module.fail_json(
1795 msg="Failed to remove volume {0} from PGs {1}: Error: {2}".format(
1796 module.params["name"], old_pgs, res.errors[0].message
1797 )
1798 )
1799 else:
1800 changed = True
1801 if not module.check_mode:
1802 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
1803 res = array.patch_volumes(
1804 names=[module.params["name"]],
1805 volume=VolumePatch(destroyed=True),
1806 context_names=[module.params["context"]],
1807 )
1808 else:
1809 res = array.patch_volumes(
1810 names=[module.params["name"]], volume=VolumePatch(destroyed=True)
1811 )
1812 if res.status_code == 200 and module.params["eradicate"]:
1813 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
1814 res = array.delete_volumes(
1815 names=[module.params["name"]],
1816 context_names=[module.params["context"]],
1817 )
1818 else:
1819 res = array.delete_volumes(names=[module.params["name"]])
1820 if res.status_code != 200:
1821 module.fail_json(
1822 msg="Eradicate volume {0} failed. Error: {1}".format(
1823 module.params["name"], res.errors[0].message
1824 )
1825 )
1826 module.exit_json(
1827 changed=changed,
1828 )
1829 elif res.status_code != 200:
1830 module.fail_json(
1831 msg="Delete volume {0} failed. Error: {1}".format(
1832 module.params["name"], res.errors[0].message
1833 )
1834 )
1835 module.exit_json(
1836 changed=changed, volume=_volfact(module, array, module.params["name"])
1837 )
1838
1839
1840 def eradicate_volume(module, array):
1841 """Eradicate Deleted Volume"""
1842 api_version = array.get_rest_version()
1843 changed = False
1844 volfact = _volfact(module, array, module.params["name"])
1845 if module.params["eradicate"]:
1846 changed = True
1847 volfact = []
1848 if not module.check_mode:
1849 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
1850 res = array.delete_volumes(
1851 names=[module.params["name"]],
1852 context_names=[module.params["context"]],
1853 )
1854 else:
1855 res = array.delete_volumes(names=[module.params["name"]])
1856 if res.status_code != 200:
1857 module.fail_json(
1858 msg="Eradication of volume {0} failed. Error: {1}".format(
1859 module.params["name"], res.errors[0].message
1860 )
1861 )
1862 module.exit_json(changed=changed, volume=volfact)
1863
1864
1865 def recover_volume(module, array):
1866 """Recover Deleted Volume"""
1867 api_version = array.get_rest_version()
1868 changed = True
1869 if not module.check_mode:
1870 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
1871 res = array.patch_volumes(
1872 names=[module.params["name"]],
1873 volume=VolumePatch(destroyed=False),
1874 context_names=[module.params["context"]],
1875 )
1876 else:
1877 res = array.patch_volumes(
1878 names=[module.params["name"]], volume=VolumePatch(destroyed=False)
1879 )
1880 if res.status_code != 200:
1881 module.fail_json(
1882 msg="Recovery of volume {0} failed. Error: {1}".format(
1883 module.params["name"], res.errors[0].message
1884 )
1885 )
1886 module.exit_json(
1887 changed=changed, volume=_volfact(module, array, module.params["name"])
1888 )
1889
1890
1891 def main():
1892 argument_spec = purefa_argument_spec()
1893 argument_spec.update(
1894 dict(
1895 name=dict(type="str", required=True),
1896 target=dict(type="str"),
1897 move=dict(type="str"),
1898 rename=dict(type="str"),
1899 overwrite=dict(type="bool", default=False),
1900 eradicate=dict(type="bool", default=False),
1901 state=dict(type="str", default="present", choices=["absent", "present"]),
1902 bw_qos=dict(type="str", aliases=["qos"]),
1903 iops_qos=dict(type="str"),
1904 pgroup=dict(type="str"),
1905 count=dict(type="int"),
1906 start=dict(type="int", default=0),
1907 digits=dict(type="int", default=1),
1908 suffix=dict(type="str", default=""),
1909 priority_operator=dict(type="str", choices=["+", "-", "="]),
1910 priority_value=dict(type="int", choices=[-10, 0, 10]),
1911 size=dict(type="str"),
1912 with_default_protection=dict(type="bool", default=True),
1913 add_to_pgs=dict(type="list", elements="str"),
1914 promotion_state=dict(type="str", choices=["promoted", "demoted"]),
1915 context=dict(type="str", default=""),
1916 )
1917 )
1918
1919 mutually_exclusive = [
1920 ["size", "target"],
1921 ["move", "rename", "target", "eradicate"],
1922 ["rename", "move", "target", "eradicate"],
1923 ]
1924 required_together = [["priority_operator", "priority_value"]]
1925
1926 module = AnsibleModule(
1927 argument_spec,
1928 mutually_exclusive=mutually_exclusive,
1929 required_together=required_together,
1930 supports_check_mode=True,
1931 )
1932
1933 size = module.params["size"]
1934 bw_qos_size = False
1935 iops_qos_size = False
1936 state = module.params["state"]
1937 destroyed = False
1938 array = get_array(module)
1939 volume = get_volume(module, array)
1940 api_version = array.get_rest_version()
1941 endpoint = get_endpoint(module, module.params["name"], array)
1942 if not HAS_PURESTORAGE:
1943 module.fail_json(msg="py-pure-client sdk is required for this module")
1944 if (
1945 LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version)
1946 and not module.params["context"]
1947 ):
1948 # If no context is provided set the context to the local array name
1949 module.params["context"] = list(array.get_arrays().items)[0].name
1950
1951 if module.params["bw_qos"]:
1952 bw_qos = int(human_to_bytes(module.params["bw_qos"]))
1953 if bw_qos in range(1048576, 549755813888) or bw_qos == 0:
1954 bw_qos_size = True
1955 else:
1956 module.fail_json(msg="Bandwidth QoS value out of range.")
1957 if module.params["iops_qos"]:
1958 iops_qos = int(human_to_real(module.params["iops_qos"]))
1959 if iops_qos in range(100, 100000000) or iops_qos == 0:
1960 iops_qos_size = True
1961 else:
1962 module.fail_json(msg="IOPs QoS value out of range.")
1963
1964 if endpoint:
1965 module.fail_json(
1966 msg="Volume {0} is an endpoint. Use purefa_endpoint module.".format(
1967 module.params["name"]
1968 )
1969 )
1970
1971 if module.params["pgroup"]:
1972 pattern = re.compile("^[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?$")
1973 if ":" in module.params["pgroup"]:
1974 if "::" in module.params["pgroup"]:
1975 pgname = module.params["pgroup"].split("::")[1]
1976 else:
1977 pgname = module.params["pgroup"].split(":")[1]
1978 if not pattern.match(pgname):
1979 module.fail_json(
1980 msg="Protection Group name {0} does not conform to naming convention".format(
1981 pgname
1982 )
1983 )
1984 else:
1985 if not pattern.match(module.params["pgroup"]):
1986 module.fail_json(
1987 msg="Protection Group name {0} does not conform to naming convention".format(
1988 pgname
1989 )
1990 )
1991 pgroup = get_pgroup(module, array)
1992 xpgroup = get_pending_pgroup(module, array)
1993 if "::" in module.params["pgroup"]:
1994 if not get_pod(module, array):
1995 module.fail_json(
1996 msg="Pod {0} does not exist.".format(
1997 module.params["pgroup"].split("::")[0]
1998 )
1999 )
2000 if not pgroup:
2001 if xpgroup:
2002 module.fail_json(
2003 msg="Protection Group {0} is currently deleted. Please restore to use.".format(
2004 module.params["pgroup"]
2005 )
2006 )
2007 else:
2008 module.fail_json(
2009 msg="Protection Group {0} does not exist.".format(
2010 module.params["pgroup"]
2011 )
2012 )
2013
2014 if not volume:
2015 destroyed = get_destroyed_volume(module, array)
2016 target = get_target(module, array)
2017 if module.params["count"]:
2018 if module.params["digits"] and module.params["digits"] not in range(1, 10):
2019 module.fail_json(msg="'digits' must be in the range of 1 to 10")
2020 if module.params["start"] < 0:
2021 module.fail_json(msg="'start' must be a positive number")
2022 volume = get_multi_volumes(module, array)
2023 if state == "present" and not volume and size:
2024 create_multi_volume(module, array)
2025 elif state == "present" and not volume and not size:
2026 module.fail_json(msg="Size must be specified to create a new volume")
2027 elif state == "absent" and not volume:
2028 module.exit_json(changed=False)
2029 else:
2030 module.warn("Method not yet supported for multi-volume")
2031 else:
2032 if state == "present" and not volume and not destroyed and size:
2033 if LooseVersion(DEFAULT_API_VERSION) <= LooseVersion(api_version):
2034 create_multi_volume(module, array, True)
2035 else:
2036 create_volume(module, array)
2037 elif (
2038 state == "present"
2039 and volume
2040 and not module.params["rename"]
2041 and (
2042 size
2043 or bw_qos_size
2044 or iops_qos_size
2045 or module.params["promotion_state"]
2046 or module.params["add_to_pgs"]
2047 )
2048 ):
2049 update_volume(module, array)
2050 elif state == "present" and not volume and module.params["move"]:
2051 module.fail_json(
2052 msg="Volume {0} cannot be moved - does not exist (maybe deleted)".format(
2053 module.params["name"]
2054 )
2055 )
2056 elif state == "present" and volume and module.params["move"]:
2057 move_volume(module, array)
2058 elif state == "present" and volume and module.params["rename"]:
2059 rename_volume(module, array)
2060 elif (
2061 state == "present"
2062 and destroyed
2063 and not module.params["move"]
2064 and not module.params["rename"]
2065 ):
2066 recover_volume(module, array)
2067 elif state == "present" and destroyed and module.params["move"]:
2068 module.fail_json(
2069 msg="Volume {0} exists, but in destroyed state".format(
2070 module.params["name"]
2071 )
2072 )
2073 elif state == "present" and volume and target:
2074 copy_from_volume(module, array)
2075 elif state == "present" and volume and not target:
2076 copy_from_volume(module, array)
2077 elif state == "absent" and volume:
2078 delete_volume(module, array)
2079 elif state == "absent" and destroyed:
2080 eradicate_volume(module, array)
2081 elif state == "present":
2082 if not volume and not size:
2083 module.fail_json(msg="Size must be specified to create a new volume")
2084 elif state == "absent" and not volume:
2085 module.exit_json(changed=False, volume=[])
2086
2087 module.exit_json(changed=False, volume=[])
2088
2089
2090 if __name__ == "__main__":
2091 main()