
    VhmJ                         d Z ddlmZmZmZ eZdZdZdZ	ddl
mZ ddlmZ ddlZdd	lmZ  ej                   d
      ZdZ G d de      Zd Zd Zd Zd Zd Zedk(  r e        yy)z/Ansible module for managing Interfaces on Unity    )absolute_importdivisionprint_functiona[  
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.
a  

- 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"
aF
  
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
    }
)AnsibleModule)utilsN)
ip_network	interfacezAnsible/1.7.1c                   L    e Zd ZdZd Zd Zd Z	 	 ddZd Zd Z	d	 Z
d
 Zd Zy)	InterfacezClass with Interface operationsc                    t        j                         | _        | j                  j                  t	                      ddgddgddgg}ddgg}t        | j                  d||      | _        t        j                  | j                         t        j                  | j                  j                  t              | _        t        j                  d	| j                  j                         y
)z-Define all parameters required by this modulenas_server_namenas_server_idethernet_port_idethernet_port_namenetmaskprefix_lengthT)argument_specsupports_check_modemutually_exclusiverequired_one_ofzCheck Mode Flag %sN)r   $get_unity_management_host_parametersmodule_paramsupdateget_interface_parametersr   moduleensure_required_libsget_unity_unisphere_connectionparamsapplication_type
unity_connLOGinfo
check_mode)selfr   r   s      k/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/dellemc/unity/plugins/modules/interface.py__init__zInterface.__init__   s    "GGI!!":"<=0/BEWYmDnqz  }L  qM  N-?@ $,, $1+	
 	""4;;/>>KK 02%t{{'='=>    c                    	 |j                         }|d   rY|d   d   D ]N  }| j                  j                  |d   d         }|j                  | j                  j
                  d   k(  sL|c S  y# t        $ rI}dt        |      z  }t        j                  |       | j                  j                  |	       Y d}~yd}~ww xY w)
zGet interface details.
            :param: nas_server_obj: NAS server object.
            :return: Returns interface details configured on NAS server.
        file_interfaceUnityFileInterfaceListUnityFileInterfaceid_idinterface_ipNz.Getting Interface details failed with error %smsg)_get_propertiesr    get_file_interface
