
    Vh_                         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  ej                  d      Zd	Z G d
 de      ZddZd Zd Zd Zd Zedk(  r e        yy)z0Ansible module for managing CIFS server on Unity    )absolute_importdivisionprint_functiona  
module: cifsserver
version_added: '1.4.0'
short_description: Manage CIFS server on Unity storage system
description:
- Managing the CIFS server on the Unity storage system includes creating CIFS server, getting CIFS server details
  and deleting CIFS server.

extends_documentation_fragment:
  - dellemc.unity.unity

author:
- Akash Shendge (@shenda1) <ansible.team@dell.com>

options:
  nas_server_name:
    description:
    - Name of the NAS server on which CIFS server will be hosted.
    type: str
  nas_server_id:
    description:
    - ID of the NAS server on which CIFS server will be hosted.
    type: str
  netbios_name:
    description:
    - The computer name of the SMB server in Windows network.
    type: str
  workgroup:
    description:
    - Standalone SMB server workgroup.
    type: str
  local_password:
    description:
    - Standalone SMB server administrator password.
    type: str
  domain:
    description:
    - The domain name where the SMB server is registered in Active Directory.
    type: str
  domain_username:
    description:
    - Active Directory domain user name.
    type: str
  domain_password:
    description:
    - Active Directory domain password.
    type: str
  cifs_server_name:
    description:
    - The name of the CIFS server.
    type: str
  cifs_server_id:
    description:
    - The ID of the CIFS server.
    type: str
  interfaces:
    description:
    - List of file IP interfaces that service CIFS protocol of SMB server.
    type: list
    elements: str
  unjoin_cifs_server_account:
    description:
    - Keep SMB server account unjoined in Active Directory after deletion.
    - C(false) specifies keep SMB server account joined after deletion.
    - C(true) specifies unjoin SMB server account from Active Directory before deletion.
    type: bool
  state:
    description:
    - Define whether the CIFS server should exist or not.
    choices: [absent, present]
    required: true
    type: str
notes:
- The I(check_mode) is supported.
a  
- name: Create CIFS server belonging to Active Directory
  dellemc.unity.cifsserver:
    unispherehost: "{{unispherehost}}"
    username: "{{username}}"
    password: "{{password}}"
    validate_certs: "{{validate_certs}}"
    nas_server_name: "test_nas1"
    cifs_server_name: "test_cifs"
    domain: "ad_domain"
    domain_username: "domain_username"
    domain_password: "domain_password"
    state: "present"

- name: Get CIFS server details using CIFS server ID
  dellemc.unity.cifsserver:
    unispherehost: "{{unispherehost}}"
    username: "{{username}}"
    password: "{{password}}"
    validate_certs: "{{validate_certs}}"
    cifs_server_id: "cifs_37"
    state: "present"

- name: Get CIFS server details using NAS server name
  dellemc.unity.cifsserver:
    unispherehost: "{{unispherehost}}"
    username: "{{username}}"
    password: "{{password}}"
    validate_certs: "{{validate_certs}}"
    nas_server_name: "test_nas1"
    state: "present"

- name: Delete CIFS server
  dellemc.unity.cifsserver:
    unispherehost: "{{unispherehost}}"
    username: "{{username}}"
    password: "{{password}}"
    validate_certs: "{{validate_certs}}"
    cifs_server_id: "cifs_37"
    unjoin_cifs_server_account: true
    domain_username: "domain_username"
    domain_password: "domain_password"
    state: "absent"

- name: Create standalone CIFS server
  dellemc.unity.cifsserver:
    unispherehost: "{{unispherehost}}"
    username: "{{username}}"
    password: "{{password}}"
    validate_certs: "{{validate_certs}}"
    netbios_name: "ANSIBLE_CIFS"
    workgroup: "ansible"
    local_password: "Password123!"
    nas_server_name: "test_nas1"
    state: "present"

- name: Get CIFS server details using netbios name
  dellemc.unity.cifsserver:
    unispherehost: "{{unispherehost}}"
    username: "{{username}}"
    password: "{{password}}"
    validate_certs: "{{validate_certs}}"
    netbios_name: "ANSIBLE_CIFS"
    state: "present"

