Debian Codegraph @

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

raw
1 #!/usr/bin/python
2
3 # (c) 2018-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
10 DOCUMENTATION = """
11 module: na_ontap_nfs
12 short_description: NetApp ONTAP NFS status
13 extends_documentation_fragment:
14 - netapp.ontap.netapp.na_ontap
15 version_added: 2.6.0
16 author: NetApp Ansible Team (@carchi8py) <[email protected]>
17 description:
18 - Enable or disable NFS on ONTAP
19 options:
20 state:
21 description:
22 - Whether NFS should exist or not.
23 choices: ['present', 'absent']
24 type: str
25 default: present
26 service_state:
27 description:
28 - Whether the specified NFS should be enabled or disabled. Creates NFS service if doesnt exist.
29 choices: ['started', 'stopped']
30 type: str
31 vserver:
32 description:
33 - Name of the vserver to use.
34 required: true
35 type: str
36 nfsv3:
37 description:
38 - status of NFSv3.
39 choices: ['enabled', 'disabled']
40 type: str
41 nfsv3_fsid_change:
42 description:
43 - status of if NFSv3 clients see change in FSID as they traverse filesystems.
44 - REST support added in collection version 23.2.0 and requires ONTAP 9.11.0 or later.
45 choices: ['enabled', 'disabled']
46 type: str
47 version_added: 2.7.0
48 nfsv4_fsid_change:
49 description:
50 - status of if NFSv4 clients see change in FSID as they traverse filesystems.
51 - REST support added in collection version 23.2.0 and requires ONTAP 9.13.1 or later.
52 choices: ['enabled', 'disabled']
53 type: str
54 version_added: 2.9.0
55 nfsv4:
56 description:
57 - status of NFSv4.
58 choices: ['enabled', 'disabled']
59 type: str
60 nfsv41:
61 description:
62 - status of NFSv41.
63 - usage of C(nfsv4.1) is deprecated as it does not match Ansible naming convention. The alias will be removed.
64 - please use C(nfsv41) exclusively for this option.
65 aliases: ['nfsv4.1']
66 choices: ['enabled', 'disabled']
67 type: str
68 nfsv41_pnfs:
69 description:
70 - status of NFSv41 pNFS.
71 choices: ['enabled', 'disabled']
72 type: str
73 version_added: 2.9.0
74 nfsv4_numeric_ids:
75 description:
76 - status of NFSv4 numeric ID's.
77 choices: ['enabled', 'disabled']
78 type: str
79 version_added: 2.9.0
80 vstorage_state:
81 description:
82 - status of vstorage_state.
83 choices: ['enabled', 'disabled']
84 type: str
85 nfsv4_id_domain:
86 description:
87 - Name of the nfsv4_id_domain to use.
88 type: str
89 nfsv40_acl:
90 description:
91 - status of NFS v4.0 ACL feature
92 choices: ['enabled', 'disabled']
93 type: str
94 version_added: 2.7.0
95 nfsv40_read_delegation:
96 description:
97 - status for NFS v4.0 read delegation feature.
98 choices: ['enabled', 'disabled']
99 type: str
100 version_added: 2.7.0
101 nfsv40_write_delegation:
102 description:
103 - status for NFS v4.0 write delegation feature.
104 choices: ['enabled', 'disabled']
105 type: str
106 version_added: 2.7.0
107 nfsv41_acl:
108 description:
109 - status of NFS v4.1 ACL feature
110 choices: ['enabled', 'disabled']
111 type: str
112 version_added: 2.7.0
113 nfsv41_read_delegation:
114 description:
115 - status for NFS v4.1 read delegation feature.
116 choices: ['enabled', 'disabled']
117 type: str
118 version_added: 2.7.0
119 nfsv41_write_delegation:
120 description:
121 - status for NFS v4.1 write delegation feature.
122 choices: ['enabled', 'disabled']
123 type: str
124 version_added: 2.7.0
125 nfsv40_referrals:
126 description:
127 - status for NFS v4.0 referrals.
128 - REST support added in collection version 23.2.0 and requires ONTAP 9.13.1 or later.
129 choices: ['enabled', 'disabled']
130 type: str
131 version_added: 2.9.0
132 nfsv41_referrals:
133 description:
134 - status for NFS v4.1 referrals.
135 - REST support added in collection version 23.2.0 and requires ONTAP 9.13.1 or later.
136 choices: ['enabled', 'disabled']
137 type: str
138 version_added: 2.9.0
139 tcp:
140 description:
141 - Enable TCP (support from ONTAP 9.3 onward).
142 choices: ['enabled', 'disabled']
143 type: str
144 udp:
145 description:
146 - Enable UDP (support from ONTAP 9.3 onward).
147 choices: ['enabled', 'disabled']
148 type: str
149 showmount:
150 description:
151 - Whether SVM allows showmount.
152 - With REST, supported from ONTAP 9.8 version.
153 choices: ['enabled', 'disabled']
154 type: str
155 version_added: 2.7.0
156 tcp_max_xfer_size:
157 description:
158 - TCP Maximum Transfer Size (bytes). The default value is 65536.
159 - This option requires ONTAP 9.11.0 or later in REST.
160 version_added: 2.8.0
161 type: int
162 windows:
163 description:
164 - This option can be set or modified when using REST.
165 - It requires ONTAP 9.11.0 or later.
166 version_added: 22.3.0
167 type: dict
168 suboptions:
169 default_user:
170 description:
171 - Specifies the default Windows user for the NFS server.
172 type: str
173 map_unknown_uid_to_default_user:
174 description:
175 - Specifies whether or not the mapping of an unknown UID to the default Windows user is enabled.
176 type: bool
177 v3_ms_dos_client_enabled:
178 description:
179 - Specifies whether NFSv3 MS-DOS client support is enabled.
180 type: bool
181 root:
182 description:
183 - This option can be set or modified when using REST.
184 - It requires ONTAP 9.11.0 or later.
185 type: dict
186 version_added: 22.3.0
187 suboptions:
188 ignore_nt_acl:
189 description:
190 - Specifies whether Windows ACLs affect root access from NFS.
191 - If this option is enabled, root access from NFS ignores the NT ACL set on the file or directory.
192 type: bool
193 skip_write_permission_check:
194 description:
195 - Specifies if permission checks are to be skipped for NFS WRITE calls from root/owner.
196 - For copying read-only files to a destination folder which has inheritable ACLs, this option must be enabled.
197 type: bool
198 security:
199 description:
200 - This option can be set or modified when using REST.
201 - It requires ONTAP 9.11.0 or later.
202 type: dict
203 version_added: 22.3.0
204 suboptions:
205 chown_mode:
206 description:
207 - Specifies whether file ownership can be changed only by the superuser, or if a non-root user can also change file ownership.
208 - If this option is set to restricted, file ownership can be changed only by the superuser,
209 even though the on-disk permissions allow a non-root user to change file ownership.
210 - If this option is set to unrestricted, file ownership can be changed by the superuser and by the non-root user,
211 depending upon the access granted by on-disk permissions.
212 - If this option is set to use-export-policy, file ownership can be changed in accordance with the relevant export rules.
213 choices: ['restricted', 'unrestricted', 'use_export_policy']
214 type: str
215 nt_acl_display_permission:
216 description:
217 - Controls the permissions that are displayed to NFSv3 and NFSv4 clients on a file or directory that has an NT ACL set.
218 - When true, the displayed permissions are based on the maximum access granted by the NT ACL to any user.
219 - When false, the displayed permissions are based on the minimum access granted by the NT ACL to any user.
220 type: bool
221 ntfs_unix_security:
222 description:
223 - Specifies how NFSv3 security changes affect NTFS volumes.
224 - If this option is set to ignore, ONTAP ignores NFSv3 security changes.
225 - If this option is set to fail, this overrides the UNIX security options set in the relevant export rules.
226 - If this option is set to use_export_policy, ONTAP processes NFSv3 security changes in accordance with the relevant export rules.
227 choices: ['ignore', 'fail', 'use_export_policy']
228 type: str
229 permitted_encryption_types:
230 description:
231 - Specifies the permitted encryption types for Kerberos over NFS.
232 type: list
233 elements: str
234 rpcsec_context_idle:
235 description:
236 - Specifies, in seconds, the amount of time a RPCSEC_GSS context is permitted to remain unused before it is deleted.
237 type: int
238 nfsv3_hide_snapdir:
239 description:
240 - Specifies whether hiding a snapshot directory under a NFSv3 mount point is enabled (support from ONTAP 9.13 onward).
241 - Supported only with REST.
242 choices: ['enabled', 'disabled']
243 type: str
244 version_added: 23.1.0
245 nfsv3_mount_root_only:
246 description:
247 - Specifies whether the SVM allows MOUNT protocol calls only from privileged ports (port numbers less than 1024).
248 - Supported only with REST.
249 choices: ['enabled', 'disabled']
250 type: str
251 version_added: 23.2.0
252 nfsv3_ejukebox_enabled:
253 description:
254 - Specifies whether NFSv3 EJUKEBOX error is enabled.
255 - Supported only with REST.
256 choices: ['enabled', 'disabled']
257 type: str
258 version_added: 23.2.0
259 nfsv3_connection_drop:
260 description:
261 - Specifies whether the dropping of a connection when an NFSv3 request is dropped is enabled.
262 - Supported only with REST.
263 choices: ['enabled', 'disabled']
264 type: str
265 version_added: 23.2.0
266 nfsv3_64bit_identifiers_enabled:
267 description:
268 - Specifies whether 64-bit support for NFSv3 FSIDs and file IDs is enabled.
269 - Supported only with REST.
270 choices: ['enabled', 'disabled']
271 type: str
272 version_added: 23.2.0
273 nfsv4_64bit_identifiers_enabled:
274 description:
275 - Specifies whether 64-bit support for NFSv4.x FSIDs and file IDs is enabled.
276 - Supported only with REST.
277 choices: ['enabled', 'disabled']
278 type: str
279 version_added: 23.2.0
280 nfsv42_xattrs_enabled:
281 description:
282 - Specifies whether NFSv4.2 or later extended attributes is enabled.
283 - Supported only with REST.
284 choices: ['enabled', 'disabled']
285 type: str
286 version_added: 23.2.0
287 nfsv42_seclabel_enabled:
288 description:
289 - Specifies whether NFSv4.2 or later security label is enabled.
290 - Supported only with REST.
291 choices: ['enabled', 'disabled']
292 type: str
293 version_added: 23.2.0
294 nfsv40_acl_preserve:
295 description:
296 - Specifies if the NFSv4 ACL is preserved or dropped when chmod is performed.
297 - In unified security style, this parameter also specifies if NTFS file permissions are preserved or dropped when chmod,
298 chgrp, or chown are performed.
299 - Supported only with REST.
300 choices: ['enabled', 'disabled']
301 type: str
302 version_added: 23.2.0
303 nfsv4_grace_seconds:
304 description:
305 - Specifies the grace period for clients to reclaim file locks after a server failure.
306 - Supported only with REST.
307 type: int
308 version_added: 23.2.0
309 nfsv4_lease_seconds:
310 description:
311 - Specifies the lease seconds of the NFSv4 clients.
312 If it is inactive for more than the time displayed, all of the file lock states on a node might be lost
313 - Supported only with REST.
314 type: int
315 version_added: 23.2.0
316 credential_cache:
317 description:
318 - This option can be set or modified when using REST.
319 - If not specified in POST, ONTAP uses default values for the TTLs.
320 - It requires ONTAP 9.11.0 or later.
321 type: dict
322 version_added: 23.4.0
323 suboptions:
324 positive_ttl:
325 description:
326 - Specifies the age in milliseconds, of the positive cached credentials after which they are cleared from the cache.
327 type: int
328 transient_error_ttl:
329 description:
330 - Specifies the age in milliseconds, of the cached entries during a transient error situation.
331 type: int
332 negative_ttl:
333 description:
334 - Specifies the age in milliseconds, of the negative cached credentials after which they are cleared from the cache.
335 type: int
336 lambda_config:
337 description:
338 - Configuration parameters for AWS Lambda proxy functionality.
339 - These option and suboptions are only supported with REST.
340 type: dict
341 version_added: 23.5.0
342 suboptions:
343 function_name:
344 description:
345 - The name of the AWS Lambda function to invoke.
346 type: str
347 required: true
348 aws_region:
349 description:
350 - The name of the AWS region.
351 type: str
352 required: true
353 aws_profile:
354 description:
355 - The name of the AWS profile to use for authentication.
356 type: str
357
358 notes:
359 - Supports AWS Lambda proxy functionality when using REST. See the README file for examples.
360 """
361
362 EXAMPLES = """
363 - name: Change nfs status
364 netapp.ontap.na_ontap_nfs:
365 state: present
366 service_state: stopped
367 vserver: vs_hack
368 nfsv3: disabled
369 nfsv4: disabled
370 nfsv41: enabled
371 tcp: disabled
372 udp: disabled
373 vstorage_state: disabled
374 nfsv4_id_domain: example.com
375 hostname: "{{ netapp_hostname }}"
376 username: "{{ netapp_username }}"
377 password: "{{ netapp_password }}"
378
379 - name: Create nfs configuration - REST
380 netapp.ontap.na_ontap_nfs:
381 state: present
382 service_state: stopped
383 vserver: vs_hack
384 nfsv3: disabled
385 nfsv4: disabled
386 nfsv41: enabled
387 tcp: disabled
388 udp: disabled
389 vstorage_state: disabled
390 nfsv4_id_domain: example.com
391 hostname: "{{ netapp_hostname }}"
392 username: "{{ netapp_username }}"
393 password: "{{ netapp_password }}"
394
395 - name: Modify nfs configuration - REST
396 netapp.ontap.na_ontap_nfs:
397 state: present
398 vserver: vs_hack
399 root:
400 ignore_nt_acl: true
401 skip_write_permission_check: true
402 security:
403 chown_mode: restricted
404 nt_acl_display_permission: true
405 ntfs_unix_security: fail
406 rpcsec_context_idle: 5
407 windows:
408 v3_ms_dos_client_enabled: true
409 map_unknown_uid_to_default_user: false
410 default_user: test_user
411 tcp_max_xfer_size: 16384
412 nfsv3_hide_snapdir: enabled
413 hostname: "{{ netapp_hostname }}"
414 username: "{{ netapp_username }}"
415 password: "{{ netapp_password }}"
416
417 - name: Delete nfs configuration
418 netapp.ontap.na_ontap_nfs:
419 state: absent
420 vserver: vs_hack
421 hostname: "{{ netapp_hostname }}"
422 username: "{{ netapp_username }}"
423 password: "{{ netapp_password }}"
424 """
425
426 RETURN = """
427 """
428 import traceback
429 from ansible.module_utils.basic import AnsibleModule
430 from ansible.module_utils._text import to_native
431 import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils
432 from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule
433 from ansible_collections.netapp.ontap.plugins.module_utils import rest_generic
434
435
436 class NetAppONTAPNFS:
437 """ object initialize and class methods """
438
439 def __init__(self):
440
441 self.argument_spec = netapp_utils.na_ontap_host_argument_spec()
442 self.argument_spec.update(dict(
443 state=dict(required=False, type='str', choices=['present', 'absent'], default='present'),
444 service_state=dict(required=False, type='str', choices=['started', 'stopped']),
445 vserver=dict(required=True, type='str'),
446 nfsv3=dict(required=False, type='str', default=None, choices=['enabled', 'disabled']),
447 nfsv3_fsid_change=dict(required=False, type='str', default=None, choices=['enabled', 'disabled']),
448 nfsv4_fsid_change=dict(required=False, type='str', default=None, choices=['enabled', 'disabled']),
449 nfsv4=dict(required=False, type='str', default=None, choices=['enabled', 'disabled']),
450 nfsv41=dict(required=False, type='str', default=None, choices=['enabled', 'disabled'], aliases=['nfsv4.1']),
451 nfsv41_pnfs=dict(required=False, type='str', default=None, choices=['enabled', 'disabled']),
452 nfsv4_numeric_ids=dict(required=False, type='str', default=None, choices=['enabled', 'disabled']),
453 vstorage_state=dict(required=False, type='str', default=None, choices=['enabled', 'disabled']),
454 tcp=dict(required=False, default=None, type='str', choices=['enabled', 'disabled']),
455 udp=dict(required=False, default=None, type='str', choices=['enabled', 'disabled']),
456 nfsv4_id_domain=dict(required=False, type='str', default=None),
457 nfsv40_acl=dict(required=False, type='str', default=None, choices=['enabled', 'disabled']),
458 nfsv40_read_delegation=dict(required=False, type='str', default=None, choices=['enabled', 'disabled']),
459 nfsv40_referrals=dict(required=False, type='str', default=None, choices=['enabled', 'disabled']),
460 nfsv40_write_delegation=dict(required=False, type='str', default=None, choices=['enabled', 'disabled']),
461 nfsv41_acl=dict(required=False, type='str', default=None, choices=['enabled', 'disabled']),
462 nfsv41_read_delegation=dict(required=False, type='str', default=None, choices=['enabled', 'disabled']),
463 nfsv41_referrals=dict(required=False, type='str', default=None, choices=['enabled', 'disabled']),
464 nfsv41_write_delegation=dict(required=False, type='str', default=None, choices=['enabled', 'disabled']),
465 showmount=dict(required=False, default=None, type='str', choices=['enabled', 'disabled']),
466 tcp_max_xfer_size=dict(required=False, default=None, type='int'),
467
468 # security
469 security=dict(type='dict', options=dict(
470 rpcsec_context_idle=dict(required=False, type='int'),
471 ntfs_unix_security=dict(required=False, type='str', choices=['ignore', 'fail', 'use_export_policy']),
472 chown_mode=dict(required=False, type='str', choices=['restricted', 'unrestricted', 'use_export_policy']),
473 nt_acl_display_permission=dict(required=False, type='bool'),
474 permitted_encryption_types=dict(type='list', elements='str', required=False),
475 )),
476 # root
477 root=dict(type='dict', options=dict(
478 ignore_nt_acl=dict(required=False, type='bool'),
479 skip_write_permission_check=dict(required=False, type='bool'),
480 )),
481 # windows
482 windows=dict(type='dict', options=dict(
483 map_unknown_uid_to_default_user=dict(required=False, type='bool'),
484 v3_ms_dos_client_enabled=dict(required=False, type='bool'),
485 default_user=dict(required=False, type='str'),
486 )),
487 nfsv3_hide_snapdir=dict(required=False, type='str', default=None, choices=['enabled', 'disabled']),
488 nfsv3_mount_root_only=dict(required=False, type='str', default=None, choices=['enabled', 'disabled']),
489 nfsv3_ejukebox_enabled=dict(required=False, type='str', default=None, choices=['enabled', 'disabled']),
490 nfsv3_connection_drop=dict(required=False, type='str', default=None, choices=['enabled', 'disabled']),
491 nfsv3_64bit_identifiers_enabled=dict(required=False, type='str', default=None, choices=['enabled', 'disabled']),
492 nfsv4_64bit_identifiers_enabled=dict(required=False, type='str', default=None, choices=['enabled', 'disabled']),
493 nfsv42_xattrs_enabled=dict(required=False, type='str', default=None, choices=['enabled', 'disabled']),
494 nfsv42_seclabel_enabled=dict(required=False, type='str', default=None, choices=['enabled', 'disabled']),
495 nfsv40_acl_preserve=dict(required=False, type='str', default=None, choices=['enabled', 'disabled']),
496 nfsv4_lease_seconds=dict(required=False, type='int'),
497 nfsv4_grace_seconds=dict(required=False, type='int'),
498 credential_cache=dict(type='dict', options=dict(
499 positive_ttl=dict(required=False, type='int'),
500 transient_error_ttl=dict(required=False, type='int'),
501 negative_ttl=dict(required=False, type='int'),
502 )),
503 ))
504
505 self.argument_spec.update(netapp_utils.na_ontap_lambda_argument_spec())
506
507 self.module = AnsibleModule(
508 argument_spec=self.argument_spec,
509 supports_check_mode=True,
510 required_if=[
511 ['use_lambda', True, ('lambda_config',)]
512 ],
513 )
514
515 self.na_helper = NetAppModule()
516 self.parameters = self.na_helper.set_parameters(self.module.params)
517 self.zapi_names = {
518 'nfsv3': 'is-nfsv3-enabled', # REST: protocol.v3_enabled
519 'nfsv3_fsid_change': 'is-nfsv3-fsid-change-enabled', # REST: protocol.v3_features.fsid_change
520 'nfsv4_fsid_change': 'is-nfsv4-fsid-change-enabled', # REST: protocol.v4_fsid_change
521 'nfsv4': 'is-nfsv40-enabled', # REST: protocol.v40_enabled
522 'nfsv41': 'is-nfsv41-enabled', # REST: protocol.v41_enabled
523 'nfsv41_pnfs': 'is-nfsv41-pnfs-enabled', # protocol.v41_features.pnfs_enabled
524 'nfsv4_numeric_ids': 'is-nfsv4-numeric-ids-enabled',
525 'vstorage_state': 'is-vstorage-enabled', # REST: vstorage_enabled
526 'nfsv4_id_domain': 'nfsv4-id-domain', # REST: protocol.v4_id_domain
527 'tcp': 'is-tcp-enabled', # REST: transport.tcp_enabled
528 'udp': 'is-udp-enabled', # REST: transport.udp_enabled
529 'nfsv40_acl': 'is-nfsv40-acl-enabled', # REST: protocol.v40_features.acl_enabled
530 'nfsv40_read_delegation': 'is-nfsv40-read-delegation-enabled', # REST: protocol.v40_features.read_delegation_enabled
531 'nfsv40_referrals': 'is-nfsv40-referrals-enabled', # REST: protocol.v40_features.referrals_enabled
532 'nfsv40_write_delegation': 'is-nfsv40-write-delegation-enabled', # REST: protocol.v40_features.write_delegation_enabled
533 'nfsv41_acl': 'is-nfsv41-acl-enabled', # REST: protocol.v41_features.acl_enabled
534 'nfsv41_read_delegation': 'is-nfsv41-read-delegation-enabled', # REST: protocol.v41_features.read_delegation_enabled
535 'nfsv41_referrals': 'is-nfsv41-referrals-enabled', # REST: protocol.v41_features.referrals_enabled
536 'nfsv41_write_delegation': 'is-nfsv41-write-delegation-enabled', # REST: protocol.v41_features.write_delegation_enabled
537 'showmount': 'showmount', # REST: showmount_enabled
538 'tcp_max_xfer_size': 'tcp-max-xfer-size'
539 }
540
541 self.rest_api = netapp_utils.OntapRestAPI(self.module)
542 unsupported_rest_properties = ['nfsv4_numeric_ids']
543 partially_supported_rest_properties = [['showmount', (9, 8)], ['root', (9, 11, 0)], ['windows', (9, 11, 0)], ['security', (9, 11, 0)],
544 ['tcp_max_xfer_size', (9, 11, 0)], ['nfsv3_hide_snapdir', (9, 13, 1)], ['nfsv3_fsid_change', (9, 11, 0)],
545 ['nfsv4_fsid_change', (9, 13, 1)], ['nfsv40_referrals', (9, 13, 1)], ['nfsv41_referrals', (9, 13, 1)],
546 ['nfsv3_mount_root_only', (9, 11, 0)], ['nfsv3_ejukebox_enabled', (9, 11, 0)],
547 ['nfsv3_connection_drop', (9, 11, 0)], ['nfsv3_64bit_identifiers_enabled', (9, 8)],
548 ['nfsv4_64bit_identifiers_enabled', (9, 8)], ['nfsv4_lease_seconds', (9, 13, 1)],
549 ['nfsv4_grace_seconds', (9, 13, 1)], ['nfsv40_acl_preserv', (9, 12, 0)],
550 ['nfsv42_xattrs_enabled', (9, 11, 0)], ['nfsv42_seclabel_enabled', (9, 11, 0)],
551 ['credential_cache', (9, 11, 0)],]
552 self.use_rest = self.rest_api.is_rest_supported_properties(self.parameters, unsupported_rest_properties, partially_supported_rest_properties)
553 if 'nfsv4.1' in self.parameters:
554 self.module.warn('Error: "nfsv4.1" option conflicts with Ansible naming conventions - please use "nfsv41".')
555 self.svm_uuid = None
556 self.unsupported_zapi_properties = ['root', 'windows', 'security', 'nfsv3_hide_snapdir', 'nfsv3_mount_root_only', 'nfsv3_ejukebox_enabled',
557 'nfsv3_connection_drop', 'nfsv3_64bit_identifiers_enabled', 'nfsv4_64bit_identifiers_enabled',
558 'nfsv42_xattrs_enabled', 'nfsv42_seclabel_enabled', 'nfsv40_acl_preserve',
559 'nfsv4_lease_seconds', 'nfsv4_grace_seconds', 'credential_cache']
560 self.parameters = self.na_helper.filter_out_none_entries(self.parameters)
561 if not self.use_rest:
562 if self.parameters.get('use_lambda'):
563 self.module.fail_json(msg="Error: AWS Lambda proxy for ONTAP APIs is only supported with REST.")
564 if not netapp_utils.has_netapp_lib():
565 self.module.fail_json(msg=netapp_utils.netapp_lib_is_required())
566 for unsupported_zapi_property in self.unsupported_zapi_properties:
567 if self.parameters.get(unsupported_zapi_property) is not None:
568 msg = "Error: %s option is not supported with ZAPI. It can only be used with REST." % unsupported_zapi_property
569 self.module.fail_json(msg=msg)
570 self.server = netapp_utils.setup_na_ontap_zapi(module=self.module, vserver=self.parameters['vserver'])
571
572 def get_nfs_service(self):
573 if self.use_rest:
574 return self.get_nfs_service_rest()
575 nfs_get_iter = netapp_utils.zapi.NaElement('nfs-service-get-iter')
576 nfs_info = netapp_utils.zapi.NaElement('nfs-info')
577 nfs_info.add_new_child('vserver', self.parameters['vserver'])
578 query = netapp_utils.zapi.NaElement('query')
579 query.add_child_elem(nfs_info)
580 nfs_get_iter.add_child_elem(query)
581 result = self.server.invoke_successfully(nfs_get_iter, True)
582 if result.get_child_by_name('num-records') and int(result.get_child_content('num-records')) >= 1:
583 return self.format_return(result)
584 return None
585
586 def format_return(self, result):
587 attributes_list = result.get_child_by_name('attributes-list').get_child_by_name('nfs-info')
588 return {
589 'nfsv3': self.convert_from_bool(attributes_list.get_child_content('is-nfsv3-enabled')),
590 'nfsv3_fsid_change': self.convert_from_bool(attributes_list.get_child_content('is-nfsv3-fsid-change-enabled')),
591 'nfsv4_fsid_change': self.convert_from_bool(attributes_list.get_child_content('is-nfsv4-fsid-change-enabled')),
592 'nfsv4': self.convert_from_bool(attributes_list.get_child_content('is-nfsv40-enabled')),
593 'nfsv41': self.convert_from_bool(attributes_list.get_child_content('is-nfsv41-enabled')),
594 'nfsv41_pnfs': self.convert_from_bool(attributes_list.get_child_content('is-nfsv41-pnfs-enabled')),
595 'nfsv4_numeric_ids': self.convert_from_bool(attributes_list.get_child_content('is-nfsv4-numeric-ids-enabled')),
596 'vstorage_state': self.convert_from_bool(attributes_list.get_child_content('is-vstorage-enabled')),
597 'nfsv4_id_domain': attributes_list.get_child_content('nfsv4-id-domain'),
598 'tcp': self.convert_from_bool(attributes_list.get_child_content('is-tcp-enabled')),
599 'udp': self.convert_from_bool(attributes_list.get_child_content('is-udp-enabled')),
600 'nfsv40_acl': self.convert_from_bool(attributes_list.get_child_content('is-nfsv40-acl-enabled')),
601 'nfsv40_read_delegation': self.convert_from_bool(attributes_list.get_child_content('is-nfsv40-read-delegation-enabled')),
602 'nfsv40_referrals': self.convert_from_bool(attributes_list.get_child_content('is-nfsv40-referrals-enabled')),
603 'nfsv40_write_delegation': self.convert_from_bool(attributes_list.get_child_content('is-nfsv40-write-delegation-enabled')),
604 'nfsv41_acl': self.convert_from_bool(attributes_list.get_child_content('is-nfsv41-acl-enabled')),
605 'nfsv41_read_delegation': self.convert_from_bool(attributes_list.get_child_content('is-nfsv41-read-delegation-enabled')),
606 'nfsv41_referrals': self.convert_from_bool(attributes_list.get_child_content('is-nfsv41-referrals-enabled')),
607 'nfsv41_write_delegation': self.convert_from_bool(attributes_list.get_child_content('is-nfsv41-write-delegation-enabled')),
608 'showmount': self.convert_from_bool(attributes_list.get_child_content('showmount')),
609 'tcp_max_xfer_size': self.na_helper.get_value_for_int(True, attributes_list.get_child_content('tcp-max-xfer-size'))
610 }
611
612 def get_nfs_status(self):
613 nfs_status = netapp_utils.zapi.NaElement('nfs-status')
614 result = self.server.invoke_successfully(nfs_status, True)
615 return result.get_child_content('is-enabled')
616
617 def create_nfs_service(self):
618 if self.use_rest:
619 return self.create_nfs_service_rest()
620 # This is what the old module did, not sure what happens if nfs dosn't exist.
621 self.enable_nfs()
622
623 def enable_nfs(self):
624 """
625 enable nfs (online). If the NFS service was not explicitly created,
626 this API will create one with default options.
627 """
628 nfs_enable = netapp_utils.zapi.NaElement.create_node_with_children('nfs-enable')
629 try:
630 self.server.invoke_successfully(nfs_enable, enable_tunneling=True)
631 except netapp_utils.zapi.NaApiError as error:
632 self.module.fail_json(msg='Error changing the service_state of nfs %s to %s: %s' %
633 (self.parameters['vserver'], self.parameters['service_state'], to_native(error)),
634 exception=traceback.format_exc())
635
636 def disable_nfs(self):
637 """
638 disable nfs (offline).
639 """
640 nfs_disable = netapp_utils.zapi.NaElement.create_node_with_children('nfs-disable')
641 try:
642 self.server.invoke_successfully(nfs_disable, enable_tunneling=True)
643 except netapp_utils.zapi.NaApiError as error:
644 self.module.fail_json(msg='Error changing the service_state of nfs %s to %s: %s' %
645 (self.parameters['vserver'], self.parameters['service_state'], to_native(error)),
646 exception=traceback.format_exc())
647
648 def modify_nfs_service(self, modify):
649 if self.use_rest:
650 return self.modify_nfs_service_rest(modify)
651 # This is what the old module did, not sure what happens if nfs dosn't exist.
652 nfs_modify = netapp_utils.zapi.NaElement('nfs-service-modify')
653 service_state = modify.pop('service_state', None)
654 self.modify_service_state(service_state)
655 for each in modify:
656 if each in ['nfsv4_id_domain', 'tcp_max_xfer_size']:
657 nfs_modify.add_new_child(self.zapi_names[each], str(modify[each]))
658 else:
659 nfs_modify.add_new_child(self.zapi_names[each], self.convert_to_bool(modify[each]))
660 try:
661 self.server.invoke_successfully(nfs_modify, enable_tunneling=True)
662 except netapp_utils.zapi.NaApiError as error:
663 self.module.fail_json(msg='Error modifying nfs: %s' % (to_native(error)),
664 exception=traceback.format_exc())
665
666 def modify_service_state(self, service_state):
667 nfs_enabled = self.get_nfs_status()
668 if service_state == 'started' and nfs_enabled == 'false':
669 self.enable_nfs()
670 elif service_state == 'stopped' and nfs_enabled == 'true':
671 self.disable_nfs()
672
673 def delete_nfs_service(self):
674 """
675 delete nfs service.
676 """
677 if self.use_rest:
678 return self.delete_nfs_service_rest()
679 nfs_delete = netapp_utils.zapi.NaElement.create_node_with_children('nfs-service-destroy')
680 try:
681 self.server.invoke_successfully(nfs_delete, enable_tunneling=True)
682 except netapp_utils.zapi.NaApiError as error:
683 self.module.fail_json(msg='Error deleting nfs: %s' % (to_native(error)),
684 exception=traceback.format_exc())
685
686 def get_nfs_service_rest(self):
687 api = 'protocols/nfs/services'
688 params = {'svm.name': self.parameters['vserver'],
689 'fields': 'protocol.v3_enabled,'
690 'protocol.v40_enabled,'
691 'protocol.v41_enabled,'
692 'protocol.v41_features.pnfs_enabled,'
693 'vstorage_enabled,'
694 'protocol.v4_id_domain,'
695 'transport.tcp_enabled,'
696 'transport.udp_enabled,'
697 'protocol.v40_features.acl_enabled,'
698 'protocol.v40_features.read_delegation_enabled,'
699 'protocol.v40_features.write_delegation_enabled,'
700 'protocol.v41_features.acl_enabled,'
701 'protocol.v41_features.read_delegation_enabled,'
702 'protocol.v41_features.write_delegation_enabled,'
703 'enabled,'
704 'svm.uuid,'}
705 if self.parameters.get('showmount'):
706 params['fields'] += 'showmount_enabled,'
707 if self.rest_api.meets_rest_minimum_version(self.use_rest, 9, 8):
708 params['fields'] += 'protocol.v3_64bit_identifiers_enabled,protocol.v4_64bit_identifiers_enabled,'
709 if self.rest_api.meets_rest_minimum_version(self.use_rest, 9, 11, 0):
710 params['fields'] += 'root.*,security.*,windows.*,transport.tcp_max_transfer_size,protocol.v3_features.mount_root_only,'\
711 'protocol.v3_features.ejukebox_enabled,protocol.v3_features.connection_drop,protocol.v42_features.xattrs_enabled,'\
712 'protocol.v42_features.seclabel_enabled,protocol.v3_features.fsid_change,credential_cache.positive_ttl,'\
713 'credential_cache.transient_error_ttl,credential_cache.negative_ttl,'
714 if self.rest_api.meets_rest_minimum_version(self.use_rest, 9, 12, 0):
715 params['fields'] += 'protocol.v40_features.acl_preserve,'
716 if self.rest_api.meets_rest_minimum_version(self.use_rest, 9, 13, 1):
717 params['fields'] += 'protocol.v3_features.hide_snapshot_enabled,protocol.v4_lease_seconds,protocol.v4_grace_seconds,'\
718 'protocol.v41_features.referrals_enabled,protocol.v40_features.referrals_enabled,protocol.v4_fsid_change,'
719 # TODO: might return more than 1 record, find out
720 record, error = rest_generic.get_one_record(self.rest_api, api, params)
721 if error:
722 self.module.fail_json(msg='Error getting nfs services for SVM %s: %s' % (self.parameters['vserver'], to_native(error)),
723 exception=traceback.format_exc())
724 if self.rest_api.meets_rest_minimum_version(self.use_rest, 9, 11, 0):
725 if record:
726 if 'windows' in record and 'default_user' not in record.get('windows'):
727 record['windows']['default_user'] = None
728 return self.format_get_nfs_service_rest(record) if record else record
729
730 def format_get_nfs_service_rest(self, record):
731 return {
732 'nfsv3': self.convert_from_bool(self.na_helper.safe_get(record, ['protocol', 'v3_enabled'])),
733 'nfsv4': self.convert_from_bool(self.na_helper.safe_get(record, ['protocol', 'v40_enabled'])),
734 'nfsv41': self.convert_from_bool(self.na_helper.safe_get(record, ['protocol', 'v41_enabled'])),
735 'nfsv41_pnfs': self.convert_from_bool(self.na_helper.safe_get(record, ['protocol', 'v41_features', 'pnfs_enabled'])),
736 'vstorage_state': self.convert_from_bool(self.na_helper.safe_get(record, ['vstorage_enabled'])),
737 'nfsv4_id_domain': self.na_helper.safe_get(record, ['protocol', 'v4_id_domain']),
738 'tcp': self.convert_from_bool(self.na_helper.safe_get(record, ['transport', 'tcp_enabled'])),
739 'udp': self.convert_from_bool(self.na_helper.safe_get(record, ['transport', 'udp_enabled'])),
740 'tcp_max_xfer_size': self.na_helper.safe_get(record, ['transport', 'tcp_max_transfer_size']),
741 'nfsv40_acl': self.convert_from_bool(self.na_helper.safe_get(record, ['protocol', 'v40_features', 'acl_enabled'])),
742 'nfsv40_referrals': self.convert_from_bool(self.na_helper.safe_get(record, ['protocol', 'v40_features', 'referrals_enabled'])),
743 'nfsv40_read_delegation': self.convert_from_bool(self.na_helper.safe_get(record, ['protocol', 'v40_features', 'read_delegation_enabled'])),
744 'nfsv40_write_delegation': self.convert_from_bool(self.na_helper.safe_get(record, ['protocol', 'v40_features', 'write_delegation_enabled'])),
745 'nfsv41_acl': self.convert_from_bool(self.na_helper.safe_get(record, ['protocol', 'v41_features', 'acl_enabled'])),
746 'nfsv41_referrals': self.convert_from_bool(self.na_helper.safe_get(record, ['protocol', 'v41_features', 'referrals_enabled'])),
747 'nfsv41_read_delegation': self.convert_from_bool(self.na_helper.safe_get(record, ['protocol', 'v41_features', 'read_delegation_enabled'])),
748 'nfsv41_write_delegation': self.convert_from_bool(self.na_helper.safe_get(record, ['protocol', 'v41_features', 'write_delegation_enabled'])),
749 'showmount': self.convert_from_bool(self.na_helper.safe_get(record, ['showmount_enabled'])),
750 'svm_uuid': self.na_helper.safe_get(record, ['svm', 'uuid']),
751 'service_state': self.convert_from_bool_to_started(self.na_helper.safe_get(record, ['enabled'])),
752 'root': self.na_helper.safe_get(record, ['root']),
753 'windows': self.na_helper.safe_get(record, ['windows']),
754 'security': self.na_helper.safe_get(record, ['security']),
755 'nfsv3_hide_snapdir': self.convert_from_bool(self.na_helper.safe_get(record, ['protocol', 'v3_features', 'hide_snapshot_enabled'])),
756 'nfsv3_mount_root_only': self.convert_from_bool(self.na_helper.safe_get(record, ['protocol', 'v3_features', 'mount_root_only'])),
757 'nfsv3_ejukebox_enabled': self.convert_from_bool(self.na_helper.safe_get(record, ['protocol', 'v3_features', 'ejukebox_enabled'])),
758 'nfsv3_connection_drop': self.convert_from_bool(self.na_helper.safe_get(record, ['protocol', 'v3_features', 'connection_drop'])),
759 'nfsv3_64bit_identifiers_enabled': self.convert_from_bool(self.na_helper.safe_get(record, ['protocol', 'v3_64bit_identifiers_enabled'])),
760 'nfsv4_64bit_identifiers_enabled': self.convert_from_bool(self.na_helper.safe_get(record, ['protocol', 'v4_64bit_identifiers_enabled'])),
761 'nfsv42_xattrs_enabled': self.convert_from_bool(self.na_helper.safe_get(record, ['protocol', 'v42_features', 'xattrs_enabled'])),
762 'nfsv42_seclabel_enabled': self.convert_from_bool(self.na_helper.safe_get(record, ['protocol', 'v42_features', 'seclabel_enabled'])),
763 'nfsv40_acl_preserve': self.convert_from_bool(self.na_helper.safe_get(record, ['protocol', 'v40_features', 'acl_preserve'])),
764 'nfsv4_lease_seconds': self.na_helper.safe_get(record, ['protocol', 'v4_lease_seconds']),
765 'nfsv4_grace_seconds': self.na_helper.safe_get(record, ['protocol', 'v4_grace_seconds']),
766 'nfsv3_fsid_change': self.convert_from_bool(self.na_helper.safe_get(record, ['protocol', 'v3_features', 'fsid_change'])),
767 'nfsv4_fsid_change': self.convert_from_bool(self.na_helper.safe_get(record, ['protocol', 'v4_fsid_change'])),
768 'credential_cache': self.na_helper.safe_get(record, ['credential_cache']),
769 }
770
771 def create_nfs_service_rest(self):
772 api = 'protocols/nfs/services'
773 body = {'svm.name': self.parameters['vserver']}
774 body.update(self.create_modify_body(body))
775 dummy, error = rest_generic.post_async(self.rest_api, api, body, job_timeout=120)
776 if error:
777 self.module.fail_json(msg='Error creating nfs service for SVM %s: %s' % (self.parameters['vserver'], to_native(error)),
778 exception=traceback.format_exc())
779
780 def delete_nfs_service_rest(self):
781 if self.svm_uuid is None:
782 self.module.fail_json(msg='Error deleting nfs service for SVM %s: svm.uuid is None' % self.parameters['vserver'])
783 dummy, error = rest_generic.delete_async(self.rest_api, 'protocols/nfs/services', self.svm_uuid, job_timeout=120)
784 if error:
785 self.module.fail_json(msg='Error deleting nfs service for SVM %s' % self.parameters['vserver'])
786
787 def modify_nfs_service_rest(self, modify):
788 if self.svm_uuid is None:
789 self.module.fail_json(msg='Error modifying nfs service for SVM %s: svm.uuid is None' % self.parameters['vserver'])
790 api = 'protocols/nfs/services'
791 body = {}
792 body.update(self.create_modify_body(body, modify))
793 dummy, error = rest_generic.patch_async(self.rest_api, api, self.svm_uuid, body, job_timeout=120)
794 if error:
795 self.module.fail_json(msg='Error modifying nfs service for SVM %s: %s' % (self.parameters['vserver'], to_native(error)),
796 exception=traceback.format_exc())
797
798 def create_modify_body(self, body, modify=None):
799 params = modify or self.parameters
800 if params.get('nfsv3') is not None:
801 body['protocol.v3_enabled'] = self.convert_to_bool(params['nfsv3'])
802 if params.get('nfsv4') is not None:
803 body['protocol.v40_enabled'] = self.convert_to_bool(params['nfsv4'])
804 if params.get('nfsv41') is not None:
805 body['protocol.v41_enabled'] = self.convert_to_bool(params['nfsv41'])
806 if params.get('nfsv41_pnfs') is not None:
807 body['protocol.v41_features.pnfs_enabled'] = self.convert_to_bool(params['nfsv41_pnfs'])
808 if params.get('vstorage_state') is not None:
809 body['vstorage_enabled'] = self.convert_to_bool(params['vstorage_state'])
810 if params.get('nfsv4_id_domain') is not None:
811 body['protocol.v4_id_domain'] = params['nfsv4_id_domain']
812 if params.get('tcp') is not None:
813 body['transport.tcp_enabled'] = self.convert_to_bool(params['tcp'])
814 if params.get('udp') is not None:
815 body['transport.udp_enabled'] = self.convert_to_bool(params['udp'])
816 if params.get('nfsv40_acl') is not None:
817 body['protocol.v40_features.acl_enabled'] = self.convert_to_bool(params['nfsv40_acl'])
818 if params.get('nfsv40_referrals') is not None:
819 body['protocol.v40_features.referrals_enabled'] = self.convert_to_bool(params['nfsv40_referrals'])
820 if params.get('nfsv40_read_delegation') is not None:
821 body['protocol.v40_features.read_delegation_enabled'] = self.convert_to_bool(params['nfsv40_read_delegation'])
822 if params.get('nfsv40_write_delegation') is not None:
823 body['protocol.v40_features.write_delegation_enabled'] = self.convert_to_bool(params['nfsv40_write_delegation'])
824 if params.get('nfsv41_acl') is not None:
825 body['protocol.v41_features.acl_enabled'] = self.convert_to_bool(params['nfsv41_acl'])
826 if params.get('nfsv41_referrals') is not None:
827 body['protocol.v41_features.referrals_enabled'] = self.convert_to_bool(params['nfsv41_referrals'])
828 if params.get('nfsv41_read_delegation') is not None:
829 body['protocol.v41_features.read_delegation_enabled'] = self.convert_to_bool(params['nfsv41_read_delegation'])
830 if params.get('nfsv41_write_delegation') is not None:
831 body['protocol.v41_features.write_delegation_enabled'] = self.convert_to_bool(params['nfsv41_write_delegation'])
832 if params.get('showmount') is not None:
833 body['showmount_enabled'] = self.convert_to_bool(params['showmount'])
834 # Tested this out, in both create and modify, changing the service_state will enable and disabled the service
835 # during both a create and modify.
836 if params.get('service_state') is not None:
837 body['enabled'] = self.convert_to_bool(params['service_state'])
838 if params.get('root') is not None:
839 body['root'] = params['root']
840 if params.get('windows') is not None:
841 body['windows'] = params['windows']
842 if params.get('security') is not None:
843 body['security'] = params['security']
844 if params.get('tcp_max_xfer_size') is not None:
845 body['transport.tcp_max_transfer_size'] = params['tcp_max_xfer_size']
846 if params.get('nfsv3_hide_snapdir') is not None:
847 body['protocol.v3_features.hide_snapshot_enabled'] = self.convert_to_bool(params['nfsv3_hide_snapdir'])
848 if params.get('nfsv3_mount_root_only') is not None:
849 body['protocol.v3_features.mount_root_only'] = self.convert_to_bool(params['nfsv3_mount_root_only'])
850 if params.get('nfsv3_ejukebox_enabled') is not None:
851 body['protocol.v3_features.ejukebox_enabled'] = self.convert_to_bool(params['nfsv3_ejukebox_enabled'])
852 if params.get('nfsv3_connection_drop') is not None:
853 body['protocol.v3_features.connection_drop'] = self.convert_to_bool(params['nfsv3_connection_drop'])
854 if params.get('nfsv3_64bit_identifiers_enabled') is not None:
855 body['protocol.v3_64bit_identifiers_enabled'] = self.convert_to_bool(params['nfsv3_64bit_identifiers_enabled'])
856 if params.get('nfsv4_64bit_identifiers_enabled') is not None:
857 body['protocol.v4_64bit_identifiers_enabled'] = self.convert_to_bool(params['nfsv4_64bit_identifiers_enabled'])
858 if params.get('nfsv42_xattrs_enabled') is not None:
859 body['protocol.v42_features.xattrs_enabled'] = self.convert_to_bool(params['nfsv42_xattrs_enabled'])
860 if params.get('nfsv42_seclabel_enabled') is not None:
861 body['protocol.v42_features.seclabel_enabled'] = self.convert_to_bool(params['nfsv42_seclabel_enabled'])
862 if params.get('nfsv40_acl_preserve') is not None:
863 body['protocol.v40_features.acl_preserve'] = self.convert_to_bool(params['nfsv40_acl_preserve'])
864 if params.get('nfsv4_lease_seconds') is not None:
865 body['protocol.v4_lease_seconds'] = params['nfsv4_lease_seconds']
866 if params.get('nfsv4_grace_seconds') is not None:
867 body['protocol.v4_grace_seconds'] = params['nfsv4_grace_seconds']
868 if params.get('nfsv3_fsid_change') is not None:
869 body['protocol.v3_features.fsid_change'] = self.convert_to_bool(params['nfsv3_fsid_change'])
870 if params.get('nfsv4_fsid_change') is not None:
871 body['protocol.v4_fsid_change'] = self.convert_to_bool(params['nfsv4_fsid_change'])
872 if params.get('credential_cache') is not None:
873 body['credential_cache'] = params['credential_cache']
874 return body
875
876 def convert_to_bool(self, value):
877 return 'true' if value in ['enabled', 'started'] else 'false'
878
879 def convert_from_bool(self, value):
880 return 'enabled' if value in ['true', True] else 'disabled'
881
882 def convert_from_bool_to_started(self, value):
883 return 'started' if value in ['true', True] else 'stopped'
884
885 def validate_modify(self, current, modify):
886 '''Earlier ONTAP versions do not support tcp_max_xfer_size'''
887 if 'tcp_max_xfer_size' in modify and current['tcp_max_xfer_size'] is None:
888 self.module.fail_json(msg='Error: tcp_max_xfer_size is not supported on ONTAP 9.3 or earlier.')
889
890 def apply(self):
891 current = self.get_nfs_service()
892 if self.use_rest and current is not None:
893 self.svm_uuid = current.get('svm_uuid')
894 cd_action = self.na_helper.get_cd_action(current, self.parameters)
895 modify = None
896 if cd_action is None and self.parameters['state'] == 'present':
897 modify = self.na_helper.get_modified_attributes(current, self.parameters)
898 if not self.use_rest:
899 self.validate_modify(current, modify)
900 if self.na_helper.changed and not self.module.check_mode:
901 if cd_action == 'create':
902 self.create_nfs_service()
903 elif cd_action == 'delete':
904 self.delete_nfs_service()
905 elif modify:
906 self.modify_nfs_service(modify)
907 result = netapp_utils.generate_result(self.na_helper.changed, cd_action, modify)
908 self.module.exit_json(**result)
909
910
911 def main():
912 """ Create object and call apply """
913 obj = NetAppONTAPNFS()
914 obj.apply()
915
916
917 if __name__ == '__main__':
918 main()