summaryrefslogtreecommitdiffstats
path: root/ansible_collections/dellemc/unity/plugins/modules/nfsserver.py
blob: 30d2c787f4037c06f85dfef9fc595c653db5ba38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
#!/usr/bin/python
# Copyright: (c) 2022, Dell Technologies

# Apache License version 2.0 (see MODULE-LICENSE or http://www.apache.org/licenses/LICENSE-2.0.txt)

"""Ansible module for managing NFS server on Unity"""

from __future__ import absolute_import, division, print_function

__metaclass__ = type

DOCUMENTATION = r'''
module: nfsserver
version_added: '1.4.0'
short_description: Manage NFS server on Unity storage system
description:
- Managing the NFS server on the Unity storage system includes creating NFS server, getting NFS server details
  and deleting NFS server attributes.

extends_documentation_fragment:
  - dellemc.unity.unity

author:
- Meenakshi Dembi (@dembim) <ansible.team@dell.com>

options:
  nas_server_name:
    description:
    - Name of the NAS server on which NFS server will be hosted.
    type: str
  nas_server_id:
    description:
    - ID of the NAS server on which NFS server will be hosted.
    type: str
  nfs_server_id:
    description:
    - ID of the NFS server.
    type: str
  host_name:
    description:
    - Host name of the NFS server.
    type: str
  nfs_v4_enabled:
    description:
    - Indicates whether the NFSv4 is enabled on the NAS server.
    type: bool
  is_secure_enabled:
    description:
    - Indicates whether the secure NFS is enabled.
    type: bool
  kerberos_domain_controller_type:
    description:
    - Type of Kerberos Domain Controller used for secure NFS service.
    choices: [CUSTOM, UNIX, WINDOWS]
    type: str
  kerberos_domain_controller_username:
    description:
    - Kerberos Domain Controller administrator username.
    type: str
  kerberos_domain_controller_password:
    description:
    - Kerberos Domain Controller administrator password.
    type: str
  is_extended_credentials_enabled:
    description:
    - Indicates whether support for more than 16 unix groups in a Unix credential.
    type: bool
  remove_spn_from_kerberos:
    description:
    - Indicates whether to remove the SPN from Kerberos Domain Controller.
    default: true
    type: bool
  state:
    description:
    - Define whether the NFS server should exist or not.
    choices: [absent, present]
    required: true
    type: str
notes:
- The I(check_mode) is supported.
- Modify operation for NFS Server is not supported.
- When I(kerberos_domain_controller_type) is C(UNIX), I(kdc_type) in I(nfs_server_details) output is displayed as C(null).
'''

EXAMPLES = r'''

    - name: Create NFS server with kdctype as Windows
      dellemc.unity.nfsserver:
        unispherehost: "{{unispherehost}}"
        username: "{{username}}"
        password: "{{password}}"
        validate_certs: "{{validate_certs}}"
        nas_server_name: "dummy_nas"
        host_name: "dummy_nas23"
        is_secure_enabled: true
        kerberos_domain_controller_type: "WINDOWS"
        kerberos_domain_controller_username: "administrator"
        kerberos_domain_controller_password: "Password123!"
        is_extended_credentials_enabled: true
        nfs_v4_enabled: true
        state: "present"

    - name: Create NFS server with kdctype as Unix
      dellemc.unity.nfsserver:
        unispherehost: "{{unispherehost}}"
        username: "{{username}}"
        password: "{{password}}"
        validate_certs: "{{validate_certs}}"
        nas_server_name: "dummy_nas"
        host_name: "dummy_nas23"
        is_secure_enabled: true
        kerberos_domain_controller_type: "UNIX"
        is_extended_credentials_enabled: true
        nfs_v4_enabled: true
        state: "present"

    - name: Get NFS server details
      dellemc.unity.nfsserver:
        unispherehost: "{{unispherehost}}"
        username: "{{username}}"
        password: "{{password}}"
        validate_certs: "{{validate_certs}}"
        nas_server_name: "dummy_nas"
        state: "present"

    - name: Delete NFS server
      dellemc.unity.nfsserver:
        unispherehost: "{{unispherehost}}"
        username: "{{username}}"
        password: "{{password}}"
        validate_certs: "{{validate_certs}}"
        nas_server_name: "dummy_nas"
        kerberos_domain_controller_username: "administrator"
        kerberos_domain_controller_password: "Password123!"
        unjoin_server_account: false
        state: "absent"
'''