ip_addressr   r   	Exceptionstrr!   error	fail_json)r$   nas_server_objnas_server_obj_propertiesiteminterface_ide	error_msgs          r%   get_interface_detailszInterface.get_interface_details  s    	1(6(F(F(H%()9:56FGH`a ,D#'??#E#E$OcJdeiJj#E#kL#..$++2D2D^2TT++,  	1),/F4IIIi KK!!i!00		1s$   A(A1 +A1 /A1 1	C:?B>>Cc                    t         j                  d       	 |r| j                  j                  |      }|S |rh| j                  j                  |      }|j	                         d   r|S d|z  }t         j                  |       | j                  j                  |       yy# t        $ rI}dt        |      z  }t         j                  |       | j                  j                  |       Y d}~yd}~ww xY w)	zGet 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
        zGetting NAS server objectnamer-   existedz$NAS server with id %s does not existr0   z2Failed to get details of NAS server with error: %sN)
r!   r"   r    get_nas_serverr2   r7   r   r8   r5   r6   )r$   r   r   obj_nasr1   r=   s         r%   get_nas_server_objzInterface.get_nas_server_obj  s     	,-	+//88o8N//88]8K**,Y7"N@MRCIIcNKK))c)2   	+F#a&QCIIcNKK!!c!**	+s"   B" 2B" *6B" "	C4+?C//C4Nc
                    t         j                  d       	 |j                         }
|
d   r^|
d   d   D ]S  }| j                  j	                  |d   d         }|j                         d   | j
                  j                  d   k(  sS y	 |rt        |      }|r(| j                  j                  |
      }|j                  }| j
                  j                  sJt        j                  j                  | j                  j                  |j                         ||||||		       y# t         $ rI}dt#        |      z  }t         j%                  |       | j
                  j'                  |       Y d}~yd}~ww xY w)aY  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.
        zAdding interface to NAS Serverr)   r*   r+   r,   r-   r4   r/   FrA   )	cli
nas_serverip_portroleipr   v6_prefix_lengthgatewayvlan_idTz4Failed to add interface to NAS Server with error: %sr0   N)r!   r"   r2   r    r3   r   r   get_role_enumget_ethernet_portr,   r#   r   r+   create_cliget_idr5   r6   r7   r8   )r$   r9   r   r   rK   r/   r   r   rN   rO   r:   r;   r<   
role_valueethernet_port_infor=   r1   s                    r%   add_interfacezInterface.add_interface.  sm    	12	+(6(F(F(H%()9:56FGH`a %D#'??#E#E$OcJdeiJj#E#kL#335lCt{{GYGYZhGii$% *40
!%)__%F%FL^%F%_"#5#8#8 ;;))((//DOO4H4HUcUjUjUl  wG5?LZa  uB8? 0 R  	+HCPQFSCIIcNKK!!c!**	+s%   A/D" D" 	BD" "	E4+?E//E4c                     g d}|D ]=  }| j                   j                  |   s| j                   j                  |   ||   k7  s= y y)zCheck if modification is required in existing interface/s configured for NAS Server
            :param: interface_details: Existing interface details
            :return: True if modification is required
        )rO   rN   r   TF)r   r   )r$   interface_detailskey_listr;   s       r%   is_modification_requiredz"Interface.is_modification_requiredT  sO    
 5 	D{{!!$'DKK,>,>t,DHYZ^H_,_	 r'   c                 &   t         j                  d       	 | j                  j                  s|j	                          y# t
        $ rI}dt        |      z  }t         j                  |       | j                  j                  |       Y d}~yd}~ww xY w)zDelete NFS server.
            :param: interface_obj: Interface object.
            :return: Return True if interface is deleted.
        zDeleting interfaceTz)Failed to delete interface with error: %sr0   N)	r!   r"   r   r#   deleter5   r6   r7   r8   )r$   interface_objr=   r1   s       r%   delete_interfacezInterface.delete_interface_  ss     	%&	+;;))$$& 	+=QHCIIcNKK!!c!**	+s   &> 	B?BBc                    g d}|D ]  }d|z  }| j                   j                  |   "t        | j                   j                  |   j                               dk(  sV|j	                  |      }| j                   j                  |        | j                   j                  d   T| j                   j                  d   dk  s| j                   j                  d   dk\  r| j                   j                  d	       | j                   j                  d
   r>t        | j                   j                  d
         s| j                   j                  d       | j                   j                  d   r>t        | j                   j                  d         s| j                   j                  d       | j                   j                  d   rHt        j                  | j                   j                  d         s| j                   j                  d       | j                   j                  d
   rCt        | j                   j                  d
         dk(  r| j                   j                  d       yyy)zValidates input parameters)r   r   r   r   rK   r/   r   rN   z"Please provide valid value for: %sNr   r0   rO      i  z+vlan_id should be in the range of 3 to 4094r/   z%The value for interface ip is invalidrN   z The value for gateway is invalidr   z*Invalid IPV4 address specified for netmask   zIPv6 format is not supported)