- name: Delete standalone CIFS server
  dellemc.unity.cifsserver:
    unispherehost: "{{unispherehost}}"
    username: "{{username}}"
    password: "{{password}}"
    validate_certs: "{{validate_certs}}"
    cifs_server_id: "cifs_40"
    state: "absent"
a  
changed:
    description: Whether or not the resource has changed.
    returned: always
    type: bool
    sample: true

cifs_server_details:
    description: Details of the CIFS server.
    returned: When CIFS server exists
    type: dict
    contains:
        id:
            description: Unique identifier of the CIFS server instance.
            type: str
        name:
            description: User-specified name for the SMB server.
            type: str
        netbios_name:
            description: Computer Name of the SMB server in windows network.
            type: str
        description:
            description: Description of the SMB server.
            type: str
        domain:
            description: Domain name where SMB server is registered in Active Directory.
            type: str
        workgroup:
            description: Windows network workgroup for the SMB server.
            type: str
        is_standalone:
            description: Indicates whether the SMB server is standalone.
            type: bool
        nasServer:
            description: Information about the NAS server in the storage system.
            type: dict
            contains:
                UnityNasServer:
                    description: Information about the NAS server in the storage system.
                    type: dict
                    contains:
                        id:
                            description: Unique identifier of the NAS server instance.
                            type: str
        file_interfaces:
            description: The file interfaces associated with the NAS server.
            type: dict
            contains:
                UnityFileInterfaceList:
                    description: List of file interfaces associated with the NAS server.
                    type: list
                    contains:
                        UnityFileInterface:
                            description: Details of file interface associated with the NAS server.
                            type: dict
                            contains:
                                id:
                                    description: Unique identifier of the file interface.
                                    type: str
        smb_multi_channel_supported:
            description: Indicates whether the SMB 3.0+ multichannel feature is supported.
            type: bool
        smb_protocol_versions:
            description: Supported SMB protocols, such as 1.0, 2.0, 2.1, 3.0, and so on.
            type: list
        smbca_supported:
            description: Indicates whether the SMB server supports continuous availability.
            type: bool
    sample: {
        "description": null,
        "domain": "xxx.xxx.xxx.com",
        "existed": true,
        "file_interfaces": {
            "UnityFileInterfaceList": [
                {
                    "UnityFileInterface": {
                        "hash": -9223363258905013637,
                        "id": "if_43"
                    }
                }
            ]
        },
        "hash": -9223363258905010379,
        "health": {
            "UnityHealth": {
                "hash": 8777949765559
            }
        },
        "id": "cifs_40",
        "is_standalone": false,
        "last_used_organizational_unit": "ou=Computers,ou=Dell NAS servers",
        "name": "ansible_cifs",
        "nas_server": {
            "UnityNasServer": {
                "hash": 8777949765531,
                "id": "nas_18"
            }
        },
        "netbios_name": "ANSIBLE_CIFS",
        "smb_multi_channel_supported": true,
        "smb_protocol_versions": [
            "1.0",
            "2.0",
            "2.1",
            "3.0"
        ],
        "smbca_supported": true,
        "workgroup": null
    }
)AnsibleModule)utils
cifsserverzAnsible/1.7.1c                   V    e Zd ZdZd ZddZd ZddZd Zd Z	d	 Z
	 	 dd
Zd Zd Zy)
CIFSServerz!Class with CIFS server operationsc                    t        j                         | _        | j                  j                  t	                      ddgddgddgg}g d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cifs_server_idcifs_server_namenetbios_name)r   r   r   r   r   T)argument_specsupports_check_modemutually_exclusiverequired_one_ofzCheck Mode Flag %sN)r   $get_unity_management_host_parametersmodule_paramsupdateget_cifs_server_parametersr   moduleensure_required_libsget_unity_unisphere_connectionparamsapplication_type
unity_connLOGinfo
check_mode)selfr   r   s      l/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/dellemc/unity/plugins/modules/cifsserver.py__init__zCIFSServer.__init__  s    "GGI!!"<">?0/BEUWiDj/@Buv $,, $1+	
 	""4;;/>>KK 02%t{{'='=>    Nc                 8   t         j                  d       t        ||||      }	 |r'| j                  j	                  |      }t        |      S |r'| j                  j	                  |      }t        |      S |rC| j                  j	                  |      }t        |      dkD  rt        |j                               S |rC| j                  j	                  |      }t        |      dkD  rt        |j                               S y# t        j                  $ r<}|j                  dk(  rd	|d