RETURN = r'''
changed:
    description: Whether or not the resource has changed.
    returned: always
    type: bool
    sample: true
nfs_server_details:
    description: Details of the NFS server.
    returned: When NFS server exists
    type: dict
    contains:
        credentials_cache_ttl:
            description: Credential cache refresh timeout. Resolution is in minutes. Default value is 15 minutes.
            type: str
        existed:
            description: Indicates if NFS Server exists.
            type: bool
        host_name:
            description: Host name of the NFS server.
            type: str
        id:
            description: Unique identifier of the NFS Server instance.
            type: str
        is_extended_credentials_enabled:
            description: Indicates whether the NFS server supports more than 16 Unix groups in a Unix credential.
            type: bool
        is_secure_enabled:
            description: Indicates whether secure NFS is enabled on the NFS server.
            type: bool
        kdc_type:
            description: Type of Kerberos Domain Controller used for secure NFS service.
            type: str
        nfs_v4_enabled:
            description: Indicates whether NFSv4 is enabled on the NAS server.
            type: bool
        servicee_principal_name:
            description: The Service Principal Name (SPN) for the NFS Server.
            type: str
    sample: {
        "credentials_cache_ttl": "0:15:00",
        "existed": true,
        "file_interfaces": {
            "UnityFileInterfaceList": [
                {
                    "UnityFileInterface": {
                        "hash": 8778980109421,
                        "id": "if_37"
                    }
                }
            ]
        },
        "hash": 8778980109388,
        "host_name": "dummy_nas23.pie.lab.emc.com",
        "id": "nfs_51",
        "is_extended_credentials_enabled": true,
        "is_secure_enabled": true,
        "kdc_type": "KdcTypeEnum.WINDOWS",
        "nas_server": {
            "UnityNasServer": {
                "hash": 8778980109412
            }
        },
        "nfs_v4_enabled": true,
        "servicee_principal_name": null
    }
'''

from ansible.module_utils.basic import AnsibleModule
from ansible_collections.dellemc.unity.plugins.module_utils.storage.dell \
    import utils

LOG = utils.get_logger('nfsserver')

application_type = "Ansible/1.7.1"


