Debian Codegraph @

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

raw
1 #!/usr/bin/python
2
3 # (c) 2021-2026, NetApp, Inc
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 from __future__ import absolute_import, division, print_function
7 __metaclass__ = type
8
9 DOCUMENTATION = """
10 module: na_ontap_volume_efficiency
11 short_description: NetApp ONTAP enables, disables or modifies volume efficiency
12 extends_documentation_fragment:
13 - netapp.ontap.netapp.na_ontap
14 version_added: '21.2.0'
15 author: NetApp Ansible Team (@carchi8py) <[email protected]>
16 description:
17 - Enable, modify or disable volume efficiency.
18 - Either path or volume_name is required.
19 - Only admin user can modify volume efficiency.
20 options:
21 state:
22 description:
23 - Whether the specified volume efficiency should be enabled or not.
24 choices: ['present', 'absent']
25 default: present
26 type: str
27
28 vserver:
29 description:
30 - Specifies the vserver for the volume.
31 required: true
32 type: str
33
34 path:
35 description:
36 - Specifies the path for the volume.
37 - Either C(path) or C(volume_name) is required.
38 - Requires ONTAP 9.9.1 or later with REST.
39 type: str
40
41 volume_name:
42 description:
43 - Specifies the volume name.
44 version_added: 22.3.0
45 type: str
46
47 schedule:
48 description:
49 - Specifies the storage efficiency schedule.
50 - Only supported with ZAPI.
51 type: str
52
53 policy:
54 description:
55 - Specifies the storage efficiency policy to use.
56 - By default, the following names are available 'auto', 'default', 'inline-only', '-'.
57 - Requires ONTAP 9.7 or later with REST.
58 type: str
59
60 enable_compression:
61 description:
62 - Specifies if compression is to be enabled.
63 type: bool
64
65 enable_inline_compression:
66 description:
67 - Specifies if in-line compression is to be enabled.
68 type: bool
69
70 enable_inline_dedupe:
71 description:
72 - Specifies if in-line deduplication is to be enabled, only supported on AFF systems or hybrid aggregates.
73 type: bool
74
75 enable_data_compaction:
76 description:
77 - Specifies if compaction is to be enabled.
78 type: bool
79
80 enable_cross_volume_inline_dedupe:
81 description:
82 - Specifies if in-line cross volume inline deduplication is to be enabled, this can only be enabled when inline deduplication is enabled.
83 type: bool
84
85 enable_cross_volume_background_dedupe:
86 description:
87 - Specifies if cross volume background deduplication is to be enabled, this can only be enabled when inline deduplication is enabled.
88 type: bool
89
90 volume_efficiency:
91 description:
92 - Start or Stop a volume efficiency operation on a given volume path.
93 - Requires ONTAP 9.11.1 or later with REST.
94 choices: ['start', 'stop']
95 version_added: '21.4.0'
96 type: str
97
98 start_ve_scan_all:
99 description:
100 - Specifies the scanner to scan the entire volume without applying share block optimization.
101 - Only supported with ZAPI.
102 version_added: '21.4.0'
103 type: bool
104
105 start_ve_build_metadata:
106 description:
107 - Specifies the scanner to scan the entire and generate fingerprint database without attempting the sharing.
108 - Only supported with ZAPI.
109 version_added: '21.4.0'
110 type: bool
111
112 start_ve_delete_checkpoint:
113 description:
114 - Specifies the scanner to delete existing checkpoint and start the operation from the begining.
115 - Only supported with ZAPI.
116 version_added: '21.4.0'
117 type: bool
118
119 start_ve_queue_operation:
120 description:
121 - Specifies the operation to queue if an exisitng operation is already running on the volume and in the fingerprint verification phase.
122 - Only supported with ZAPI.
123 version_added: '21.4.0'
124 type: bool
125
126 start_ve_scan_old_data:
127 description:
128 - Specifies the operation to scan the file system to process all the existing data.
129 - Requires ONTAP 9.11.1 or later with REST.
130 version_added: '21.4.0'
131 type: bool
132
133 start_ve_qos_policy:
134 description:
135 - Specifies the QoS policy for the operation.
136 - Default is best-effort in ZAPI.
137 - Only supported with ZAPI.
138 choices: ['background', 'best-effort']
139 version_added: '21.4.0'
140 type: str
141
142 stop_ve_all_operations:
143 description:
144 - Specifies that all running and queued operations to be stopped.
145 - Only supported with ZAPI.
146 version_added: '21.4.0'
147 type: bool
148
149 storage_efficiency_mode:
150 description:
151 - Storage efficiency mode used by volume. This parameter is only supported on AFF platforms.
152 - Requires ONTAP 9.10.1 or later.
153 choices: ['default', 'efficient']
154 type: str
155 version_added: '21.14.0'
156
157 time_out:
158 version_added: 23.4.0
159 description:
160 - With C(wait_for_completion) set, specifies time to wait for volume efficiency operation in seconds.
161 - Only supported with REST.
162 type: int
163 default: 30
164
165 wait_for_completion:
166 version_added: 23.4.0
167 description:
168 - Set this parameter to 'true' for synchronous execution.
169 - For asynchronous, execution exits as soon as the request is sent, and the operation continues in the background.
170 - Only supported with REST.
171 type: bool
172 default: true
173
174 rest_timeout:
175 version_added: 23.5.0
176 description:
177 - Specifies time to wait for REST API response in seconds.
178 - Only supported with REST.
179 type: int
180 default: 60
181
182 lambda_config:
183 description:
184 - Configuration parameters for AWS Lambda proxy functionality.
185 - These option and suboptions are only supported with REST.
186 type: dict
187 version_added: 23.4.0
188 suboptions:
189 function_name:
190 description:
191 - The name of the AWS Lambda function to invoke.
192 type: str
193 required: true
194 aws_region:
195 description:
196 - The name of the AWS region.
197 type: str
198 required: true
199 aws_profile:
200 description:
201 - The name of the AWS profile to use for authentication.
202 type: str
203
204 notes:
205 - supports ZAPI and REST. REST requires ONTAP 9.6 or later.
206 - supports check mode.
207 - Supports AWS Lambda proxy functionality when using REST. See the README file for examples.
208 """
209
210 EXAMPLES = """
211 - name: Enable Volume efficiency
212 netapp.ontap.na_ontap_volume_efficiency:
213 state: present
214 vserver: "TESTSVM"
215 path: "/vol/test_sis"
216 hostname: "{{ netapp_hostname }}"
217 username: "{{ netapp_username }}"
218 password: "{{ netapp_password }}"
219 https: true
220 validate_certs: false
221
222 - name: Disable Volume efficiency test
223 netapp.ontap.na_ontap_volume_efficiency:
224 state: absent
225 vserver: "TESTSVM"
226 path: "/vol/test_sis"
227 hostname: "{{ netapp_hostname }}"
228 username: "{{ netapp_username }}"
229 password: "{{ netapp_password }}"
230 https: true
231 validate_certs: false
232
233 - name: Modify storage efficiency schedule with ZAPI.
234 netapp.ontap.na_ontap_volume_efficiency:
235 state: present
236 vserver: "TESTSVM"
237 path: "/vol/test_sis"
238 schedule: "mon-sun@0,1,23"
239 enable_compression: true
240 enable_inline_compression: true
241 hostname: "{{ netapp_hostname }}"
242 username: "{{ netapp_username }}"
243 password: "{{ netapp_password }}"
244 https: true
245 validate_certs: false
246
247 - name: Start volume efficiency
248 netapp.ontap.na_ontap_volume_efficiency:
249 state: present
250 vserver: "TESTSVM"
251 path: "/vol/test_sis"
252 volume_efficiency: "start"
253 hostname: "{{ netapp_hostname }}"
254 username: "{{ netapp_username }}"
255 password: "{{ netapp_password }}"
256 https: true
257 validate_certs: false
258
259 - name: Stop volume efficiency
260 netapp.ontap.na_ontap_volume_efficiency:
261 state: present
262 vserver: "TESTSVM"
263 path: "/vol/test_sis"
264 volume_efficiency: "stop"
265 hostname: "{{ netapp_hostname }}"
266 username: "{{ netapp_username }}"
267 password: "{{ netapp_password }}"
268 https: true
269 validate_certs: false
270
271 - name: Modify volume efficiency with volume name in REST.
272 netapp.ontap.na_ontap_volume_efficiency:
273 state: present
274 vserver: "TESTSVM"
275 volume_name: "test_sis"
276 volume_efficiency: "stop"
277 enable_compression: true
278 hostname: "{{ netapp_hostname }}"
279 username: "{{ netapp_username }}"
280 password: "{{ netapp_password }}"
281 https: true
282 validate_certs: false
283 """
284
285 RETURN = """
286
287 """
288
289 import copy
290 import traceback
291 from ansible.module_utils.basic import AnsibleModule
292 from ansible.module_utils._text import to_native
293 import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils
294 from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule
295 from ansible_collections.netapp.ontap.plugins.module_utils import rest_generic
296
297
298 class NetAppOntapVolumeEfficiency(object):
299 """
300 Creates, Modifies and Disables a Volume Efficiency
301 """
302 def __init__(self):
303 """
304 Initialize the ONTAP Volume Efficiency class
305 """
306 self.argument_spec = netapp_utils.na_ontap_host_argument_spec()
307 self.argument_spec.update(dict(
308 state=dict(required=False, choices=['present', 'absent'], default='present'),
309 vserver=dict(required=True, type='str'),
310 path=dict(required=False, type='str'),
311 volume_name=dict(required=False, type='str'),
312 schedule=dict(required=False, type='str'),
313 policy=dict(required=False, type='str'),
314 enable_inline_compression=dict(required=False, type='bool'),
315 enable_compression=dict(required=False, type='bool'),
316 enable_inline_dedupe=dict(required=False, type='bool'),
317 enable_data_compaction=dict(required=False, type='bool'),
318 enable_cross_volume_inline_dedupe=dict(required=False, type='bool'),
319 enable_cross_volume_background_dedupe=dict(required=False, type='bool'),
320 storage_efficiency_mode=dict(required=False, choices=['default', 'efficient'], type='str'),
321 volume_efficiency=dict(required=False, choices=['start', 'stop'], type='str'),
322 start_ve_scan_all=dict(required=False, type='bool'),
323 start_ve_build_metadata=dict(required=False, type='bool'),
324 start_ve_delete_checkpoint=dict(required=False, type='bool'),
325 start_ve_queue_operation=dict(required=False, type='bool'),
326 start_ve_scan_old_data=dict(required=False, type='bool'),
327 start_ve_qos_policy=dict(required=False, choices=['background', 'best-effort'], type='str'),
328 stop_ve_all_operations=dict(required=False, type='bool'),
329 time_out=dict(required=False, type='int', default=30),
330 rest_timeout=dict(required=False, type='int', default=60),
331 wait_for_completion=dict(required=False, type='bool', default=True),
332 ))
333
334 self.argument_spec.update(netapp_utils.na_ontap_lambda_argument_spec())
335
336 self.module = AnsibleModule(
337 argument_spec=self.argument_spec,
338 supports_check_mode=True,
339 required_if=[('start_ve_scan_all', True, ['start_ve_scan_old_data']),
340 ['use_lambda', True, ('lambda_config',)]],
341 required_one_of=[('path', 'volume_name')],
342 mutually_exclusive=[('policy', 'schedule'), ('path', 'volume_name')]
343 )
344
345 # set up variables
346 self.na_helper = NetAppModule()
347 self.parameters = self.na_helper.set_parameters(self.module.params)
348 if self.parameters['state'] == 'present':
349 self.parameters['enabled'] = 'enabled'
350 else:
351 self.parameters['enabled'] = 'disabled'
352
353 self.rest_api = netapp_utils.OntapRestAPI(self.module, timeout=self.parameters.get('rest_timeout'))
354 partially_supported_rest_properties = [
355 ['policy', (9, 7)], ['storage_efficiency_mode', (9, 10, 1)], ['path', (9, 9, 1)],
356 # make op_state active/idle is supported from 9.11.1 or later with REST.
357 ['volume_efficiency', (9, 11, 1)], ['start_ve_scan_old_data', (9, 11, 1)]
358 ]
359 unsupported_rest_properties = [
360 'schedule', 'start_ve_scan_all', 'start_ve_build_metadata', 'start_ve_delete_checkpoint',
361 'start_ve_queue_operation', 'start_ve_qos_policy', 'stop_ve_all_operations'
362 ]
363 self.use_rest = self.rest_api.is_rest_supported_properties(self.parameters, unsupported_rest_properties, partially_supported_rest_properties)
364 self.volume_uuid = None
365 if 'volume_efficiency' in self.parameters:
366 if self.parameters['volume_efficiency'] == 'start':
367 self.parameters['status'] = 'running' if not self.use_rest else 'active'
368 else:
369 self.parameters['status'] = 'idle'
370 if not self.use_rest:
371 if self.parameters.get('use_lambda'):
372 self.module.fail_json(msg="Error: AWS Lambda proxy for ONTAP APIs is only supported with REST.")
373 if not netapp_utils.has_netapp_lib():
374 self.module.fail_json(msg=netapp_utils.netapp_lib_is_required())
375 self.validate_and_configure_zapi()
376 self.server = netapp_utils.setup_na_ontap_zapi(module=self.module, vserver=self.parameters['vserver'])
377
378 def validate_and_configure_zapi(self):
379 if self.parameters.get('storage_efficiency_mode'):
380 self.module.fail_json(msg="Error: cannot set storage_efficiency_mode in ZAPI")
381 # set default value for ZAPI like before as REST currently not support this option.
382 if not self.parameters.get('start_ve_qos_policy'):
383 self.parameters['start_ve_qos_policy'] = 'best-effort'
384 if self.parameters.get('volume_name'):
385 self.parameters['path'] = '/vol/' + self.parameters['volume_name']
386 self.module.warn("ZAPI requires '/vol/' present in the volume path, updated path: %s" % self.parameters['path'])
387
388 def get_volume_efficiency(self):
389 """
390 get the storage efficiency for a given path
391 :return: dict of sis if exist, None if not
392 """
393
394 return_value = None
395
396 if self.use_rest:
397 api = 'storage/volumes'
398 query = {'svm.name': self.parameters['vserver'], 'fields': 'uuid,efficiency'}
399 if self.parameters.get('path'):
400 query['efficiency.volume_path'] = self.parameters['path']
401 else:
402 query['name'] = self.parameters['volume_name']
403 record, error = rest_generic.get_one_record(self.rest_api, api, query)
404 if error:
405 path_or_volume = self.parameters.get('path') or self.parameters.get('volume_name')
406 self.module.fail_json(msg='Error getting volume efficiency for path %s on vserver %s: %s' % (
407 path_or_volume, self.parameters['vserver'], to_native(error)), exception=traceback.format_exc()
408 )
409 if record:
410 return_value = self.format_rest_record(record)
411 return return_value
412
413 else:
414
415 sis_get_iter = netapp_utils.zapi.NaElement('sis-get-iter')
416 sis_status_info = netapp_utils.zapi.NaElement('sis-status-info')
417 sis_status_info.add_new_child('path', self.parameters['path'])
418 query = netapp_utils.zapi.NaElement('query')
419 query.add_child_elem(sis_status_info)
420 sis_get_iter.add_child_elem(query)
421 try:
422 result = self.server.invoke_successfully(sis_get_iter, True)
423 if result.get_child_by_name('attributes-list'):
424 sis_status_attributes = result['attributes-list']['sis-status-info']
425 return_value = {
426 'path': sis_status_attributes['path'],
427 'enabled': sis_status_attributes['state'],
428 'status': sis_status_attributes['status'],
429 'schedule': sis_status_attributes['schedule'],
430 'enable_inline_compression': self.na_helper.get_value_for_bool(
431 True, sis_status_attributes.get_child_content('is-inline-compression-enabled')
432 ),
433 'enable_compression': self.na_helper.get_value_for_bool(True, sis_status_attributes.get_child_content('is-compression-enabled')),
434 'enable_inline_dedupe': self.na_helper.get_value_for_bool(True, sis_status_attributes.get_child_content('is-inline-dedupe-enabled')),
435 'enable_data_compaction': self.na_helper.get_value_for_bool(
436 True, sis_status_attributes.get_child_content('is-data-compaction-enabled')
437 ),
438 'enable_cross_volume_inline_dedupe': self.na_helper.get_value_for_bool(
439 True, sis_status_attributes.get_child_content('is-cross-volume-inline-dedupe-enabled')
440 ),
441 'enable_cross_volume_background_dedupe': self.na_helper.get_value_for_bool(
442 True, sis_status_attributes.get_child_content('is-cross-volume-background-dedupe-enabled')
443 )
444 }
445
446 if sis_status_attributes.get_child_by_name('policy'):
447 return_value['policy'] = sis_status_attributes['policy']
448 else:
449 return_value['policy'] = '-'
450
451 except netapp_utils.zapi.NaApiError as error:
452 self.module.fail_json(msg='Error getting volume efficiency for path %s on vserver %s: %s' % (
453 self.parameters['path'], self.parameters['vserver'], to_native(error)), exception=traceback.format_exc()
454 )
455 return return_value
456
457 def enable_volume_efficiency(self):
458 """
459 Enables Volume efficiency for a given volume by path
460 """
461 sis_enable = netapp_utils.zapi.NaElement("sis-enable")
462 sis_enable.add_new_child("path", self.parameters['path'])
463
464 try:
465 self.server.invoke_successfully(sis_enable, True)
466 except netapp_utils.zapi.NaApiError as error:
467 self.module.fail_json(msg='Error enabling storage efficiency for path %s on vserver %s: %s' % (self.parameters['path'],
468 self.parameters['vserver'], to_native(error)), exception=traceback.format_exc())
469
470 def disable_volume_efficiency(self):
471 """
472 Disables Volume efficiency for a given volume by path
473 """
474 sis_disable = netapp_utils.zapi.NaElement("sis-disable")
475 sis_disable.add_new_child("path", self.parameters['path'])
476
477 try:
478 self.server.invoke_successfully(sis_disable, True)
479 except netapp_utils.zapi.NaApiError as error:
480 self.module.fail_json(msg='Error disabling storage efficiency for path %s: %s' % (self.parameters['path'], to_native(error)),
481 exception=traceback.format_exc())
482
483 def modify_volume_efficiency(self, body=None):
484 """
485 Modifies volume efficiency settings for a given volume by path
486 """
487
488 if self.use_rest:
489 if not body:
490 return
491 query = {'return_timeout': 0} if not self.parameters.get('wait_for_completion') else None
492 timeout = 0 if not self.parameters.get('wait_for_completion') else self.parameters['time_out']
493 dummy, error = rest_generic.patch_async(self.rest_api, 'storage/volumes', self.volume_uuid, body, query, job_timeout=timeout)
494 if error:
495 if 'job reported error:' in error and 'Timeout error: Process still running' in error:
496 if not self.parameters.get('wait_for_completion'):
497 warning = "Volume efficiency modification is still running in the background, "\
498 "exiting with no further waiting as 'wait_for_completion' is set to false."
499 else:
500 warning = ('Volume efficiency modification is still in progress after %d seconds.' % self.parameters['time_out'])
501 self.module.warn(warning)
502 return
503 else:
504 if 'Unexpected argument "storage_efficiency_mode".' in error or \
505 'The \"-storage-efficiency-mode\" parameter is only supported on AFF.' in error:
506 error = "cannot modify storage_efficiency mode in non AFF platform."
507 elif 'not authorized' in error:
508 error = "%s user is not authorized to modify volume efficiency" % self.parameters.get('username')
509 self.module.fail_json(msg='Error in volume/efficiency patch: %s' % error)
510
511 else:
512
513 sis_config_obj = netapp_utils.zapi.NaElement("sis-set-config")
514 sis_config_obj.add_new_child('path', self.parameters['path'])
515 if 'schedule' in self.parameters:
516 sis_config_obj.add_new_child('schedule', self.parameters['schedule'])
517 if 'policy' in self.parameters:
518 sis_config_obj.add_new_child('policy-name', self.parameters['policy'])
519 if 'enable_compression' in self.parameters:
520 sis_config_obj.add_new_child('enable-compression', self.na_helper.get_value_for_bool(False, self.parameters['enable_compression']))
521 if 'enable_inline_compression' in self.parameters:
522 sis_config_obj.add_new_child('enable-inline-compression', self.na_helper.get_value_for_bool(
523 False, self.parameters['enable_inline_compression'])
524 )
525 if 'enable_inline_dedupe' in self.parameters:
526 sis_config_obj.add_new_child('enable-inline-dedupe', self.na_helper.get_value_for_bool(
527 False, self.parameters['enable_inline_dedupe'])
528 )
529 if 'enable_data_compaction' in self.parameters:
530 sis_config_obj.add_new_child('enable-data-compaction', self.na_helper.get_value_for_bool(
531 False, self.parameters['enable_data_compaction'])
532 )
533 if 'enable_cross_volume_inline_dedupe' in self.parameters:
534 sis_config_obj.add_new_child('enable-cross-volume-inline-dedupe', self.na_helper.get_value_for_bool(
535 False, self.parameters['enable_cross_volume_inline_dedupe'])
536 )
537 if 'enable_cross_volume_background_dedupe' in self.parameters:
538 sis_config_obj.add_new_child('enable-cross-volume-background-dedupe', self.na_helper.get_value_for_bool(
539 False, self.parameters['enable_cross_volume_background_dedupe'])
540 )
541
542 try:
543 self.server.invoke_successfully(sis_config_obj, True)
544 except netapp_utils.zapi.NaApiError as error:
545 self.module.fail_json(msg='Error modifying storage efficiency for path %s: %s' % (self.parameters['path'], to_native(error)),
546 exception=traceback.format_exc())
547
548 def start_volume_efficiency(self):
549 """
550 Starts volume efficiency for a given flex volume by path
551 """
552
553 sis_start = netapp_utils.zapi.NaElement('sis-start')
554 sis_start.add_new_child('path', self.parameters['path'])
555
556 if 'start_ve_scan_all' in self.parameters:
557 sis_start.add_new_child('scan-all', self.na_helper.get_value_for_bool(
558 False, self.parameters['start_ve_scan_all'])
559 )
560 if 'start_ve_build_metadata' in self.parameters:
561 sis_start.add_new_child('build-metadata', self.na_helper.get_value_for_bool(
562 False, self.parameters['start_ve_build_metadata'])
563 )
564 if 'start_ve_delete_checkpoint' in self.parameters:
565 sis_start.add_new_child('delete-checkpoint', self.na_helper.get_value_for_bool(
566 False, self.parameters['start_ve_delete_checkpoint'])
567 )
568 if 'start_ve_queue_operation' in self.parameters:
569 sis_start.add_new_child('queue-operation', self.na_helper.get_value_for_bool(
570 False, self.parameters['start_ve_queue_operation'])
571 )
572 if 'start_ve_scan_old_data' in self.parameters:
573 sis_start.add_new_child('scan', self.na_helper.get_value_for_bool(
574 False, self.parameters['start_ve_scan_old_data'])
575 )
576 if 'start_ve_qos_policy' in self.parameters:
577 sis_start.add_new_child('qos-policy', self.parameters['start_ve_qos_policy'])
578
579 try:
580 self.server.invoke_successfully(sis_start, True)
581 except netapp_utils.zapi.NaApiError as error:
582 self.module.fail_json(msg='Error starting storage efficiency for path %s on vserver %s: %s' % (self.parameters['path'],
583 self.parameters['vserver'], to_native(error)), exception=traceback.format_exc())
584
585 def stop_volume_efficiency(self):
586 """
587 Stops volume efficiency for a given flex volume by path
588 """
589 sis_stop = netapp_utils.zapi.NaElement('sis-stop')
590 sis_stop.add_new_child('path', self.parameters['path'])
591 if 'stop_ve_all_operations' in self.parameters:
592 sis_stop.add_new_child('all-operations', self.na_helper.get_value_for_bool(
593 False, self.parameters['stop_ve_all_operations'])
594 )
595
596 try:
597 self.server.invoke_successfully(sis_stop, True)
598 except netapp_utils.zapi.NaApiError as error:
599 self.module.fail_json(msg='Error stopping storage efficiency for path %s on vserver %s: %s' % (self.parameters['path'],
600 self.parameters['vserver'], to_native(error)), exception=traceback.format_exc())
601
602 def format_rest_record(self, record):
603 """
604 returns current efficiency values.
605 """
606 self.volume_uuid = record['uuid']
607 return_value = {
608 'enabled': self.na_helper.safe_get(record, ['efficiency', 'state']),
609 'status': self.na_helper.safe_get(record, ['efficiency', 'op_state']),
610 'enable_compression': self.na_helper.safe_get(record, ['efficiency', 'compression']),
611 'enable_inline_dedupe': self.na_helper.safe_get(record, ['efficiency', 'dedupe']),
612 'enable_data_compaction': self.na_helper.safe_get(record, ['efficiency', 'compaction']),
613 'enable_cross_volume_inline_dedupe': self.na_helper.safe_get(record, ['efficiency', 'cross_volume_dedupe'])
614 }
615 if not self.rest_api.meets_rest_minimum_version(self.use_rest, 9, 9, 1):
616 # efficiency is enabled if dedupe is either background or both.
617 # it's disabled if both dedupe and compression is none.
618 dedupe = self.na_helper.safe_get(record, ['efficiency', 'dedupe'])
619 if dedupe in ['background', 'both']:
620 return_value['enabled'] = 'enabled'
621 elif dedupe == 'none' and self.na_helper.safe_get(record, ['efficiency', 'compression']) == 'none':
622 return_value['enabled'] = 'disabled'
623 if self.parameters.get('storage_efficiency_mode'):
624 return_value['storage_efficiency_mode'] = self.na_helper.safe_get(record, ['efficiency', 'storage_efficiency_mode'])
625 if self.parameters.get('policy'):
626 return_value['policy'] = self.na_helper.safe_get(record, ['efficiency', 'policy', 'name'])
627 compression, inline_compression, cross_volume_inline_dedupe, cross_volume_background_dedupe = False, False, False, False
628 inline_dedupe, compaction = False, False
629 if return_value['enable_compression'] in ['background', 'both']:
630 compression = True
631 if return_value['enable_compression'] in ['inline', 'both']:
632 inline_compression = True
633 if return_value['enable_cross_volume_inline_dedupe'] in ['inline', 'both']:
634 cross_volume_inline_dedupe = True
635 if return_value['enable_cross_volume_inline_dedupe'] in ['background', 'both']:
636 cross_volume_background_dedupe = True
637 if return_value['enable_inline_dedupe'] in ['inline', 'both']:
638 inline_dedupe = True
639 if return_value['enable_data_compaction'] == 'inline':
640 compaction = True
641 return_value['enable_compression'] = compression
642 return_value['enable_inline_compression'] = inline_compression
643 return_value['enable_cross_volume_inline_dedupe'] = cross_volume_inline_dedupe
644 return_value['enable_cross_volume_background_dedupe'] = cross_volume_background_dedupe
645 return_value['enable_inline_dedupe'] = inline_dedupe
646 return_value['enable_data_compaction'] = compaction
647 return return_value
648
649 def form_modify_body_rest(self, modify, current):
650 # disable volume efficiency requires dedupe and compression set to 'none'.
651 if modify.get('enabled') == 'disabled':
652 return {'efficiency': {'dedupe': 'none', 'compression': 'none', 'compaction': 'none', 'cross_volume_dedupe': 'none'}}
653 body = {}
654 if modify.get('enabled') == 'enabled':
655 body['efficiency.dedupe'] = 'background'
656 # there are cases where ZAPI allows setting cross_volume_background_dedupe and inline_dedupe and REST not.
657 if 'enable_compression' in modify or 'enable_inline_compression' in modify:
658 body['efficiency.compression'] = self.derive_efficiency_type(modify.get('enable_compression'), modify.get('enable_inline_compression'),
659 current.get('enable_compression'), current.get('enable_inline_compression'))
660
661 if 'enable_cross_volume_background_dedupe' in modify or 'enable_cross_volume_inline_dedupe' in modify:
662 body['efficiency.cross_volume_dedupe'] = self.derive_efficiency_type(modify.get('enable_cross_volume_background_dedupe'),
663 modify.get('enable_cross_volume_inline_dedupe'),
664 current.get('enable_cross_volume_background_dedupe'),
665 current.get('enable_cross_volume_inline_dedupe'))
666
667 if modify.get('enable_data_compaction'):
668 body['efficiency.compaction'] = 'inline'
669 elif modify.get('enable_data_compaction') is False:
670 body['efficiency.compaction'] = 'none'
671
672 if modify.get('enable_inline_dedupe'):
673 body['efficiency.dedupe'] = 'both'
674 elif modify.get('enable_inline_dedupe') is False:
675 body['efficiency.dedupe'] = 'background'
676 # REST changes policy to default, so use policy in params.
677 if self.parameters.get('policy'):
678 body['efficiency.policy.name'] = self.parameters['policy']
679 if modify.get('storage_efficiency_mode'):
680 body['efficiency.storage_efficiency_mode'] = modify['storage_efficiency_mode']
681
682 # start/stop vol efficiency
683 if modify.get('status'):
684 body['efficiency.scanner.state'] = modify['status']
685 if 'start_ve_scan_old_data' in self.parameters:
686 body['efficiency.scanner.scan_old_data'] = self.parameters['start_ve_scan_old_data']
687 return body
688
689 @staticmethod
690 def derive_efficiency_type(desired_background, desired_inline, current_background, current_inline):
691 if ((desired_background and desired_inline) or
692 (desired_background and desired_inline is None and current_inline) or
693 (desired_background is None and desired_inline and current_background)):
694 return 'both'
695 elif ((desired_background and desired_inline is False) or
696 (desired_background and desired_inline is None and not current_inline) or
697 (desired_background is None and desired_inline is False and current_background)):
698 return 'background'
699 elif ((desired_background is False and desired_inline) or
700 (desired_background is False and desired_inline is None and current_inline) or
701 (desired_background is None and desired_inline and not current_background)):
702 return 'inline'
703 elif ((desired_background is False and desired_inline is False) or
704 (desired_background is False and desired_inline is None and not current_inline) or
705 (desired_background is None and desired_inline is False and not current_background)):
706 return 'none'
707
708 def validate_efficiency_compression(self, modify):
709 """
710 validate:
711 - no efficiency keys are set when state is disabled.
712 """
713 if self.parameters['enabled'] == 'disabled':
714 # if any of the keys are set, efficiency gets enabled, error out if any of eff keys are set and state is absent.
715 unsupported_enable_eff_keys = [
716 'enable_compression', 'enable_inline_compression', 'enable_inline_dedupe',
717 'enable_cross_volume_inline_dedupe', 'enable_cross_volume_background_dedupe', 'enable_data_compaction'
718 ]
719 used_unsupported_enable_eff_keys = [key for key in unsupported_enable_eff_keys if self.parameters.get(key)]
720 if used_unsupported_enable_eff_keys:
721 disable_str = 'when volume efficiency already disabled, retry with state: present'
722 if modify.get('enabled') == 'disabled':
723 disable_str = 'when trying to disable volume efficiency'
724 self.module.fail_json(msg="Error: cannot set compression keys: %s %s" % (used_unsupported_enable_eff_keys, disable_str))
725
726 def apply(self):
727 current = self.get_volume_efficiency()
728 ve_status = None
729 # If the volume efficiency does not exist for a given path to create this current is set to disabled
730 # this is for ONTAP systems that do not enable efficiency by default.
731 if current is None:
732 current = {'enabled': 'disabled'}
733 modify = self.na_helper.get_modified_attributes(current, self.parameters)
734 to_modify = copy.deepcopy(modify)
735 self.validate_efficiency_compression(modify)
736 if self.na_helper.changed and not self.module.check_mode:
737 # enable/disable, start/stop & modify vol efficiency handled in REST PATCH.
738 if self.use_rest:
739 self.modify_volume_efficiency(self.form_modify_body_rest(modify, current))
740 else:
741 if 'enabled' in modify:
742 if modify['enabled'] == 'enabled':
743 self.enable_volume_efficiency()
744 # Checking to see if there are any additional parameters that need to be set after
745 # enabling volume efficiency required for Non-AFF systems
746 current = self.get_volume_efficiency()
747 modify = self.na_helper.get_modified_attributes(current, self.parameters)
748 to_modify['modify_after_enable'] = copy.deepcopy(modify)
749 elif modify['enabled'] == 'disabled':
750 self.disable_volume_efficiency()
751 # key may not exist anymore, if modify is refreshed at line 686
752 modify.pop('enabled', None)
753
754 if 'status' in modify:
755 ve_status = modify['status']
756 del modify['status']
757
758 # Removed the enabled and volume efficiency status,
759 # if there is anything remaining in the modify dict we need to modify.
760 if modify:
761 self.modify_volume_efficiency()
762
763 if ve_status == 'running':
764 self.start_volume_efficiency()
765 elif ve_status == 'idle':
766 self.stop_volume_efficiency()
767
768 result = netapp_utils.generate_result(self.na_helper.changed, modify=to_modify)
769 self.module.exit_json(**result)
770
771
772 def main():
773 """
774 Enables, modifies or disables NetApp Ontap volume efficiency
775 """
776 obj = NetAppOntapVolumeEfficiency()
777 obj.apply()
778
779
780 if __name__ == '__main__':
781 main()