t        |      }nd	|dt        |      }Y d}~nd}~wt        j                  $ r d|z  }t         j                  |       Y yt        j                  $ r5}d	|dt        |      d| j                  j                   d   }Y d}~n*d}~wt"        $ r}d|dt        |      }Y d}~nd}~ww xY wt         j%                  |       | j                  j'                  |       y)al  Get CIFS server details.
            :param: cifs_server_id: The ID of the CIFS server
            :param: cifs_server_name: The name of the CIFS server
            :param: netbios_name: Name of the SMB server in windows network
            :param: nas_server_id: The ID of the NAS server
            :return: Dict containing CIFS server details if exists
        zGetting CIFS server details)_idname)r   r   )
nas_serverNi  zFailed to get CIFS server: z+ due to incorrect username/password error:  with error: z CIFS server with ID %s not foundz!. Please check unispherehost IP: unispherehostz&Failed to get details of CIFS server: msg)r   r    get_id_namer   get_cifs_serverprocess_responselenprocess_dict_get_propertiesr   	HttpErrorhttp_statusstrUnityResourceNotFoundErrorStoropsConnectTimeoutErrorr   r   	Exceptionerror	fail_json)	r"   r   r   r   r   
id_or_namecifs_server_detailser.   s	            r#   get_detailszCIFSServer.get_details4  s    	./ 1A<Q^_
!	c&*oo&E&E.&E&Y#'(;<<&*oo&E&EK[&E&\#'(;<<&*oo&E&ES_&E&`#*+a/'(;(K(K(MNN&*oo&E&EQ^&E&_#*+a/'(;(K(K(MNN 	\}}#7A3q6K $ ISTWXYTZ[// 	4~ECHHSM// 	ICFDKK$6$6$GIC 	cOY[^_`[abC	c		##&sI   (D (D 7AD <AD G'2E0G'>G'+G  G'G""G'c                 "   	 t         j                  j                  | j                  j                  |      }|S # t
        $ rL}d|dt        |      }t        j                  |       | j                  j                  |       Y d}~yd}~ww xY w)zGet CIFS server instance.
            :param: cifs_server_id: The ID of the CIFS server
            :return: Return CIFS server instance if exists
        )clir'   zFailed to get the CIFS server z instance with error r-   N)r   UnityCifsServergetr   _clir:   r7   r   r;   r   r<   )r"   r   cifs_server_objr?   	error_msgs        r#   get_cifs_server_instancez#CIFSServer.get_cifs_server_instancee  sx    	1#3377DOO<P<PVd7eO"" 	1,:CFDIIIi KK!!i!00		1s   69 	BAB		Bc                 X   t         j                  d       	 | j                  j                  s&| j	                  |      }|j                  |||       y# t        $ rL}d|dt        |      }t         j                  |       | j                  j                  |       Y d}~yd}~ww xY w)	au  Delete CIFS server.
            :param: cifs_server_id: The ID of the CIFS 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 CIFS server is deleted
        zDeleting CIFS server)r   )skip_domain_unjoinusernamepasswordTzFailed to delete CIFS server: r+   r-   N)
r   r    r   r!   rH   deleter:   r7   r;   r<   )r"   r   skip_unjoindomain_usernamedomain_passwordcifs_objr?   r.   s           r#   delete_cifs_serverzCIFSServer.delete_cifs_serveru  s     	'(		+;;))888W;crs 	+GUWZ[\W]^CIIcNKK!!c!**	+s   <A 	B)AB$$B)c                 6   t         j                  d       	 | j                  j                  |      }|j	                         S # t
        $ rL}d|dt        |      }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
            :return: Return NAS server ID if exists
        zGetting NAS server IDr(   z%Failed to get details of NAS server: r+   r-   N)