class NFSServer(object):
    """Class with NFS server operations"""

    def __init__(self):
        """Define all parameters required by this module"""
        self.module_params = utils.get_unity_management_host_parameters()
        self.module_params.update(get_nfs_server_parameters())

        mutually_exclusive = [['nas_server_name', 'nas_server_id']]
        required_one_of = [['nfs_server_id', 'nas_server_name', 'nas_server_id']]

        # initialize the Ansible module
        self.module = AnsibleModule(
            argument_spec=self.module_params,
            supports_check_mode=True,
            mutually_exclusive=mutually_exclusive,
            required_one_of=required_one_of
        )
        utils.ensure_required_libs(self.module)

        self.unity_conn = utils.get_unity_unisphere_connection(
            self.module.params, application_type)
        LOG.info('Check Mode Flag %s', self.module.check_mode)

    def get_nfs_server_details(self, nfs_server_id=None, nas_server_id=None):
        """Get NFS server details.
            :param: nfs_server_id: The ID of the NFS server
            :param: nas_server_id: The name of the NAS server
            :return: Dict containing NFS server details if exists
        """
        LOG.info("Getting NFS server details")
        try:
            if nfs_server_id:
                nfs_server_details = self.unity_conn.get_nfs_server(_id=nfs_server_id)
                return nfs_server_details._get_properties()
            elif nas_server_id:
                nfs_server_details = self.unity_conn.get_nfs_server(nas_server=nas_server_id)
                if len(nfs_server_details) > 0:
                    return process_dict(nfs_server_details._get_properties())
                return None
        except utils.HttpError as e:
            if e.http_status == 401:
                msg = 'Incorrect username or password provided.'
                LOG.error(msg)
                self.module.fail_json(msg=msg)
            else:
                err_msg = "Failed to get details of NFS Server" \
                          " with error {0}".format(str(e))
                LOG.error(err_msg)
                self.module.fail_json(msg=err_msg)

        except utils.UnityResourceNotFoundError as e:
            err_msg = "Failed to get details of NFS Server" \
                      " with error {0}".format(str(e))
            LOG.error(err_msg)
            return None

    def get_nfs_server_instance(self, nfs_server_id):
        """Get NFS server instance.
            :param: nfs_server_id: The ID of the NFS server
            :return: Return NFS server instance if exists
        """

        try:
            nfs_server_obj = self.unity_conn.get_nfs_server(_id=nfs_server_id)
            return nfs_server_obj
        except Exception as e:
            error_msg = "Failed to get the NFS server %s instance" \
                        " with error %s" % (nfs_server_id, str(e))
            LOG.error(error_msg)
            self.module.fail_json(msg=error_msg)

    def delete_nfs_server(self, nfs_server_id, skip_unjoin=None, domain_username=None, domain_password=None):
        """Delete NFS server.
            :param: nfs_server_id: The ID of the NFS server
            :param: skip_unjoin: Flag indicating whether to unjoin SMB server account from AD before deletion
            :param: domain_username: The domain username
            :param: domain_password: The domain password
            :return: Return True if NFS server is deleted
        """

        LOG.info("Deleting NFS server")
        try:
            if not self.module.check_mode:
                nfs_obj = self.get_nfs_server_instance(nfs_server_id=nfs_server_id)
                nfs_obj.delete(skip_kdc_unjoin=skip_unjoin, username=domain_username, password=domain_password)
            return True
        except Exception as e:
            msg = "Failed to delete NFS server: %s with error: %s" % (nfs_server_id, str(e))
            LOG.error(msg)
            self.module.fail_json(msg=msg)

    def get_nas_server_id(self, nas_server_name):
        """Get NAS server ID.
            :param: nas_server_name: The name of NAS server
            :return: Return NAS server ID if exists
        """

        LOG.info("Getting NAS server ID")
        try:
            obj_nas = self.unity_conn.get_nas_server(name=nas_server_name)
            return obj_nas.get_id()
        except Exception as e:
            msg = "Failed to get details of NAS server: %s with error: %s" % (nas_server_name, str(e))
            LOG.error(msg)
            self.module.fail_json(msg=msg)

    def is_modification_required(self, is_extended_credentials_enabled, nfs_server_details):
        """Check if modification is required in existing NFS server
            :param: is_extended_credentials_enabled: Indicates whether the NFS server supports more than 16 Unix groups in a Unix credential.
            :param: nfs_server_details: NFS server details
            :return: True if modification is required
        """

        LOG.info("Checking if any modification is required")
        # Check for Extend Credential
        if is_extended_credentials_enabled is not None and \
                is_extended_credentials_enabled != nfs_server_details['is_extended_credentials_enabled']:
            return True

    def create_nfs_server(self, nas_server_id, host_name=None, nfs_v4_enabled=None, is_secure_enabled=None,
                          kerberos_domain_controller_type=None, kerberos_domain_controller_username=None,
                          kerberos_domain_controller_password=None, is_extended_credentials_enabled=None):
        """Create NFS server.
            :param: nas_server_id: The ID of NAS server.
            :param: host_name: Name of NFS Server.
            :param: nfs_v4_enabled: Indicates whether the NFSv4 is enabled on the NAS server.
            :param: is_secure_enabled: Indicates whether the secure NFS is enabled.
            :param: kerberos_domain_controller_type: Type of Kerberos Domain Controller used for secure NFS service.
            :param: kerberos_domain_controller_username: Kerberos Domain Controller administrator username.
            :param: kerberos_domain_controller_password: Kerberos Domain Controller administrator password.
            :param: is_extended_credentials_enabled: Indicates whether support for more than 16 unix groups in a Unix credential.
        """

        LOG.info("Creating NFS server")
        try:
            if not self.module.check_mode:
                kdc_enum_type = get_enum_kdctype(kerberos_domain_controller_type)
                if kerberos_domain_controller_type == "UNIX":
                    is_extended_credentials_enabled = None
                    is_secure_enabled = None
                utils.UnityNfsServer.create(cli=self.unity_conn._cli, nas_server=nas_server_id, host_name=host_name,
                                            nfs_v4_enabled=nfs_v4_enabled,
                                            is_secure_enabled=is_secure_enabled, kdc_type=kdc_enum_type,
                                            kdc_username=kerberos_domain_controller_username,
                                            kdc_password=kerberos_domain_controller_password,
                                            is_extended_credentials_enabled=is_extended_credentials_enabled)
            return True
        except Exception as e:
            msg = "Failed to create NFS server with on NAS Server %s with error: %s" % (nas_server_id, str(e))
            LOG.error(msg)
            self.module.fail_json(msg=msg)

    def validate_input_params(self):
        param_list = ["nfs_server_id", "nas_server_id", "nas_server_name", "host_name", "kerberos_domain_controller_username",
                      "kerberos_domain_controller_password"]

        for param in param_list:
            msg = "Please provide valid value for: %s" % param
            if self.module.params[param] is not None and len(self.module.params[param].strip()) == 0:
                errmsg = msg.format(param)
                self.module.fail_json(msg=errmsg)

    def perform_module_operation(self):
        """
        Perform different actions on NFS server module based on parameters
        passed in the playbook
        """
        nfs_server_id = self.module.params['nfs_server_id']
        nas_server_id = self.module.params['nas_server_id']
        nas_server_name = self.module.params['nas_server_name']
        host_name = self.module.params['host_name']
        nfs_v4_enabled = self.module.params['nfs_v4_enabled']
        is_secure_enabled = self.module.params['is_secure_enabled']
        kerberos_domain_controller_type = self.module.params['kerberos_domain_controller_type']
        kerberos_domain_controller_username = self.module.params['kerberos_domain_controller_username']
        kerberos_domain_controller_password = self.module.params['kerberos_domain_controller_password']
        is_extended_credentials_enabled = self.module.params['is_extended_credentials_enabled']
        remove_spn_from_kerberos = self.module.params['remove_spn_from_kerberos']
        state = self.module.params['state']

        # result is a dictionary that contains changed status and NFS server details
        result = dict(
            changed=False,
            nfs_server_details={}
        )

        modify_flag = False

        self.validate_input_params()

        if nas_server_name:
            nas_server_id = self.get_nas_server_id(nas_server_name)

        nfs_server_details = self.get_nfs_server_details(nfs_server_id=nfs_server_id,
                                                         nas_server_id=nas_server_id)

        # Check if modification is required
        if nfs_server_details and state == 'present':
            modify_flag = self.is_modification_required(is_extended_credentials_enabled, nfs_server_details)
            if modify_flag:
                self.module.fail_json(msg="Modification of NFS Server parameters is not supported through Ansible module")

        if not nfs_server_details and state == 'present':
            if not nas_server_id:
                self.module.fail_json(msg="Please provide nas server id/name to create NFS server.")

            result['changed'] = self.create_nfs_server(nas_server_id, host_name, nfs_v4_enabled,
                                                       is_secure_enabled, kerberos_domain_controller_type,
                                                       kerberos_domain_controller_username,
                                                       kerberos_domain_controller_password,
                                                       is_extended_credentials_enabled)

        if state == 'absent' and nfs_server_details:
            skip_unjoin = not remove_spn_from_kerberos
            result['changed'] = self.delete_nfs_server(nfs_server_details["id"], skip_unjoin,
                                                       kerberos_domain_controller_username,
                                                       kerberos_domain_controller_password)

        if state == 'present':
            result['nfs_server_details'] = self.get_nfs_server_details(nfs_server_id=nfs_server_id,
                                                                       nas_server_id=nas_server_id)
        self.module.exit_json(**result)