r   r   lenstripformatr8   is_valid_ipr   is_valid_netmaskget_ip_version)r$   
param_listparamr1   errmsgs        r%   validate_input_paramszInterface.validate_input_paramso  s   <
   	2E6>C{{!!%(4**51779:a?E*%%&%1	2 ;;i(4##I.!3##I.$6KK!! ';! < ;;n- 2 2> BCKK!!&M!N;;i( 2 29 =>KK!!&H!I;;i(&&t{{'9'9)'DEKK!! '4! 5 ;;n- 2 2> BCqHKK!!&D!E I .r'   c                    | j                   j                  d   | j                   j                  d       | j                   j                  d   7| j                   j                  d   | j                   j                  d       yyy)z/Validates input parameters for adding interfacerK   NzARole is a mandatory parameter for adding interface to NAS Server.r0   r   r   z^ethernet_port_name/ethernet_port_id is mandatory parameter for adding interface to NAS Server.)r   r   r8   )r$   s    r%   validate_create_paramsz Interface.validate_create_params  s    ;;f%-KK!! 'M! N;;23;""#56>KK!! 'A! B ? <r'   c                    | j                   j                  d   }| j                   j                  d   }| j                   j                  d   }| j                   j                  d   }| j                   j                  d   }| j                   j                  d   }| j                   j                  d   }| j                   j                  d   }| j                   j                  d	   }	| j                   j                  d
   }
| j                   j                  d   }t        di       }d}| j                          d}| j	                  ||      }| j                  |      }|rD|dk(  r?|j                         }| j                  |      }|r| j                   j                  d       |s1|dk(  r,| j                          | j                  ||||||||	|
	      |d<   |r|dk(  r| j                  |      |d<   |d   r5| j	                  ||      }| j                  |      }|r|j                         }||d<    | j                   j                  di | y)zr
        Perform different actions on Interface module based on parameters
        passed in the playbook
        r   r   r   r   rK   r/   r   r   rN   rO   stateF)changedrY   NpresentzQModification of Interfaces for NAS server is not supported through Ansible moduler0   rq   absentrY    )r   r   dictrl   rF   r?   r2   r[   r8   rn   rW   r_   	exit_json)r$   r   r   r   r   rK   r/   r   r   rN   rO   rp   resultmodify_flagrY   r9   r^   s                    r%   perform_module_operationz"Interface.perform_module_operation  s8   
 **?;++,,->?![[//0DE;;--.@A{{!!&){{)).9++$$Y/**?;++$$Y/++$$Y/""7+  
 ""$ 00-P22>BUi/ - = = ?778IJK%%*}%~)!3'') $ 2 2>CSUgim3?-Y`bi!kF9 Uh. $ 5 5m DF9)!44_mTN 66~FM$1$A$A$C!&7"#''r'   )NNNNNNNN)__name__
__module____qualname____doc__r&   r?   rF   rW   r[   r_   rl   rn   ry   rt   r'   r%   r   r      s@    )?*1(+2 uyNR$+L	+ "FH	B7(r'   r   c                     t        t        d      t        d      t        d      t        d      t        dddg      t        dd      t        d      t        d      t        d      t        d      t        ddd	d
g            S )zXThis method provide parameters required for the ansible
       Interface module on Unityr6   )type
PRODUCTIONBACKUP)r   choicesT)requiredr   intrr   rs   )r   r   r   )r   r   r   r   rK   r/   r   r   rN   rO   rp   )ru   rt   r'   r%   r   r     s{     &%(U+5)u|X&>?4e4% &% % Duy(6KL r'   c                 T    t         j                  |    rt         j                  |    } | S y)zGetting correct enum values for role
        :param: role: Indicates role of interface.
        :return: enum value for role.
    N)r   FileInterfaceRoleEnum)rK   s    r%   rP   rP     s,    
 ""4(**40 )r'   c                 N    	 t        j                  |        y# t        $ r Y yw xY w)zaValidating IP address format
        :param: address: IP address to be validated for format.
    TF)	ipaddressr4   
ValueError)addresss    r%   rf   rf     s*    W% s    	$$c                 v    	 dj                  |       } t        | d      }|j                  S # t        $ r Y yw xY w)z\Returns IP address version
        :param: val: IP address to be validated for version.
    z{0}F)strictr   )re   r   versionr   )valrL   s     r%   rh   rh     s=    mmC E*zz s   ), 	88c                  8    t               } | j                          y)z_Create Unity Interface object and perform action on it
       based on user input from playbookN)r   ry   )objs    r%   mainr     s     +C  "r'   __main__)r}   
__future__r   r   r   r   __metaclass__DOCUMENTATIONEXAMPLESRETURNansible.module_utils.basicr   Cansible_collections.dellemc.unity.plugins.module_utils.storage.dellr   r   r   
get_loggerr!   r   objectr   r   rP   rf   rh   r   rz   rt   r'   r%   <module>r      s    6 @ @BH3jW
r 5   e{#" l( l(^$	# zF r'   