r   r    r   get_nas_serverget_idr:   r7   r;   r   r<   )r"   r   obj_nasr?   r.   s        r#   get_nas_server_idzCIFSServer.get_nas_server_id  s~     	()	+oo44/4JG>>## 	+N]_bcd_efCIIcNKK!!c!**	+s   +A 	BABBc                     g }|d   d   r$|d   d   D ]  }|j                  |d   d           | j                  j                  d   D ]  }||vs y y)zCheck if modification is required in existing interfaces
            :param: cifs_server_details: CIFS server details
            :return: Flag indicating if modification is required
        file_interfacesUnityFileInterfaceListUnityFileInterfaceid
interfacesTF)appendr   r   )r"   r>   existing_interfaces	interfaces       r#   is_modify_interfaceszCIFSServer.is_modify_interfaces  s     !012JK01BCD\] R	#**95I+J4+PQR ++L9 	I 33	 r%   c                    t         j                  d       ddg}|D ]Q  }| j                  j                  |   ||   #| j                  j                  |   j	                         ||   k7  sQ y | j                  j                  d   %|d    | j                  j                  d   |d   k7  ry| j                  j                  d   | j                  |      S y)zCheck if modification is required in existing CIFS server
            :param: cifs_server_details: CIFS server details
            :return: Flag indicating if modification is required
        z(Checking if any modification is requiredr   	workgroupTdomainr]   F)r   r    r   r   upperra   )r"   r>   
param_listparams       r#   is_modification_requiredz#CIFSServer.is_modification_required  s     	;<$k2
 	E{{!!%(49LU9S9_KK&&u-3359LU9SS	 ;;h'38KH8U8a""8,0CH0MM ;;l+7,,-@AAr%   c
                    t         j                  d       	 | j                  j                  s=t        j
                  j                  | j                  j                  |||||||||	
       y# t        $ rI}
dt        |
      z  }t         j                  |       | j                  j                  |       Y d}
~
yd}
~
ww xY w)a  Create CIFS server.
            :param: nas_server_id: The ID of NAS server
            :param: interfaces: List of file interfaces
            :param: netbios_name: Name of the SMB server in windows network
            :param: cifs_server_name: Name of the CIFS server
            :param: domain: The domain name where the SMB server is registered in Active Directory
            :param: domain_username: The domain username
            :param: domain_password: The domain password
            :param: workgroup: Standalone SMB server workgroup
            :param: local_password: Standalone SMB server admin password
            :return: Return True if CIFS server is created
        zCreating CIFS server)
rB   r*   r]   r   r)   rd   rO   rP   rc   local_passwordTz+Failed to create CIFS server with error: %sr-   N)r   r    r   r!   r   rC   creater   rE   r:   r7   r;   r<   )r"   r   r]   r   r   rd   rO   rP   rc   rj   r?   r.   s               r#   create_cifs_serverzCIFSServer.create_cifs_server  s     	'(
	+;;))%%,,1E1ER_lv:FM]fl=L^m7@Q_ - a  	+?3q6JCIIcNKK!!c!**	+s   AA+ +	B=4?B88B=c                    g d}d}|D ]}  }| j                   j                  |   t        | j                   j                  |   j                               dk(  sQ|j	                  |      }| j                   j                  |        y)z Validate the parameters
        )
r   r   rd   r   r   rj   r   rc   rO   rP   zPlease provide valid {0}Nr   r-   )r   r   r2   stripformatr<   )r"   rf   r.   rg   errmsgs        r#   validate_paramszCIFSServer.validate_params  s~    k
 ) 	2E{{!!%(4T[[=O=OPU=V=\=\=^9_cd9dE*%%&%1	2r%   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   }| j                   j                  d   }| j                   j                  d   }t        di       }| j                          || j	                  |      }| j                  ||||      }|r6||d   }| j                  |      }|r| j                   j                  d       |s|dk(  r|s| j                   j                  d       t        |||g      rAt        |||g      s3d}t        j                  |       | j                   j                  |       | j                  ||||||	|
