Debian Codegraph @

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

raw
1 #!/usr/bin/python
2
3 # (c) 2018-2025, NetApp, Inc
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 '''
7 na_ontap_aggregate
8 '''
9
10 from __future__ import absolute_import, division, print_function
11 __metaclass__ = type
12
13 DOCUMENTATION = '''
14
15 module: na_ontap_aggregate
16 short_description: NetApp ONTAP manage aggregates.
17 extends_documentation_fragment:
18 - netapp.ontap.netapp.na_ontap
19 version_added: 2.6.0
20 author: NetApp Ansible Team (@carchi8py) <[email protected]>
21
22 description:
23 - Create, delete, or manage aggregates on ONTAP.
24
25 options:
26
27 state:
28 description:
29 - Whether the specified aggregate should exist or not.
30 choices: ['present', 'absent']
31 default: 'present'
32 type: str
33
34 service_state:
35 description:
36 - Whether the specified aggregate should be enabled or disabled. Creates aggregate if doesnt exist.
37 - Supported from 9.11.1 or later in REST.
38 choices: ['online', 'offline']
39 type: str
40
41 name:
42 description:
43 - The name of the aggregate to manage.
44 required: true
45 type: str
46
47 from_name:
48 description:
49 - Name of the aggregate to be renamed.
50 type: str
51 version_added: 2.7.0
52
53 nodes:
54 description:
55 - Node(s) for the aggregate to be created on. If no node specified, mgmt lif home will be used.
56 - ZAPI only - if multiple nodes specified an aggr stripe will be made.
57 - With REST, only one node can be specified. If disk_count is present, node name is required.
58 type: list
59 elements: str
60
61 disk_type:
62 description:
63 - Type of disk to use to build aggregate.
64 - Not supported with REST - see C(disk_class).
65 - SSD-NVM, SSD-CAP were added with ONTAP 9.6.
66 - VMLUN was added with ONTAP 9.9.
67 choices: ['ATA', 'BSAS', 'FCAL', 'FSAS', 'LUN', 'MSATA', 'SAS', 'SSD', 'SSD-CAP', 'SSD-NVM', 'VMDISK', 'VMLUN', 'VMLUN-SSD']
68 type: str
69 version_added: 2.7.0
70
71 disk_class:
72 description:
73 - Class of disk to use to build aggregate.
74 - C(capacity_flash) is listed in swagger, but rejected as invalid by ONTAP.
75 choices: ['capacity', 'performance', 'archive', 'solid_state', 'array', 'virtual', 'data_center', 'capacity_flash']
76 type: str
77 version_added: 21.16.0
78
79 disk_count:
80 description:
81 - Number of disks to place into the aggregate, including parity disks.
82 - The disks in this newly-created aggregate come from the spare disk pool.
83 - The smallest disks in this pool join the aggregate first, unless the C(disk-size) argument is provided.
84 - Either C(disk-count) or C(disks) must be supplied. Range [0..2^31-1].
85 - Required when C(state=present).
86 - Modifiable only if specified disk_count is larger than current disk_count.
87 - Cannot create raidgroup with 1 disk when using raid type raid4.
88 - If the disk_count % raid_size == 1, only disk_count/raid_size * raid_size will be added.
89 - If disk_count is 6, raid_type is raid4, raid_size 4, all 6 disks will be added.
90 - If disk_count is 5, raid_type is raid4, raid_size 4, 5/4 * 4 = 4 will be added. 1 will not be added.
91 - With REST, C(nodes) is required if C(disk_count) is present.
92 type: int
93
94 disk_size:
95 description:
96 - Disk size to use in 4K block size. Disks within 10% of specified size will be used.
97 - With REST, this is converted to bytes using 4096. Use C(disk_size_with_unit) to skip the conversion.
98 type: int
99 version_added: 2.7.0
100
101 disk_size_with_unit:
102 description:
103 - Disk size to use in the specified unit.
104 - It is a positive integer number followed by unit of T/G/M/K. For example, 72G, 1T and 32M.
105 - Or the unit can be omitted for bytes (REST also accepts B).
106 - This option is ignored if a specific list of disks is specified through the "disks" parameter.
107 - You must only use one of either "disk-size" or "disk-size-with-unit" parameters.
108 - With REST, this is converted to bytes, assuming K=1024.
109 type: str
110
111 raid_size:
112 description:
113 - Sets the maximum number of drives per raid group.
114 type: int
115 version_added: 2.7.0
116
117 raid_type:
118 description:
119 - Specifies the type of RAID groups to use in the new aggregate.
120 - raid_0 is only available on ONTAP Select.
121 choices: ['raid4', 'raid_dp', 'raid_tec', 'raid_0']
122 type: str
123 version_added: 2.7.0
124
125 unmount_volumes:
126 description:
127 - If set to "true", this option specifies that all of the volumes hosted by the given aggregate are to be unmounted
128 before the offline operation is executed.
129 - By default, the system will reject any attempt to offline an aggregate that hosts one or more online volumes.
130 - Not supported with REST, by default REST unmount volumes when trying to offline aggregate.
131 type: bool
132
133 disks:
134 description:
135 - Specific list of disks to use for the new aggregate.
136 - To create a "mirrored" aggregate with a specific list of disks, both 'disks' and 'mirror_disks' options must be supplied.
137 Additionally, the same number of disks must be supplied in both lists.
138 - Not supported with REST.
139 type: list
140 elements: str
141 version_added: 2.8.0
142
143 is_mirrored:
144 description:
145 - Specifies that the new aggregate be mirrored (have two plexes).
146 - If set to true, then the indicated disks will be split across the two plexes. By default, the new aggregate will not be mirrored.
147 - This option cannot be used when a specific list of disks is supplied with either the 'disks' or 'mirror_disks' options.
148 type: bool
149 version_added: 2.8.0
150
151 mirror_disks:
152 description:
153 - List of mirror disks to use. It must contain the same number of disks specified in 'disks'.
154 - Not supported with REST.
155 type: list
156 elements: str
157 version_added: 2.8.0
158
159 spare_pool:
160 description:
161 - Specifies the spare pool from which to select spare disks to use in creation of a new aggregate.
162 - Not supported with REST.
163 choices: ['Pool0', 'Pool1']
164 type: str
165 version_added: 2.8.0
166
167 wait_for_online:
168 description:
169 - Set this parameter to 'true' for synchronous execution during create (wait until aggregate status is online).
170 - Set this parameter to 'false' for asynchronous execution.
171 - For asynchronous, execution exits as soon as the request is sent, without checking aggregate status.
172 - Ignored with REST (always wait).
173 type: bool
174 default: false
175 version_added: 2.8.0
176
177 time_out:
178 description:
179 - time to wait for aggregate creation in seconds.
180 - default is set to 100 seconds.
181 type: int
182 default: 100
183 version_added: 2.8.0
184
185 object_store_name:
186 description:
187 - Name of the object store configuration attached to the aggregate.
188 type: str
189 version_added: 2.9.0
190
191 allow_flexgroups:
192 description:
193 - This optional parameter allows attaching object store to an aggregate containing FlexGroup constituents. The default value is false.
194 - Mixing FabricPools and non-FabricPools within a FlexGroup is not recommended.
195 - All aggregates hosting constituents of a FlexGroup should be attached to the object store.
196 type: bool
197 version_added: 22.3.0
198
199 snaplock_type:
200 description:
201 - Type of snaplock for the aggregate being created.
202 choices: ['compliance', 'enterprise', 'non_snaplock']
203 type: str
204 version_added: 20.1.0
205
206 ignore_pool_checks:
207 description:
208 - only valid when I(disks) option is used.
209 - disks in a plex should belong to the same spare pool, and mirror disks to another spare pool.
210 - when set to true, these checks are ignored.
211 - Ignored with REST as I(disks) is not supported.
212 type: bool
213 version_added: 20.8.0
214
215 encryption:
216 description:
217 - whether to enable software encryption.
218 - this is equivalent to -encrypt-with-aggr-key when using the CLI.
219 - requires a VE license.
220 type: bool
221 version_added: 21.14.0
222
223 tags:
224 description:
225 - Tags are an optional way to track the uses of a resource.
226 - Tag values must be formatted as key:value strings, example ["team:csi", "environment:test"]
227 type: list
228 elements: str
229 version_added: 22.6.0
230
231 lambda_config:
232 description:
233 - Configuration parameters for AWS Lambda proxy functionality.
234 - These option and suboptions are only supported with REST.
235 type: dict
236 version_added: 23.2.0
237 suboptions:
238 function_name:
239 description:
240 - The name of the AWS Lambda function to invoke.
241 type: str
242 required: true
243 aws_region:
244 description:
245 - The name of the AWS region.
246 type: str
247 required: true
248 aws_profile:
249 description:
250 - The name of the AWS profile to use for authentication.
251 type: str
252
253 notes:
254 - Supports check_mode.
255 - Supports both ZAPI and REST.
256 - Supports AWS Lambda proxy functionality when using REST. See README for example usage.
257
258 '''
259
260 EXAMPLES = """
261 - name: Create Aggregates and wait 5 minutes until aggregate is online in ZAPI.
262 netapp.ontap.na_ontap_aggregate:
263 state: present
264 service_state: online
265 name: ansibleAggr
266 disk_count: 10
267 wait_for_online: true
268 time_out: 300
269 snaplock_type: non_snaplock
270 use_rest: never
271 hostname: "{{ netapp_hostname }}"
272 username: "{{ netapp_username }}"
273 password: "{{ netapp_password }}"
274
275 - name: Create Aggregates in REST.
276 netapp.ontap.na_ontap_aggregate:
277 state: present
278 service_state: online
279 name: ansibleAggr
280 disk_count: 10
281 nodes: ontap-node
282 snaplock_type: non_snaplock
283 use_rest: always
284 hostname: "{{ netapp_hostname }}"
285 username: "{{ netapp_username }}"
286 password: "{{ netapp_password }}"
287
288 - name: Manage Aggregates in ZAPI, modify service state.
289 netapp.ontap.na_ontap_aggregate:
290 state: present
291 service_state: offline
292 unmount_volumes: true
293 name: ansibleAggr
294 disk_count: 10
295 use_rest: never
296 hostname: "{{ netapp_hostname }}"
297 username: "{{ netapp_username }}"
298 password: "{{ netapp_password }}"
299
300 - name: Manage Aggregates in REST, increase disk count.
301 netapp.ontap.na_ontap_aggregate:
302 state: present
303 name: ansibleAggr
304 disk_count: 20
305 nodes: ontap-node
306 hostname: "{{ netapp_hostname }}"
307 username: "{{ netapp_username }}"
308 password: "{{ netapp_password }}"
309
310 - name: Attach object store
311 netapp.ontap.na_ontap_aggregate:
312 state: present
313 name: aggr4
314 object_store_name: sgws_305
315 hostname: "{{ netapp_hostname }}"
316 username: "{{ netapp_username }}"
317 password: "{{ netapp_password }}"
318
319 - name: Rename Aggregates
320 netapp.ontap.na_ontap_aggregate:
321 state: present
322 service_state: online
323 from_name: ansibleAggr
324 name: ansibleAggr2
325 disk_count: 20
326 hostname: "{{ netapp_hostname }}"
327 username: "{{ netapp_username }}"
328 password: "{{ netapp_password }}"
329
330 - name: Delete Aggregates
331 netapp.ontap.na_ontap_aggregate:
332 state: absent
333 service_state: offline
334 unmount_volumes: true
335 name: ansibleAggr
336 hostname: "{{ netapp_hostname }}"
337 username: "{{ netapp_username }}"
338 password: "{{ netapp_password }}"
339 """
340
341 RETURN = """
342
343 """
344 import re
345 import time
346 import traceback
347
348 from ansible.module_utils.basic import AnsibleModule
349 from ansible.module_utils._text import to_native
350 import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils
351 from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule
352 from ansible_collections.netapp.ontap.plugins.module_utils.netapp import OntapRestAPI
353 from ansible_collections.netapp.ontap.plugins.module_utils import rest_generic
354 import ansible_collections.netapp.ontap.plugins.module_utils.rest_response_helpers as rrh
355
356
357 class NetAppOntapAggregate:
358 ''' object initialize and class methods '''
359
360 def __init__(self):
361 self.argument_spec = netapp_utils.na_ontap_host_argument_spec()
362 self.argument_spec.update(dict(
363 name=dict(required=True, type='str'),
364 disks=dict(required=False, type='list', elements='str'),
365 disk_count=dict(required=False, type='int', default=None),
366 disk_size=dict(required=False, type='int'),
367 disk_size_with_unit=dict(required=False, type='str'),
368 disk_class=dict(required=False,
369 choices=['capacity', 'performance', 'archive', 'solid_state', 'array', 'virtual', 'data_center', 'capacity_flash']),
370 disk_type=dict(required=False,
371 choices=['ATA', 'BSAS', 'FCAL', 'FSAS', 'LUN', 'MSATA', 'SAS', 'SSD', 'SSD-CAP', 'SSD-NVM', 'VMDISK', 'VMLUN', 'VMLUN-SSD']),
372 from_name=dict(required=False, type='str'),
373 mirror_disks=dict(required=False, type='list', elements='str'),
374 nodes=dict(required=False, type='list', elements='str'),
375 is_mirrored=dict(required=False, type='bool'),
376 raid_size=dict(required=False, type='int'),
377 raid_type=dict(required=False, choices=['raid4', 'raid_dp', 'raid_tec', 'raid_0']),
378 service_state=dict(required=False, choices=['online', 'offline']),
379 spare_pool=dict(required=False, choices=['Pool0', 'Pool1']),
380 state=dict(required=False, choices=['present', 'absent'], default='present'),
381 unmount_volumes=dict(required=False, type='bool'),
382 wait_for_online=dict(required=False, type='bool', default=False),
383 time_out=dict(required=False, type='int', default=100),
384 object_store_name=dict(required=False, type='str'),
385 allow_flexgroups=dict(required=False, type='bool'),
386 snaplock_type=dict(required=False, type='str', choices=['compliance', 'enterprise', 'non_snaplock']),
387 ignore_pool_checks=dict(required=False, type='bool'),
388 encryption=dict(required=False, type='bool'),
389 tags=dict(required=False, type='list', elements='str')
390 ))
391 self.argument_spec.update(netapp_utils.na_ontap_lambda_argument_spec())
392
393 self.module = AnsibleModule(
394 argument_spec=self.argument_spec,
395 mutually_exclusive=[
396 ('is_mirrored', 'disks'),
397 ('is_mirrored', 'mirror_disks'),
398 ('is_mirrored', 'spare_pool'),
399 ('spare_pool', 'disks'),
400 ('disk_count', 'disks'),
401 ('disk_size', 'disk_size_with_unit'),
402 ('disk_class', 'disk_type'),
403 ],
404 required_if=[
405 ['use_lambda', True, ('lambda_config',)]
406 ],
407 supports_check_mode=True
408 )
409 self.na_helper = NetAppModule()
410 self.parameters = self.na_helper.set_parameters(self.module.params)
411 self.rest_api = OntapRestAPI(self.module)
412 self.uuid = None
413 # some attributes are not supported in earlier REST implementation
414 unsupported_rest_properties = ['disks', 'disk_type', 'mirror_disks', 'spare_pool', 'unmount_volumes']
415 partially_supported_rest_properties = [['service_state', (9, 11, 1)], ['tags', (9, 13, 1)]]
416 self.use_rest = self.rest_api.is_rest_supported_properties(self.parameters, unsupported_rest_properties, partially_supported_rest_properties)
417 if not self.use_rest:
418 if self.parameters.get('use_lambda'):
419 self.module.fail_json(msg="Error: AWS Lambda proxy for ONTAP APIs is only supported with REST.")
420 if not netapp_utils.has_netapp_lib():
421 self.module.fail_json(msg=netapp_utils.netapp_lib_is_required())
422 if 'tags' in self.parameters:
423 self.module.fail_json(msg="Error: tags only supported with REST.")
424 self.server = netapp_utils.setup_na_ontap_zapi(module=self.module)
425
426 if self.parameters['state'] == 'present':
427 self.validate_options()
428
429 def validate_options(self):
430 errors = []
431 if self.use_rest:
432 if len(self.parameters.get('nodes', [])) > 1:
433 errors.append('only one node can be specified when using rest, found %s' % self.parameters['nodes'])
434 if 'disk_count' in self.parameters and 'nodes' not in self.parameters:
435 errors.append('nodes is required when disk_count is present')
436 else:
437 if self.parameters.get('mirror_disks') is not None and self.parameters.get('disks') is None:
438 errors.append('mirror_disks require disks options to be set')
439 if errors:
440 plural = 's' if len(errors) > 1 else ''
441 self.module.fail_json(msg='Error%s when validating options: %s.' % (plural, '; '.join(errors)))
442
443 def aggr_get_iter(self, name):
444 """
445 Return aggr-get-iter query results
446 :param name: Name of the aggregate
447 :return: NaElement if aggregate found, None otherwise
448 """
449
450 aggr_get_iter = netapp_utils.zapi.NaElement('aggr-get-iter')
451 query_details = netapp_utils.zapi.NaElement.create_node_with_children(
452 'aggr-attributes', **{'aggregate-name': name})
453 query = netapp_utils.zapi.NaElement('query')
454 query.add_child_elem(query_details)
455 aggr_get_iter.add_child_elem(query)
456 result = None
457 try:
458 result = self.server.invoke_successfully(aggr_get_iter, enable_tunneling=False)
459 except netapp_utils.zapi.NaApiError as error:
460 if to_native(error.code) != '13040':
461 self.module.fail_json(msg='Error getting aggregate: %s' % to_native(error), exception=traceback.format_exc())
462 return result
463
464 def get_aggr(self, name=None):
465 """
466 Fetch details if aggregate exists.
467 :param name: Name of the aggregate to be fetched
468 :return:
469 Dictionary of current details if aggregate found
470 None if aggregate is not found
471 """
472 if name is None:
473 name = self.parameters.get('name')
474 if self.use_rest:
475 return self.get_aggr_rest(name)
476 aggr_get = self.aggr_get_iter(name)
477 if aggr_get and aggr_get.get_child_by_name('num-records') and int(aggr_get.get_child_content('num-records')) >= 1:
478 attr = aggr_get.get_child_by_name('attributes-list').get_child_by_name('aggr-attributes')
479 current_aggr = {'service_state': attr.get_child_by_name('aggr-raid-attributes').get_child_content('state')}
480 if attr.get_child_by_name('aggr-raid-attributes').get_child_content('disk-count'):
481 current_aggr['disk_count'] = int(attr.get_child_by_name('aggr-raid-attributes').get_child_content('disk-count'))
482 if attr.get_child_by_name('aggr-raid-attributes').get_child_content('encrypt-with-aggr-key'):
483 current_aggr['encryption'] = attr.get_child_by_name('aggr-raid-attributes').get_child_content('encrypt-with-aggr-key') == 'true'
484 snaplock_type = self.na_helper.safe_get(attr, ['aggr-snaplock-attributes', 'snaplock-type'])
485 if snaplock_type:
486 current_aggr['snaplock_type'] = snaplock_type
487 return current_aggr
488 return None
489
490 def disk_get_iter(self, name):
491 """
492 Return storage-disk-get-iter query results
493 Filter disk list by aggregate name, and only reports disk-name and plex-name
494 :param name: Name of the aggregate
495 :return: NaElement
496 """
497
498 disk_get_iter = netapp_utils.zapi.NaElement('storage-disk-get-iter')
499 query_details = {
500 'query': {
501 'storage-disk-info': {
502 'disk-raid-info': {
503 'disk-aggregate-info': {
504 'aggregate-name': name
505 }
506 }
507 }
508 }
509 }
510 disk_get_iter.translate_struct(query_details)
511 attributes = {
512 'desired-attributes': {
513 'storage-disk-info': {
514 'disk-name': None,
515 'disk-raid-info': {
516 'disk_aggregate_info': {
517 'plex-name': None
518 }
519 }
520 }
521 }
522 }
523 disk_get_iter.translate_struct(attributes)
524
525 result = None
526 try:
527 result = self.server.invoke_successfully(disk_get_iter, enable_tunneling=False)
528 except netapp_utils.zapi.NaApiError as error:
529 self.module.fail_json(msg='Error getting disks: %s' % to_native(error), exception=traceback.format_exc())
530 return result
531
532 def get_aggr_disks(self, name):
533 """
534 Fetch disks that are used for this aggregate.
535 :param name: Name of the aggregate to be fetched
536 :return:
537 list of tuples (disk-name, plex-name)
538 empty list if aggregate is not found
539 """
540 disks = []
541 aggr_get = self.disk_get_iter(name)
542 if aggr_get and aggr_get.get_child_by_name('num-records') and int(aggr_get.get_child_content('num-records')) >= 1:
543 attr = aggr_get.get_child_by_name('attributes-list')
544 disks = [(disk_info.get_child_content('disk-name'),
545 disk_info.get_child_by_name('disk-raid-info').get_child_by_name('disk-aggregate-info').get_child_content('plex-name'))
546 for disk_info in attr.get_children()]
547 return disks
548
549 def object_store_get_iter(self, name):
550 """
551 Return aggr-object-store-get query results
552 :return: NaElement if object-store for given aggregate found, None otherwise
553 """
554
555 object_store_get_iter = netapp_utils.zapi.NaElement('aggr-object-store-get-iter')
556 query_details = netapp_utils.zapi.NaElement.create_node_with_children(
557 'object-store-information', **{'object-store-name': self.parameters.get('object_store_name'),
558 'aggregate': name})
559 query = netapp_utils.zapi.NaElement('query')
560 query.add_child_elem(query_details)
561 object_store_get_iter.add_child_elem(query)
562 result = None
563 try:
564 result = self.server.invoke_successfully(object_store_get_iter, enable_tunneling=False)
565 except netapp_utils.zapi.NaApiError as error:
566 self.module.fail_json(msg='Error getting object store: %s' % to_native(error), exception=traceback.format_exc())
567 return result
568
569 def get_object_store(self, name):
570 """
571 Fetch details if object store attached to the given aggregate exists.
572 :return:
573 Dictionary of current details if object store attached to the given aggregate is found
574 None if object store is not found
575 """
576 if self.use_rest:
577 return self.get_object_store_rest()
578 object_store_get = self.object_store_get_iter(name)
579 if object_store_get and object_store_get.get_child_by_name('num-records') and int(object_store_get.get_child_content('num-records')) >= 1:
580 attr = object_store_get.get_child_by_name('attributes-list').get_child_by_name('object-store-information')
581 return {'object_store_name': attr.get_child_content('object-store-name')}
582 return None
583
584 def aggregate_online(self):
585 """
586 Set state of an offline aggregate to online
587 :return: None
588 """
589 if self.use_rest:
590 return self.patch_aggr_rest('make service state online for', {'state': 'online'})
591 online_aggr = netapp_utils.zapi.NaElement.create_node_with_children(
592 'aggr-online', **{'aggregate': self.parameters['name'],
593 'force-online': 'true'})
594 try:
595 self.server.invoke_successfully(online_aggr,
596 enable_tunneling=True)
597 except netapp_utils.zapi.NaApiError as error:
598 self.module.fail_json(msg='Error changing the state of aggregate %s to %s: %s' %
599 (self.parameters['name'], self.parameters['service_state'], to_native(error)),
600 exception=traceback.format_exc())
601
602 def aggregate_offline(self):
603 """
604 Set state of an online aggregate to offline
605 :return: None
606 """
607 if self.use_rest:
608 return self.patch_aggr_rest('make service state offline for', {'state': 'offline'})
609 offline_aggr = netapp_utils.zapi.NaElement.create_node_with_children(
610 'aggr-offline', **{'aggregate': self.parameters['name'],
611 'force-offline': 'false',
612 'unmount-volumes': str(self.parameters.get('unmount_volumes', False))})
613
614 # if disk add operation is in progress, cannot offline aggregate, retry few times.
615 retry = 10
616 while retry > 0:
617 try:
618 self.server.invoke_successfully(offline_aggr, enable_tunneling=True)
619 break
620 except netapp_utils.zapi.NaApiError as error:
621 if 'disk add operation is in progress' in to_native(error):
622 retry -= 1
623 if retry > 0:
624 continue
625 self.module.fail_json(msg='Error changing the state of aggregate %s to %s: %s' %
626 (self.parameters['name'], self.parameters['service_state'], to_native(error)),
627 exception=traceback.format_exc())
628
629 @staticmethod
630 def get_disks_or_mirror_disks_object(name, disks):
631 '''
632 create ZAPI object for disks or mirror_disks
633 '''
634 disks_obj = netapp_utils.zapi.NaElement(name)
635 for disk in disks:
636 disk_info_obj = netapp_utils.zapi.NaElement('disk-info')
637 disk_info_obj.add_new_child('name', disk)
638 disks_obj.add_child_elem(disk_info_obj)
639 return disks_obj
640
641 def create_aggr(self):
642 """
643 Create aggregate
644 :return: None
645 """
646 if self.use_rest:
647 return self.create_aggr_rest()
648 options = {'aggregate': self.parameters['name']}
649 if self.parameters.get('disk_class'):
650 options['disk-class'] = self.parameters['disk_class']
651 if self.parameters.get('disk_type'):
652 options['disk-type'] = self.parameters['disk_type']
653 if self.parameters.get('raid_type'):
654 options['raid-type'] = self.parameters['raid_type']
655 if self.parameters.get('snaplock_type'):
656 options['snaplock-type'] = self.parameters['snaplock_type']
657 if self.parameters.get('spare_pool'):
658 options['spare-pool'] = self.parameters['spare_pool']
659 # int to str
660 if self.parameters.get('disk_count'):
661 options['disk-count'] = str(self.parameters['disk_count'])
662 if self.parameters.get('disk_size'):
663 options['disk-size'] = str(self.parameters['disk_size'])
664 if self.parameters.get('disk_size_with_unit'):
665 options['disk-size-with-unit'] = str(self.parameters['disk_size_with_unit'])
666 if self.parameters.get('raid_size'):
667 options['raid-size'] = str(self.parameters['raid_size'])
668 # boolean to str
669 if self.parameters.get('is_mirrored'):
670 options['is-mirrored'] = str(self.parameters['is_mirrored']).lower()
671 if self.parameters.get('ignore_pool_checks'):
672 options['ignore-pool-checks'] = str(self.parameters['ignore_pool_checks']).lower()
673 if self.parameters.get('encryption'):
674 options['encrypt-with-aggr-key'] = str(self.parameters['encryption']).lower()
675 aggr_create = netapp_utils.zapi.NaElement.create_node_with_children('aggr-create', **options)
676 if self.parameters.get('nodes'):
677 nodes_obj = netapp_utils.zapi.NaElement('nodes')
678 aggr_create.add_child_elem(nodes_obj)
679 for node in self.parameters['nodes']:
680 nodes_obj.add_new_child('node-name', node)
681 if self.parameters.get('disks'):
682 aggr_create.add_child_elem(self.get_disks_or_mirror_disks_object('disks', self.parameters.get('disks')))
683 if self.parameters.get('mirror_disks'):
684 aggr_create.add_child_elem(self.get_disks_or_mirror_disks_object('mirror-disks', self.parameters.get('mirror_disks')))
685
686 try:
687 self.server.invoke_successfully(aggr_create, enable_tunneling=False)
688 if self.parameters.get('wait_for_online'):
689 # round off time_out
690 retries = (self.parameters['time_out'] + 5) / 10
691 current = self.get_aggr()
692 status = None if current is None else current['service_state']
693 while status != 'online' and retries > 0:
694 time.sleep(10)
695 retries = retries - 1
696 current = self.get_aggr()
697 status = None if current is None else current['service_state']
698 else:
699 current = self.get_aggr()
700 if current is not None and current.get('disk_count') != self.parameters.get('disk_count'):
701 self.module.warn("Aggregate created with mismatched disk_count: created %s not %s"
702 % (current.get('disk_count'), self.parameters.get('disk_count')))
703 except netapp_utils.zapi.NaApiError as error:
704 self.module.fail_json(msg="Error provisioning aggregate %s: %s"
705 % (self.parameters['name'], to_native(error)),
706 exception=traceback.format_exc())
707
708 def delete_aggr(self):
709 """
710 Delete aggregate.
711 :return: None
712 """
713 if self.use_rest:
714 return self.delete_aggr_rest()
715 aggr_destroy = netapp_utils.zapi.NaElement.create_node_with_children(
716 'aggr-destroy', **{'aggregate': self.parameters['name']})
717
718 try:
719 self.server.invoke_successfully(aggr_destroy,
720 enable_tunneling=False)
721 except netapp_utils.zapi.NaApiError as error:
722 self.module.fail_json(msg="Error removing aggregate %s: %s" % (self.parameters['name'], to_native(error)),
723 exception=traceback.format_exc())
724
725 def rename_aggregate(self):
726 """
727 Rename aggregate.
728 """
729 if self.use_rest:
730 return self.rename_aggr_rest()
731 aggr_rename = netapp_utils.zapi.NaElement.create_node_with_children(
732 'aggr-rename', **{'aggregate': self.parameters['from_name'],
733 'new-aggregate-name': self.parameters['name']})
734
735 try:
736 self.server.invoke_successfully(aggr_rename, enable_tunneling=False)
737 except netapp_utils.zapi.NaApiError as error:
738 self.module.fail_json(msg="Error renaming aggregate %s: %s"
739 % (self.parameters['from_name'], to_native(error)),
740 exception=traceback.format_exc())
741
742 def modify_aggr(self, modify):
743 """
744 Modify state of the aggregate
745 :param modify: dictionary of parameters to be modified
746 :return: None
747 """
748 # online aggregate first, so disk can be added after online.
749 if modify.get('service_state') == 'online':
750 self.aggregate_online()
751 # modify tags
752 if modify.get('tags') is not None:
753 self.patch_aggr_rest('modify tags for', {'_tags': modify['tags']})
754 # add disk before taking aggregate offline.
755 disk_size = self.parameters.get('disk_size', 0)
756 disk_size_with_unit = self.parameters.get('disk_size_with_unit')
757 if modify.get('disk_count'):
758 self.add_disks(modify['disk_count'], disk_size=disk_size, disk_size_with_unit=disk_size_with_unit)
759 if modify.get('disks_to_add') or modify.get('mirror_disks_to_add'):
760 self.add_disks(0, modify.get('disks_to_add'), modify.get('mirror_disks_to_add'))
761 # offline aggregate after adding additional disks.
762 if modify.get('service_state') == 'offline':
763 self.aggregate_offline()
764 if modify.get('raid_type'):
765 self.patch_aggr_rest('modify', {'block_storage': {'primary': {'raid_type': modify['raid_type']}}})
766 if modify.get('encryption') is not None:
767 self.patch_aggr_rest('modify encryption for', {'data_encryption': {'software_encryption_enabled': modify['encryption']}})
768
769 def attach_object_store_to_aggr(self):
770 """
771 Attach object store to aggregate.
772 :return: None
773 """
774 if self.use_rest:
775 return self.attach_object_store_to_aggr_rest()
776 store_obj = {'aggregate': self.parameters['name'], 'object-store-name': self.parameters['object_store_name']}
777 if 'allow_flexgroups' in self.parameters:
778 store_obj['allow-flexgroup'] = self.na_helper.get_value_for_bool(False, self.parameters['allow_flexgroups'])
779 attach_object_store = netapp_utils.zapi.NaElement.create_node_with_children('aggr-object-store-attach', **store_obj)
780
781 try:
782 self.server.invoke_successfully(attach_object_store,
783 enable_tunneling=False)
784 except netapp_utils.zapi.NaApiError as error:
785 self.module.fail_json(msg="Error attaching object store %s to aggregate %s: %s" %
786 (self.parameters['object_store_name'], self.parameters['name'], to_native(error)),
787 exception=traceback.format_exc())
788
789 def add_disks(self, count=0, disks=None, mirror_disks=None, disk_size=0, disk_size_with_unit=None):
790 """
791 Add additional disks to aggregate.
792 :return: None
793 """
794 if self.use_rest:
795 return self.add_disks_rest(count, disks, mirror_disks, disk_size, disk_size_with_unit)
796 options = {'aggregate': self.parameters['name']}
797 if count:
798 options['disk-count'] = str(count)
799 if disks and self.parameters.get('ignore_pool_checks'):
800 options['ignore-pool-checks'] = str(self.parameters['ignore_pool_checks'])
801 if disk_size:
802 options['disk-size'] = str(disk_size)
803 if disk_size_with_unit:
804 options['disk-size-with-unit'] = disk_size_with_unit
805 if self.parameters.get('disk_class'):
806 options['disk-class'] = self.parameters['disk_class']
807 if self.parameters.get('disk_type'):
808 options['disk-type'] = self.parameters['disk_type']
809 aggr_add = netapp_utils.zapi.NaElement.create_node_with_children(
810 'aggr-add', **options)
811 if disks:
812 aggr_add.add_child_elem(self.get_disks_or_mirror_disks_object('disks', disks))
813 if mirror_disks:
814 aggr_add.add_child_elem(self.get_disks_or_mirror_disks_object('mirror-disks', mirror_disks))
815
816 try:
817 self.server.invoke_successfully(aggr_add,
818 enable_tunneling=True)
819 except netapp_utils.zapi.NaApiError as error:
820 self.module.fail_json(msg='Error adding additional disks to aggregate %s: %s' %
821 (self.parameters['name'], to_native(error)),
822 exception=traceback.format_exc())
823
824 def map_plex_to_primary_and_mirror(self, plex_disks, disks, mirror_disks):
825 '''
826 we have N plexes, and disks, and maybe mirror_disks
827 we're trying to find which plex is used for disks, and which one, if applicable, for mirror_disks
828 :return: a tuple with the names of the two plexes (disks_plex, mirror_disks_plex)
829 the second one can be None
830 '''
831 disks_plex = None
832 mirror_disks_plex = None
833 error = ''
834 for plex in plex_disks:
835 common = set(plex_disks[plex]).intersection(set(disks))
836 if common:
837 if disks_plex is None:
838 disks_plex = plex
839 else:
840 error = 'found overlapping plexes: %s and %s' % (disks_plex, plex)
841 if mirror_disks is not None:
842 common = set(plex_disks[plex]).intersection(set(mirror_disks))
843 if common:
844 if mirror_disks_plex is None:
845 mirror_disks_plex = plex
846 else:
847 error = 'found overlapping mirror plexes: %s and %s' % (mirror_disks_plex, plex)
848 if not error:
849 # make sure we found a match
850 if disks_plex is None:
851 error = 'cannot match disks with current aggregate disks'
852 if mirror_disks is not None and mirror_disks_plex is None:
853 if error:
854 error += ', and '
855 error += 'cannot match mirror_disks with current aggregate disks'
856 if error:
857 self.module.fail_json(msg="Error mapping disks for aggregate %s: %s. Found: %s" %
858 (self.parameters['name'], error, str(plex_disks)))
859 return disks_plex, mirror_disks_plex
860
861 def get_disks_to_add(self, aggr_name, disks, mirror_disks):
862 '''
863 Get list of disks used by the aggregate, as primary and mirror.
864 Report error if:
865 the plexes in use cannot be matched with user inputs (we expect some overlap)
866 the user request requires some disks to be removed (not supported)
867 : return: a tuple of two lists of disks: disks_to_add, mirror_disks_to_add
868 '''
869 # let's see if we need to add disks
870 disks_in_use = self.get_aggr_disks(aggr_name)
871 # we expect a list of tuples (disk_name, plex_name), if there is a mirror, we should have 2 plexes
872 # let's get a list of disks for each plex
873 plex_disks = {}
874 for disk_name, plex_name in disks_in_use:
875 plex_disks.setdefault(plex_name, []).append(disk_name)
876 # find who is who
877 disks_plex, mirror_disks_plex = self.map_plex_to_primary_and_mirror(plex_disks, disks, mirror_disks)
878 # Now that we know what is which, find what needs to be removed (error), and what needs to be added
879 disks_to_remove = [disk for disk in plex_disks[disks_plex] if disk not in disks]
880 if mirror_disks_plex:
881 disks_to_remove.extend([disk for disk in plex_disks[mirror_disks_plex] if disk not in mirror_disks])
882 if disks_to_remove:
883 error = 'these disks cannot be removed: %s' % str(disks_to_remove)
884 self.module.fail_json(msg="Error removing disks is not supported. Aggregate %s: %s. In use: %s" %
885 (aggr_name, error, str(plex_disks)))
886 # finally, what's to be added
887 disks_to_add = [disk for disk in disks if disk not in plex_disks[disks_plex]]
888 mirror_disks_to_add = []
889 if mirror_disks_plex:
890 mirror_disks_to_add = [disk for disk in mirror_disks if disk not in plex_disks[mirror_disks_plex]]
891 if mirror_disks_to_add and not disks_to_add:
892 self.module.fail_json(msg="Error cannot add mirror disks %s without adding disks for aggregate %s. In use: %s" %
893 (str(mirror_disks_to_add), aggr_name, str(plex_disks)))
894 if disks_to_add or mirror_disks_to_add:
895 self.na_helper.changed = True
896
897 return disks_to_add, mirror_disks_to_add
898
899 def set_disk_count(self, current, modify):
900 if modify.get('disk_count'):
901 if int(modify['disk_count']) < int(current['disk_count']):
902 self.module.fail_json(msg="Error: specified disk_count is less than current disk_count. Only adding disks is allowed.")
903 else:
904 modify['disk_count'] = modify['disk_count'] - current['disk_count']
905
906 def get_aggr_actions(self):
907 aggr_name = self.parameters.get('name')
908 rename, cd_action, modify = None, None, {}
909 current = self.get_aggr()
910 cd_action = self.na_helper.get_cd_action(current, self.parameters)
911 if cd_action == 'create' and self.parameters.get('from_name'):
912 # create by renaming existing aggregate
913 old_aggregate = self.get_aggr(self.parameters['from_name'])
914 rename = self.na_helper.is_rename_action(old_aggregate, current)
915 if rename is None:
916 self.module.fail_json(msg='Error renaming aggregate %s: no aggregate with from_name %s.'
917 % (self.parameters['name'], self.parameters['from_name']))
918 if rename:
919 current = old_aggregate
920 aggr_name = self.parameters['from_name']
921 cd_action = None
922 if cd_action is None and self.parameters['state'] == 'present':
923 modify = self.na_helper.get_modified_attributes(current, self.parameters)
924 if 'encryption' in modify and not self.use_rest:
925 self.module.fail_json(msg='Error: modifying encryption is not supported with ZAPI.')
926 if 'snaplock_type' in modify:
927 self.module.fail_json(msg='Error: snaplock_type is not modifiable. Cannot change to: %s.' % modify['snaplock_type'])
928 if self.parameters.get('disks'):
929 modify['disks_to_add'], modify['mirror_disks_to_add'] = \
930 self.get_disks_to_add(aggr_name, self.parameters['disks'], self.parameters.get('mirror_disks'))
931 self.set_disk_count(current, modify)
932
933 return current, cd_action, rename, modify
934
935 def get_object_store_action(self, current, rename):
936 object_store_cd_action = None
937 if self.parameters.get('object_store_name'):
938 aggr_name = self.parameters['from_name'] if rename else self.parameters['name']
939 object_store_current = self.get_object_store(aggr_name) if current else None
940 object_store_cd_action = self.na_helper.get_cd_action(object_store_current, self.parameters.get('object_store_name'))
941 if object_store_cd_action is None and object_store_current is not None\
942 and object_store_current['object_store_name'] != self.parameters.get('object_store_name'):
943 self.module.fail_json(msg='Error: object store %s is already associated with aggregate %s.' %
944 (object_store_current['object_store_name'], aggr_name))
945 return object_store_cd_action
946
947 def get_aggr_rest(self, name):
948 if not name:
949 return None
950 api = 'storage/aggregates'
951 query = {'name': name}
952 fields = 'uuid,state,block_storage.primary.disk_count,data_encryption,snaplock_type,block_storage.primary.raid_type'
953 if 'tags' in self.parameters:
954 fields += ',_tags'
955 record, error = rest_generic.get_one_record(self.rest_api, api, query, fields)
956 if error:
957 self.module.fail_json(msg='Error: failed to get aggregate %s: %s' % (name, error))
958 if record:
959 return {
960 'tags': record.get('_tags', []),
961 'disk_count': self.na_helper.safe_get(record, ['block_storage', 'primary', 'disk_count']),
962 'raid_type': self.na_helper.safe_get(record, ['block_storage', 'primary', 'raid_type']),
963 'encryption': self.na_helper.safe_get(record, ['data_encryption', 'software_encryption_enabled']),
964 'service_state': record['state'],
965 'snaplock_type': record['snaplock_type'],
966 'uuid': record['uuid'],
967 }
968 return None
969
970 def get_multiplier(self, unit):
971 if not unit:
972 return 1
973 try:
974 return netapp_utils.POW2_BYTE_MAP[unit[0].lower()]
975 except KeyError:
976 self.module.fail_json(msg='Error: unexpected unit in disk_size_with_unit: %s' % self.parameters['disk_size_with_unit'])
977
978 def get_disk_size(self):
979 if 'disk_size' in self.parameters:
980 return self.parameters['disk_size'] * 4 * 1024
981 if 'disk_size_with_unit' in self.parameters:
982 match = re.match(r'([\d.]+)(.*)', self.parameters['disk_size_with_unit'])
983 if match:
984 size, unit = match.groups()
985 mul = self.get_multiplier(unit)
986 return int(float(size) * mul)
987 self.module.fail_json(msg='Error: unexpected value in disk_size_with_unit: %s' % self.parameters['disk_size_with_unit'])
988 return None
989
990 def create_aggr_rest(self):
991 api = 'storage/aggregates'
992
993 disk_size = self.get_disk_size()
994 # Interestingly, REST expects True/False in body, but 'true'/'false' in query
995 # I guess it's because we're using json in the body
996 query = {'return_records': 'true'} # in order to capture UUID
997 if disk_size:
998 query['disk_size'] = disk_size
999 # query = {'disk_size': disk_size} if disk_size else None
1000
1001 body = {'name': self.parameters['name']} if 'name' in self.parameters else {}
1002 block_storage = {}
1003 primary = {}
1004 if self.parameters.get('nodes'):
1005 body['node.name'] = self.parameters['nodes'][0]
1006 if self.parameters.get('disk_class'):
1007 primary['disk_class'] = self.parameters['disk_class']
1008 if self.parameters.get('disk_count'):
1009 primary['disk_count'] = self.parameters['disk_count']
1010 if self.parameters.get('raid_size'):
1011 primary['raid_size'] = self.parameters['raid_size']
1012 if self.parameters.get('raid_type'):
1013 primary['raid_type'] = self.parameters['raid_type']
1014 if primary:
1015 block_storage['primary'] = primary
1016 mirror = {}
1017 if self.parameters.get('is_mirrored'):
1018 mirror['enabled'] = self.parameters['is_mirrored']
1019 if mirror:
1020 block_storage['mirror'] = mirror
1021 if block_storage:
1022 body['block_storage'] = block_storage
1023 if self.parameters.get('encryption') is not None:
1024 body['data_encryption'] = {'software_encryption_enabled': self.parameters['encryption']}
1025 if self.parameters.get('snaplock_type'):
1026 body['snaplock_type'] = self.parameters['snaplock_type']
1027 if self.parameters.get('tags') is not None:
1028 body['_tags'] = self.parameters['tags']
1029 response, error = rest_generic.post_async(self.rest_api, api, body or None, query, job_timeout=self.parameters['time_out'])
1030 if error:
1031 self.module.fail_json(msg='Error: failed to create aggregate: %s' % error)
1032 if response:
1033 record, error = rrh.check_for_0_or_1_records(api, response, error, query)
1034 if not error and record and 'uuid' not in record:
1035 error = 'uuid key not present in %s:' % record
1036 if error:
1037 self.module.fail_json(msg='Error: failed to parse create aggregate response: %s' % error)
1038 if record:
1039 self.uuid = record['uuid']
1040
1041 def delete_aggr_rest(self):
1042 api = 'storage/aggregates'
1043 dummy, error = rest_generic.delete_async(self.rest_api, api, self.uuid)
1044 if error:
1045 self.module.fail_json(msg='Error: failed to delete aggregate: %s' % error)
1046
1047 def patch_aggr_rest(self, action, body, query=None):
1048 api = 'storage/aggregates'
1049 dummy, error = rest_generic.patch_async(self.rest_api, api, self.uuid, body, query)
1050 if error:
1051 self.module.fail_json(msg='Error: failed to %s aggregate: %s' % (action, error))
1052
1053 def add_disks_rest(self, count=0, disks=None, mirror_disks=None, disk_size=0, disk_size_with_unit=None):
1054 """
1055 Add additional disks to aggregate.
1056 :return: None
1057 """
1058 if disks or mirror_disks:
1059 self.module.fail_json(msg='Error: disks or mirror disks are mot supported with rest: %s, %s.' % (disks, mirror_disks))
1060 if self.parameters.get('disk_class'):
1061 self.module.warn('disk_class is ignored when adding disks to an exiting aggregate')
1062 primary = {'disk_count': self.parameters['disk_count']} if count else None
1063 body = {'block_storage': {'primary': primary}} if primary else None
1064 if body:
1065 disk_size = self.get_disk_size()
1066 query = {'disk_size': disk_size} if disk_size else None
1067 self.patch_aggr_rest('increase disk count for', body, query)
1068
1069 def rename_aggr_rest(self):
1070 body = {'name': self.parameters['name']}
1071 self.patch_aggr_rest('rename', body)
1072
1073 def get_object_store_rest(self):
1074 '''TODO: support mirror in addition to primary'''
1075 api = 'storage/aggregates/%s/cloud-stores' % self.uuid
1076 record, error = rest_generic.get_one_record(self.rest_api, api, query={'primary': True})
1077 if error:
1078 self.module.fail_json(msg='Error: failed to get cloud stores for aggregate: %s' % error)
1079 return record
1080
1081 def get_cloud_target_uuid_rest(self):
1082 api = 'cloud/targets'
1083 query = {'name': self.parameters['object_store_name']}
1084 record, error = rest_generic.get_one_record(self.rest_api, api, query)
1085 if error or not record:
1086 self.module.fail_json(msg='Error: failed to find cloud store with name %s: %s' % (self.parameters['object_store_name'], error))
1087 return record['uuid']
1088
1089 def attach_object_store_to_aggr_rest(self):
1090 '''TODO: support mirror in addition to primary'''
1091
1092 if self.uuid is None:
1093 error = 'aggregate UUID is not set.'
1094 self.module.fail_json(msg='Error: cannot attach cloud store with name %s: %s' % (self.parameters['object_store_name'], error))
1095 body = {'target': {'uuid': self.get_cloud_target_uuid_rest()}}
1096 api = 'storage/aggregates/%s/cloud-stores' % self.uuid
1097 query = None
1098 if 'allow_flexgroups' in self.parameters:
1099 query = {'allow_flexgroups': 'true' if self.parameters['allow_flexgroups'] else 'false'}
1100 record, error = rest_generic.post_async(self.rest_api, api, body, query)
1101 if error:
1102 self.module.fail_json(msg='Error: failed to attach cloud store with name %s: %s' % (self.parameters['object_store_name'], error))
1103 return record
1104
1105 def validate_expensive_options(self, cd_action, modify):
1106 if cd_action == 'create' or (modify and 'disk_count' in modify):
1107 # report an error if disk_size_with_unit is not valid
1108 self.get_disk_size()
1109
1110 def apply(self):
1111 """
1112 Apply action to the aggregate
1113 :return: None
1114 """
1115 current, cd_action, rename, modify = self.get_aggr_actions()
1116 if current:
1117 self.uuid = current.get('uuid')
1118 object_store_cd_action = self.get_object_store_action(current, rename)
1119
1120 if self.na_helper.changed and self.module.check_mode:
1121 # additional validations that are done at runtime
1122 self.validate_expensive_options(cd_action, modify)
1123
1124 if self.na_helper.changed and not self.module.check_mode:
1125 if cd_action == 'create':
1126 self.create_aggr()
1127 # offine aggregate after create.
1128 if self.parameters.get('service_state') == 'offline':
1129 self.modify_aggr({'service_state': 'offline'})
1130 elif cd_action == 'delete':
1131 self.delete_aggr()
1132 else:
1133 if rename:
1134 self.rename_aggregate()
1135 if modify:
1136 self.modify_aggr(modify)
1137 if object_store_cd_action == 'create':
1138 self.attach_object_store_to_aggr()
1139 if rename:
1140 modify['name'] = self.parameters['name']
1141 result = netapp_utils.generate_result(self.na_helper.changed, cd_action, modify)
1142 self.module.exit_json(**result)
1143
1144
1145 def main():
1146 """
1147 Create Aggregate class instance and invoke apply
1148 :return: None
1149 """
1150 obj_aggr = NetAppOntapAggregate()
1151 obj_aggr.apply()
1152
1153
1154 if __name__ == '__main__':
1155 main()