def get_nfs_server_parameters():
    """This method provide parameters required for the ansible
       NFS server module on Unity"""
    return dict(
        nfs_server_id=dict(type='str'),
        host_name=dict(type='str'),
        nfs_v4_enabled=dict(type='bool'),
        is_secure_enabled=dict(type='bool'),
        kerberos_domain_controller_type=dict(type='str', choices=['UNIX', 'WINDOWS', 'CUSTOM']),
        kerberos_domain_controller_username=dict(type='str'),
        kerberos_domain_controller_password=dict(type='str', no_log=True),
        nas_server_name=dict(type='str'),
        nas_server_id=dict(type='str'),
        is_extended_credentials_enabled=dict(type='bool'),
        remove_spn_from_kerberos=dict(default=True, type='bool'),
        state=dict(required=True, type='str', choices=['present', 'absent']),
    )


def get_enum_kdctype(kerberos_domain_controller_type):
    """Getting correct enum values for kerberos_domain_controller_type
        :param: kerberos_domain_controller_type: Type of Kerberos Domain Controller used for secure NFS service.
        :return: enum value for kerberos_domain_controller_type.
    """

    if utils.KdcTypeEnum[kerberos_domain_controller_type]:
        kerberos_domain_controller_type = utils.KdcTypeEnum[kerberos_domain_controller_type]
        return kerberos_domain_controller_type


def process_dict(nfs_server_details):
    """Process NFS server details.
        :param: nfs_server_details: Dict containing NFS server details
        :return: Processed dict containing NFS server details
    """
    param_list = ['credentials_cache_ttl', 'file_interfaces', 'host_name', 'id', 'kdc_type', 'nas_server', 'is_secure_enabled',
                  'is_extended_credentials_enabled', 'nfs_v4_enabled', 'servicee_principal_name']

    for param in param_list:
        if param in nfs_server_details and param == 'credentials_cache_ttl':
            nfs_server_details[param] = str(nfs_server_details[param][0])
        else:
            nfs_server_details[param] = nfs_server_details[param][0]
    return nfs_server_details


def main():
    """Create Unity NFS server object and perform action on it
       based on user input from playbook"""
    obj = NFSServer()
    obj.perform_module_operation()


if __name__ == '__main__':
    main()