Debian Codegraph @

ansible_collections/netapp/ontap/plugins/modules/na_ontap_cg.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: NetApp, Inc
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 __metaclass__ = type
9
10 DOCUMENTATION = """
11 module: na_ontap_cg
12 short_description: NetApp ONTAP module to manage operations related to consistency groups.
13 extends_documentation_fragment:
14 - netapp.ontap.netapp.na_ontap_rest
15 version_added: 23.4.0
16 author: NetApp Ansible Team (@carchi8py) <[email protected]>
17 description:
18 - Create a consistency group with one or more consistency groups having new or existing volumes.
19 - Modify or delete an existing consistency group.
20 options:
21 state:
22 description:
23 - Specifies whether to create, modify, or delete a consistency group.
24 type: str
25 choices: ['present', 'absent']
26 default: present
27
28 name:
29 description:
30 - Specifies the name of the consistency group. The consistency group name must be unique within an SVM.
31 type: str
32 required: true
33
34 vserver:
35 description:
36 - Specifies the SVM in which the consistency group is located.
37 type: str
38 required: true
39
40 volumes:
41 description:
42 - List of volumes to be included in the consistency group.
43 - The volumes array can be used to create new volumes in the consistency group, add existing volumes to the consistency group.
44 - A volume can only be associated with one direct parent consistency group.
45 - Removing all volumes from a consistency group will automatically delete the consistency group.
46 type: list
47 elements: dict
48 suboptions:
49 name:
50 description:
51 - Specifies the name of the volume.
52 type: str
53 required: true
54
55 size:
56 description:
57 - Specifies the provisioned size of the volume in C(size_unit).
58 type: int
59
60 size_unit:
61 description:
62 - The unit used to interpret the size parameter.
63 type: str
64 choices: ['bytes', 'b', 'kb', 'mb', 'gb', 'tb', 'pb', 'eb', 'zb', 'yb']
65
66 provisioning_options:
67 description:
68 - Options that are applied to the operation.
69 - Module is not idempotent when 'count' option is set as the name is considered a prefix,
70 and a suffix of the form _<N> is generated
71 where N is the next available numeric index, starting with 1.
72 type: dict
73 suboptions:
74 action:
75 description: Operation to perform on the volume.
76 type: str
77 choices: ['create', 'add', 'remove']
78 count:
79 description: Number of elements to perform the operation on.
80 type: int
81
82 qos_policy:
83 description:
84 - The QoS policy for this volume.
85 - Only supported when provisioning new objects.
86 type: str
87
88 snapshot_policy:
89 description:
90 - The snapshot policy for this volume.
91 type: str
92
93 luns:
94 description:
95 - The LUNs array can be used to create or modify LUNs in a consistency group on a new or existing volume that is a member of the consistency group.
96 type: list
97 elements: dict
98 suboptions:
99 name:
100 description:
101 - The fully qualified path name of the LUN composed of the "/vol" prefix, the volume name, the qtree name (optional), and the base name of the LUN.
102 - Example, /vol/volume1/lun1 or /vol/volume1/qtree1/lun1.
103 type: str
104 required: true
105
106 size:
107 description:
108 - Specifies the provisioned size in C(size_unit).
109 type: int
110
111 size_unit:
112 description:
113 - The unit used to interpret the size parameter.
114 type: str
115 choices: ['bytes', 'b', 'kb', 'mb', 'gb', 'tb', 'pb', 'eb', 'zb', 'yb']
116
117 provisioning_options:
118 description:
119 - Options that are applied to the operation.
120 - Module is not idempotent when 'count' option is set as the name is considered a prefix,
121 and a suffix of the form _<N> is generated
122 where N is the next available numeric index, starting with 1.
123 type: dict
124 suboptions:
125 action:
126 description: Operation to perform.
127 type: str
128 choices: ['create']
129 count:
130 description: Number of elements to perform the operation on.
131 type: int
132
133 os_type:
134 description:
135 - The os type for the LUN.
136 type: str
137
138 lun_maps:
139 description: An array of LUN maps.
140 type: list
141 elements: dict
142 suboptions:
143 igroup_name:
144 description: The name of the initiator group.
145 type: str
146 igroup_initiators:
147 description: Lists of the initiators that are members of the group.
148 type: list
149 elements: str
150 os_type:
151 description: The os type for the initiator groups.
152 type: str
153
154 namespaces:
155 description:
156 - The namespaces array can be used to create or modify namespaces in a consistency group.
157 type: list
158 elements: dict
159 suboptions:
160 name:
161 description:
162 - The name of the NVMe namespace, of the form "/vol/<volume>[/<qtree>]/<namespace>" where the qtree name is optional.
163 - Example, /vol/volume1/namespace1 or /vol/volume1/qtree1/namespace1.
164 type: str
165 required: true
166
167 size:
168 description:
169 - Specifies the provisioned size in C(size_unit).
170 type: int
171
172 size_unit:
173 description:
174 - The unit used to interpret the size parameter.
175 type: str
176 choices: ['bytes', 'b', 'kb', 'mb', 'gb', 'tb', 'pb', 'eb', 'zb', 'yb']
177
178 os_type:
179 description:
180 - The os type for the namespace.
181 type: str
182
183 provisioning_options:
184 description:
185 - Options that are applied to the operation.
186 - Module is not idempotent when 'count' option is set as the name is considered a prefix,
187 and a suffix of the form _<N> is generated
188 where N is the next available numeric index, starting with 1.
189 type: dict
190 suboptions:
191 action:
192 description: Operation to perform.
193 type: str
194 choices: ['create']
195 count:
196 description: Number of elements to perform the operation on.
197 type: int
198
199 consistency_groups:
200 description:
201 - List of consistency groups to be included in the consistency group.
202 type: list
203 elements: dict
204 suboptions:
205 name:
206 description:
207 - Specifies the name of the consistency group.
208 type: str
209 required: true
210
211 provisioning_options:
212 description:
213 - Options that are applied to the operation.
214 type: dict
215 suboptions:
216 action:
217 description: Operation to perform.
218 type: str
219 choices: ['create', 'add', 'remove']
220 name:
221 description:
222 - New name for consistency group. Required to resolve naming collisions.
223 - Requires ONTAP 9.13.1 or later.
224 type: str
225
226 volumes:
227 description:
228 - List of volumes to be included in the consistency group.
229 - The volumes array can be used to create new volumes in the consistency group, add existing volumes to the consistency group.
230 - A volume can only be associated with one direct parent consistency group.
231 type: list
232 elements: dict
233 suboptions:
234 name:
235 description:
236 - Specifies the name of the volume.
237 type: str
238 required: true
239
240 size:
241 description:
242 - Specifies the provisioned size of the volume in C(size_unit).
243 type: int
244
245 size_unit:
246 description:
247 - The unit used to interpret the size parameter.
248 type: str
249 choices: ['bytes', 'b', 'kb', 'mb', 'gb', 'tb', 'pb', 'eb', 'zb', 'yb']
250
251 provisioning_options:
252 description:
253 - Options that are applied to the operation.
254 - Module is not idempotent when 'count' option is set as the name is considered a prefix,
255 and a suffix of the form _<N> is generated
256 where N is the next available numeric index, starting with 1.
257 type: dict
258 suboptions:
259 action:
260 description: Operation to perform on the volume.
261 type: str
262 choices: ['create', 'add', 'remove']
263 count:
264 description: Number of elements to perform the operation on.
265 type: int
266
267 qos_policy:
268 description:
269 - The QoS policy for this volume.
270 - Only supported when provisioning new objects.
271 type: str
272
273 snapshot_policy:
274 description:
275 - The snapshot policy for this volume.
276 type: str
277
278 luns:
279 description:
280 - The LUNs array can be used to create or modify LUNs in a consistency group on a new or existing volume that is a member of the consistency group.
281 type: list
282 elements: dict
283 suboptions:
284 name:
285 description:
286 - The fully qualified path name of the LUN composed of the "/vol" prefix,
287 the volume name, the qtree name (optional), and the base name of the LUN.
288 - Example, /vol/volume1/lun1 or /vol/volume1/qtree1/lun1.
289 type: str
290 required: true
291
292 size:
293 description:
294 - Specifies the provisioned size in C(size_unit).
295 type: int
296
297 size_unit:
298 description:
299 - The unit used to interpret the size parameter.
300 type: str
301 choices: ['bytes', 'b', 'kb', 'mb', 'gb', 'tb', 'pb', 'eb', 'zb', 'yb']
302
303 provisioning_options:
304 description:
305 - Options that are applied to the operation.
306 - Module is not idempotent when 'count' option is set as the name is considered a prefix,
307 and a suffix of the form _<N> is generated
308 where N is the next available numeric index, starting with 1.
309 type: dict
310 suboptions:
311 action:
312 description: Operation to perform.
313 type: str
314 choices: ['create']
315 count:
316 description: Number of elements to perform the operation on.
317 type: int
318
319 os_type:
320 description:
321 - The os type for the LUN.
322 type: str
323
324 lun_maps:
325 description: An array of LUN maps.
326 type: list
327 elements: dict
328 suboptions:
329 igroup_name:
330 description: The name of the initiator group.
331 type: str
332 igroup_initiators:
333 description: Lists of the initiators that are members of the group.
334 type: list
335 elements: str
336 os_type:
337 description: The os type for the initiator groups.
338 type: str
339
340 namespaces:
341 description:
342 - The namespaces array can be used to create or modify namespaces in a consistency group.
343 type: list
344 elements: dict
345 suboptions:
346 name:
347 description:
348 - The name of the NVMe namespace, of the form "/vol/<volume>[/<qtree>]/<namespace>" where the qtree name is optional.
349 - Example, /vol/volume1/namespace1 or /vol/volume1/qtree1/namespace1.
350 type: str
351 required: true
352
353 size:
354 description:
355 - Specifies the provisioned size in C(size_unit).
356 type: int
357
358 size_unit:
359 description:
360 - The unit used to interpret the size parameter.
361 type: str
362 choices: ['bytes', 'b', 'kb', 'mb', 'gb', 'tb', 'pb', 'eb', 'zb', 'yb']
363
364 os_type:
365 description:
366 - The os type for the namespace.
367 type: str
368
369 provisioning_options:
370 description:
371 - Options that are applied to the operation.
372 - Module is not idempotent when 'count' option is set as the name is considered a prefix,
373 and a suffix of the form _<N> is generated
374 where N is the next available numeric index, starting with 1.
375 type: dict
376 suboptions:
377 action:
378 description: Operation to perform.
379 type: str
380 choices: ['create']
381 count:
382 description: Number of elements to perform the operation on.
383 type: int
384 qos_policy:
385 description:
386 - Specifies the QoS policy for the consistency group.
387 - Only supported when provisioning new objects.
388 type: str
389
390 snapshot_policy:
391 description:
392 - Specifies the snapshot policy for the consistency group.
393 type: str
394
395 qos_policy:
396 description:
397 - Specifies the QoS policy for the consistency group.
398 - Only supported when provisioning new objects.
399 type: str
400
401 snapshot_policy:
402 description:
403 - Specifies the snapshot policy for the consistency group.
404 type: str
405
406 notes:
407 - Only supported with REST and requires ONTAP 9.10.1 or later.
408 - Update operation will never delete storage elements.
409 Mapping or unmapping a consistency group from igroups or subsystems is not supported.
410 - Delete operation will not delete any associated volumes or LUNs.
411 To delete those elements, use the appropriate object associated modules.
412 """
413
414 EXAMPLES = """
415 - name: Creating a single consistency group with a new SAN volume
416 netapp.ontap.na_ontap_cg:
417 state: present
418 vserver: svm1
419 name: test_cg1
420 volumes:
421 - name: test_vol1
422 size: 50
423 size_unit: mb
424 provisioning_options:
425 action: create
426 hostname: "{{ netapp_hostname }}"
427 username: "{{ netapp_username }}"
428 password: "{{ netapp_password }}"
429 https: true
430 validate_certs: "{{ validate_certs }}"
431
432 - name: Adding an existing volume to an existing consistency group
433 netapp.ontap.na_ontap_cg:
434 state: present
435 vserver: svm1
436 name: test_cg1
437 volumes:
438 - name: test_vol2
439 provisioning_options:
440 action: add
441 hostname: "{{ netapp_hostname }}"
442 username: "{{ netapp_username }}"
443 password: "{{ netapp_password }}"
444 https: true
445 validate_certs: "{{ validate_certs }}"
446
447 - name: Adding LUNs to an existing volume in an existing consistency group
448 netapp.ontap.na_ontap_cg:
449 state: present
450 vserver: svm1
451 name: test_cg1
452 luns:
453 - name: "/vol/test_vol2/new_lun"
454 size: 5
455 size_unit: mb
456 os_type: linux
457 provisioning_options:
458 action: create
459 lun_maps:
460 - igroup_name: new_igroup1
461 igroup_initiators:
462 - iqn.1995-08.com.example:new-initiator1
463 os_type: linux
464 hostname: "{{ netapp_hostname }}"
465 username: "{{ netapp_username }}"
466 password: "{{ netapp_password }}"
467 https: true
468 validate_certs: "{{ validate_certs }}"
469
470 - name: Adding namespaces to an existing volume in an existing consistency group
471 netapp.ontap.na_ontap_cg:
472 state: present
473 vserver: svm1
474 name: test_cg1
475 namespaces:
476 - name: "/vol/test_vol2/new_namespace"
477 size: 5
478 size_unit: mb
479 os_type: linux
480 provisioning_options:
481 action: create
482 hostname: "{{ netapp_hostname }}"
483 username: "{{ netapp_username }}"
484 password: "{{ netapp_password }}"
485 https: true
486 validate_certs: "{{ validate_certs }}"
487
488 - name: Deleting a CG (consistency group)
489 netapp.ontap.na_ontap_cg:
490 state: absent
491 vserver: svm1
492 name: test_cg1
493 hostname: "{{ netapp_hostname }}"
494 username: "{{ netapp_username }}"
495 password: "{{ netapp_password }}"
496 https: true
497 validate_certs: "{{ validate_certs }}"
498
499 - name: Creating a parent CG with two child consistency groups with existing SAN volumes
500 netapp.ontap.na_ontap_cg:
501 state: present
502 vserver: svm1
503 name: parent_cg
504 consistency_groups:
505 - name: child_1
506 volumes:
507 - name: test_vol1
508 provisioning_options:
509 action: add
510 - name: child_2
511 volumes:
512 - name: test_vol2
513 provisioning_options:
514 action: add
515 hostname: "{{ netapp_hostname }}"
516 username: "{{ netapp_username }}"
517 password: "{{ netapp_password }}"
518 https: true
519 validate_certs: "{{ validate_certs }}"
520
521 - name: Removing a child consistency group from nested CG
522 netapp.ontap.na_ontap_cg:
523 state: present
524 vserver: svm1
525 name: parent_cg
526 consistency_groups:
527 - name: child_2
528 provisioning_options:
529 action: remove
530 name: new_single_cg
531 hostname: "{{ netapp_hostname }}"
532 username: "{{ netapp_username }}"
533 password: "{{ netapp_password }}"
534 https: true
535 validate_certs: "{{ validate_certs }}"
536
537 - name: Creating a parent CG with a new child CG with existing volumes
538 netapp.ontap.na_ontap_cg:
539 state: present
540 vserver: svm1
541 name: parent_cg
542 consistency_groups:
543 - name: child_1
544 provisioning_options:
545 action: create
546 volumes:
547 - name: test_vol1
548 provisioning_options:
549 action: add
550 hostname: "{{ netapp_hostname }}"
551 username: "{{ netapp_username }}"
552 password: "{{ netapp_password }}"
553 https: true
554 validate_certs: "{{ validate_certs }}"
555 """
556
557 RETURN = """
558 """
559
560 import traceback
561 from ansible.module_utils.basic import AnsibleModule
562 from ansible.module_utils._text import to_native
563 import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils
564 from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule
565 from ansible_collections.netapp.ontap.plugins.module_utils import rest_generic
566
567 import copy
568
569
570 class NetAppOntapConsistencyGroup:
571 def __init__(self):
572 self.argument_spec = netapp_utils.na_ontap_rest_only_spec()
573 self.argument_spec.update(dict(
574 state=dict(required=False, type='str', choices=['present', 'absent'], default='present'),
575 name=dict(required=True, type='str'),
576 vserver=dict(required=True, type='str'),
577 volumes=dict(type='list', elements='dict', options=dict(
578 name=dict(required=True, type='str'),
579 size=dict(type='int'),
580 size_unit=dict(type='str', choices=['bytes', 'b', 'kb', 'mb', 'gb', 'tb',
581 'pb', 'eb', 'zb', 'yb']),
582 provisioning_options=dict(type='dict', options=dict(
583 action=dict(type='str', choices=['create', 'add', 'remove']),
584 count=dict(type='int')
585 )),
586 qos_policy=dict(type='str'),
587 snapshot_policy=dict(type='str')
588 )),
589 luns=dict(type='list', elements='dict', options=dict(
590 name=dict(required=True, type='str'),
591 size=dict(type='int'),
592 size_unit=dict(type='str', choices=['bytes', 'b', 'kb', 'mb', 'gb', 'tb',
593 'pb', 'eb', 'zb', 'yb']),
594 provisioning_options=dict(type='dict', options=dict(
595 action=dict(type='str', choices=['create']),
596 count=dict(type='int')
597 )),
598 os_type=dict(type='str'),
599 lun_maps=dict(type='list', elements='dict', options=dict(
600 igroup_name=dict(type='str'),
601 igroup_initiators=dict(type='list', elements='str'),
602 os_type=dict(type='str'),
603 )),
604 )),
605 namespaces=dict(type='list', elements='dict', options=dict(
606 name=dict(required=True, type='str'),
607 size=dict(type='int'),
608 size_unit=dict(type='str', choices=['bytes', 'b', 'kb', 'mb', 'gb', 'tb',
609 'pb', 'eb', 'zb', 'yb']),
610 os_type=dict(type='str'),
611 provisioning_options=dict(type='dict', options=dict(
612 action=dict(type='str', choices=['create']),
613 count=dict(type='int')
614 )),
615 )),
616 consistency_groups=dict(type='list', elements='dict', options=dict(
617 name=dict(required=True, type='str'),
618 provisioning_options=dict(type='dict', options=dict(
619 action=dict(type='str', choices=['create', 'add', 'remove']),
620 name=dict(type='str')
621 )),
622 volumes=dict(type='list', elements='dict', options=dict(
623 name=dict(required=True, type='str'),
624 size=dict(type='int'),
625 size_unit=dict(type='str', choices=['bytes', 'b', 'kb', 'mb', 'gb', 'tb',
626 'pb', 'eb', 'zb', 'yb']),
627 provisioning_options=dict(type='dict', options=dict(
628 action=dict(type='str', choices=['create', 'add', 'remove']),
629 count=dict(type='int')
630 )),
631 qos_policy=dict(type='str'),
632 snapshot_policy=dict(type='str'),
633 )),
634 luns=dict(type='list', elements='dict', options=dict(
635 name=dict(required=True, type='str'),
636 size=dict(type='int'),
637 size_unit=dict(type='str', choices=['bytes', 'b', 'kb', 'mb', 'gb', 'tb',
638 'pb', 'eb', 'zb', 'yb']),
639 provisioning_options=dict(type='dict', options=dict(
640 action=dict(type='str', choices=['create']),
641 count=dict(type='int')
642 )),
643 os_type=dict(type='str'),
644 lun_maps=dict(type='list', elements='dict', options=dict(
645 igroup_name=dict(type='str'),
646 igroup_initiators=dict(type='list', elements='str'),
647 os_type=dict(type='str'),
648 )),
649 )),
650 namespaces=dict(type='list', elements='dict', options=dict(
651 name=dict(required=True, type='str'),
652 size=dict(type='int'),
653 size_unit=dict(type='str', choices=['bytes', 'b', 'kb', 'mb', 'gb', 'tb',
654 'pb', 'eb', 'zb', 'yb']),
655 os_type=dict(type='str'),
656 provisioning_options=dict(type='dict', options=dict(
657 action=dict(type='str', choices=['create']),
658 count=dict(type='int')
659 )),
660 )),
661 qos_policy=dict(type='str'),
662 snapshot_policy=dict(type='str'),
663 )),
664 qos_policy=dict(type='str'),
665 snapshot_policy=dict(type='str'),
666 ))
667 self.module = AnsibleModule(
668 argument_spec=self.argument_spec,
669 supports_check_mode=True
670 )
671 self.uuid = None
672 self.na_helper = NetAppModule(self.module)
673 self.parameters = self.na_helper.check_and_set_parameters(self.module)
674 self.rest_api = netapp_utils.OntapRestAPI(self.module)
675 self.rest_api.fail_if_not_rest_minimum_version('na_ontap_cg:', 9, 10, 1)
676 self.validate_options()
677 self.validate_size_parameters()
678
679 def validate_size_parameters(self):
680 def convert_size_parameters(source_params):
681 if 'volumes' in source_params and source_params.get('volumes'):
682 for volume in source_params['volumes']:
683 if volume.get('size') is not None:
684 volume['size'] *= netapp_utils.POW2_BYTE_MAP[volume.get('size_unit')]
685 volume.pop('size_unit')
686 if 'luns' in source_params and source_params.get('luns'):
687 for lun in source_params['luns']:
688 if lun.get('size') is not None:
689 lun['size'] *= netapp_utils.POW2_BYTE_MAP[lun.get('size_unit')]
690 lun.pop('size_unit')
691 if 'namespaces' in source_params and source_params.get('namespaces'):
692 for namespace in source_params['namespaces']:
693 if namespace.get('size') is not None:
694 namespace['size'] *= netapp_utils.POW2_BYTE_MAP[namespace.get('size_unit')]
695 namespace.pop('size_unit')
696
697 convert_size_parameters(self.parameters)
698 if 'consistency_groups' in self.parameters and self.parameters['consistency_groups']:
699 for cg in self.parameters['consistency_groups']:
700 convert_size_parameters(cg)
701
702 def validate_options(self):
703 """ validate ONTAP version supports the options provided in the parameters """
704 if 'consistency_groups' in self.parameters and self.parameters['consistency_groups']:
705 for cg in self.parameters['consistency_groups']:
706 if self.na_helper.safe_get(cg, ['provisioning_options', 'name']) and not self.rest_api.meets_rest_minimum_version(True, 9, 13, 1):
707 self.module.fail_json(msg="The 'name' option under 'provisioning_options' for consistency groups requires ONTAP 9.13.1 or later.")
708
709 def get_consistency_group_rest(self):
710 """ Retrieve details of a specific consistency group """
711 api = "application/consistency-groups"
712 params = {'name': self.parameters['name'],
713 'svm.name': self.parameters['vserver'],
714 'fields': 'name,'
715 'uuid,'}
716 if 'volumes' in self.parameters and self.parameters['volumes']:
717 params['fields'] += 'volumes,'
718 if 'luns' in self.parameters and self.parameters['luns']:
719 params['fields'] += 'luns,'
720 if 'namespaces' in self.parameters and self.parameters['namespaces']:
721 params['fields'] += 'namespaces,'
722 if 'consistency_groups' in self.parameters and self.parameters['consistency_groups']:
723 params['fields'] += 'consistency_groups,'
724 if 'qos_policy' in self.parameters:
725 params['fields'] += 'qos.policy.name,'
726 if 'snapshot_policy' in self.parameters:
727 params['fields'] += 'snapshot_policy.name,'
728 record, error = rest_generic.get_one_record(self.rest_api, api, params)
729 if error:
730 self.module.fail_json(msg="Error retrieving consistency group %s: %s" % (self.parameters['name'], to_native(error)),
731 exception=traceback.format_exc())
732 if record:
733 self.uuid = record['uuid']
734 volumes = self.format_volmues_info(record.get('volumes', []))
735 luns = self.format_luns_info(record.get('luns', []))
736 namespaces = self.format_namespaces_info(record.get('namespaces', []))
737 consistency_groups = self.format_consistency_groups_info(record.get('consistency_groups', []))
738 return {
739 'name': record.get('name'),
740 'qos_policy': self.na_helper.safe_get(record, ['qos', 'policy', 'name']),
741 'snapshot_policy': self.na_helper.safe_get(record, ['snapshot_policy', 'name']),
742 'volumes': volumes,
743 'luns': luns,
744 'namespaces': namespaces,
745 'consistency_groups': consistency_groups
746 }
747 return None
748
749 def format_volmues_info(self, volumes):
750 """ Formats the volumes information from the parent list provided """
751 volumes_info = []
752 for volume in volumes:
753 volume_info = {
754 'name': volume.get('name'),
755 'size': self.na_helper.safe_get(volume, ['space', 'size']),
756 'qos_policy': self.na_helper.safe_get(volume, ['qos', 'policy', 'name']),
757 'snapshot_policy': self.na_helper.safe_get(volume, ['snapshot_policy', 'name']),
758 }
759 volumes_info.append(volume_info)
760 return volumes_info
761
762 def format_luns_info(self, luns):
763 """ Formats the luns information from the parent list provided """
764 luns_info = []
765 for lun in luns:
766 lun_info = {
767 'name': lun.get('name'),
768 'size': self.na_helper.safe_get(lun, ['space', 'size']),
769 'os_type': lun.get('os_type'),
770 }
771 if self.na_helper.safe_get(lun, ['lun_maps']):
772 lun_info['lun_maps'] = []
773 for lun_map in lun['lun_maps']:
774 lun_map_info = {
775 'igroup_name': self.na_helper.safe_get(lun_map, ['igroup', 'name']),
776 'igroup_initiators': [initiator['name'] for initiator in self.na_helper.safe_get(lun_map, ['igroup', 'initiators'], [])],
777 'os_type': lun.get('os_type'),
778 }
779 lun_info['lun_maps'].append(lun_map_info)
780 luns_info.append(lun_info)
781 return luns_info
782
783 def format_namespaces_info(self, namespaces):
784 """ Formats the namespaces information from the parent list provided """
785 namespaces_info = []
786 for namespace in namespaces:
787 namespace_info = {
788 'name': namespace.get('name'),
789 'size': self.na_helper.safe_get(namespace, ['space', 'size']),
790 'os_type': namespace.get('os_type'),
791 }
792 namespaces_info.append(namespace_info)
793 return namespaces_info
794
795 def format_consistency_groups_info(self, consistency_groups):
796 """ Formats the consistency groups information """
797 consistency_groups_info = []
798 for cg in consistency_groups:
799 cg_info = {
800 'name': cg.get('name'),
801 'qos_policy': self.na_helper.safe_get(cg, ['qos', 'policy', 'name']),
802 'snapshot_policy': self.na_helper.safe_get(cg, ['snapshot_policy', 'name']),
803 }
804 if self.na_helper.safe_get(cg, ['volumes']):
805 cg_info['volumes'] = self.format_volmues_info(cg['volumes'])
806 if self.na_helper.safe_get(cg, ['luns']):
807 cg_info['luns'] = self.format_luns_info(cg['luns'])
808 if self.na_helper.safe_get(cg, ['namespaces']):
809 cg_info['namespaces'] = self.format_namespaces_info(cg['namespaces'])
810 consistency_groups_info.append(cg_info)
811 return consistency_groups_info
812
813 def create_consistency_group_rest(self):
814 """ Creates a consistency group """
815 api = "application/consistency-groups"
816 body = {
817 'name': self.parameters['name'],
818 'svm': {
819 'name': self.parameters['vserver']
820 }
821 }
822 if self.parameters.get('qos_policy') is not None:
823 body['qos.policy.name'] = self.parameters['qos_policy']
824 if self.parameters.get('snapshot_policy') is not None:
825 body['snapshot_policy.name'] = self.parameters['snapshot_policy']
826 if self.parameters.get('volumes'):
827 body['volumes'] = self.format_volumes_body(self.parameters['volumes'])
828 if self.parameters.get('luns'):
829 body['luns'] = self.format_luns_body(self.parameters['luns'])
830 if self.parameters.get('namespaces'):
831 body['namespaces'] = self.format_namespaces_body(self.parameters['namespaces'])
832 if self.parameters.get('consistency_groups'):
833 body['consistency_groups'] = self.format_consistency_groups_body(self.parameters['consistency_groups'])
834
835 dummy, error = rest_generic.post_async(self.rest_api, api, body=body)
836 if error:
837 self.module.fail_json(msg='Error creating consistency group %s: %s.' % (self.parameters['name'], to_native(error)),
838 exception=traceback.format_exc())
839
840 def format_volumes_body(self, volumes):
841 """ Formats the volumes body for create/modify operation """
842 volumes_body = []
843 for volume in volumes:
844 volume_body = {
845 'name': volume.get('name'),
846 }
847 if volume.get('size') is not None:
848 volume_body['space.size'] = volume['size']
849 if volume.get('qos_policy') is not None:
850 volume_body['qos.policy.name'] = volume.get('qos_policy')
851 if volume.get('snapshot_policy') is not None:
852 volume_body['snapshot_policy.name'] = volume.get('snapshot_policy')
853 if volume.get('provisioning_options'):
854 if volume['provisioning_options'].get('action') is not None:
855 volume_body['provisioning_options'] = {
856 'action': volume['provisioning_options']['action']
857 }
858 if volume['provisioning_options'].get('count') is not None:
859 volume_body['provisioning_options']['count'] = volume['provisioning_options']['count']
860 volumes_body.append(volume_body)
861 return volumes_body
862
863 def format_luns_body(self, luns):
864 """ Formats the luns body for create/modify operation """
865 luns_body = []
866 for lun in luns:
867 lun_body = {
868 'name': lun.get('name'),
869 }
870 if lun.get('size') is not None:
871 lun_body['space.size'] = lun['size']
872 if lun.get('provisioning_options'):
873 if lun['provisioning_options'].get('action') is not None:
874 lun_body['provisioning_options'] = {
875 'action': lun['provisioning_options']['action']
876 }
877 if lun['provisioning_options'].get('count') is not None:
878 lun_body['provisioning_options']['count'] = lun['provisioning_options']['count']
879 if lun.get('os_type') is not None:
880 lun_body['os_type'] = lun.get('os_type')
881 if lun.get('lun_maps') and lun.get('lun_maps') != []:
882 lun_body['lun_maps'] = []
883 for lun_map in lun['lun_maps']:
884 lun_map_body = {'igroup': {}}
885 if 'igroup_name' in lun_map:
886 lun_map_body['igroup']['name'] = lun_map['igroup_name']
887 if 'igroup_initiators' in lun_map:
888 lun_map_body['igroup']['initiators'] = lun_map['igroup_initiators']
889 if 'os_type' in lun_map:
890 lun_map_body['igroup']['os_type'] = lun_map['os_type']
891 lun_body['lun_maps'].append(lun_map_body)
892 luns_body.append(lun_body)
893 return luns_body
894
895 def format_namespaces_body(self, namespaces):
896 """ Formats the namespaces body for create/modify operation """
897 namespaces_body = []
898 for namespace in namespaces:
899 namespace_body = {
900 'name': namespace.get('name'),
901 }
902 if namespace.get('size') is not None:
903 namespace_body['space.size'] = namespace['size']
904 if namespace.get('os_type') is not None:
905 namespace_body['os_type'] = namespace.get('os_type')
906 if namespace.get('provisioning_options'):
907 if namespace['provisioning_options'].get('action') is not None:
908 namespace_body['provisioning_options'] = {
909 'action': namespace['provisioning_options']['action']
910 }
911 if namespace['provisioning_options'].get('count') is not None:
912 namespace_body['provisioning_options']['count'] = namespace['provisioning_options']['count']
913 namespaces_body.append(namespace_body)
914 return namespaces_body
915
916 def format_consistency_groups_body(self, consistency_groups):
917 """ Formats the consistency groups body for create/modify operation """
918 consistency_groups_body = []
919 for cg in consistency_groups:
920 cg_body = {
921 'name': cg.get('name'),
922 }
923 if cg.get('qos_policy') is not None:
924 cg_body['qos.policy.name'] = cg.get('qos_policy')
925 if cg.get('snapshot_policy') is not None:
926 cg_body['snapshot_policy.name'] = cg.get('snapshot_policy')
927 if cg.get('provisioning_options'):
928 if cg['provisioning_options'].get('action') is not None:
929 cg_body['provisioning_options'] = {
930 'action': cg['provisioning_options']['action']
931 }
932 if cg['provisioning_options'].get('name') is not None:
933 cg_body['provisioning_options']['name'] = cg['provisioning_options']['name']
934 if cg.get('volumes'):
935 cg_body['volumes'] = self.format_volumes_body(cg['volumes'])
936 if cg.get('luns'):
937 cg_body['luns'] = self.format_luns_body(cg['luns'])
938 if cg.get('namespaces'):
939 cg_body['namespaces'] = self.format_namespaces_body(cg['namespaces'])
940 consistency_groups_body.append(cg_body)
941 return consistency_groups_body
942
943 def modify_consistency_group_rest(self, modify):
944 """ Modifies a consistency group """
945 api = "application/consistency-groups"
946 dummy, error = rest_generic.patch_async(self.rest_api, api, uuid_or_name=self.uuid, body=modify)
947 if error:
948 self.module.fail_json(msg='Error modifying consistency group %s: %s' % (self.parameters['name'], to_native(error)),
949 exception=traceback.format_exc())
950 return
951
952 def set_modify_dict(self, current, modify):
953 """ Sets the modify dict for update operation """
954 modify_dict = {}
955 if 'snapshot_policy' in modify:
956 modify_dict['snapshot_policy.name'] = modify['snapshot_policy']
957
958 # volumes
959 if self.parameters.get('volumes') and self.parameters.get('volumes') != []:
960 modify_volumes = self.set_volumes_modify_list(current.get('volumes', []),
961 self.na_helper.filter_out_none_entries(self.parameters.get('volumes', [])))
962 if modify_volumes and modify_volumes != []:
963 modify_dict['volumes'] = self.format_volumes_body(modify_volumes)
964 # luns
965 if self.parameters.get('luns') and self.parameters.get('luns') != []:
966 modify_luns = self.set_luns_modify_list(current.get('luns', []),
967 self.na_helper.filter_out_none_entries(self.parameters.get('luns', [])))
968 if modify_luns and modify_luns != []:
969 modify_dict['luns'] = self.format_luns_body(modify_luns)
970 # namespaces
971 if self.parameters.get('namespaces') and self.parameters.get('namespaces') != []:
972 modify_namespaces = self.set_namespaces_modify_list(current.get('namespaces', []),
973 self.na_helper.filter_out_none_entries(self.parameters.get('namespaces', [])))
974 if modify_namespaces and modify_namespaces != []:
975 modify_dict['namespaces'] = self.format_namespaces_body(modify_namespaces)
976 # consistyency groups
977 if self.parameters.get('consistency_groups') and self.parameters.get('consistency_groups') != []:
978 modify_cgs = []
979 current_cgs_dict = {cg['name']: cg for cg in current.get('consistency_groups', [])}
980 for cg in self.parameters['consistency_groups']:
981 if cg.get('provisioning_options'):
982 if cg['provisioning_options'].get('action') in ('create', 'add'):
983 if cg.get('name') in current_cgs_dict:
984 cg.pop('provisioning_options')
985 cg_modify = self.set_cgs_modify_list(cg, current_cgs_dict)
986 if cg_modify:
987 cg_modify['name'] = cg['name']
988 modify_cgs.append(cg_modify)
989 else:
990 modify_cgs.append(self.na_helper.filter_out_none_entries(cg))
991 elif cg['provisioning_options'].get('action') == 'remove':
992 if cg.get('name') in current_cgs_dict.keys():
993 modify_cg = {'name': cg['name'], 'provisioning_options': {'action': 'remove'}}
994 if self.na_helper.safe_get(cg, ['provisioning_options', 'name']) is not None:
995 modify_cg['provisioning_options']['name'] = cg['provisioning_options']['name']
996 modify_cgs.append(modify_cg)
997
998 if modify_cgs and modify_cgs != []:
999 cgs_to_be_modified = [cg['name'] for cg in modify_cgs]
1000 # add existing elements to the patch body
1001 for current_cg in current_cgs_dict.values():
1002 if current_cg['name'] not in cgs_to_be_modified:
1003 modify_cgs.append({'name': current_cg['name']})
1004 modify_dict['consistency_groups'] = self.format_consistency_groups_body(modify_cgs)
1005
1006 return modify_dict if modify_dict else None
1007
1008 def set_cgs_modify_list(self, cg, current_cgs_dict):
1009 """ Formats the modify dict for given consistency groups list
1010 "consistency-groups" does not support removing elements via PATCH.
1011 All existing elements with any changes must be included in the list during PATCH. """
1012 cg_modify_dict = {}
1013 if cg.get('snapshot_policy') and cg.get('snapshot_policy') != current_cgs_dict[cg['name']].get('snapshot_policy'):
1014 cg_modify_dict['snapshot_policy'] = cg.get('snapshot_policy')
1015 if cg.get('volumes') and cg.get('volumes') != []:
1016 cg_modify_volumes = self.set_volumes_modify_list(
1017 current_cgs_dict[cg['name']].get('volumes', []), self.na_helper.filter_out_none_entries(cg.get('volumes', [])))
1018 if cg_modify_volumes and cg_modify_volumes != []:
1019 cg_modify_dict['volumes'] = cg_modify_volumes
1020 if cg.get('luns') and cg.get('luns') != []:
1021 cg_modify_luns = self.set_luns_modify_list(
1022 current_cgs_dict[cg['name']].get('luns', []), self.na_helper.filter_out_none_entries(cg.get('luns', [])))
1023 if cg_modify_luns and cg_modify_luns != []:
1024 cg_modify_dict['luns'] = cg_modify_luns
1025 if cg.get('namespaces') and cg.get('namespaces') != []:
1026 cg_modify_namespaces = self.set_namespaces_modify_list(
1027 current_cgs_dict[cg['name']].get('namespaces', []), self.na_helper.filter_out_none_entries(cg.get('namespaces', [])))
1028 if cg_modify_namespaces and cg_modify_namespaces != []:
1029 cg_modify_dict['namespaces'] = cg_modify_namespaces
1030 return cg_modify_dict if cg_modify_dict else None
1031
1032 def set_volumes_modify_list(self, current_volumes, source_volumes):
1033 """ Formats the modify dict for given volumes list
1034 "volumes" does not support removing elements via PATCH.
1035 All existing elements with any changes must be included in the list during PATCH. """
1036 current_volumes_dict = {volume['name']: volume for volume in current_volumes}
1037 filtered_source_volumes = copy.deepcopy(source_volumes)
1038 volumes_modify = []
1039 for volume in source_volumes:
1040 if volume.get('provisioning_options'):
1041 if volume['provisioning_options'].get('action') in ('create', 'add'):
1042 if volume.get('name') in current_volumes_dict:
1043 volume.pop('provisioning_options')
1044 volume_modify = self.na_helper.get_modified_attributes(current_volumes_dict[volume['name']], volume)
1045 if volume_modify:
1046 volume_modify['name'] = volume['name']
1047 volumes_modify.append(volume_modify)
1048 else:
1049 volumes_modify.append(volume)
1050 elif volume['provisioning_options'].get('action') == 'remove':
1051 if volume.get('name') not in current_volumes_dict:
1052 filtered_source_volumes.remove(volume)
1053 else:
1054 volumes_modify.append(volume)
1055 if volumes_modify:
1056 volumes_to_be_modified = [volume['name'] for volume in volumes_modify]
1057 # add existing elements to the patch body
1058 for current_vol in current_volumes:
1059 if current_vol['name'] not in volumes_to_be_modified:
1060 volumes_modify.append({'name': current_vol['name']})
1061 return volumes_modify if volumes_modify else None
1062
1063 def set_luns_modify_list(self, current_luns, source_luns):
1064 """ Formats the modify dict for given luns list
1065 "luns" does not support removing elements via PATCH.
1066 All existing elements with any changes must be included in the list during PATCH. """
1067 current_luns_dict = {lun['name']: lun for lun in current_luns}
1068 luns_modify = []
1069 for lun in source_luns:
1070 if lun.get('provisioning_options'):
1071 if lun['provisioning_options'].get('action') == 'create':
1072 if lun.get('name') in current_luns_dict:
1073 lun.pop('provisioning_options')
1074 lun_modify = self.na_helper.get_modified_attributes(current_luns_dict[lun['name']], lun)
1075 if lun_modify:
1076 lun_modify['name'] = lun['name']
1077 luns_modify.append(lun_modify)
1078 else:
1079 luns_modify.append(lun)
1080 if luns_modify:
1081 luns_to_be_modified = [lun['name'] for lun in luns_modify]
1082 # add existing elements to the patch body
1083 for current_lun in current_luns:
1084 if current_lun['name'] not in luns_to_be_modified:
1085 luns_modify.append({'name': current_lun['name']})
1086 return luns_modify if luns_modify else None
1087
1088 def set_namespaces_modify_list(self, current_namespaces, source_namespaces):
1089 """ Formats the modify dict for given namespaces list
1090 "namespaces" does not support removing elements via PATCH.
1091 All existing elements with any changes must be included in the list during PATCH. """
1092 current_namespaces_dict = {namespace['name']: namespace for namespace in current_namespaces}
1093 namespaces_modify = []
1094 for namespace in source_namespaces:
1095 if namespace.get('provisioning_options'):
1096 if namespace['provisioning_options'].get('action') == 'create':
1097 if namespace.get('name') in current_namespaces_dict:
1098 namespace.pop('provisioning_options')
1099 namespace_modify = self.na_helper.get_modified_attributes(current_namespaces_dict[namespace['name']], namespace)
1100 if namespace_modify:
1101 namespace_modify['name'] = namespace['name']
1102 namespaces_modify.append(namespace_modify)
1103 else:
1104 namespaces_modify.append(namespace)
1105 if namespaces_modify:
1106 namespaces_to_be_modified = [namespace['name'] for namespace in namespaces_modify]
1107 # add existing elements to the patch body
1108 for current_namespace in current_namespaces:
1109 if current_namespace['name'] not in namespaces_to_be_modified:
1110 namespaces_modify.append({'name': current_namespace['name']})
1111 return namespaces_modify if namespaces_modify else None
1112
1113 def delete_consistency_group_rest(self):
1114 api = "application/consistency-groups"
1115 dummy, error = rest_generic.delete_async(self.rest_api, api, self.uuid)
1116 if error:
1117 self.module.fail_json(msg='Error deleting consistency group %s: %s' % (self.parameters['name'], to_native(error)),
1118 exception=traceback.format_exc())
1119
1120 def apply(self):
1121 cd_action, modify, validated_modify = None, None, None
1122 current = self.get_consistency_group_rest()
1123 cd_action = self.na_helper.get_cd_action(current, self.parameters)
1124 if cd_action is None:
1125 modify = self.na_helper.get_modified_attributes(current, self.parameters)
1126
1127 if self.na_helper.changed and not self.module.check_mode:
1128 if cd_action == 'create':
1129 self.create_consistency_group_rest()
1130 if cd_action == 'delete':
1131 self.delete_consistency_group_rest()
1132 if modify:
1133 validated_modify = self.set_modify_dict(current, modify)
1134 if not validated_modify:
1135 self.na_helper.changed = False
1136 else:
1137 self.modify_consistency_group_rest(validated_modify)
1138 result = netapp_utils.generate_result(self.na_helper.changed, cd_action, validated_modify)
1139 self.module.exit_json(**result)
1140
1141
1142 def main():
1143 consistency_group = NetAppOntapConsistencyGroup()
1144 consistency_group.apply()
1145
1146
1147 if __name__ == '__main__':
1148 main()