summaryrefslogtreecommitdiffstats
path: root/ansible_collections/dellemc/unity/plugins/modules/interface.py
blob: 2523f940e724663b51088af9f5881b6e11fc28f1 (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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
#!/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 Interfaces on Unity"""

from __future__ import absolute_import, division, print_function

__metaclass__ = type

DOCUMENTATION = r'''
module: interface
version_added: '1.4.0'
short_description: Manage Interfaces on Unity storage system
description:
- Managing the Interfaces on the Unity storage system includes adding Interfaces to NAS Server, getting
  details of interface and deleting configured interfaces.

extends_documentation_fragment:
  - dellemc.unity.unity

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

options:
  nas_server_name:
    description:
    - Name of the NAS server for which interface will be configured.
    type: str
  nas_server_id:
    description:
    - ID of the NAS server for which interface will be configured.
    type: str
  ethernet_port_name:
    description:
    - Name of the ethernet port.
    type: str
  ethernet_port_id:
    description:
    - ID of the ethernet port.
    type: str
  role:
    description:
    - Indicates whether interface is configured as production or backup.
    choices: [PRODUCTION, BACKUP]
    type: str
  interface_ip:
    description:
    - IP of network interface.
    required: true
    type: str
  netmask:
    description:
    - Netmask of network interface.
    type: str
  prefix_length:
    description:
    - Prefix length is mutually exclusive with I(netmask).
    type: int
  gateway:
    description:
    - Gateway of network interface.
    type: str
  vlan_id:
    description:
    - Vlan id of the interface.
    type: int
  state:
    description:
    - Define whether the interface should exist or not.
    choices: [present, absent]
    required: true
    type: str
notes:
- The I(check_mode) is supported.
- Modify operation for interface is not supported.
'''

EXAMPLES = r'''

    - name: Add Interface as Backup to NAS Server
      dellemc.unity.interface:
        unispherehost: "{{unispherehost}}"
        username: "{{username}}"
        password: "{{password}}"
        validate_certs: "{{validate_certs}}"
        nas_server_name: "dummy_nas"
        ethernet_port_name: "SP A 4-Port Card Ethernet Port 0"
        role: "BACKUP"
        interface_ip: "xx.xx.xx.xx"
        netmask: "xx.xx.xx.xx"
        gateway: "xx.xx.xx.xx"
        vlan_id: 324
        state: "present"

    - name: Add Interface as Production to NAS Server
      dellemc.unity.interface:
        unispherehost: "{{unispherehost}}"
        username: "{{username}}"
        password: "{{password}}"
        validate_certs: "{{validate_certs}}"
        nas_server_name: "dummy_nas"
        ethernet_port_name: "SP A 4-Port Card Ethernet Port 0"
        role: "PRODUCTION"
        interface_ip: "xx.xx.xx.xx"
        netmask: "xx.xx.xx.xx"
        gateway: "xx.xx.xx.xx"
        vlan_id: 324
        state: "present"

    - name: Get interface details
      dellemc.unity.interface:
        unispherehost: "{{unispherehost}}"
        username: "{{username}}"
        password: "{{password}}"
        validate_certs: "{{validate_certs}}"
        nas_server_name: "dummy_nas"
        interface_ip: "xx.xx.xx.xx"
        state: "present"

    - name: Delete Interface
      dellemc.unity.interface:
      unispherehost: "{{unispherehost}}"
      username: "{{username}}"
      password: "{{password}}"
      validate_certs: "{{validate_certs}}"
      nas_server_name: "dummy_nas"
      interface_ip: "xx.xx.xx.xx"
      state: "absent"
'''

RETURN = r'''
changed:
    description: Whether or not the resource has changed.
    returned: always
    type: bool
    sample: true
interface_details:
    description: Details of the interface.
    returned: When interface is configured for NAS Server.
    type: dict
    contains:
        existed:
            description: Indicates if interface exists.
            type: bool
        gateway:
            description: Gateway of network interface.
            type: str
        id:
            description: Unique identifier interface.
            type: str
        ip_address:
            description: IP address of interface.
            type: str
        ip_port:
            description: Port on which network interface is configured.
            type: dict
            contains:
                id:
                    description: ID of ip_port.
                    type: str
        ip_protocol_version:
            description: IP protocol version.
            type: str
        is_disabled:
            description: Indicates whether interface is disabled.
            type: bool
        is_preferred:
            description: Indicates whether interface is preferred.
            type: bool
        mac_address:
            description: Mac address of ip_port.
            type: bool
        name:
            description: System configured name of interface.
            type: bool
        nas_server:
            description: Details of NAS server where interface is configured.
            type: dict
            contains:
                id:
                    description: ID of NAS Server.
                    type: str
    sample: {
        "existed": true,
        "gateway": "xx.xx.xx.xx",
        "hash": 8785300560421,
        "health": {
            "UnityHealth": {
                "hash": 8785300565468
            }
        },
        "id": "if_69",
        "ip_address": "10.10.10.10",
        "ip_port": {
            "UnityIpPort": {
                "hash": 8785300565300,
                "id": "spb_ocp_0_eth0"
            }
        },
        "ip_protocol_version": "IpProtocolVersionEnum.IPv4",
        "is_disabled": false,
        "is_preferred": true,
        "mac_address": "0C:48:C6:9F:57:BF",
        "name": "36_APM00213404194",
        "nas_server": {
            "UnityNasServer": {
                "hash": 8785300565417,
                "id": "nas_10"
            }
        },
        "netmask": "10.10.10.10",
        "replication_policy": null,
        "role": "FileInterfaceRoleEnum.PRODUCTION",
        "source_parameters": null,
        "v6_prefix_length": null,
        "vlan_id": 324
    }
'''

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

LOG = utils.get_logger('interface')

application_type = "Ansible/1.7.1"


class Interface(object):
    """Class with Interface 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_interface_parameters())

        mutually_exclusive = [['nas_server_name', 'nas_server_id'], ['ethernet_port_id', 'ethernet_port_name'], ['netmask', 'prefix_length']]
        required_one_of = [['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_interface_details(self, nas_server_obj):
        """Get interface details.
            :param: nas_server_obj: NAS server object.
            :return: Returns interface details configured on NAS server.
        """

        try:
            nas_server_obj_properties = nas_server_obj._get_properties()
            if nas_server_obj_properties['file_interface']:
                for item in nas_server_obj_properties['file_interface']['UnityFileInterfaceList']:
                    interface_id = self.unity_conn.get_file_interface(_id=item['UnityFileInterface']['id'])
                    if interface_id.ip_address == self.module.params['interface_ip']:
                        return interface_id
            return None
        except Exception as e:
            error_msg = "Getting Interface details failed" \
                        " with error %s" % (str(e))
            LOG.error(error_msg)
            self.module.fail_json(msg=error_msg)

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

        LOG.info("Getting NAS server object")
        try:
            if nas_server_name:
                obj_nas = self.unity_conn.get_nas_server(name=nas_server_name)
                return obj_nas
            elif nas_server_id:
                obj_nas = self.unity_conn.get_nas_server(_id=nas_server_id)
                if obj_nas._get_properties()['existed']:
                    return obj_nas
                else:
                    msg = "NAS server with id %s does not exist" % (nas_server_id)
                    LOG.error(msg)
                    self.module.fail_json(msg=msg)
        except Exception as e:
            msg = "Failed to get details of NAS server with error: %s" % (str(e))
            LOG.error(msg)
            self.module.fail_json(msg=msg)

    def add_interface(self, nas_server_obj, ethernet_port_id=None, ethernet_port_name=None, role=None, interface_ip=None,
                      netmask=None, prefix_length=None, gateway=None, vlan_id=None):
        """Adding interface to NAS server.
            :param: nas_server_obj: The NAS server object.
            :param: ethernet_port_id: ID of ethernet port.
            :param: ethernet_port_name: Name of ethernet port.
            :param: role: Role of the interface.
            :param: interface_ip: IP of interface.
            :param: netmask: Netmask for interface.
            :param: prefix_length: Prefix length.
            :param: gateway: Gateway for interface.
            :param: vlan_id: vlan_id for interface.
            :return: Return True if interface is configured successfully.
        """

        LOG.info("Adding interface to NAS Server")
        try:
            nas_server_obj_properties = nas_server_obj._get_properties()
            if nas_server_obj_properties['file_interface']:
                for item in nas_server_obj_properties['file_interface']['UnityFileInterfaceList']:
                    interface_id = self.unity_conn.get_file_interface(_id=item['UnityFileInterface']['id'])
                    if interface_id._get_properties()['ip_address'] == self.module.params['interface_ip']:
                        return False
            if role:
                role_value = get_role_enum(role)
            if ethernet_port_name:
                ethernet_port_info = self.unity_conn.get_ethernet_port(name=ethernet_port_name)
                ethernet_port_id = ethernet_port_info.id
            if not self.module.check_mode:
                utils.UnityFileInterface.create(cli=self.unity_conn._cli, nas_server=nas_server_obj.get_id(), ip_port=ethernet_port_id,
                                                role=role_value, ip=interface_ip, netmask=netmask, v6_prefix_length=prefix_length,
                                                gateway=gateway, vlan_id=vlan_id)
            return True
        except Exception as e:
            msg = "Failed to add interface to NAS Server with error: %s" % (str(e))
            LOG.error(msg)
            self.module.fail_json(msg=msg)

    def is_modification_required(self, interface_details):
        """Check if modification is required in existing interface/s configured for NAS Server
            :param: interface_details: Existing interface details
            :return: True if modification is required
        """
        key_list = ['vlan_id', 'gateway', 'netmask']
        for item in key_list:
            if self.module.params[item] and self.module.params[item] != interface_details[item]:
                return True
        return False

    def delete_interface(self, interface_obj):
        """Delete NFS server.
            :param: interface_obj: Interface object.
            :return: Return True if interface is deleted.
        """

        LOG.info("Deleting interface")
        try:
            if not self.module.check_mode:
                interface_obj.delete()
            return True
        except Exception as e:
            msg = "Failed to delete interface with error: %s" % (str(e))
            LOG.error(msg)
            self.module.fail_json(msg=msg)

    def validate_input_params(self):
        """Validates input parameters"""
        param_list = ["nas_server_id", "nas_server_name",
                      "ethernet_port_name", "ethernet_port_id", "role",
                      "interface_ip", "netmask", "gateway"]

        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)

        if self.module.params['vlan_id'] is not None and \
                (self.module.params['vlan_id'] <= 3 or
                 self.module.params['vlan_id'] >= 4094):
            self.module.fail_json(msg='vlan_id should be in the '
                                      'range of 3 to 4094')

        if self.module.params['interface_ip'] and \
                not is_valid_ip(self.module.params['interface_ip']):
            self.module.fail_json(msg='The value for interface ip is invalid')

        if self.module.params['gateway'] and \
                not is_valid_ip(self.module.params['gateway']):
            self.module.fail_json(msg='The value for gateway is invalid')

        if self.module.params['netmask'] and not \
                utils.is_valid_netmask(self.module.params['netmask']):
            self.module.fail_json(msg='Invalid IPV4 address specified '
                                      'for netmask')

        if self.module.params['interface_ip'] and \
                (get_ip_version(self.module.params['interface_ip']) == 6):
            self.module.fail_json(msg='IPv6 format is not supported')

    def validate_create_params(self):
        """Validates input parameters for adding interface"""
        if self.module.params['role'] is None:
            self.module.fail_json(msg='Role is a mandatory parameter'
                                      ' for adding interface to NAS Server.')
        if self.module.params['ethernet_port_name'] is None and \
                self.module.params['ethernet_port_id'] is None:
            self.module.fail_json(msg='ethernet_port_name/ethernet_port_id '
                                      'is mandatory parameter for adding '
                                      'interface to NAS Server.')

    def perform_module_operation(self):
        """
        Perform different actions on Interface module based on parameters
        passed in the playbook
        """
        nas_server_id = self.module.params['nas_server_id']
        nas_server_name = self.module.params['nas_server_name']
        ethernet_port_name = self.module.params['ethernet_port_name']
        ethernet_port_id = self.module.params['ethernet_port_id']
        role = self.module.params['role']
        interface_ip = self.module.params['interface_ip']
        netmask = self.module.params['netmask']
        prefix_length = self.module.params['prefix_length']
        gateway = self.module.params['gateway']
        vlan_id = self.module.params['vlan_id']
        state = self.module.params['state']

        # result is a dictionary that contains changed status and Interface details
        result = dict(
            changed=False,
            interface_details={}
        )
        modify_flag = False

        self.validate_input_params()

        interface_details = None

        nas_server_obj = self.get_nas_server_obj(nas_server_name, nas_server_id)

        interface_obj = self.get_interface_details(nas_server_obj)

        if interface_obj and state == 'present':
            interface_details = interface_obj._get_properties()
            modify_flag = self.is_modification_required(interface_details)
            if modify_flag:
                self.module.fail_json(msg="Modification of Interfaces for NAS server is not supported through Ansible module")

        if not interface_obj and state == 'present':
            self.validate_create_params()

            result['changed'] = self.add_interface(nas_server_obj, ethernet_port_id, ethernet_port_name, role,
                                                   interface_ip, netmask, prefix_length, gateway, vlan_id)

        if interface_obj and state == 'absent':
            result['changed'] = self.delete_interface(interface_obj)

        if result['changed']:
            nas_server_obj = self.get_nas_server_obj(nas_server_name, nas_server_id)
            interface_obj = self.get_interface_details(nas_server_obj)
            if interface_obj:
                interface_details = interface_obj._get_properties()

        result['interface_details'] = interface_details

        self.module.exit_json(**result)


def get_interface_parameters():
    """This method provide parameters required for the ansible
       Interface module on Unity"""
    return dict(
        nas_server_id=dict(type='str'),
        nas_server_name=dict(type='str'),
        ethernet_port_name=dict(type='str'),
        ethernet_port_id=dict(type='str'),
        role=dict(type='str', choices=['PRODUCTION', 'BACKUP']),
        interface_ip=dict(required=True, type='str'),
        netmask=dict(type='str'),
        prefix_length=dict(type='int'),
        gateway=dict(type='str'),
        vlan_id=dict(type='int'),
        state=dict(required=True, type='str', choices=['present', 'absent'])
    )


def get_role_enum(role):
    """Getting correct enum values for role
        :param: role: Indicates role of interface.
        :return: enum value for role.
    """
    if utils.FileInterfaceRoleEnum[role]:
        role = utils.FileInterfaceRoleEnum[role]
        return role


def is_valid_ip(address):
    """Validating IP address format
        :param: address: IP address to be validated for format.
    """
    try:
        ipaddress.ip_address(address)
        return True
    except ValueError:
        return False


def get_ip_version(val):
    """Returns IP address version
        :param: val: IP address to be validated for version.
    """
    try:
        val = u'{0}'.format(val)
        ip = ip_network(val, strict=False)
        return ip.version
    except ValueError:
        return 0


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


if __name__ == '__main__':
    main()