||	      |d<   |dk(  r |rd}|| }| j                  |||	|
      |d<   |dk(  r1| j                  ||||      |d<   t        j                  d|d           | j                   j                  di | y)zt
        Perform different actions on CIFS server module based on parameters
        passed in the playbook
        r   r   r   r   r   rc   rj   rd   rO   rP   r]   unjoin_cifs_server_accountstateF)changedr>   N)r   r   r   r   r\   z4Modification is not supported through Ansible moduler-   presentz8Please provide nas server id/name to create CIFS server.zYnetbios_name, workgroup and local_password are required to create standalone CIFS server.ru   absentr>   zProcess Dict: %s )r   r   dictrq   rW   r@   rh   r<   anyallr   r;   rl   rR   r    	exit_json)r"   r   r   r   r   r   rc   rj   rd   rO   rP   r]   rs   rt   resultr>   modify_flagr.   rN   s                      r#   perform_module_operationz#CIFSServer.perform_module_operation  s   
 ++,<=;;--.@A**?;++,,->?{{)).9KK&&{3	++,<=##H-++,,->?++,,->?[[''5
%)[[%7%78T%U"""7+  "
 	& 22?CM"..n@P<H=J / L %!4T!:778KLK%%*`%a"u	'9 %%*d%eL)^<=c<YbdrJsFtG		#%%#%. $ 7 7zS_8H&Racr8A>!SF9 H!4K)5"<< $ 7 7Ud8G!IF9 I,0,<,<NN^JVKX -= -ZF() HH'0E)FG''r%   NNNN)NNN)NNNNNNNN)__name__
__module____qualname____doc__r$   r@   rH   rR   rW   ra   rh   rl   rq   r   rx   r%   r#   r
   r
     sC    +?,/'b1 +*+  . swfj+82G(r%   r
   Nc                 .    | r| }|S |r|}|S |r|}|S |}|S )a'  Get the id_or_name.
        :param: cifs_server_id: The ID of CIFS server
        :param: cifs_server_name: The name of CIFS server
        :param: netbios_name: Name of the SMB server in windows network
        :param: nas_server_id: The ID of NAS server
        :return: Return id_or_name
    rx   )r   r   r   r   r=   s        r#   r/   r/   4  sE     #
  
%

 	 
!
  #
r%   c                 <    | j                   r| j                         S y)Process CIFS server details.
        :param: cifs_server_details: Dict containing CIFS server details
        :return: Processed dict containing CIFS server details
    N)existedr4   )r>   s    r#   r1   r1   G  s!    
 """2244 #r%   c                 <    g d}|D ]  }|| v s| |   d   | |<    | S )r   )descriptionrd   rY   healthr\   is_standaloner)   nas_servernetbios_namesmb_multi_channel_supportedsmb_protocol_versionssmbca_supportedrc   r   r   rx   )r>   rf   rg   s      r#   r3   r3   P  sB    
/J  G'')<U)CA)F&G r%   c                      t        t               t               t               t               t        d      t               t               t        d      t               t               t        dd      t        d      t        dddd	g
            S )zZThis method provide parameters required for the ansible
       CIFS server module on UnityT)no_loglistr7   )typeelementsbool)r   rv   rw   )requiredr   choices)r   r   r   rc   rj   rd   rO   rP   r   r   r]   rs   rt   )ry   rx   r%   r#   r   r   _  sg     vVtv4(D0AdfVe4#'V#4Duy(6KL	 	r%   c                  8    t               } | j                          y)zaCreate Unity CIFS server object and perform action on it
       based on user input from playbookN)r
   r   )objs    r#   mainr   n  s     ,C  "r%   __main__r   )r   
__future__r   r   r   r   __metaclass__DOCUMENTATIONEXAMPLESRETURNansible.module_utils.basicr   Cansible_collections.dellemc.unity.plugins.module_utils.storage.dellr   
get_loggerr   r   objectr
   r/   r1   r3   r   r   r   rx   r%   r#   <module>r      s    7 @ @JXIVm
^ 5 Ue|$ # V( V(r&5# zF r%   